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