MINT2
DalitzEventPtrList.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/DalitzEvent.h"
5 #include "Mint/FitAmpSum.h"
6 #include "Mint/AllPossibleSij.h"
7 
8 #include "TRandom.h"
9 #include "TH1D.h"
10 #include "TH2D.h"
11 #include "TNtupleD.h"
12 #include "TObjString.h"
13 
14 #include <iostream>
15 #include <ctime>
16 
17 using namespace std;
18 using namespace MINT;
19 
20 const std::string DalitzEventPtrList::_className("DalitzEventPtrList");
21 
24 {
25 }
26 
29  , EventPtrList<DalitzEvent>(other)
30 {
31 }
32 
34 
35 }
36 
37 
39  , const DalitzEventPattern& pat
40  , TRandom* rnd
41  ){
42 
43  cout << "DalitzEventPtrList::generatePhaseSpaceEvents "
44  << " about to generate " << NumEvents
45  << " events to phase space" << endl;
46  time_t tstart = time(0);
47  int reportN = NumEvents/10; // 10 x at least;
48  if(reportN > 10000) reportN = 10000; // at least once every 10k event
49  for(int i=0; i< NumEvents; i++){
50  if( i < 5 || (!(i%reportN))){
51  cout << "DalitzEventPtrList::generatePhaseSpaceEvents "
52  << " generating " << i+1 << "th event."
53  << " with " << pat << " and " << rnd << endl;
54  }
55  counted_ptr<DalitzEvent> evt(new DalitzEvent(pat, rnd));
56  Add(evt);
57  }
58  double delT = difftime(time(0), tstart);
59  cout << " This took " << delT << " s";
60  if(delT > 0){
61  cout << " this is " << NumEvents/delT << " evts/s"
62  << " or " << (NumEvents/delT) * 60<< " evts/min";
63  }
64  cout << endl;
65 
66  return this->size();
67 }
68 
69 
70 // saving and retrieving:
71 
73  return makeNtuple(className());
74 }
75 
76 /* Works under the assumptions that all events in the list
77  follow the same pattern, e.g. all are KKpipi events.
78  Probably still works if at least all are 3 or all are 4 body
79  events.
80 */
81 
82 TNtupleD* DalitzEventPtrList::makeNtuple(const std::string& ntpName ) const{
83 
84  if(this->empty()) return (TNtupleD*) 0;
85  std::string varNameString= (*(this->begin()))->makeNtupleVarnames();
86  TNtupleD* ntp = new TNtupleD(className().c_str()
87  , ntpName.c_str()
88  , varNameString.c_str());
89 
90  unsigned int arraySize = (*(this->begin()))->ntupleVarArraySize();
91  vector<Double_t> array(arraySize);
92 
93 
94  for(vector<counted_ptr<DalitzEvent> >::const_iterator it = this->begin();
95  it != this->end(); it++){
96 
97  bool success = (*it)->fillNtupleVarArray(array);
98  if(! success){
99  cout << "ERROR in DalitzEventListt::makeNtuple"
100  << ", call to DalitzEvent::fillNtupleVarArray"
101  << " returned failure"
102  << endl;
103  }else{
104  ntp->Fill(&(array[0]));
105  }
106  }
107  return ntp;
108 }
109 
111  DalitzHistoSet hs;
112  for(unsigned int i=0; i< this->size(); i++){
113  DalitzEvent evt(this->getEventRef(i));
114  hs.addEvent(evt);
115  }
116  return hs;
117 }
119  // mainly for diagnostics
120  DalitzHistoSet hs;
121  for(unsigned int i=0; i< this->size(); i++){
122  DalitzEvent evt(this->getEventRef(i));
123  hs.addEvent(evt, evt.getWeight());
124  }
125  return hs;
126 }
128  // mainly for diagnostics
129  DalitzHistoSet hs;
130  if(0 == w) return hs;
131  for(unsigned int i=0; i< this->size(); i++){
132  DalitzEvent evt(this->getEventRef(i));
133  hs.addEvent(evt, w->RealVal(evt) );
134  }
135  return hs;
136 }
137 
139  // mainly for diagnostics
140  DalitzHistoSet hs;
141  if(0 == w) return hs;
142  for(unsigned int i=0; i< this->size(); i++){
143  DalitzEvent evt(this->getEventRef(i));
144  hs.addEvent(evt, w->RealVal(evt) * evt.getWeight());
145  }
146  return hs;
147 }
148 
149 
150 bool DalitzEventPtrList::save(const std::string& fname)const{
151  return saveAsNtuple(fname);
152 }
153 bool DalitzEventPtrList::fromFile(const std::string& fname){
154  return fromNtupleFile(fname);
155 }
156 
157 bool DalitzEventPtrList::saveAsNtuple(const std::string& fname
158  ) const{
159  return saveAsNtuple(fname, className());
160 }
161 
162 bool DalitzEventPtrList::saveAsNtuple(const std::string& fname
163  , const std::string& ntpName
164  ) const{
165  if(this->empty()){
166  cout << "WARNING in DalitzEventPtrList::saveAsNtuple!"
167  << "\n\tyou are trying to save me to the file: "
168  << fname
169  << "\n\tbut I have only " << this->size()
170  << " events."
171  << " I won't create the file."
172  << endl;
173  return false;
174  }
175  TFile* f = TFile::Open(fname.c_str(), "RECREATE");
176  f->cd();
177  TNtupleD* ntp = makeNtuple(ntpName);
178  if(0 != ntp) ntp->Write();
179  f->Close();
180  //ntp->Delete("all");
181 
182  // delete ntp;
183  return true;
184 }
185 
187  bool dbThis=false;
188  if(dbThis) cout << "about to read ntuple with ptr " << ntp << endl;
189  if(0==ntp) return false;
190  if(ntp->GetEntries() <=0) return false;
191  if(dbThis) cout << " number of entries: " << ntp->GetEntries() << endl;
192  //if(dbThis) cout << " number of variables " << ntp->GetNvar() << endl;
193  bool success=true;
194  for(Long64_t i=0; i< ntp->GetEntries(); i++){
195  if(dbThis){
196  cout << "DalitzEventPtrList::fromNtuple "
197  << " getting " << i << " th entry" << endl;
198  }
199  ntp->GetEntry(i);
200  if(dbThis) cout << " got it" << endl;
202  // success &= evt.fromNtuple(ntp);
203  success &= evt->fromTree(ntp);
204  if(dbThis) cout << " made event" << endl;
205  if(! success){
206  cout << "ERROR in DalitzEventPtrList::fromNtuple"
207  << ", call to DalitzEvent::fromNtuple returned false!"
208  << endl;
209  return false;
210  }
211  this->Add(evt);
212  if(dbThis) cout << " added event" << endl;
213  }
214  if(dbThis) cout << "DalitzEventPtrList::fromNtuple worked!" << endl;
215  return success;
216 }
217 
218 bool DalitzEventPtrList::fromNtupleFile(const std::string& fname){
219  TFile f(fname.c_str());
220  f.cd();
221  TTree* ntp = (TTree*) f.Get(className().c_str());
222  if(0 == ntp){
223  cout << "ERROR in DalitzEventPtrList::fromNtupleFile"
224  << "\n > Can't get ntuple for filename = "
225  << "\n > " << fname << endl;
226  return false;
227  }
228  return fromNtuple(ntp);
229 }
230 
231 
232 //
233 
234 
235 
236 //
virtual const DalitzEvent & getEventRef(unsigned int i) const
Definition: EventPtrList.h:49
std::vector< MINT::counted_ptr< DalitzEvent > >::iterator end()
static const std::string _className
virtual bool Add(const DalitzEvent &evt)
Definition: EventPtrList.h:88
DalitzHistoSet weighedReWeightedHistoSet(MINT::IReturnRealForEvent< IDalitzEvent > *w)
bool fromTree(TTree *tree)
virtual double RealVal(EVENT_TYPE &evt)=0
int generatePhaseSpaceEvents(int NumEvents, const DalitzEventPattern &pat, TRandom *rnd=0)
DalitzHistoSet histoSet() const
virtual unsigned int size() const
Definition: EventPtrList.h:76
bool fromFile(const std::string &fname="DalitzEvents.root")
const std::string & className() const
virtual double getWeight() const
std::vector< MINT::counted_ptr< DalitzEvent > >::iterator begin()
void addEvent(const IDalitzEvent &evt, double weight=1)
DalitzHistoSet weightedHistoSet() const
bool fromNtupleFile(const std::string &fname="DalitzEvents.root")
bool saveAsNtuple(const std::string &fname="DalitzEvents.root") const
bool fromNtuple(TTree *ntp)
TNtupleD * makeNtuple() const
DalitzHistoSet reWeightedHistoSet(MINT::IReturnRealForEvent< IDalitzEvent > *w)
bool save(const std::string &fname="DalitzEvents.root") const