MINT2
DiskResidentEventList.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:18:00 GMT
4 #include <Mint/Utils.h>
5 
6 #include <TNtupleD.h>
7 #include <TFile.h>
8 #include <TRandom.h>
9 
10 #include <iostream>
11 #include <fstream>
12 #include <boost/algorithm/string.hpp>
13 
14 using namespace std;
15 using namespace MINT;
16 
17 //int DiskResidentEventList::__maxBytes = 100000000; // 100 MB
18 //int DiskResidentEventList::__maxBytes = 50000000; // 50 MB
19 int DiskResidentEventList::__maxBytes = 10000000; // 10 MB
20 //int DiskResidentEventList::__maxBytes = 1000000; // 1 MB
21 //int DiskResidentEventList::__maxBytes = 1000; // 1 kB
22 
24  return _cName;}
25 
27  return _ntpName;}
28 
30  string baseName = "DiskResidentEventList";
31  string name = baseName;
32  bool exists = true;
33  ofstream checkFile;
34  int i=0;
35  do{
36  name = baseName + "_" + anythingToString(i++) + ".root";
37  checkFile.open( name.c_str(), ios::in );
38  exists = ! checkFile.fail();
39  checkFile.close();
40  }while(exists);
41 
42  return name;
43 }
44 
46  : _fname(generateFname())
47  , _opt("UPDATE")
48  , _f(0)//new TFile(generateFname().c_str(), "UPDATE"))
49  , _ntp(0)
50  , _scaleData(1)
51  , _cName("DalitzEventList")
52  , _ntpName("DalitzEventList")
53 {
54  openFile();
55  init();
56 }
57 
59  , int scale
60  , const std::string& treeName
61  , const std::string& opt)
62  : _fname(fname)
63  , _opt(boost::to_upper_copy(opt))
64  , _f(0)//new TFile(fname.c_str(), opt.c_str()))
65  , _ntp(0)
66  , _scaleData(scale)
67  , _cName(treeName)
68  , _ntpName(treeName+"NTP")
69 {
70  bool dbThis=true;
71  openFile();
72  cout << "DiskResidentEventList created with file " << fname << endl;
73  fromFile();
74  if(dbThis)cout << " from file done, now init: " << endl;
75  init();
76  if(dbThis)cout << " finished creation" << endl;
77 }
78 
80  , const std::string& opt)
81  : _fname(fname)
82  , _opt(boost::to_upper_copy(opt))
83  , _f(0)//new TFile(fname.c_str(), opt.c_str()))
84  , _ntp(0)
85  , _scaleData(1)
86  , _cName("DalitzEventList")
87  , _ntpName("DalitzEventList")
88 {
89  bool dbThis=false;
90  openFile();
91  cout << "DiskResidentEventList created with file " << fname << endl;
92  fromFile();
93  if(dbThis)cout << " from file done, now init: " << endl;
94  init();
95  if(dbThis)cout << " finished creation" << endl;
96 }
97 
98 
100  : _fname(generateFname())
101  , _opt("UPDATE")
102  , _f(0)//new TFile(generateFname().c_str(), "UPDATE"))
103  , _ntp(0)
104  , _scaleData(1)
105  , _cName("DalitzEventList")
106  , _ntpName("DalitzEventList")
107 {
108  openFile();
109  Add(otherList);
110  init();
111 }
112 /*DiskResidentEventList::DiskResidentEventList(const IMinimalEventList<IDalitzEvent>& otherList)
113  : _fname(generateFname())
114  , _opt("UPDATE")
115  , _f(0)//new TFile(generateFname().c_str(), "UPDATE"))
116  , _ntp(0)
117  , _scaleData(1)
118  , _cName("DalitzEventList")
119  , _ntpName("DalitzEventList")
120 {
121  openFile();
122  Add(otherList);
123  init();
124  }*/
126  , const std::string& newFname
127  , const std::string& opt
128  )
129  : _fname(newFname)
130  , _opt(boost::to_upper_copy(opt))
131  , _f(0)//new TFile(newFname.c_str(), opt.c_str()))
132  // , _counted_ntp(0)
133  , _ntp(0)
134  , _scaleData(1)
135  , _cName("DalitzEventList")
136  , _ntpName("DalitzEventList")
137 {
138  openFile();
139  cout << " copy with new filename : " << newFname << endl;
140  cout << " file pointer: " << _f << endl;
141  fromFile();
142  Add(otherList);
143  init();
144 }
145 /*DiskResidentEventList::DiskResidentEventList(const IMinimalEventList<IDalitzEvent>& otherList
146  , const std::string& newFname
147  , const std::string& opt
148  )
149  : _fname(newFname)
150  , _opt(opt)
151  , _f(0)//new TFile(newFname.c_str(), opt.c_str()))
152  // , _counted_ntp(0)
153  , _ntp(0)
154  , _scaleData(1)
155  , _cName("DalitzEventList")
156  , _ntpName("DalitzEventList")
157 {
158  openFile();
159  cout << " copy with new filename : " << newFname << endl;
160  cout << " file pointer: " << _f << endl;
161  fromFile();
162  Add(otherList);
163  init();
164  }*/
165 
166 /*
167 DiskResidentEventList::DiskResidentEventList(TNtupleD* ntp)
168  : _currentEvent(0)
169  , _f(0)
170  , _counted_ntp(0)
171  , _ntp(ntp)
172 {
173  init();
174 }
175 */
176 
178  : _fname(generateFname())
179  , _opt("UPDATE")
180  , _f(0)//new TFile(generateFname().c_str(), "RECREATE"))
181  , _ntp(0)
182  , _scaleData(1)
183  , _cName("DalitzEventList")
184  , _ntpName("DalitzEventList")
185 {
186  openFile();
187  DalitzEvent evt(pat);
188  makeNtp(evt); // evt won't be saved, just to use "makeNtupleVarname" (clumsy)
189  init();
190 }
191 
193  , const std::string& fname
194  , const std::string& opt
195  )
196  : _fname(fname)
197  , _opt(boost::to_upper_copy(opt))
198  , _f(0)//new TFile(fname.c_str(), opt.c_str()))
199  , _ntp(0)
200  , _scaleData(1)
201  , _cName("DalitzEventList")
202  , _ntpName("DalitzEventList")
203 {
204  openFile();
205  fromFile();
206  DalitzEvent evt(pat);
207  if(0 == _ntp) makeNtp(evt);
208  init();
209 }
210 
212  this->Close();
213 }
214 
216  bool dbThis=true;
217  if(dbThis){
218  cout << "Hello from DiskResidentEventList::openFile()" << endl;
219  cout << " calling: TFile("
220  << _fname << ", " << _opt << ", " << _fname << ")"
221  << endl;
222  }
223  if(0 == _f)_f = TFile::Open(_fname.c_str(), _opt.c_str()
224  , _fname.c_str()
225  );
226 
227  if(0 == _f) return makeNewFile();
228  if(_f->IsZombie()) return makeNewFile();
229  if(! _f->IsOpen()) return makeNewFile();
230  if(! _f->IsWritable()) {
231  //return makeNewFile();
232  cout << "Warning in DiskResidentEventList: file "
233  << _fname << " not writable!"
234  << endl;
235  }
236  if(dbThis){
237  cout << " got _f = " << _f << endl;
238  cout << " _f->ls() " << endl;
239  _f->ls();
240  cout << "?" << endl;
241  }
242 
243 // if (1 ==1) return makeNewFile();
244  return true;
245 }
247  bool dbThis=false;
248  if(dbThis) cout << "DiskResidentEventList::fromFile() called" << endl;
249  if(0 == _f) makeNewFile();
250  if(_f->IsZombie()){
251  cout << "WARNING in DiskResidentEventList::fromFile():"
252  << " current file is zombie - making new one" << endl;
253  makeNewFile();
254  }
255  if(dbThis) cout << " opened file" << endl;
256  if(dbThis) _f->ls(); cout << " that's in it" << endl;
257  _f->cd();
258  if(dbThis){
259  cout << " cd'ed to file " << endl;
260  cout << " now calling " << "_f->Get(" << cName() << ")" << endl;
261  }
262  _ntp = (TNtupleD*) _f->Get(cName().c_str());
263  if(dbThis) cout << " got ntuple : " << _ntp << endl;
264  if(0 == _ntp){
265  return false;
266  }else{
267  if(dbThis){
268  cout << "Success in DiskResidentEventList::fromFile(): "
269  << " found ntuple: " << _ntp->ClassName() << ", " << cName()
270  << " in file: ";
271  _f->Print();
272  cout << endl;
273  }
274  }
275  if(dbThis) cout << " now calling init" << endl;
276  init();
277  if(dbThis) cout << "DiskResidentEventList::fromFile() returning 'true'"
278  << endl;
279  return true;
280 }
281 
283  if(0 != _ntp) _ntp->SetAutoSave(__maxBytes);
284  return (0 != _ntp);
285 }
286 
287 unsigned int DiskResidentEventList::size() const{
288  if(0 == _ntp) return 0;
289  return _ntp->GetEntries()/_scaleData;
290 }
292  return (0 == this->size());
293 }
294 
296  _f = TFile::Open(generateFname().c_str(), "RECREATE");
297  // owned by gDirectory, I don't delete this myself.
298  return (0 != _f);
299 }
300 
302  if(0 == _f){
303  cout << "WARNING DiskResidentEventList::makeNtp - no file. "
304  << "That shouldn't happen. I'll have one made." << endl;
305  makeNewFile();
306  }
307  _f->cd();
308  _ntp = (TNtupleD*) _f->FindObject(ntpName().c_str());
309  if(0 == _ntp){
310  _ntp = new TNtupleD(cName().c_str(), ntpName().c_str()
311  , evt.makeNtupleVarnames().c_str()
312  );
313  cout << "made new ntuple" << endl;
314  }else{
315  cout << "found old ntuple" << endl;
316  }
317  if(0 != _ntp){
318  _ntp->SetAutoSave(__maxBytes);
319  }
320  return 0 != _ntp;
321 }
323  if(0 == _f) makeNewFile();
324  _f->cd();
325  if(0 == _ntp) makeNtp(evt);
326  unsigned int arraySize = evt.ntupleVarArraySize();
327  vector<Double_t> array(arraySize);
328 
329  bool success = evt.fillNtupleVarArray(array);
330  if(! success){
331  cout << "ERROR in DiskResidentEventList::Add(const DalitzEvent& evt)"
332  << ", call to DalitzEvent::fillNtupleVarArray"
333  << " returned failure for event:\n" << evt
334  << endl;
335  }else{
336  //_f = _ntp->GetCurrentFile();
337  //_f->cd();
338  _ntp->Fill(&(array[0]));
339  _f = _ntp->GetCurrentFile();
340  // above lines: to pick up when TNtuple (TTree) changes
341  // files in case the current one is too big. All this
342  // rubbish is necessary because of root's crazy way of
343  // handling this.
344  }
345 
346  return true;
347 }
348 
350  DalitzEvent nevt(&evt);
351  return Add(nevt);
352 }
354  DalitzEvent nevt(evt);
355  return Add(nevt);
356 }
358  DalitzEvent nevt(evt.get());
359  return Add(nevt);
360 }
362  if(0 == otherList.size()) return false;
363  for(unsigned int i=0; i < otherList.size(); i++){
364  Add(otherList.getEvent(i));
365  }
366  return true;
367 }
368 /*bool DiskResidentEventList::Add(const IMinimalEventList<IDalitzEvent>& otherList){
369  if(0 == otherList.size()) return false;
370  for(unsigned int i=0; i < otherList.size(); i++){
371  Add(otherList.getEvent(i));
372  }
373  return true;
374  }*/
376  bool dbThis=false;
377  if(0 == _ntp) return false;
378  if(0 == _f) return false;
379  bool success=true;
380  if(dbThis){
381  cout << "DiskResidentEventList::save() called"
382  << " for filename = " << _fname << endl;
383  }
384  //_f->cd();
385  // success &=
386  //_f = _ntp->GetCurrentFile();
387  //_f->cd();
388  if(0 == _f || (! _f->IsWritable()) || (! _f->IsOpen())){
389  cout << "big problem with f in DiskResidentEventList::save()" << endl;
390  throw "Scheibenkartoffel";
391  }
392  _ntp->AutoSave();
393  if(dbThis)cout << " done the saving" << endl;
394  return success;
395 }
396 
398  bool dbThis=false;
399  bool success = true;
400 
401  if(dbThis){
402  cout << "DiskResidentEventList::Close() called"
403  << " for filename = " << _fname << endl;
404  }
405  if(0 == _f) return false;
406  if(0 == _ntp) return false;
407  // Don't understand why sometimes the TFile is already closed by the time the destructor is called,
408  // but this avoids crases since _ntp is invalidated.
409  if(!_f->IsOpen()) return false ;
410 
411  if(_opt.size() != 0 && _opt != "OPEN" && _opt != "READ"){
412  success &= save();
413  if(dbThis && ! success){
414  cout << "DiskResidentEventList::Close() failure in saving when closing"
415  << endl;
416  }
417  if(dbThis) cout << "saved file with success = " << success << endl;
418  }
419  //_f->cd();
420  //_ntp->Write();
421  //_f->Write();
422  if(dbThis) {
423  cout << "_f " << _f << " _ntp " << _ntp << endl ;
424  cout << "_f->IsOpen() " << _f->IsOpen() << endl ;
425  cout << "mustcleanup " << _f->TestBit(kMustCleanup) << endl ;
426  cout << "_ntp->GetName() " << _ntp->GetName() << endl ;
427  cout << "getting current file" << endl;
428  }
429  _f = _ntp->GetCurrentFile();
430  if(dbThis) cout << "_f = " << _f << endl;
431  _f->cd();
432  if(dbThis) cout << "cd'ed to _f and now about to Write" << endl;
433  // _f->Write();
434  if(dbThis) cout << "written,now closing" <<endl;
435  _f->Close();
436  if(dbThis) cout << "closed." << endl;
437  //delete _ntp;
438  //delete _f;
439  _f = 0;
440  _ntp = 0;
441 
442  (void)dbThis;
443  return success;
444 }
445 
446 //DalitzEvent DiskResidentEventList::operator[](unsigned int i) const{
448  if(i > size()){
449  cout << "FATAL ERROR in DiskResidentEventList::operator()"
450  << " index i=" << i << " out of range " << size()
451  << endl;
452  throw "index out of range";
453  }
454  if(0 == _ntp){
455  cout << "FATAL ERROR in DiskResidentEventList::operator()"
456  << "there is no ntuple "
457  << endl;
458  }
459  _ntp->GetEvent(i);
460  return DalitzEvent(_ntp);
461 }
462 
464  DalitzHistoSet hs;
465  for(unsigned int i=0; i< this->size(); i++){
466  hs.addEvent(this->getEvent(i));
467  }
468  return hs;
469 }
470 
472  // mainly for diagnostics
473  DalitzHistoSet hs;
474  for(unsigned int i=0; i< this->size(); i++){
475  DalitzEvent evt(this->getEvent(i));
476  hs.addEvent(evt, evt.getWeight());
477  }
478  return hs;
479 }
481  // mainly for diagnostics
482  DalitzHistoSet hs;
483  if(0 == w) return hs;
484  for(unsigned int i=0; i< this->size(); i++){
485  DalitzEvent evt( this->getEvent(i));
486  hs.addEvent(evt, w->RealVal(evt));
487  }
488  return hs;
489 }
490 
492  // mainly for diagnostics
493  DalitzHistoSet hs;
494  if(0 == w) return hs;
495  for(unsigned int i=0; i< this->size(); i++){
496  DalitzEvent evt(this->getEvent(i));
497  hs.addEvent(evt, w->RealVal(evt) * (evt.getWeight()));
498  }
499  return hs;
500 }
501 
502 bool DiskResidentEventList::makePlots(const std::string& filename){
503  histoSet().save(filename);
504  return true;
505 }
506 
507 //
508 
bool makeNtp(const DalitzEvent &evt)
DalitzHistoSet reWeightedHistoSet(MINT::IReturnRealForEvent< IDalitzEvent > *w) const
DalitzHistoSet weighedReWeightedHistoSet(MINT::IReturnRealForEvent< IDalitzEvent > *w) const
DalitzHistoSet weightedHistoSet() const
std::string makeNtupleVarnames() const
bool exists(const string &fname)
bool fillNtupleVarArray(std::vector< Double_t > &array) const
bool makePlots(const std::string &filename)
virtual double RealVal(EVENT_TYPE &evt)=0
virtual bool Add(const DalitzEvent &evt)
virtual unsigned int size() const
virtual double getWeight() const
virtual EVENT_TYPE getEvent(unsigned int i) const =0
static std::string generateFname()
unsigned int ntupleVarArraySize() const
void addEvent(const IDalitzEvent &evt, double weight=1)
std::string anythingToString(const T &anything)
Definition: Utils.h:62
DalitzHistoSet histoSet() const
DalitzEvent getEvent(unsigned int i) const
virtual unsigned int size() const =0
bool save(const std::string &filename="DalitzHistos.root") const
virtual bool empty() const
X * get() const
Definition: counted_ptr.h:123