ROOTPWA
pwaPlotter.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright 2009 Sebastian Neubert
4 //
5 // This file is part of rootpwa
6 //
7 // rootpwa is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // rootpwa is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with rootpwa. If not, see <http://www.gnu.org/licenses/>.
19 //
21 
22 // This Class' Header ------------------
23 #include "pwaPlotter.h"
24 
25 // C/C++ Headers ----------------------
26 #include <iostream>
27 #include <limits>
28 
29 // Collaborating Class Headers --------
30 #include "TFile.h"
31 #include "TTree.h"
32 #include "TString.h"
33 #include "TH2D.h"
34 #include "TF1.h"
35 #include "TMultiGraph.h"
36 #include "TAxis.h"
37 #include "TMath.h"
38 #include "TGraphErrors.h"
39 #include "fitResult.h"
40 
41 // Class Member definitions -----------
42 
43 using namespace std;
44 using namespace rpwa;
45 
46 TH2D* drawDensity(TGraphErrors* g, TH2D* h, double weight){
47 
48  unsigned int ybins=h->GetNbinsY();
49  unsigned int xpoints=g->GetN();
50  TAxis* ax=h->GetYaxis();
51  double ymin=ax->GetXmin();
52  double ymax=ax->GetXmax();
53 
54  TF1 gaus("gaus","gausn(0)",ymin,ymax);
55  TString tit=g->GetTitle();
56 
57  for(unsigned int ip=0; ip<xpoints; ++ip){
58  double x=g->GetX()[ip];
59  double err=g->GetEY()[ip];
60  double val=g->GetY()[ip];
61 
62  // if(tit.Contains("1-1++0+sigma_01_a11269") && val<100 && x>1.6 && x<2.0){
63 // //cerr << "x="<<x
64 // // << " err="<< err
65 // // << " val="<< val << endl;
66 // //return h;
67 // }
68 
69  gaus.SetParameters(1,val,err);
70  for(unsigned int ibin=1; ibin<ybins;++ibin){
71  double y=ax->GetBinCenter(ibin);
72  //if(fabs(y-val)<5.*err){
73  double w=gaus.Eval(ax->GetBinCenter(ibin))*weight;
74  if(w==w)h->Fill(x,y,w);
75  //}
76  }
77  }
78  return h;
79 }
80 
81 
82 
83 string
84 getIGJPCMEps(const std::string& wavename){
85  return wavename.substr(0,7);
86 }
87 
88 
89 
91 
92 pwaPlotter::pwaPlotter()
93  : mMinEvidence(0)
94 {
95  mLogLikelihood= new TMultiGraph();
96  mLogLikelihood->SetTitle("LogLikelihood");
97  mLogLikelihood->SetName("LogLikelihood");
98  mLogLikelihoodPerEvent=new TMultiGraph();
99  mLogLikelihoodPerEvent->SetTitle("LogLikelihood/Event");
100  mLogLikelihoodPerEvent->SetName("LogLikelihoodPerEvent");
101  mEvidence= new TMultiGraph();
102  mEvidence->SetTitle("Evidence");
103  mEvidence->SetName("Evidence");
104  mEvidencePerEvent=new TMultiGraph();
105  mEvidencePerEvent->SetTitle("Evidence/Event");
106  mEvidencePerEvent->SetName("EvidencePerEvent");
107 
108  if(0){
109  std::vector<string> waves;
110  waves.push_back("1-2-+0+pi-_02_f21270=pi-+_1_a11269=pi+-_0_rho770.amp");
111  waves.push_back("1-2-+0+pi-_22_f21270=pi-+_11_a11269=pi+-_01_rho770.amp");
112  waves.push_back("1-2-+0+rho770_02_a21320=pi-_2_rho770.amp");
113  waves.push_back("1-2-+0+rho770_02_a11269=pi-_0_rho770.amp");
114  waves.push_back("1-0-+0+pi-_00_f01500=rho770_00_rho770.amp");
115  waves.push_back("1-0-+0+pi-_00_f01500=sigma_0_sigma.amp");
116  waves.push_back("1-0-+0+rho770_00_a11269=pi-_0_rho770.amp");
117  waves.push_back("1-0-+0+rho770_22_a11269=pi-_01_rho770.amp");
118  waves.push_back("1-0-+0+pi-_22_f21270=sigma_2_sigma.amp");
119  waves.push_back("1-1++0+pi-_01_eta11600=pi-+_10_pi1300=pi+-_00_sigma.amp");
120  waves.push_back("1-1++0+pi-_01_eta11600=pi-+_01_a11269=pi+-_01_rho770.amp");
121 
122  waves.push_back("1-1++0+sigma_01_a11269=pi-_0_rho770.amp");
123  waves.push_back("1-1++0+sigma_01_a11269=pi-_1_sigma.amp");
124  waves.push_back("1-1++0+rho770_11_a11269=pi-_0_rho770.amp");
125  waves.push_back("1-1++0+rho770_12_a11269=pi-_0_rho770.amp");
126  waves.push_back("1-1++0+rho770_01_pi1300=pi-_1_rho770.amp");
127 
128  waves.push_back("1-1++0+pi-_11_f11285=pi-+_11_a11269=pi+-_0_rho770.amp");
129  waves.push_back("1-1++0+pi-_01_rho1600=sigma_01_rho770.amp");
130  waves.push_back("1-1++0+pi-_10_f01370=rho770_00_rho770.amp");
131  waves.push_back("1-1++0+pi-_10_f01500=sigma_0_sigma.amp");
132  waves.push_back("1-1++0+pi-_12_b21800=pi-+_12_a21320=pi+-_21_rho770.amp");
133 
134  std::vector<strpair> phlist;
135  for(unsigned int i=0;i<waves.size();++i){
136  for(unsigned int j=i+1;j<waves.size();++j){
137  phlist.push_back(strpair(waves[i],waves[j]));
138  }
139  }
140 
141 
142 
143  for(unsigned int i=0;i<phlist.size();++i){
144  mPhases[phlist[i]]=new TMultiGraph();
145  std::stringstream name;
146  name << "PHI"<<phlist[i].first<<"---"<<"PHI"<<phlist[i].second;
147  mPhases[phlist[i]]->SetTitle(name.str().c_str());
148  mPhases[phlist[i]]->SetName(name.str().c_str());
149  }
150  }
151 
152 }
153 
154 
156  mWavenames.clear();
157 
158 }
159 
160 void
161 pwaPlotter::addFit(const std::string& filename,
162  const std::string& title,
163  const unsigned int colour,
164  const std::string& treename,
165  const std::string& branchname,
166  const unsigned int numb_bins){
167 
168  // Open and test file and tree
169  TFile* infile = TFile::Open(filename.c_str(),"READ");
170  if(infile==NULL || infile->IsZombie()){
171  cerr << "Input file "<<filename<<" is not a valid file!" << endl;
172  return;
173  }
174  TTree* intree=(TTree*)infile->FindObjectAny(treename.c_str());
175  if(intree==NULL || intree->IsZombie()){
176  cerr << "Tree "<<treename<<" not found in file "<<filename<< endl;
177  return;
178  }
179  fitResult* result=0;
180  if(intree->FindBranch(branchname.c_str())==NULL){
181  cerr << "Invalid branch "<<treename<<"."<<branchname<<" in file "
182  <<filename<<endl;
183  return;
184  }
185 
186  unsigned int ifit=mResultMetaInfo.size();
187  cerr << "Adding file "<< filename << endl;
188 
189  intree->SetBranchAddress(branchname.c_str(),&result);
190  unsigned int nbins=intree->GetEntries();
191  if(numb_bins!=0 && nbins!=numb_bins){
192  cerr << "Wrong number of bins "<<nbins<<" in file "
193  <<filename<<endl;
194  return;
195  }
196  // extract info for this fit
197  // loop through bins
198  // -> getRange in Mass bins
199  // -> collect all used waves
200  // -> integrate loglikelihood and evidence
201  double mass_min=1E6;
202  double mass_max=0;
203  double logli=0;
204  double logliperevt=0;
205  double evi=0;
206  double eviperevt=0;
207  unsigned int numwaves=0;
208  set<string> wavesinthisfit;
209 
210  for(unsigned int i=0;i<nbins;++i){
211  intree->GetEntry(i);
212 
213  double massBinCenter=result->massBinCenter()*0.001;
214  if(massBinCenter>mass_max)mass_max=massBinCenter;
215  if(massBinCenter<mass_min)mass_min=massBinCenter;
216 
217  registerWave(".*"); // Total intensity
218  wavesinthisfit.insert(".*");
219  registerWave("^.....0"); // Total M=0
220  wavesinthisfit.insert("^.....0");
221  registerWave("^.....1"); // Total M=1
222  wavesinthisfit.insert("^.....1");
223 
224  registerWave ("^......\\+"); // Total Eps=+
225  wavesinthisfit.insert("^......\\+");
226  registerWave("^......-"); // Total Eps=-
227  wavesinthisfit.insert("^......-");
228 
229  // check fitResult for used waves
230  // if not already registered -> register wave (will create TMultiGraph)
231  const vector<string>& waveNames=result->waveNames();
232  numwaves=waveNames.size();
233  // cout << "Number of Waves ="<< nwaves << endl;
234  for(unsigned int iw=0;iw<numwaves;++iw){
235  registerWave(waveNames[iw]);
236  wavesinthisfit.insert(waveNames[iw]);
237  // spin totals...
238  registerWave(getIGJPCMEps(waveNames[iw]));
239  wavesinthisfit.insert(getIGJPCMEps(waveNames[iw]));
240  }
241 
242  // get loglikelihoods
243  logli+=result->logLikelihood();
244  evi+=result->evidence();
245  logliperevt+=result->logLikelihood()/result->nmbEvents();
246  eviperevt+=result->evidence()/result->nmbEvents();
247  }
248  double binwidth=(mass_max-mass_min)/(double)(nbins-1);
249  cerr << "Number of bins: " << nbins
250  << " Width: " << binwidth << endl;
251 
252 
253  // create intensity plots ----------------------------------------------
254  // We have registered all graphs in the step before...
255  // This has to be done in a separate step! Try not to merge the following
256  // with the loop above! You will loose generality!!! You have been warned!
257 
258  //cout << "creating graphs" << endl;
259 
260 
261  // create graphs for this fit
262  set<string>::iterator it=wavesinthisfit.begin();
263  while(it!=wavesinthisfit.end()){
264  TPwaFitGraphErrors* g = new TPwaFitGraphErrors(nbins,ifit);
265  stringstream graphName;
266  if(*it==".*") graphName << "g" << title << "_total";
267  else if(*it=="^.....0") graphName << "g" << title << "_M0";
268  else if(*it=="^.....1") graphName << "g" << title << "_M1";
269  else if(*it=="^......\\+") graphName << "g" << title << "_E+";
270  else if(*it=="^......-") graphName << "g" << title << "_E-";
271  else graphName << "g" << title << "_" << *it;
272 
273  //cout << "creating graph " << graphName.str() << endl;
274 
275  g->SetName (graphName.str().c_str());
276  g->SetTitle(graphName.str().c_str());
277  //g->SetMarkerStyle(21);
278  g->SetMarkerSize(0.5);
279  g->SetMarkerColor(colour);
280  g->SetLineColor(colour);
281  g->GetXaxis()->SetTitle("mass (GeV/c^{2})");
282  g->GetYaxis()->SetTitle("intensity");
283  mIntensities[*it]->Add(g,"p");
284  mWaveEvidence[*it]+=evi;
285  ++it;
286 
287  }
288 
289  //cout << "building Likelihood graphs" << endl;
290 
291  // evidence and likelihood
292  TGraph* gLikeli=new TGraph(nbins);
293  stringstream graphName;
294  graphName << "g" << title << "_LogLikelihood";
295  gLikeli->SetName (graphName.str().c_str());
296  gLikeli->SetTitle(graphName.str().c_str());
297  gLikeli->SetMarkerStyle(21);
298  gLikeli->SetMarkerSize(0.5);
299  gLikeli->SetMarkerColor(colour);
300  gLikeli->SetLineColor(colour);
301  mLogLikelihood->Add(gLikeli,"p");
302  TGraph* gLikeliPE=new TGraph(nbins);
303  graphName.clear();
304  graphName << "g" << title << "_LogLikelihoodPerEvent";
305  gLikeliPE->SetName (graphName.str().c_str());
306  gLikeliPE->SetTitle(graphName.str().c_str());
307  gLikeliPE->SetMarkerStyle(21);
308  gLikeliPE->SetMarkerSize(0.5);
309  gLikeliPE->SetMarkerColor(colour);
310  gLikeliPE->SetLineColor(colour);
311  mLogLikelihoodPerEvent->Add(gLikeliPE,"p");
312  TGraph* gEvidence=new TGraph(nbins);
313  graphName.clear();
314  graphName << "g" << title << "_Evidence";
315  gEvidence->SetName (graphName.str().c_str());
316  gEvidence->SetTitle(graphName.str().c_str());
317  gEvidence->SetMarkerStyle(21);
318  gEvidence->SetMarkerSize(0.5);
319  gEvidence->SetMarkerColor(colour);
320  gEvidence->SetLineColor(colour);
321  mEvidence->Add(gEvidence,"p");
322  TGraph* gEvidencePE=new TGraph(nbins);
323  graphName.clear();
324  graphName << "g" << title << "_EvidencePerEvent";
325  gEvidencePE->SetName (graphName.str().c_str());
326  gEvidencePE->SetTitle(graphName.str().c_str());
327  gEvidencePE->SetMarkerStyle(21);
328  gEvidencePE->SetMarkerSize(0.5);
329  gEvidencePE->SetMarkerColor(colour);
330  gEvidencePE->SetLineColor(colour);
331  mEvidencePerEvent->Add(gEvidencePE,"p");
332 
333 
334  // create Phase graphs
335  std::map<strpair,TMultiGraph*>::iterator iph=mPhases.begin();
336  while(iph!=mPhases.end()){
337  // check if both waves have bee used in this fit
338  std::string w1=iph->first.first;
339  std::string w2=iph->first.second;
340  if(wavesinthisfit.find(w1)!=wavesinthisfit.end() &&
341  wavesinthisfit.find(w2)!=wavesinthisfit.end() ){
342  TPwaFitGraphErrors* g = new TPwaFitGraphErrors(nbins*3,ifit);
343  stringstream graphName;
344  graphName << "PHI"<<w1<<"---"<<"PHI"<<w2;
345 
346  cout << "creating graph " << graphName.str() << endl;
347 
348  g->SetName (graphName.str().c_str());
349  g->SetTitle(graphName.str().c_str());
350  g->SetMarkerStyle(21);
351  g->SetMarkerSize(0.5);
352  g->SetMarkerColor(colour);
353  g->SetLineColor(colour);
354  g->GetXaxis()->SetTitle("mass (GeV/c^{2})");
355  g->GetYaxis()->SetTitle("#Delta #Phi");
356  iph->second->Add(g,"p");
357 
358  } // endif both waves available
359  ++iph;
360  } // end create phase graphs
361 
362 
363 
364 
365 
366 
367 
368  //cout << "filling data" << endl;
369 
370  // loop again over fitResults and extract all info simultaneously
371 
372  for(unsigned int i=0;i<nbins;++i){
373  intree->GetEntry(i);
374  // loop through waves
375  it=wavesinthisfit.begin();
376  while(it!=wavesinthisfit.end()){
377  // check if this is a single wave
378  if(it->find("amp")!=it->npos){
379  // check if Phase Space is already filled
380  bool fillps=mPhaseSpace[*it]->GetN()!=(int)nbins;
381  if(fillps){
382  unsigned int waveid=result->waveIndex(*it);
383  mPhaseSpace[*it]->Set(i+1);
384  mPhaseSpace[*it]->SetPoint(i,
385  result->massBinCenter()*0.001,
386  result->normIntegral(waveid,
387  waveid).real());
388  }
389  } // end ccheck for single wave
390 
391  TMultiGraph* mg=mIntensities[*it];
392  TGraphErrors* g=dynamic_cast<TGraphErrors*>(mg->GetListOfGraphs()->Last());
393  g->SetPoint(i,
394  result->massBinCenter()*0.001,
395  result->intensity(it->c_str()));
396  g->SetPointError(i,
397  binwidth*0.5,
398  result->intensityErr(it->c_str()));
399 
400  ++it;
401 
402  }// end loop through waves
403 
404  // loop through phase plots
405  iph=mPhases.begin();
406  while(iph!=mPhases.end()){
407  std::string w1=iph->first.first;
408  std::string w2=iph->first.second;
409  if(wavesinthisfit.find(w1)!=wavesinthisfit.end() &&
410  wavesinthisfit.find(w2)!=wavesinthisfit.end() ){
411  TGraphErrors* g=dynamic_cast<TGraphErrors*>(iph->second->GetListOfGraphs()->Last());
412 
413  double ph=result->phase(w1,w2);
414  // check if we should make a transformation by 2pi
415  // this is needed because of cyclical variable phi
416  if(i>11){
417  double mpre;
418  double phpre;
419  g->GetPoint(i-3,mpre,phpre);
420  double diff1=fabs(ph-phpre);
421  double diff2=fabs(ph+360-phpre);
422  double diff3=fabs(ph-360-phpre);
423  if(diff2<diff1 && diff2<diff3)ph+=360;
424  else if(diff3<diff1 && diff3<diff2)ph-=360;
425  }
426 
427 
428 
429  g->SetPoint(i*3,
430  result->massBinCenter()*0.001,
431  ph);
432  g->SetPointError(i*3,
433  binwidth*0.5,
434  result->phaseErr(w1,w2));
435 
436  // add point +- 360 degree
437  g->SetPoint(i*3+1,
438  result->massBinCenter()*0.001,
439  ph+360);
440  g->SetPointError(i*3+1,
441  binwidth*0.5,
442  result->phaseErr(w1,w2));
443 
444  g->SetPoint(i*3+2,
445  result->massBinCenter()*0.001,
446  ph-360);
447  g->SetPointError(i*3+2,
448  binwidth*0.5,
449  result->phaseErr(w1,w2));
450 
451 
452 
453  }
454  ++iph;
455  } // end loop over phase graphs
456 
457 
458  gLikeli->SetPoint(i,
459  result->massBinCenter()*0.001,
460  result->logLikelihood());
461  gLikeliPE->SetPoint(i,
462  result->massBinCenter()*0.001,
463  result->logLikelihood()/result->nmbEvents());
464  gEvidence->SetPoint(i,
465  result->massBinCenter()*0.001,
466  result->evidence());
467  gEvidencePE->SetPoint(i,
468  result->massBinCenter()*0.001,
469  result->evidence()/result->nmbEvents());
470  }
471 
472  //cout << "writing meta" << endl;
473  // write MetaInfo
474  fitResultMetaInfo meta(filename,
475  title,
476  colour,treename,
477  branchname);
478  meta.setLikelihoods(logli,logliperevt,evi,eviperevt);
479  meta.setBinRange(mass_min-binwidth*0.5,mass_max+binwidth*0.5,nbins);
480  meta.setNWaves(numwaves);
481  mResultMetaInfo.push_back(meta);
482  mMinEvidence=(mMinEvidence*ifit+evi)/(ifit+1);
483 
484  cout << "Fit Quality Summary: " << endl;
485  cout << " LogLikelihood: " << logli << endl;
486  cout << " Evidence: " << evi << endl;
487  cout << " Number of waves: " << numwaves << endl;
488 
489  // cleanup
490  infile->Close();
491  cerr << endl;
492 
493 
494 }
495 
496 
497 
498 bool
499 pwaPlotter::registerWave(const std::string& wavename){
500  pair<set<string>::iterator,bool> inserted=mWavenames.insert(wavename);
501  if(inserted.second){ // we had a true insterion
502  cerr << "New wave ("<<mWavenames.size()<<"): " << wavename << endl;
503  // create intensity graph:
504  mIntensities[wavename]=new TMultiGraph();
505  mIntensities[wavename]->SetTitle(wavename.c_str());
506  mIntensities[wavename]->SetName(wavename.c_str());
507 
508  mPhaseSpace[wavename]=new TGraph();
509  string psname=wavename;psname.append("PS");
510  mPhaseSpace[wavename]->SetTitle(psname.c_str());
511  mPhaseSpace[wavename]->SetName(psname.c_str());
512  mWaveEvidence[wavename]=0;
513  }
514 
515  return inserted.second;
516 }
517 
518 
519 
520 void
522  map<string,TMultiGraph*>::iterator it=mIntensities.begin();
523  multimap<unsigned int, string> m;
524  while(it!=mIntensities.end()){
525  // get ranges
526  TList* graphs=it->second->GetListOfGraphs();
527  unsigned int ng=graphs->GetEntries();
528  m.insert(pair<unsigned int,string>(ng,it->first));
529  //cout << it->first << " used:" << ng << endl;
530  ++it;
531  }
532  multimap<unsigned int, string>::iterator it2=m.begin();
533 
534  while(it2!=m.end()){
535  cout << it2->second << " used " << it2->first << " times"
536  << " with average evidence " << mWaveEvidence[it2->second]/(double)it2->first<< endl;
537  ++it2;
538  }
539 
540  double numwave=0;
541  for(unsigned int i=0;i<mResultMetaInfo.size();++i){
542  numwave+=mResultMetaInfo[i].nWaves;
543  }
544  numwave/=(double)mResultMetaInfo.size();
545  cout << "Average number of waves: " << numwave << endl;
546 
547 }
548 
549 
550 
551 
552 
553 void
555  map<string,TMultiGraph*>::iterator it=mIntensities.begin();
556  while(it!=mIntensities.end()){
557  // get ranges
558  TList* graphs=it->second->GetListOfGraphs();
559  unsigned int ng=graphs->GetEntries();
560  double xmin=1E9;double ymin=1E9;
561  double xmax=0; double ymax=-1E9;
562  unsigned int nbins=0;
563  for(unsigned int ig=0;ig<ng;++ig){
564  TPwaFitGraphErrors* g=dynamic_cast<TPwaFitGraphErrors*>(graphs->At(ig));
565  double xmin1,xmax1,ymin1,ymax1;
566  g->ComputeRange(xmin1,ymin1,xmax1,ymax1);
567 
568  if(ymin > ymin1)ymin=ymin1;
569  if(ymax < ymax1)ymax=ymax1;
570  unsigned int ifit=g->fitindex;
571  if(xmin > mResultMetaInfo[ifit].mMin)xmin=mResultMetaInfo[ifit].mMin;
572  if(xmax < mResultMetaInfo[ifit].mMax)xmax=mResultMetaInfo[ifit].mMax;
573  if(nbins< mResultMetaInfo[ifit].mNumBins)
574  nbins=mResultMetaInfo[ifit].mNumBins;
575  }
576  double r=fabs(ymax-ymin)*0.1;
577  // create 2D Histogram:
578  string name="d";name.append(it->first);
579 
580  //cerr << ymin << " .. " << ymax << endl;
581  TH2D* h=new TH2D(name.c_str(),name.c_str(),
582  nbins,xmin,xmax,
583  400,ymin-r,ymax+r);
584 
585  mIntensityDensityPlots[it->first]=h;
586 
587  // fill histo
588  for(unsigned int ig=0;ig<ng;++ig){
589  TPwaFitGraphErrors* g=dynamic_cast<TPwaFitGraphErrors*>(graphs->At(ig));
590  unsigned int ifit=g->fitindex;
591  double w=(mResultMetaInfo[ifit].mTotalPerEventEvidence)/(double)mResultMetaInfo[ifit].mNumBins;
592  //double likeli=0;
593  //cout << "weight: "<<TMath::Exp(w)<<endl;
594  if(w==w)h=drawDensity(g,h,TMath::Exp(w));
595  }
596  ++it;
597  // rescale each x-bin
598  for(unsigned int ibin=1; ibin<=nbins; ++ibin){
599  // get maximum bin in y for this x-bin
600  double max=0;
601  for(unsigned int iy=0;iy<400;++iy){
602  unsigned int bin = h->GetBin(ibin,iy);
603  double val=h->GetBinContent(bin);
604  if(val>max)max=val;
605  }
606  if(max!=0 && max==max){
607  for(unsigned int iy=0;iy<400;++iy){
608  unsigned int bin = h->GetBin(ibin,iy);
609  h->SetBinContent(bin,h->GetBinContent(bin)/max);
610  }
611  }
612  }
613 
614 
615  }// end loop over waves
616 
617 }
618 
619 
620 
621 void
623  TFile* outfile=TFile::Open(filename.c_str(),"RECREATE");
624  if(outfile!=0 && !outfile->IsZombie()){
625  writeAll(outfile);
626  outfile->Close();
627  }
628  else{
629  cerr << "Error opening file " << filename << endl;
630  }
631 }
632 
633 void
634 pwaPlotter::writeAll(TFile* outfile){
635  outfile->cd();
636  // write evidence and loglikelihoods
637  mLogLikelihood->Write();
638  mLogLikelihoodPerEvent->Write();
639  mEvidence->Write();
640  mEvidencePerEvent->Write();
641  writeAllIntensities(outfile);
642 }
643 
644 
645 void
647  TFile* outfile=TFile::Open(filename.c_str(),"RECREATE");
648  if(outfile!=0 && !outfile->IsZombie()){
649  writeAllIntensities(outfile);
650  outfile->Close();
651  }
652  else{
653  cerr << "Error opening file " << filename << endl;
654  }
655 }
656 
657 
658 
659 void
661  outfile->cd();
662  map<string,TMultiGraph*>::iterator it=mIntensities.begin();
663  while(it!=mIntensities.end()){
664  it->second->Write();
665  ++it;
666  }
667  map<string,TH2D*>::iterator itd=mIntensityDensityPlots.begin();
668  while(itd!=mIntensityDensityPlots.end()){
669  itd->second->Write();
670  ++itd;
671  }
672  map<string,TGraph*>::iterator itps=mPhaseSpace.begin();
673  while(itps!=mPhaseSpace.end()){
674  itps->second->Write();
675  ++itps;
676  }
677  map<strpair,TMultiGraph*>::iterator itph=mPhases.begin();
678  while(itph!=mPhases.end()){
679  itph->second->Write();
680  ++itph;
681  }
682 
683 }
684 
685 
686 
687 
688 
689 
690 // void
691 // pwaPlotter::plotIntensity(const std::string& wavename, TTree* tr){
692 // stringstream drawExpr;
693 // drawExpr << branchName << ".intensity(\"" << waveName << "\"):"
694 // << branchName << ".intensityErr(\"" << waveName << "\"):"
695 // << branchName << ".massBinCenter() >> h" << waveName << "_" << i;
696 // cout << " running TTree::Draw() expression '" << drawExpr.str() << "' "
697 // << "on tree '" << trees[i]->GetName() << "', '" << trees[i]->GetTitle() << "'" << endl;
698 
699 // cerr << "Drawing" << endl;
700 
701 // try{
702 // trees[i]->Draw(drawExpr.str().c_str(), selectExpr.c_str(), "goff");
703 // }
704 // catch(std::exception&){
705 // cerr << "Cought Exception" << endl;
706 // continue;
707 // }
708 
709 
710 
711 // // extract data from TTree::Draw() result and build graph
712 // const int nmbBins = trees[i]->GetSelectedRows();
713 // vector<double> x(nmbBins), xErr(nmbBins);
714 // vector<double> y(nmbBins), yErr(nmbBins);
715 // for (int j = 0; j < nmbBins; ++j) {
716 // x [j] = trees[i]->GetV3()[j] * 0.001; // convert mass to GeV
717 // xErr[j] = 0;
718 // y [j] = trees[i]->GetV1()[j] * normalization; // scale intensities
719 // yErr[j] = trees[i]->GetV2()[j] * normalization; // scale intensity errors
720 // }
721 
722 
723 
724 // TGraphErrors* g = new TGraphErrors(nmbBins,
725 // &(*(x.begin())), // mass
726 // &(*(y.begin())), // intensity
727 // &(*(xErr.begin())), // mass error
728 // &(*(yErr.begin()))); // intensity error
729 // {
730 // stringstream graphName;
731 // graphName << ((graphTitle == "") ? waveName : graphTitle) << "_" << i;
732 // g->SetName (graphName.str().c_str());
733 // g->SetTitle(graphName.str().c_str());
734 // }
735 // g->SetMarkerStyle(21);
736 // g->SetMarkerSize(0.5);
737 // if (graphColors) {
738 // g->SetMarkerColor(graphColors[i]);
739 // g->SetLineColor (graphColors[i]);
740 // }
741 // graph->Add(g);
742 
743 // // compute maximum for y-axis
744 // for (int j = 0; j < nmbBins; ++j)
745 // if (maxYVal < (y[j] + yErr[j]))
746 // maxYVal = y[j] + yErr[j];
747 // const double yMean = g->GetMean(2);
748 // if (maxYMean < yMean)
749 // maxYMean = yMean;
750 // }