ndh 0.0.7
Loading...
Searching...
No Matches
HnSparse.cc
1#include <TFile.h>
2#include <TTree.h>
3#include <TAxis.h>
4#include <TH2D.h>
5#include <THnSparse.h>
6
7#include "HnSparse.hh"
8#include "ndh.hh"
9
11ClassImp(NDH::HnSparse);
13
14namespace NDH {
15
16HnSparse::HnSparse() : THnSparse()
17{
21}
22
23HnSparse::HnSparse(const char * name, const char * title, Int_t dim, const Int_t * nbins, const Double_t * xmin,
24 const Double_t * xmax, Int_t chunksize)
25 : THnSparse(name, title, dim, nbins, xmin, xmax, chunksize)
26{
30}
31
32Bool_t HnSparse::Import(std::vector<Int_t> r, TString filename, TString objname, TString cacheDir)
33{
37
38 if (!cacheDir.IsNull()) TFile::SetCacheFileDir(cacheDir.Data(), 1, 1);
39
40 if (filename.IsNull()) {
41 Printf("Error: filename is empty !!!");
42 return kFALSE;
43 }
44 if (objname.IsNull()) {
45 Printf("Error: objname is empty !!!");
46 return kFALSE;
47 }
48
49 Printf("Opening file='%s' obj='%s' ...", filename.Data(), objname.Data());
50 TFile * f = TFile::Open(filename.Data());
51 if (f == nullptr) return kFALSE;
52
53 THnSparse * s = (THnSparse *)f->Get(objname.Data());
54
55 // s->Print();
56
57 TObjArray * newAxis = (TObjArray *)s->GetListOfAxes()->Clone();
58 for (Int_t iDim = 0; iDim < newAxis->GetEntries(); iDim++) {
59 TAxis * a = (TAxis *)newAxis->At(iDim);
60 // Printf("%s %d %.2f %.2f", a->GetName(), a->GetNbins(), a->GetXmin(), a->GetXmax());
61 // a->Print();
62 if (std::find(r.begin(), r.end(), iDim) != r.end()) {
63 // Printf("%s %d %.2f %.2f", a->GetName(), a->GetNbins(), a->GetXmin(), a->GetXmax());
64 }
65 else {
66 /* v does not contain x */
67 // Printf("Reset %s %d %.2f %.2f", a->GetName(), a->GetNbins(), a->GetXmin(), a->GetXmax());
68 a->Set(1, a->GetXmin(), a->GetXmax());
69 }
70 }
71
72 Init(TString::Format("ndh_%s", s->GetName()).Data(), "", newAxis, kTRUE);
73
74 Int_t dims[fNdimensions];
75 Int_t c[fNdimensions];
76 for (Int_t iDim = 0; iDim < newAxis->GetEntries(); iDim++) {
77 TAxis * a = s->GetAxis(iDim);
78 // Printf("%s %d %.2f %.2f", a->GetName(), a->GetNbins(), a->GetXmin(), a->GetXmax());
79 dims[iDim] = iDim;
80 c[iDim] = 1;
81 }
82 TFile * fileOut = TFile::Open(fOutputFileName.Data(), "RECREATE");
83 fTree = new TTree("ndh", "NDH tree");
84 fTree->Branch("h", &s);
85
86 RecursiveLoop(s, 0, c, dims, r);
87
88 // Reset cuts
89 for (Int_t iDim = 0; iDim < GetNdimensions(); iDim++) {
90 GetAxis(iDim)->SetRange();
91 }
92
93 fTree->GetUserInfo()->Add(Clone());
94 fTree->Write();
95 fileOut->Close();
96
97 delete s;
98 f->Close();
99
100 return kTRUE;
101}
102
103bool HnSparse::RecursiveLoop(THnSparse * s, Int_t level, Int_t * coord, Int_t * dims, std::vector<Int_t> & r)
104{
108
109 if (level >= r.size()) return true;
110
111 // Printf("level=%d axis_id=%d", level, r[level]);
112
113 for (Int_t iBin = 1; iBin <= GetAxis(r[level])->GetNbins(); iBin++) {
114
115 coord[r[level]] = iBin;
116 s->GetAxis(r[level])->SetRange(iBin, iBin);
117 Bool_t finished = RecursiveLoop(s, level + 1, coord, dims, r);
118 if (finished) {
119 THnSparse * ss = (THnSparse *)s->ProjectionND(s->GetNdimensions(), dims, "O");
120
121 ss->SetName(GetName());
122 ss->SetEntries(0);
123 // GetBin(coord, kTRUE);
124 if (ss->GetNbins() > 0) {
125 SetBinContent(coord, ss->GetNbins());
126 Printf("level=%d axis_id=%d iBin=%d binsFilled=%d", level, r[level], iBin, ss->GetNbins());
127 fTree->SetBranchAddress("h", &ss);
128 fTree->Fill();
129 }
130 else {
131 Printf("[NotFilled] level=%d axis_id=%d iBin=%d binsFilled=%d", level, r[level], iBin, ss->GetNbins());
132 }
133 delete ss;
134 }
135 else {
136 Printf("level=%d axis_id=%d iBin=%d", level, r[level], iBin);
137 }
138 }
139
140 return false;
141}
142
143void HnSparse::ReserveBins(Long64_t nBins)
144{
148 Printf("Reserving %lld bins ...", nBins);
149 Reserve(nBins);
150 Printf("%lld bins reserved.", nBins);
151}
152} // namespace NDH
HnSparse object.
Definition HnSparse.hh:18
bool RecursiveLoop(THnSparse *s, Int_t level, Int_t *coord, Int_t *dims, std::vector< Int_t > &r)
Definition HnSparse.cc:103
void ReserveBins(Long64_t nBins)
Definition HnSparse.cc:143
TString fOutputFileName
Output filename.
Definition HnSparse.hh:40
TTree * fTree
Container.
Definition HnSparse.hh:39
Bool_t Import(std::vector< Int_t > r, TString filename, TString objname, TString cacheDir=gSystem->HomeDirectory())
Definition HnSparse.cc:32