MINT2
DalitzEventList.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
3 #include "Mint/DalitzEventList.h"
4 #include "Mint/DalitzEvent.h"
5 #include "Mint/FitAmpSum.h"
6 #include "Mint/AllPossibleSij.h"
7 
8 #include "Mint/IReturnReal.h"
9 
10 #include "TRandom.h"
11 #include "TH1D.h"
12 #include "TH2D.h"
13 #include "TNtupleD.h"
14 #include "TObjString.h"
15 
16 #include "TTree.h"
17 
18 #include "Mint/counted_ptr.h"
19 
20 #include <iostream>
21 #include <ctime>
22 
23 using namespace std;
24 using namespace MINT;
25 
26 const std::string DalitzEventList::_className("DalitzEventList");
27 
28 
31  , _file(0)
32 {
33 }
34 
37  , EventList<DalitzEvent>(other)
38  , _file(other._file)
39 {
40 }
41 
44  , _file(0)
45 {
46  bool success = fromNtuple(ntp);
47  if(! success){
48  cout << "ERROR in DalitzEventList::DalitzEventList(TNtupleD* ntp)"
49  << "\n > problem creating myself from ntuple"
50  << endl;
51  }
52 }
53 
55  //if(0 != _file) _file->Close();
56 }
58  , const DalitzEventPattern& pat
59  , TRandom* rnd
60  ){
61 
62  cout << "DalitzEventList::generatePhaseSpaceEvents "
63  << " about to generate " << NumEvents
64  << " events to phase space" << endl;
65  time_t tstart = time(0);
66  int reportN = NumEvents/10; // 10 x at least;
67  if(reportN > 10000) reportN = 10000; // at least once every 10k event
68  for(int i=0; i< NumEvents; i++){
69  if( i < 5 || (!(i%reportN))){
70  cout << "DalitzEventList::generatePhaseSpaceEvents "
71  << " generating " << i+1 << "th event."
72  << " with " << pat << " and " << rnd << endl;
73  }
74  Add(DalitzEvent(pat, rnd));
75  }
76  double delT = difftime(time(0), tstart);
77  cout << " This took " << delT << " s";
78  if(delT > 0){
79  cout << " this is " << NumEvents/delT << " evts/s"
80  << " or " << (NumEvents/delT) * 60<< " evts/min";
81  }
82  cout << endl;
83 
84  return this->size();
85 }
86 
87 TH1D* DalitzEventList::makePlot(const std::vector<int> sijIndices
88  , const std::string& name
89  , IReturnRealForEvent<IDalitzEvent>* weightFunction //=0
90  , int nbins //=100
91  , double units //=1
92  , char opt // = s, m
93  ){
94  if(this->empty()) return (TH1D*)0;
95 
96  double eps=0.1;
97  double mi = ((*this)[0]).sijMin(sijIndices)/units;
98  double ma = ((*this)[0]).sijMax(sijIndices)/units;
99  if(opt=='m'){
100  mi = sqrt(mi);
101  ma = sqrt(ma);
102  }
103  mi -= (ma-mi)*eps;
104  ma += (ma-mi)*eps;
105 
106  TH1D* histo = new TH1D(name.c_str(), name.c_str(), nbins, mi, ma);
107 
108  for(unsigned int i = 0; i < this->size(); i++){
109  double w=1;
110  DalitzEvent& evt((*this)[i]);
111  if(0 != weightFunction) w = weightFunction->RealVal(evt);
112  double x= evt.sij(sijIndices)/units;
113  if(opt == 'm') x = sqrt(x);
114  histo->Fill(x, w);
115  }
116  return histo;
117 }
118 
119 TH2D* DalitzEventList::makePlot2D(const std::vector<int> sijIndicesX
120  ,const std::vector<int> sijIndicesY
121  , const std::string& name
122  , IReturnRealForEvent<IDalitzEvent>* weightFunction //=0
123  , int nbins //=20
124  , double units //=1
125  , char opt // = s, m
126  ){
127  if(this->empty()) return (TH2D*)0;
128 
129 
130  double eps = 0.125;
131  double miX = ((*this)[0]).sijMin(sijIndicesX)/units;
132  double maX = ((*this)[0]).sijMax(sijIndicesX)/units;
133  double miY = ((*this)[0]).sijMin(sijIndicesY)/units;
134  double maY = ((*this)[0]).sijMax(sijIndicesY)/units;
135  if(opt=='m'){
136  miX = sqrt(miX);
137  maX = sqrt(maX);
138  miY = sqrt(miY);
139  maY = sqrt(maY);
140  }
141  miX -= (maX - miX)*eps;
142  maX += (maX - miX)*eps;
143  miY -= (maY - miY)*eps;
144  maY += (maY - miY)*eps;
145 
146  TH2D* histo = new TH2D(name.c_str()
147  , name.c_str()
148  , nbins, miX, maX
149  , nbins, miY, maY);
150  for(unsigned int i = 0; i < this->size(); i++){
151  double w=1;
152  DalitzEvent& evt((*this)[i]);
153  if(0 != weightFunction) w = weightFunction->RealVal(evt);
154  double x= evt.sij(sijIndicesX)/units;
155  double y= evt.sij(sijIndicesY)/units;
156  if(opt == 'm'){
157  x = sqrt(x);
158  y = sqrt(y);
159  }
160  histo->Fill(x, y, w);
161  }
162  return histo;
163 }
164 
165 TNtupleD* DalitzEventList::makePlotNtp(const std::string& name_prefix
166  , IReturnRealForEvent<IDalitzEvent>* weightFunction // =0
167  , double units // = GeV*GeV
168  ){
169  std::string ntp_str="";
170  int nd = ((*this)[0]).eventPattern().size()-1;
171  if(nd < 3) return (TNtupleD*)0;
172  AllPossibleSij sijList(nd);
173 
174  int arraySize = 0;
175  for(namedVMap::const_iterator it = sijList.begin();
176  it != sijList.end(); it++){
177  std::string entry_name = "s" + it->first + ":";
178  ntp_str += entry_name;
179  arraySize++;
180  }
181  for(namedVMap::const_iterator it = sijList.begin();
182  it != sijList.end(); it++){
183  std::string entry_name = "m" + it->first + ":";
184  ntp_str += entry_name;
185  arraySize++;
186  }
187  ntp_str += "weight:";
188  arraySize++;
189  ntp_str += "fcn"; // last one doesn't get a ":
190 
191  std::string ntp_id = className() + "_sij";
192  std::string name = name_prefix + "_sij";
193  TNtupleD* ntp = new TNtupleD(ntp_id.c_str()
194  , name.c_str()
195  , ntp_str.c_str());
196 
197  Double_t* array = new Double_t[arraySize];
198 
199  for(unsigned int i = 0; i < this->size(); i++){
200  int arrayIndex = 0;
201  for(namedVMap::const_iterator it = sijList.begin();
202  it != sijList.end(); it++){
203  array[arrayIndex]=((*this)[i]).sij(it->second)/units;
204  arrayIndex++;
205  }
206  for(namedVMap::const_iterator it = sijList.begin();
207  it != sijList.end(); it++){
208  std::string entry_name = "m" + it->first;
209  double sijtemp = ((*this)[i]).sij(it->second)/units;
210  if(sijtemp >=0){
211  array[arrayIndex]=sqrt(sijtemp);
212  }else{
213  array[arrayIndex] = -9999.;
214  }
215  arrayIndex++;
216  }
217  DalitzEvent& evt((*this)[i]);
218  array[arrayIndex] = evt.getWeight();
219  arrayIndex++;
220  double fcn=1;
221  if(0 != weightFunction) fcn = weightFunction->RealVal(evt);
222  array[arrayIndex] = fcn;
223  arrayIndex++;
224 
225  ntp->Fill(array);
226  }
227  delete[] array;
228 
229  return ntp;
230 }
231 PlotSet DalitzEventList::makeAllPlots( const std::string& name_prefix
232  , IReturnRealForEvent<IDalitzEvent>* weightFunction //=0
233  , int nbins1D //= 100
234  , int nbins2D //= 20
235  , double units //= GeV*GeV
236  ){
237  PlotSet ps;
238  if(this->empty()) return ps;
239 
240  int nd = ((*this)[0]).eventPattern().size()-1;
241  if(nd < 3) return ps;
242 
243  AllPossibleSij sijList(nd);
244 
245  for(namedVMap::const_iterator it = sijList.begin();
246  it != sijList.end(); it++){
247  std::string name = name_prefix + "_s" + it->first;
248 
249  TH1D* newPlots = makePlot(it->second
250  , name
251  , weightFunction
252  , nbins1D
253  , units
254  , 's');
255  if(0 != newPlots) ps.push_back(newPlots);
256 
257  name = name_prefix + "_m" + it->first;
258  TH1D* newPlotm = makePlot(it->second
259  , name
260  , weightFunction
261  , nbins1D
262  , units
263  , 'm');
264  if(0 != newPlotm) ps.push_back(newPlotm);
265  namedVMap::const_iterator jt = it;
266  jt++;
267  for(; jt != sijList.end(); jt++){
268  std::string name2D = name_prefix + jt->first + "_vs_" + it->first;
269 
270  TH2D* newPlot2D = makePlot2D(it->second, jt->second, name2D
271  , weightFunction, nbins2D, units
272  , 's');
273 
274  if(0 != newPlot2D) ps.push_back(newPlot2D);
275  }
276  }
277  TNtupleD* newPlotNtp = makePlotNtp(name_prefix, weightFunction, units);
278  ps.push_back(newPlotNtp);
279 
280  return ps;
281 }
282 
283 
284 TNtupleD* DalitzEventList::makeNtuple()const{
285  return makeNtuple(className());
286 }
287 
288 /* Works under the assumptions that all events in the list
289  follow the same pattern, e.g. all are KKpipi events.
290  Probably still works if at least all are 3 or all are 4 body
291  events.
292 */
293 
294 TNtupleD* DalitzEventList::makeNtuple(const std::string& ntpName ) const{
295 
296  if(this->empty()) return (TNtupleD*) 0;
297  std::string varNameString= ((this->begin()))->makeNtupleVarnames();
298  TNtupleD* ntp = new TNtupleD(className().c_str()
299  , ntpName.c_str()
300  , varNameString.c_str());
301 
302  ntp->SetDirectory(0);
303 
304  unsigned int arraySize = ((this->begin()))->ntupleVarArraySize();
305  vector<Double_t> array(arraySize);
306 
307 
308  for(vector<DalitzEvent>::const_iterator it = this->begin();
309  it != this->end(); it++){
310 
311  bool success = (it)->fillNtupleVarArray(array);
312  if(! success){
313  cout << "ERROR in DalitzEventList::makeNtuple"
314  << ", call to DalitzEvent::fillNtupleVarArray"
315  << " returned failure"
316  << endl;
317  }else{
318  ntp->Fill(&(array[0]));
319  }
320  }
321  return ntp;
322 }
323 
324 bool DalitzEventList::save(const std::string& fname)const{
325  return saveAsNtuple(fname);
326 }
327 bool DalitzEventList::fromFile(const std::string& fname){
328  return fromNtupleFile(fname);
329 }
330 
331 bool DalitzEventList::saveAsNtuple(const std::string& fname
332  ) const{
333  return saveAsNtuple(fname, className());
334 }
335 
336 bool DalitzEventList::saveAsNtuple(const std::string& fname
337  , const std::string& ntpName
338  ) const{
339  if(this->empty()){
340  cout << "WARNING in DalitzEventList::saveAsNtuple!"
341  << "\n\tyou are trying to save me to the file: "
342  << fname
343  << "\n\tbut I have only " << this->size()
344  << " events."
345  << " I won't create the file."
346  << endl;
347  return false;
348  }
349  TFile* f= TFile::Open(fname.c_str(), "RECREATE");
350  f->cd();
351  TNtupleD* ntp = makeNtuple(ntpName);
352  if(0 != ntp) ntp->Write();
353  f->Close();
354  //ntp->Delete("all");
355  //delete ntp;
356  return true;
357 }
358 
359 bool DalitzEventList::fromNtuple(TTree* ntp){
360  bool dbThis=false;
361  if(dbThis) cout << "about to read ntuple with ptr " << ntp << endl;
362  if(0==ntp) return false;
363  if(ntp->GetEntries() <=0) return false;
364  if(dbThis) cout << " number of entries: " << ntp->GetEntries() << endl;
365  //if(dbThis) cout << " number of variables " << ntp->GetNvar() << endl;
366  bool success=true;
367  for(Long64_t i=0; i< ntp->GetEntries(); i++){
368  if(dbThis){
369  cout << "DalitzEventList::fromNtuple "
370  << " getting " << i << " th entry" << endl;
371  }
372  ntp->GetEntry(i);
373  if(dbThis) cout << " got it" << endl;
374  DalitzEvent evt;
375  // success &= evt.fromNtuple(ntp);
376  success &= evt.fromTree(ntp);
377  if(dbThis) cout << " made event" << endl;
378  if(! success){
379  cout << "ERROR in DalitzEventList::fromNtuple"
380  << ", call to DalitzEvent::fromNtuple returned false!"
381  << endl;
382  return false;
383  }
384  this->Add(evt);
385  if(dbThis) cout << " added event" << endl;
386  }
387  if(dbThis) cout << "DalitzEventList::fromNtuple worked!" << endl;
388  return success;
389 }
390 
391 bool DalitzEventList::fromNtuple(TTree* ntp, double num){
392  bool dbThis=false;
393  if(dbThis) cout << "about to read ntuple with ptr " << ntp << endl;
394  if(0==ntp) return false;
395  if(ntp->GetEntries() <=0) return false;
396  if(dbThis) cout << " number of entries: " << ntp->GetEntries() << endl;
397  //if(dbThis) cout << " number of variables " << ntp->GetNvar() << endl;
398  TRandom Rand(500);
399  bool success=true;
400  for(Long64_t i=0; i< ntp->GetEntries(); i++){
401  if(dbThis){
402  cout << "DalitzEventList::fromNtuple "
403  << " getting " << i << " th entry" << endl;
404  }
405  double rand = Rand.Rndm();
406  if (rand < num)
407  {
408  ntp->GetEntry(i);
409  if(dbThis) cout << " got it" << endl;
410  DalitzEvent evt;
411  // success &= evt.fromNtuple(ntp);
412  success &= evt.fromTree(ntp);
413  if(dbThis) cout << " made event" << endl;
414  if(! success){
415  cout << "ERROR in DalitzEventList::fromNtuple"
416  << ", call to DalitzEvent::fromNtuple returned false!"
417  << endl;
418  return false;
419  }
420  this->Add(evt);
421  if(dbThis) cout << " added event" << endl;
422  }
423  }
424  if(dbThis) cout << "DalitzEventList::fromNtuple worked!" << endl;
425  return success;
426 }
427 
428 bool DalitzEventList::fromNtupleFile(const std::string& fname){
429  TFile* f(new TFile(fname.c_str()));
430  if(0 ==f){
431  cout << "ERROR in DalitzEventList::fromNtupleFile"
432  << "\n > Can't open file with filename = "
433  << "\n > " << fname << endl;
434  return false;
435  }
436  _file = f;
437  _file->cd();
438  TTree* ntp = (TTree*) _file->Get(className().c_str());
439  if(0 == ntp){
440  cout << "ERROR in DalitzEventList::fromNtupleFile"
441  << "\n > Can't get ntuple for filename = "
442  << "\n > " << fname << endl;
443  return false;
444  }
445 
446  return fromNtuple(ntp);
447 }
448 
450  DalitzHistoSet hs;
451  for(unsigned int i=0; i< this->size(); i++){
452  hs.addEvent(this->at(i));
453  }
454  return hs;
455 }
457  // mainly for diagnostics
458  DalitzHistoSet hs;
459  for(unsigned int i=0; i< this->size(); i++){
460  DalitzEvent evt(this->at(i));
461  hs.addEvent(evt, evt.getWeight());
462  }
463  return hs;
464 }
466  // mainly for diagnostics
467  DalitzHistoSet hs;
468  if(0 == w) return hs;
469  for(unsigned int i=0; i< this->size(); i++){
470  DalitzEvent evt(this->at(i));
471  hs.addEvent(evt, w->RealVal(evt));
472  }
473  return hs;
474 }
475 
477  DalitzHistoSet hs;
478  if(0 == w) return hs;
479  for(unsigned int i=0; i< this->size(); i++){
480  DalitzEvent evt(this->at(i));
481  hs.addEvent(evt, w->RealVal(evt) * evt.getWeight());
482  }
483  return hs;
484 }
485 
486 bool DalitzEventList::makePlots(const std::string& filename) const{
487  histoSet().save(filename);
488  return true;
489 }
490 //
virtual bool Add(const DalitzEvent &evt)
Definition: EventList.h:63
bool fromFile(const std::string &fname="DalitzEvents.root")
virtual ~DalitzEventList()
TNtupleD * makeNtuple() const
std::vector< DalitzEvent >::iterator end()
void push_back(const T &c)
DalitzHistoSet weighedReWeightedHistoSet(MINT::IReturnRealForEvent< IDalitzEvent > *w)
bool save(const std::string &fname="DalitzEvents.root") const
TH2D * makePlot2D(const std::vector< int > sijIndicesX, const std::vector< int > sijIndicesY, const std::string &name, MINT::IReturnRealForEvent< IDalitzEvent > *weightFunction=0, int nbins=10, double units=GeV *GeV, char opt='s')
bool fromTree(TTree *tree)
virtual double RealVal(EVENT_TYPE &evt)=0
DalitzHistoSet reWeightedHistoSet(MINT::IReturnRealForEvent< IDalitzEvent > *w)
bool fromNtupleFile(const std::string &fname="DalitzEvents.root")
static const std::string _className
DalitzHistoSet weightedHistoSet() const
TNtupleD * makePlotNtp(const std::string &name_prefix="DalitzPlotNtp", MINT::IReturnRealForEvent< IDalitzEvent > *weightFunction=0, double units=GeV *GeV)
bool makePlots(const std::string &filename) const
bool fromNtuple(TTree *ntp)
virtual double getWeight() const
PlotSet makeAllPlots(const std::string &name_prefix, MINT::IReturnRealForEvent< IDalitzEvent > *weightFunction=0, int nbins1D=100, int nbins2D=10, double units=GeV *GeV)
DalitzEvent & at(unsigned int i)
std::vector< DalitzEvent >::iterator begin()
void addEvent(const IDalitzEvent &evt, double weight=1)
unsigned int size() const
virtual unsigned int size() const
Definition: EventList.h:59
const std::string & className() const
DalitzHistoSet histoSet() const
bool saveAsNtuple(const std::string &fname="DalitzEvents.root") const
int generatePhaseSpaceEvents(int NumEvents, const DalitzEventPattern &pat, TRandom *rnd=0)
TH1D * makePlot(const std::vector< int > sij, const std::string &name, MINT::IReturnRealForEvent< IDalitzEvent > *weightFunction=0, int nbins=100, double units=GeV *GeV, char opt='s')
bool save(const std::string &filename="DalitzHistos.root") const