RecordList.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \author Christopher Backhouse -- bckhouse@caltech.edu
3 ////////////////////////////////////////////////////////////////////////
4 
6 
7 #include "TFile.h"
8 #include "TTree.h"
9 
10 #include <algorithm>
11 #include <cassert>
12 #include <iostream>
13 
14 namespace gibuu
15 {
16  //___________________________________________________________________________
17  Record::Record() : E(0), weight(0)
18  {
19  }
20 
21  //___________________________________________________________________________
22  Record::Record(float _E, float _w, int _prod_id, const std::vector<Particle>& _ps)
23  : E(_E), weight(_w), prod_id(_prod_id), parts(_ps)
24  {
25  }
26 
27  //___________________________________________________________________________
28  bool Record::operator<(const Record& rhs) const
29  {
30  return E < rhs.E;
31  }
32 
33  //___________________________________________________________________________
35  {
36  fFile = new TFile(fname);
37  assert(!fFile->IsZombie());
38 
39  fTree = (TTree*)fFile->Get("tr");
40  assert(fTree);
41 
42  fTree->SetBranchAddress("Enu", &Enu);
43  fTree->SetBranchAddress("weight", &weight);
44  fTree->SetBranchAddress("prod_id", &prod_id);
45 
46  fTree->SetBranchAddress("nparts", &nparts);
47 
48  fTree->SetBranchAddress("pdg", &pdgs);
49  fTree->SetBranchAddress("E", &Es);
50  fTree->SetBranchAddress("px", &px);
51  fTree->SetBranchAddress("py", &py);
52  fTree->SetBranchAddress("pz", &pz);
53  }
54 
55  //___________________________________________________________________________
57  {
58  delete fFile;
59  }
60 
61  //___________________________________________________________________________
63  {
64  return fTree->GetEntries();
65  }
66 
67  //___________________________________________________________________________
69  {
70  fTree->GetEntry(i);
71 
72  assert(nparts <= 1024);
73  std::vector<Particle> parts(nparts);
74  for(int j = 0; j < nparts; ++j){
75  parts[j].pdg = pdgs[j];
76  parts[j].E = Es[j];
77  parts[j].px = px[j];
78  parts[j].py = py[j];
79  parts[j].pz = pz[j];
80  } // end for j
81 
82  return Record(Enu, weight, prod_id, parts);
83  }
84 
85  //___________________________________________________________________________
87  {
88  std::cout << "Loading " << fname;
89  RecordLoader loader(fname);
90 
91  const int N = loader.NRecords();
92  fRecs.reserve(N);
93  for(int i = 0; i < N; ++i){
94  if(i%(N/8) == 0) std::cout << "." << std::flush;
95 
96  fRecs.push_back(loader.GetRecord(i));
97  } // end for i
99 
100  std::sort(fRecs.begin(), fRecs.end());
101 
102  // TODO - This should really happen in convert.C
103  for(Record& r: fRecs) r.weight *= fRecs.size();
104  }
105 
106  //___________________________________________________________________________
108  {
109  auto it = std::lower_bound(fRecs.begin(), fRecs.end(), Record(E, 0, 0, {}));
110  if(it == fRecs.end()) return 0;
111  return &(*it);
112  }
113 
114  //___________________________________________________________________________
116  : fLoader(fname)
117  {
118  std::cout << "Loading index to " << fname;
119 
120  TFile f(fname);
121  assert(!f.IsZombie());
122 
123  TTree* tr = (TTree*)f.Get("tr");
124  assert(tr);
125 
126  float Enu;
127  tr->SetBranchAddress("Enu", &Enu);
128 
129  const int N = tr->GetEntries();
130  fEnergies.reserve(N);
131 
132  for(int i = 0; i < N; ++i){
133  if(i%(N/8) == 0) std::cout << "." << std::flush;
134  tr->GetEntry(i);
135 
136  fEnergies.emplace_back(Enu, i);
137  } // end for i
138  std::cout << std::endl;
139 
140  std::sort(fEnergies.begin(), fEnergies.end());
141  }
142 
143  //___________________________________________________________________________
145  {
146  auto it = std::lower_bound(fEnergies.begin(), fEnergies.end(),
147  std::make_pair(E, 0));
148  if(it == fEnergies.end()) return 0;
149 
150  fRecord = fLoader.GetRecord(it->second);
151 
152  // TODO - This should really happen in convert.C
153  fRecord.weight *= fEnergies.size();
154 
155  return &fRecord;
156  }
157 } // namespace
const Record * GetRecord(float E) const override
Definition: RecordList.cxx:107
RecordLoader(const char *fname)
Definition: RecordList.cxx:34
Helper for SimpleRecordList and OnDemandRecordList.
Definition: RecordList.h:46
set< int >::iterator it
bool operator<(const Record &rhs) const
Definition: RecordList.cxx:28
const Var weight
SimpleRecordList(const char *fname)
Definition: RecordList.cxx:86
std::vector< Particle > parts
Definition: RecordList.h:33
std::pair< Spectrum *, CheatDecomp * > make_pair(SpectrumLoaderBase &loader_data, SpectrumLoaderBase &loader_mc, HistAxis *axis, Cut *cut, const SystShifts &shift, const Var &wei)
Definition: DataMCLoad.C:336
const Record * GetRecord(float E) const override
Definition: RecordList.cxx:144
Float_t E
Definition: plot.C:20
std::vector< std::pair< float, int > > fEnergies
Definition: RecordList.h:88
long NRecords() const
Definition: RecordList.cxx:62
const double j
Definition: BetheBloch.cxx:29
loader
Definition: demo0.py:10
Record GetRecord(int i) const
Definition: RecordList.cxx:68
OStream cout
Definition: OStream.cxx:6
assert(nhit_max >=nhit_nbins)
TRandom3 r(0)
OnDemandRecordList(const char *fname)
Definition: RecordList.cxx:115
float weight
Definition: RecordList.h:31