ROOTPWA
plotGlobalWeightedEvts_Kpipi.C
Go to the documentation of this file.
1 /*
2  * IMPORTANT:
3  *
4  * The naming scheme of histogram files is critical in this program.
5  * More complex diagrams, that are automatically generated, rely on the specific scheme defined below.
6  * The program requests names for the Monte Carlo histograms to contain "MC_"
7  * or end with "MC". (the reason for _ is that the string doesnt no accidentally contain another "MC")
8  * The Data histograms must have the same filename as the corresponding MC histogram but "Data" replaced for "MC".
9  *
10  * Also for each massbin create a directory and create the same filenames for each variable
11  */
12 
13 #include "TFile.h"
14 #include "TLatex.h"
15 #include "TH1D.h"
16 #include "TH2D.h"
17 #include "TLine.h"
18 #include "TROOT.h"
19 #include "TStyle.h"
20 #include "TColor.h"
21 #include "TString.h"
22 #include "TObject.h"
23 #include "TCollection.h"
24 #include "TCanvas.h"
25 #include "TExec.h"
26 #include "TGaxis.h"
27 #include <iostream>
28 #include <string>
29 #include <sstream>
30 #include <map>
31 #include <stdlib.h>
32 #include <cmath>
33 #include <vector>
34 #include <algorithm>
35 
36 
37 int massbinwidth = 0;
38 const unsigned int numbercolors = 31;
39 
40 struct booky_page {
41  TCanvas *page_canvas; // canvas that will be print on that page
42  double mass; // mass value with can be used for sorting purposes
43  TString page_title; // title displayed on top of page
44 };
45 
46 booky_page createBookyPage(TCanvas *c, double mass = 0.0, TString title = "") {
47  booky_page p;
48  p.page_canvas = c;
49  p.mass = mass;
50  p.page_title = title;
51  return p;
52 }
53 
54 // the make booky method will run through this map and create 1 booky for each map entry
55 // all canvases in the vector will be appended to this booky ps file
56 // key is the filename of the booky (without .ps), vector<TCanvas*> is the canvases that will be attached to this booky
57 // we save pairs in the vector instead, to be able to sort the canvases with ascending mass order (the double is the mass)
58 std::map<TString, std::vector<booky_page> > booky_map;
59 
60 std::map<TString, std::vector<TString> > booky_setup_map;
61 // here just fill a vector with Histogram names and add this to the booky_setup_map with the name of the booky
62 void setupBookies() {
63  // lets create a booky for the the 5 1D projections (3 FS particle case)
64  std::vector<TString> histnames_neutral;
65  std::vector<TString> histnames_charged;
66  // neutral isobar decay
67  histnames_neutral.push_back(TString("hMIsobarMC_Kpi")); // Canvas spot 1
68  histnames_neutral.push_back(TString("spacer")); // Canvas spot 2
69  histnames_neutral.push_back(TString("hGJMC_Kpi")); // Canvas spot 3
70  histnames_neutral.push_back(TString("hTYMC_Kpi")); // Canvas spot 4
71  histnames_neutral.push_back(TString("hHThetaMC_Kpi")); // Canvas spot 5
72  histnames_neutral.push_back(TString("hHPhiMC_Kpi")); // Canvas spot 6
73  // charged isobar decay
74  histnames_charged.push_back(TString("hMIsobarMC_pipi")); // Canvas spot 1
75  histnames_charged.push_back(TString("spacer")); // Canvas spot 2
76  histnames_charged.push_back(TString("hGJMC_pipi")); // Canvas spot 3
77  histnames_charged.push_back(TString("hTYMC_pipi")); // Canvas spot 4
78  histnames_charged.push_back(TString("hHThetaMC_pipi")); // Canvas spot 5
79  histnames_charged.push_back(TString("hHPhiMC_pipi")); // Canvas spot 6
80 
81  booky_setup_map.insert(std::pair<TString, std::vector<TString> >("Booky_Kpi_isobar", histnames_neutral));
82  booky_setup_map.insert(std::pair<TString, std::vector<TString> >("Booky_pipi_isobar", histnames_charged));
83 }
84 
85 bool comparePairs (const booky_page &i, const booky_page &j) {
86  return (i.mass < j.mass);
87 }
88 
89 void makeBookies() {
90  // loop through map
91  std::map<TString, std::vector<booky_page> >::iterator it;
92  for(it = booky_map.begin(); it != booky_map.end(); it++) {
93  TString psFileName(it->first);
94  psFileName.Append(".ps");
95  TCanvas dummyCanv("dummy", "dummy", 800, 800);
96  dummyCanv.Print((psFileName + "["));
97  // sort vector
98  std::vector<booky_page> vec = it->second;
99  std::sort(vec.begin(), vec.end(), comparePairs);
100  // loop through vector and append canvases
101  for (unsigned int i = 0; i < vec.size(); i++) {
102  TString textlabel;
103  if (vec[i].page_title.Length() == 0) {
104  textlabel = "#scale[0.5]{Mass Bin ";
105  textlabel += i + 1;
106  textlabel += " [";
107  textlabel += (int) (vec[i].mass * 1000 - massbinwidth / 2);
108  textlabel += " - ";
109  textlabel += ((int) (vec[i].mass * 1000) + massbinwidth / 2);
110  textlabel += "] MeV/c^{2}}";
111  }
112  else {
113  textlabel = vec[i].page_title;
114  }
115  TLatex label(0.37, 1.0, textlabel);
116  label.SetNDC(true);
117  label.SetTextAlign(23);
118  vec[i].page_canvas->cd();
119  label.Draw();
120 
121  vec[i].page_canvas->Print(psFileName);
122  }
123  dummyCanv.Print((psFileName + "]"));
124  }
125 }
126 
127 void fillDiffvsMassPlot(const TH1D* hist, std::string dirname, int massbins, double mass_start, double mass_end,
128  std::map<std::string, std::pair<double, std::pair<double, double> > > diffbounds, TFile* ofile) {
129  // first change into global directory of outfile
130  ofile->cd("global");
131  // now parse mass from dirname
132  int pos = dirname.find(".");
133  std::string masslow = dirname.substr(0, pos+1);
134  std::string masshigh = dirname.substr(pos+1);
135  double mass = atof(masslow.c_str()) + atof(masshigh.c_str());
136  mass = mass/2/1000;
137  // then get or create 2d hist with number of bins in x equal to number of massbins
138  std::string s(hist->GetName());
139  std::map<std::string, std::pair<double, std::pair<double, double> > >::iterator iter = diffbounds.find(s);
140  s.insert(s.size(), "vsMass");
141  TH2D* hdiffvsmass = (TH2D*)ofile->Get((std::string("global/")+s).c_str());
142  if(!hdiffvsmass) {
143  TCanvas *c = new TCanvas(s.c_str(), s.c_str(), 800, 800);
144  hdiffvsmass = new TH2D(s.c_str(), hist->GetTitle(), massbins, mass_start, mass_end,
145  hist->GetNbinsX(), iter->second.second.first, iter->second.second.second);
146  hdiffvsmass->SetContour(numbercolors);
147  hdiffvsmass->SetXTitle("Resonance Mass [GeV/c^{2}]");
148  hdiffvsmass->SetYTitle(hist->GetXaxis()->GetTitle());
149  hdiffvsmass->SetMaximum(iter->second.first);
150  hdiffvsmass->SetMinimum(-iter->second.first);
151  hdiffvsmass->SetStats(0);
152  hdiffvsmass->Draw("colz");
153 
154  // now lets add this canvas to its corresponding booky
155  // first check if our booky_map already has an open booky for this type and get the vector
156  std::vector<booky_page>& tempvec = booky_map["Booky_mass_overview"];
157  // create new entry for this vector
158  tempvec.push_back(createBookyPage(c, mass, TString(hist->GetName())));
159  }
160  // then loop over 1d hist bins -> corresponding value + get content and fill 2d hist
161  for(int i = 0; i < hist->GetNbinsX(); i++) {
162  double diffpos = hist->GetBinCenter(i);
163  double diffval = hist->GetBinContent(i);
164  if(diffval != 0.0)
165  hdiffvsmass->Fill(mass, diffpos, diffval);
166  }
167 }
168 
169 
170 void make2DOverviewCanvas(TH2D *mchist, TH2D *datahist, TH2D *diffhist, TH2D *reldiffhist, double mass) {
171  if (mchist && datahist && diffhist && reldiffhist) {
172  double scale = datahist->Integral();
173  scale = scale / (mchist->Integral());
174  mchist->Scale(scale);
175 
176  double max = mchist->GetMaximum();
177  if(datahist->GetMaximum() > max)
178  mchist->SetMaximum(datahist->GetMaximum());
179  else
180  datahist->SetMaximum(max);
181 
182 
183  mchist->GetXaxis()->SetRangeUser(0.0, pow(mass,2));
184  mchist->GetYaxis()->SetRangeUser(0.0, pow(mass,2));
185  datahist->GetXaxis()->SetRangeUser(0.0, pow(mass,2));
186  datahist->GetYaxis()->SetRangeUser(0.0, pow(mass,2));
187  diffhist->GetXaxis()->SetRangeUser(0.0, pow(mass,2));
188  diffhist->GetYaxis()->SetRangeUser(0.0, pow(mass,2));
189  reldiffhist->GetXaxis()->SetRangeUser(0.0, pow(mass,2));
190  reldiffhist->GetYaxis()->SetRangeUser(0.0, pow(mass,2));
191  reldiffhist->SetMaximum(1.0);
192  reldiffhist->SetMinimum(-1.0);
193 
194 
195  TString s("setDiffColorStyle(");
196  s += numbercolors;
197  s += ");";
198 
199  char name[200];
200  sprintf(name, "%s_%f", mchist->GetName(), mass);
201  TCanvas *c = new TCanvas(name, mchist->GetTitle(), 800, 800);
202  c->Divide(2,2);
203  c->cd(1);
204  mchist->Draw("colz");
205  mchist->SetContour(numbercolors);
206  //TExec *ex1 = new TExec("ex1", "setNormalColorStyle();");
207  TExec *ex1 = new TExec("ex1", "gStyle->SetPalette(1);");
208  ex1->Draw();
209  mchist->Draw("colz same");
210  c->cd(2);
211  datahist->Draw("colz");
212  datahist->SetContour(numbercolors);
213  //TExec *ex2 = new TExec("ex2", "setNormalColorStyle();");
214  TExec *ex2 = new TExec("ex2", "gStyle->SetPalette(1);");
215  ex2->Draw();
216  datahist->Draw("colz same");
217  c->cd(3);
218  diffhist->Draw("colz");
219  diffhist->SetContour(numbercolors);
220  TExec *ex3 = new TExec("ex3", s);
221  ex3->Draw();
222  diffhist->Draw("colz same");
223  c->cd(4);
224  reldiffhist->Draw("colz");
225  reldiffhist->SetContour(numbercolors);
226  TExec *ex4 = new TExec("ex4", s);
227  ex4->Draw();
228  reldiffhist->Draw("colz same");
229  c->Update();
230  c->Write();
231 
232  // now lets add this canvas to its corresponding booky
233  // first check if our booky_map already has an open booky for this type and get the vector
234  std::vector<booky_page>& tempvec = booky_map[mchist->GetName()];
235  // create new entry for this vector
236  tempvec.push_back(createBookyPage(c, mass));
237  }
238 }
239 
240 
241 void make1DOverviewCanvas(TFile *infile, TFile *outfile, TList *mclist, std::string dirname) {
242  double mass = 0.0;
243  int massstart = 0;
244  int massend = 0;
245  // check if directory is mass bin dir
246  unsigned int pointpos = dirname.find(".");
247  if (!(pointpos == 0 || pointpos == dirname.size())) {
248  std::string masslow = dirname.substr(0, pointpos + 1);
249  std::string masshigh = dirname.substr(pointpos + 1);
250  massstart = atoi(masslow.c_str());
251  massend = atoi(masshigh.c_str());
252  mass = 1.0 * (massstart + massend) / 2 / 1000;
253  }
254 
255  std::map<TString, std::vector<TString> >::iterator it;
256  for (it = booky_setup_map.begin(); it != booky_setup_map.end(); it++) {
257  TString name(it->first.Data());
258  name += dirname.c_str();
259  string isoname;
260  if (it->first == "Booky_Kpi_isobar"){
261  isoname = "(K^{-} #pi^{+})";
262  } else {
263  isoname = "(#pi^{-} #pi^{+})";
264  }
265 
266  TCanvas *c = new TCanvas(name, "", 800, 800);
267  c->Divide(2,3);
268  std::vector<TString> histlist = it->second;
269 
270  for (unsigned int i = 0; i < histlist.size(); i++) {
271  // CompareTo returns 0 if its a match....
272  if (histlist[i].CompareTo("spacer")) {
273  TIter histiter = TIter(mclist);
274  TH1D *reldiffhist, *diffhist, *mchist, *datahist;
275  // generate difference histograms
276  std::string hnamemc(histlist[i].Data());
277  // create new string with MC exchanged for Diff
278  std::string hnamediff(hnamemc);
279  int pos = hnamemc.find("MC");
280  hnamediff.erase(pos, 2);
281  hnamediff.insert(pos, "Diff");
282  // create new string with MC exchanged for RelDiff
283  std::string hnamereldiff(hnamemc);
284  hnamereldiff.erase(pos, 2);
285  hnamereldiff.insert(pos, "RelDiff");
286  // create new string with MC exchanged for Data
287  std::string hnamedata(hnamemc);
288  hnamedata.erase(pos, 2);
289  hnamedata.insert(pos, "Data");
290 
291  infile->GetObject((dirname + "/" + hnamereldiff).c_str(), reldiffhist);
292  infile->GetObject((dirname + "/" + hnamediff).c_str(), diffhist);
293  infile->GetObject((dirname + "/" + hnamedata).c_str(), datahist);
294  infile->GetObject((dirname + "/" + hnamemc).c_str(), mchist);
295 
296  outfile->cd(dirname.c_str());
297  if (mchist && datahist) {
298  stringstream title;
299  title << isoname << " " << mchist->GetXaxis()->GetTitle();
300  mchist->GetXaxis()->SetTitle( title.str().c_str() );
301  title.str("");
302  title << isoname << " " << datahist->GetXaxis()->GetTitle();
303  datahist->GetXaxis()->SetTitle( title.str().c_str() );
304  title.str("");
305  c->cd(i + 1);
306 
307  // std::cout<<i<<std::endl;
308  double scale = datahist->Integral();
309  scale = scale / (mchist->Integral());
310  mchist->Scale(scale);
311 
312  mchist->SetLineColor(kRed);
313  mchist->SetFillColor(kRed);
314  mchist->Draw("E4");
315  datahist->Draw("same");
316  if (diffhist) {
317  title << isoname << " " << diffhist->GetXaxis()->GetTitle();
318  diffhist->GetXaxis()->SetTitle( title.str().c_str() );
319  title.str("");
320  if (reldiffhist){
321  title << isoname << " " << reldiffhist->GetXaxis()->GetTitle();
322  reldiffhist->GetXaxis()->SetTitle( title.str().c_str() );
323  title.str("");
324  }
325  TLine* line = new TLine(mchist->GetXaxis()->GetXmin(), 0, mchist->GetXaxis()->GetXmax(), 0);
326  line->SetLineStyle(3);
327  line->Draw();
328  diffhist->SetLineColor(kOrange - 3);
329  diffhist->Draw("same");
330  }
331  double max = mchist->GetMaximum();
332  double min = mchist->GetMinimum();
333  if (max < datahist->GetMaximum())
334  max = datahist->GetMaximum();
335  if (diffhist)
336  min = diffhist->GetMinimum();
337  mchist->GetYaxis()->SetRangeUser(diffhist->GetMinimum() * 1.5, max * 1.2);
338  c->Update();
339  }
340  }
341  }
342  c->Write();
343  // now lets add this canvas to its corresponding booky
344  // first check if our booky_map already has an open booky for this type and get the vector
345  std::vector<booky_page>& tempvec = booky_map[it->first];
346  // create new entry for this vector
347  tempvec.push_back(createBookyPage(c, mass));
348  }
349 }
350 
351 /*
352  * this script takes 2 TStrings as root filenames as a parameters
353  * basic functionality:
354  * loop through all directories (the mass bins) in the root file
355  * -> create difference plots
356  * -> create global plots
357  * -> create 2D diff vs mass plots
358  * -> etc...
359  */
360 void plotGlobalWeightedEvts_Kpipi(TString input_filename, TString output_filename) {
361  setupBookies();
362 
363  gROOT->SetStyle("Plain");
364  gStyle->SetTitleFont(10*13+2,"xyz");
365  gStyle->SetTitleSize(0.06, "xyz");
366  gStyle->SetTitleOffset(1.3,"y");
367  gStyle->SetTitleOffset(1.3,"z");
368  gStyle->SetLabelFont(10*13+2,"xyz");
369  gStyle->SetLabelSize(0.06,"xyz");
370  gStyle->SetLabelOffset(0.009,"xyz");
371  gStyle->SetPadBottomMargin(0.16);
372  gStyle->SetPadTopMargin(0.16);
373  gStyle->SetPadLeftMargin(0.16);
374  gStyle->SetPadRightMargin(0.16);
375  gStyle->SetOptTitle(0);
376  gStyle->SetOptStat(0);
377  gROOT->ForceStyle();
378  gStyle->SetFrameFillColor(0);
379  gStyle->SetFrameFillStyle(0);
380  TGaxis::SetMaxDigits(3);
381  //IsPhDStyle = true;
382 
383  int massbins =0;
384  double mass= 0.0, massstart =1000.0, massend=0.0;
385  std::map<std::string, std::pair<double, std::pair<double, double> > > diffbounds;
386 
387  TFile* infile = TFile::Open(input_filename, "READ");
388  TFile* outfile = new TFile(output_filename, "RECREATE");
389  outfile->mkdir("global");
390 
391  TList *dirlist = infile->GetListOfKeys();
392  massbins = dirlist->GetSize();
393  infile->cd();
394  TIter diriter(dirlist);
395  TDirectory *dir;
396 
397  std::cout<< "scanning directories and creating overview canvases..." <<std::endl;
398  while ((dir = (TDirectory *)diriter())) {
399  std::string dirname = dir->GetName();
400  // check if directory is mass bin dir
401  unsigned int pointpos = dirname.find(".");
402  if(pointpos == 0 || pointpos == dirname.size()) continue;
403  std::string masslow = dirname.substr(0, pointpos+1);
404  std::string masshigh = dirname.substr(pointpos+1);
405  double massstarttemp = atof(masslow.c_str())/1000;
406  double massendtemp = atof(masshigh.c_str())/1000;
407  if((int)(massendtemp - massstarttemp) != massbinwidth)
408  massbinwidth = (int)(massendtemp - massstarttemp);
409  mass = (massstarttemp + massendtemp)/2;
410  if(massstart > massstarttemp) massstart = massstarttemp;
411  if(massend < massendtemp) massend = massendtemp;
412 
413  outfile->cd();
414  outfile->mkdir(dir->GetName());
415  infile->cd(dir->GetName());
416 
417  // make list of MC Histograms
418  TList mclist;
419  TList *histlist = gDirectory->GetListOfKeys();
420  TIter histiter(histlist);
421  TObject *obj;
422  while ((obj = histiter())) {
423  TString s(obj->GetName());
424  if(s.EndsWith("MC"))
425  mclist.Add(obj);
426  else if(s.Contains("MC_"))
427  mclist.Add(obj);
428  }
429  make1DOverviewCanvas(infile, outfile, &mclist, dirname);
430  histiter = TIter(&mclist);
431  TH1D *diffhist, *mchist;
432  while ((mchist = (TH1D*)histiter())) {
433  // generate difference histograms
434  std::string hnamemc(mchist->GetName());
435  // create new string with MC exchanged for Diff
436  std::string hnamediff(hnamemc);
437  int pos = hnamemc.find("MC");
438  hnamediff.erase(pos, 2);
439  hnamediff.insert(pos, "Diff");
440 
441  infile->GetObject((std::string(dir->GetName())+"/"+hnamediff).c_str(), diffhist);
442 
443  if (diffhist) {
444  // get diff min max values
445  std::pair<double, std::pair<double, double> > p;
446 
447  bool change =false;
448  double maxdiff = diffhist->GetMaximum();
449  double maxdifftemp = diffhist->GetMinimum();
450  if(abs(maxdifftemp) > maxdiff) maxdiff = maxdifftemp;
451 
452  double diffmintemp = diffhist->GetXaxis()->GetXmin();
453  double diffmaxtemp = diffhist->GetXaxis()->GetXmax();
454  std::map<std::string, std::pair<double, std::pair<double, double> > >::iterator iter = diffbounds.find(
455  diffhist->GetName());
456  if (iter != diffbounds.end()) {
457  p.first = iter->second.first;
458  p.second.first = iter->second.second.first;
459  p.second.second = iter->second.second.second;
460 
461  if (iter->second.first < maxdiff) {
462  change = true;
463  p.first = maxdiff;
464  }
465  if (iter->second.second.first > diffmintemp) {
466  change = true;
467  p.second.first = diffmintemp;
468  }
469  if (iter->second.second.second < diffmaxtemp) {
470  change = true;
471  p.second.second = diffmaxtemp;
472  }
473 
474  if (change) {
475  diffbounds[diffhist->GetName()] = p;
476  }
477  }
478  else {
479  p.first = maxdiff;
480  p.second.first = diffmintemp;
481  p.second.second = diffmaxtemp;
482  diffbounds.insert(std::pair<std::string, std::pair<double, std::pair<double, double> > >(diffhist->GetName(),
483  p));
484  }
485  }
486  }
487  histiter = TIter(&mclist);
488  TH2D *reldiffhist2d, *diffhist2d, *mchist2d, *datahist2d;
489  while ((mchist2d = (TH2D*) histiter())) {
490  // generate difference histograms
491  std::string hnamemc(mchist2d->GetName());
492  // create new string with MC exchanged for Diff
493  std::string hnamediff(hnamemc);
494  int pos = hnamemc.find("MC");
495  hnamediff.erase(pos, 2);
496  hnamediff.insert(pos, "Diff");
497  // create new string with MC exchanged for RelDiff
498  std::string hnamereldiff(hnamemc);
499  hnamereldiff.erase(pos, 2);
500  hnamereldiff.insert(pos, "RelDiff");
501  // create new string with MC exchanged for Data
502  std::string hnamedata(hnamemc);
503  hnamedata.erase(pos, 2);
504  hnamedata.insert(pos, "Data");
505 
506  infile->GetObject((std::string(dir->GetName()) + "/" + hnamereldiff).c_str(), reldiffhist2d);
507  infile->GetObject((std::string(dir->GetName()) + "/" + hnamediff).c_str(), diffhist2d);
508  infile->GetObject((std::string(dir->GetName()) + "/" + hnamedata).c_str(), datahist2d);
509  infile->GetObject((std::string(dir->GetName()) + "/" + hnamemc).c_str(), mchist2d);
510 
511  outfile->cd(dir->GetName());
512  make2DOverviewCanvas(mchist2d, datahist2d, diffhist2d, reldiffhist2d, mass);
513  }
514  }
515 
516  dirlist = infile->GetListOfKeys();
517  infile->cd();
518  diriter = TIter(dirlist);
519 
520  std::cout << "creating global histograms and 2D diff vs mass plots..." << std::endl;
521  while ((dir = (TDirectory *) diriter())) {
522  std::string dirname = dir->GetName();
523  // check if directory is mass bin dir
524  unsigned int pointpos = dirname.find(".");
525  if (pointpos == 0 || pointpos == dirname.size())
526  continue;
527 
528  infile->cd(dir->GetName());
529 
530  // make list of MC Histograms
531  TList mclist;
532  TList *histlist = gDirectory->GetListOfKeys();
533  TIter histiter(histlist);
534  TObject *obj;
535  while ((obj = histiter())) {
536  TString s(obj->GetName());
537  if (s.EndsWith("MC"))
538  mclist.Add(obj);
539  else if (s.Contains("MC_"))
540  mclist.Add(obj);
541  }
542  histiter = TIter(&mclist);
543  TH1D *hist;
544  TH1D *diffhist, *mchist, *datahist;
545  while ((hist = (TH1D*) histiter())) {
546  // generate difference histograms
547  std::string hnamemc(hist->GetName());
548  // create new string with MC exchanged for Diff
549  std::string hname(hnamemc);
550  int pos = hnamemc.find("MC");
551  hname.erase(pos, 2);
552  hname.insert(pos, "Diff");
553  // create new string with MC exchanged for Data
554  std::string hnamedata(hnamemc);
555  hnamedata.erase(pos, 2);
556  hnamedata.insert(pos, "Data");
557  // create new string for MC Global Histogram
558  std::string hnamemcglob(hnamemc);
559  hnamemcglob.insert(pos + 2, "Global");
560  // create new string for MC Global Histogram
561  std::string hnamedataglob(hnamemc);
562  hnamedataglob.erase(pos, 2);
563  hnamedataglob.insert(pos, "DataGlobal");
564 
565  infile->GetObject((std::string(dir->GetName()) + "/" + hname + ";1").c_str(), diffhist);
566  infile->GetObject((std::string(dir->GetName()) + "/" + hnamedata + ";1").c_str(), datahist);
567  infile->GetObject((std::string(dir->GetName()) + "/" + hnamemc + ";1").c_str(), mchist);
568  if (datahist) {
569  // make global histograms in global folder
570  outfile->cd("global");
571  TH1D* hmcglob = (TH1D*) outfile->Get(std::string("global/"+hnamemcglob).c_str());
572  if (hmcglob == NULL)
573  hmcglob = new TH1D(hnamemcglob.c_str(), mchist->GetTitle(), mchist->GetNbinsX(),
574  mchist->GetXaxis()->GetXmin(), mchist->GetXaxis()->GetXmax());
575  hmcglob->Add(mchist);
576  TH1D* hdataglob = (TH1D*) outfile->Get(std::string("global/"+hnamedataglob).c_str());
577  if (hdataglob == NULL)
578  hdataglob = new TH1D(hnamedataglob.c_str(), datahist->GetTitle(), datahist->GetNbinsX(),
579  datahist->GetXaxis()->GetXmin(), datahist->GetXaxis()->GetXmax());
580  hdataglob->Add(datahist);
581 
582  // make diff vs. mass plots
583  fillDiffvsMassPlot(diffhist, dir->GetName(), massbins, massstart, massend, diffbounds, outfile);
584  }
585  }
586  histiter = TIter(&mclist);
587  TH2D *mchist2d, *datahist2d;
588  while ((mchist2d = (TH2D*) histiter())) {
589  // generate difference histograms
590  std::string hnamemc(mchist2d->GetName());
591  // create new string with MC exchanged for Diff
592  std::string hnamediff(hnamemc);
593  int pos = hnamemc.find("MC");
594  hnamediff.erase(pos, 2);
595  hnamediff.insert(pos, "Diff");
596  // create new string with MC exchanged for Data
597  std::string hnamedata(hnamemc);
598  hnamedata.erase(pos, 2);
599  hnamedata.insert(pos, "Data");
600  // create new string for MC Global Histogram
601  std::string hnamemcglob(hnamemc);
602  hnamemcglob.insert(pos + 2, "Global");
603  // create new string for MC Global Histogram
604  std::string hnamedataglob(hnamemc);
605  hnamedataglob.erase(pos, 2);
606  hnamedataglob.insert(pos, "DataGlobal");
607 
608  infile->GetObject((std::string(dir->GetName()) + "/" + hnamedata + ";1").c_str(), datahist2d);
609  infile->GetObject((std::string(dir->GetName()) + "/" + hnamemc + ";1").c_str(), mchist2d);
610  if (datahist2d) {
611  // make global histograms in global folder
612  outfile->cd("global");
613  TH2D* hmcglob = (TH2D*) outfile->Get(std::string("global/" + hnamemcglob).c_str());
614  if (hmcglob == NULL) {
615  hmcglob = new TH2D(hnamemcglob.c_str(), mchist2d->GetTitle(), mchist->GetNbinsX(),
616  mchist2d->GetXaxis()->GetXmin(), mchist2d->GetXaxis()->GetXmax(), mchist2d->GetNbinsY(),
617  mchist2d->GetYaxis()->GetXmin(), mchist2d->GetYaxis()->GetXmax());
618  hmcglob->SetXTitle(mchist2d->GetXaxis()->GetTitle());
619  hmcglob->SetYTitle(mchist2d->GetYaxis()->GetTitle());
620  }
621  hmcglob->Add(mchist2d);
622  TH2D* hdataglob = (TH2D*) outfile->Get(std::string("global/" + hnamedataglob).c_str());
623  if (hdataglob == NULL) {
624  hdataglob = new TH2D(hnamedataglob.c_str(), datahist2d->GetTitle(), datahist2d->GetNbinsX(),
625  datahist2d->GetXaxis()->GetXmin(), datahist2d->GetXaxis()->GetXmax(), datahist2d->GetNbinsY(),
626  datahist2d->GetYaxis()->GetXmin(), datahist2d->GetYaxis()->GetXmax());
627  hdataglob->SetXTitle(datahist2d->GetXaxis()->GetTitle());
628  hdataglob->SetYTitle(datahist2d->GetYaxis()->GetTitle());
629  }
630  hdataglob->Add(datahist2d);
631  }
632  }
633  }
634 
635  makeBookies();
636 
637  std::cout<< "saving to disk..." <<std::endl;
638  outfile->Write();
639  std::cout<< "done!" <<std::endl;
640 
641  /*// ok lets make a canvas and plug some plots into it -> add to booky
642  TCanvas* c = new TCanvas("KineValidate" + massbin, "Weighted Events", 10, 10, 600, 800);
643  c->Divide(4, 4);
644 
645  // first column contains neutral isobar histograms
646  c->cd(1);
647 
648  double totMC = GJHB_neutral_isobar.isobar_mass[0]->Integral();
649  double totDATA = GJHB_neutral_isobar.isobar_mass[1]->Integral();
650 
651  if (totMC != 0)
652  GJHB_neutral_isobar.isobar_mass[0]->Scale(totDATA / totMC);
653  GJHB_neutral_isobar.isobar_mass[0]->SetLineColor(kRed);
654  GJHB_neutral_isobar.isobar_mass[0]->SetFillColor(kRed);
655  GJHB_neutral_isobar.isobar_mass[0]->Draw("E4");
656 
657  TH1D* hDiffMIsobar = new TH1D(*GJHB_neutral_isobar.isobar_mass[0]);
658  hDiffMIsobar->Add(GJHB_neutral_isobar.isobar_mass[1], -1.);
659  GJHB_neutral_isobar.isobar_mass[1]->Draw("same");
660  hDiffMIsobar->SetLineColor(kOrange - 3);
661  hDiffMIsobar->Draw("same");
662 
663  GJHB_neutral_isobar.isobar_mass[0]->GetYaxis()->SetRangeUser(hDiffMIsobar->GetMinimum() * 1.5,
664  GJHB_neutral_isobar.isobar_mass[0]->GetMaximum() * 1.2);
665  gPad->Update();
666 
667  c->cd(5);
668  totMC = GJHB_neutral_isobar.costheta_GJF[0]->Integral();
669  totDATA = GJHB_neutral_isobar.costheta_GJF[1]->Integral();
670  if (totMC != 0)
671  GJHB_neutral_isobar.costheta_GJF[0]->Scale(totDATA / totMC);
672  GJHB_neutral_isobar.costheta_GJF[0]->SetLineColor(kRed);
673  GJHB_neutral_isobar.costheta_GJF[0]->SetFillColor(kRed);
674  GJHB_neutral_isobar.costheta_GJF[0]->Draw("E4");
675 
676  GJHB_neutral_isobar.costheta_GJF[1]->Draw("same E");
677 
678  totMC = GJHB_neutral_isobar.costheta_GJF_MC_raw->Integral();
679  GJHB_neutral_isobar.costheta_GJF_MC_raw->Scale(totDATA / totMC);
680  GJHB_neutral_isobar.costheta_GJF_MC_raw->Draw("same");
681 
682  GJHB_neutral_isobar.costheta_GJF[0]->GetYaxis()->SetRangeUser(0, GJHB_neutral_isobar.costheta_GJF[0]->GetMaximum() * 1.5);
683  gPad->Update();
684 
685  c->cd(9);
686  totMC = GJHB_neutral_isobar.phi_GJF[0]->Integral();
687  totDATA = GJHB_neutral_isobar.phi_GJF[1]->Integral();
688  GJHB_neutral_isobar.phi_GJF[0]->Sumw2();
689  if (totMC != 0)
690  GJHB_neutral_isobar.phi_GJF[0]->Scale(totDATA / totMC);
691  GJHB_neutral_isobar.phi_GJF[0]->SetLineColor(kRed);
692  GJHB_neutral_isobar.phi_GJF[0]->SetFillColor(kRed);
693  GJHB_neutral_isobar.phi_GJF[0]->Draw("E4");
694 
695  GJHB_neutral_isobar.phi_GJF[1]->Draw("same E");
696  GJHB_neutral_isobar.phi_GJF[0]->GetYaxis()->SetRangeUser(0, GJHB_neutral_isobar.phi_GJF[0]->GetMaximum() * 1.5);
697  gPad->Update();
698 
699  c->cd(13);
700  totMC = HHB_neutral_isobar.costheta_HF[0]->Integral();
701  totDATA = HHB_neutral_isobar.costheta_HF[1]->Integral();
702  HHB_neutral_isobar.costheta_HF[0]->Sumw2();
703  if (totMC != 0)
704  HHB_neutral_isobar.costheta_HF[0]->Scale(totDATA / totMC);
705  HHB_neutral_isobar.costheta_HF[0]->SetLineColor(kRed);
706  HHB_neutral_isobar.costheta_HF[0]->SetFillColor(kRed);
707  HHB_neutral_isobar.costheta_HF[0]->Draw("E4");
708 
709  HHB_neutral_isobar.costheta_HF[1]->Draw("same E");
710  HHB_neutral_isobar.costheta_HF[0]->GetYaxis()->SetRangeUser(0,
711  HHB_neutral_isobar.costheta_HF[0]->GetMaximum() * 1.5);
712  gPad->Update();
713 
714  c->cd(15);
715  totMC = HHB_neutral_isobar.phi_HF[0]->Integral();
716  totDATA = HHB_neutral_isobar.phi_HF[1]->Integral();
717  HHB_neutral_isobar.phi_HF[0]->Sumw2();
718  if (totMC != 0)
719  HHB_neutral_isobar.phi_HF[0]->Scale(totDATA / totMC);
720  HHB_neutral_isobar.phi_HF[0]->SetLineColor(kRed);
721  HHB_neutral_isobar.phi_HF[0]->SetFillColor(kRed);
722  HHB_neutral_isobar.phi_HF[0]->Draw("E4");
723 
724  HHB_neutral_isobar.phi_HF[1]->Draw("same E");
725  HHB_neutral_isobar.phi_HF[0]->GetYaxis()->SetRangeUser(0,
726  HHB_neutral_isobar.phi_HF[0]->GetMaximum() * 1.5);
727  gPad->Update();
728 
729  c->cd(2);
730 
731  totMC = GJHB_charged_isobar.isobar_mass[0]->Integral();
732  totDATA = GJHB_charged_isobar.isobar_mass[1]->Integral();
733 
734  if (totMC != 0)
735  GJHB_charged_isobar.isobar_mass[0]->Scale(totDATA / totMC);
736  GJHB_charged_isobar.isobar_mass[0]->SetLineColor(kRed);
737  GJHB_charged_isobar.isobar_mass[0]->SetFillColor(kRed);
738  GJHB_charged_isobar.isobar_mass[0]->Draw("E4");
739 
740  TH1D* hDiffMIsobar2 = new TH1D(*GJHB_charged_isobar.isobar_mass[0]);
741  hDiffMIsobar2->Add(GJHB_charged_isobar.isobar_mass[1], -1.);
742  GJHB_charged_isobar.isobar_mass[1]->Draw("same");
743  hDiffMIsobar2->SetLineColor(kOrange - 3);
744  hDiffMIsobar2->Draw("same");
745 
746  GJHB_charged_isobar.isobar_mass[0]->GetYaxis()->SetRangeUser(hDiffMIsobar->GetMinimum() * 1.5,
747  GJHB_charged_isobar.isobar_mass[0]->GetMaximum() * 1.2);
748  gPad->Update();
749 
750  c->cd(6);
751  totMC = GJHB_charged_isobar.costheta_GJF[0]->Integral();
752  totDATA = GJHB_charged_isobar.costheta_GJF[1]->Integral();
753  //hGJ[0]->Sumw2();
754  if (totMC != 0)
755  GJHB_charged_isobar.costheta_GJF[0]->Scale(totDATA / totMC);
756  GJHB_charged_isobar.costheta_GJF[0]->SetLineColor(kRed);
757  GJHB_charged_isobar.costheta_GJF[0]->SetFillColor(kRed);
758  GJHB_charged_isobar.costheta_GJF[0]->Draw("E4");
759 
760  GJHB_charged_isobar.costheta_GJF[1]->Draw("same E");
761 
762  totMC = GJHB_charged_isobar.costheta_GJF_MC_raw->Integral();
763  GJHB_charged_isobar.costheta_GJF_MC_raw->Scale(totDATA / totMC);
764  GJHB_charged_isobar.costheta_GJF_MC_raw->Draw("same");
765 
766  GJHB_charged_isobar.costheta_GJF[0]->GetYaxis()->SetRangeUser(0, GJHB_charged_isobar.costheta_GJF[0]->GetMaximum() * 1.5);
767  gPad->Update();
768 
769  c->cd(10);
770  totMC = GJHB_charged_isobar.phi_GJF[0]->Integral();
771  totDATA = GJHB_charged_isobar.phi_GJF[1]->Integral();
772  GJHB_charged_isobar.phi_GJF[0]->Sumw2();
773  if (totMC != 0)
774  GJHB_charged_isobar.phi_GJF[0]->Scale(totDATA / totMC);
775  GJHB_charged_isobar.phi_GJF[0]->SetLineColor(kRed);
776  GJHB_charged_isobar.phi_GJF[0]->SetFillColor(kRed);
777  GJHB_charged_isobar.phi_GJF[0]->Draw("E4");
778 
779  GJHB_charged_isobar.phi_GJF[1]->Draw("same E");
780  GJHB_charged_isobar.phi_GJF[0]->GetYaxis()->SetRangeUser(0, GJHB_charged_isobar.phi_GJF[0]->GetMaximum() * 1.5);
781  gPad->Update();
782 
783  c->cd(14);
784  totMC = HHB_charged_isobar.costheta_HF[0]->Integral();
785  totDATA = HHB_charged_isobar.costheta_HF[1]->Integral();
786  HHB_charged_isobar.costheta_HF[0]->Sumw2();
787  if (totMC != 0)
788  HHB_charged_isobar.costheta_HF[0]->Scale(totDATA / totMC);
789  HHB_charged_isobar.costheta_HF[0]->SetLineColor(kRed);
790  HHB_charged_isobar.costheta_HF[0]->SetFillColor(kRed);
791  HHB_charged_isobar.costheta_HF[0]->Draw("E4");
792 
793  HHB_charged_isobar.costheta_HF[1]->Draw("same E");
794  HHB_charged_isobar.costheta_HF[0]->GetYaxis()->SetRangeUser(0,
795  HHB_charged_isobar.costheta_HF[0]->GetMaximum() * 1.5);
796  gPad->Update();
797 
798  c->cd(16);
799  totMC = HHB_charged_isobar.phi_HF[0]->Integral();
800  totDATA = HHB_charged_isobar.phi_HF[1]->Integral();
801  HHB_charged_isobar.phi_HF[0]->Sumw2();
802  if (totMC != 0)
803  HHB_charged_isobar.phi_HF[0]->Scale(totDATA / totMC);
804  HHB_charged_isobar.phi_HF[0]->SetLineColor(kRed);
805  HHB_charged_isobar.phi_HF[0]->SetFillColor(kRed);
806  HHB_charged_isobar.phi_HF[0]->Draw("E4");
807 
808  HHB_charged_isobar.phi_HF[1]->Draw("same E");
809  HHB_charged_isobar.phi_HF[0]->GetYaxis()->SetRangeUser(0,
810  HHB_charged_isobar.phi_HF[0]->GetMaximum() * 1.5);
811  gPad->Update();
812 
813  // add global diagrams
814 
815  c->cd(1);
816  TLatex* Label = new TLatex();
817  Label->PaintLatex(2, GJHB_neutral_isobar.isobar_mass[0]->GetMaximum() * 0.8, 0, 0.1, massbin.Data());
818 
819  c->Update();
820 
821  TList* Hlist = gDirectory->GetList();
822  Hlist->Remove(mctr);
823  Hlist->Remove(datatr);
824  //Hlist->Remove("hWeights");
825 
826  TFile* outfile = TFile::Open(outfilename, "UPDATE");
827  TString psFileName = outfilename;
828  psFileName.ReplaceAll(".root", massbin);
829  psFileName.Append(".ps");
830 
831  if (totMC != 0) {
832  // get mass-integrated plots (or create them if not there)
833 
834  // neutral isobar
835  TH1D* hMIsobarMCGlob = (TH1D*) outfile->Get("hMIsobarMCGlob");
836  if (hMIsobarMCGlob == NULL)
837  hMIsobarMCGlob = new TH1D("hMIsobarMCGlob", "2#pi neutral Isobar Mass (MC)", nbninsm, 0.2, 2.5);
838  hMIsobarMCGlob->Add(GJHB_neutral_isobar.isobar_mass[0]);
839  hMIsobarMCGlob->Write();
840  TH1D* hMIsobarDataGlob = (TH1D*) outfile->Get("hMIsobarDataGlob");
841  if (hMIsobarDataGlob == NULL)
842  hMIsobarDataGlob = new TH1D("hMIsobarDataGlob", "2#pi neutral Isobar Mass (DATA)", nbninsm, 0.2, 2.5);
843  hMIsobarDataGlob->Add(GJHB_neutral_isobar.isobar_mass[1]);
844  hMIsobarDataGlob->Write();
845 
846  TH1D* hGJMCGlob = (TH1D*) outfile->Get("hGJMCGlob");
847  if (hGJMCGlob == NULL)
848  hGJMCGlob = new TH1D("hGJMCGlob", "Gottfried-Jackson Theta (MC)", nbinsang, -1, 1);
849  hGJMCGlob->Add(GJHB_neutral_isobar.costheta_GJF[0]);
850  hGJMCGlob->Write();
851  TH1D* hGJDataGlob = (TH1D*) outfile->Get("hGJDataGlob");
852  if (hGJDataGlob == NULL)
853  hGJDataGlob = new TH1D("hGJDataGlob", "Gottfried-Jackson Theta (Data)", nbinsang, -1, 1);
854  hGJDataGlob->Add(GJHB_neutral_isobar.costheta_GJF[1]);
855  hGJDataGlob->Write();
856 
857  TH1D* hTYMCGlob = (TH1D*) outfile->Get("hTYMCGlob");
858  if (hTYMCGlob == NULL)
859  hTYMCGlob = new TH1D("hTYMCGlob", "Treiman-Yang Phi (MC)", nbinsang, -TMath::Pi(),TMath::Pi());
860  hTYMCGlob->Add(GJHB_neutral_isobar.phi_GJF[0]);
861  hTYMCGlob->Write();
862  TH1D* hTYDataGlob = (TH1D*) outfile->Get("hTYDataGlob");
863  if (hTYDataGlob == NULL)
864  hTYDataGlob = new TH1D("hTYDataGlob", "Treiman-Yang Phi (Data)", nbinsang, -TMath::Pi(),TMath::Pi());
865  hTYDataGlob->Add(GJHB_neutral_isobar.phi_GJF[1]);
866  hTYDataGlob->Write();
867 
868  c->cd(3);
869  hMIsobarMCGlob->SetLineColor(kRed);
870  hMIsobarMCGlob->SetFillColor(kRed);
871  hMIsobarMCGlob->Draw("E4");
872  hMIsobarDataGlob->Draw("E SAME");
873 
874  c->cd(7);
875  hGJMCGlob->SetLineColor(kRed);
876  hGJMCGlob->SetFillColor(kRed);
877  hGJMCGlob->Draw("E4");
878  hGJDataGlob->Draw("E SAME");
879 
880  c->cd(11);
881  hTYMCGlob->SetLineColor(kRed);
882  hTYMCGlob->SetFillColor(kRed);
883  hTYMCGlob->Draw("E4");
884  hTYDataGlob->Draw("E SAME");
885 
886 
887  // charged isobar
888  TH1D* hMIsobarMCGlob2 = (TH1D*) outfile->Get("hMIsobarMCGlob2");
889  if (hMIsobarMCGlob2 == NULL)
890  hMIsobarMCGlob2 = new TH1D("hMIsobarMCGlob2", "2#pi charged Isobar Mass (MC)", nbninsm, 0.2,
891  2.5);
892  hMIsobarMCGlob2->Add(GJHB_charged_isobar.isobar_mass[0]);
893  hMIsobarMCGlob2->Write();
894  TH1D* hMIsobarDataGlob2 = (TH1D*) outfile->Get("hMIsobarDataGlob2");
895  if (hMIsobarDataGlob2 == NULL)
896  hMIsobarDataGlob2 = new TH1D("hMIsobarDataGlob2", "2#pi charged Isobar mass (DATA)", nbninsm,
897  0.2, 2.5);
898  hMIsobarDataGlob2->Add(GJHB_charged_isobar.isobar_mass[1]);
899  hMIsobarDataGlob2->Write();
900 
901  TH1D* hGJMCGlob2 = (TH1D*) outfile->Get("hGJMCGlob2");
902  if (hGJMCGlob2 == NULL)
903  hGJMCGlob2 = new TH1D("hGJMCGlob2", "Gottfried-Jackson Theta (MC)", nbinsang, -1, 1);
904  hGJMCGlob2->Add(GJHB_charged_isobar.costheta_GJF[0]);
905  hGJMCGlob2->Write();
906  TH1D* hGJDataGlob2 = (TH1D*) outfile->Get("hGJDataGlob2");
907  if (hGJDataGlob2 == NULL)
908  hGJDataGlob2 = new TH1D("hGJDataGlob2", "Gottfried-Jackson Theta (Data)", nbinsang, -1, 1);
909  hGJDataGlob2->Add(GJHB_charged_isobar.costheta_GJF[1]);
910  hGJDataGlob2->Write();
911 
912  TH1D* hTYMCGlob2 = (TH1D*) outfile->Get("hTYMCGlob2");
913  if (hTYMCGlob2 == NULL)
914  hTYMCGlob2 = new TH1D("hTYMCGlob2", "Treiman-Yang Phi (MC)", nbinsang, -TMath::Pi(),TMath::Pi());
915  hTYMCGlob2->Add(GJHB_charged_isobar.phi_GJF[0]);
916  hTYMCGlob2->Write();
917  TH1D* hTYDataGlob2 = (TH1D*) outfile->Get("hTYDataGlob2");
918  if (hTYDataGlob2 == NULL)
919  hTYDataGlob2 = new TH1D("hTYDataGlob2", "Treiman-Yang Phi (Data)", nbinsang, -TMath::Pi(),TMath::Pi());
920  hTYDataGlob2->Add(GJHB_charged_isobar.phi_GJF[1]);
921  hTYDataGlob2->Write();
922 
923  c->cd(4);
924  hMIsobarMCGlob2->SetLineColor(kRed);
925  hMIsobarMCGlob2->SetFillColor(kRed);
926  hMIsobarMCGlob2->Draw("E4");
927  hMIsobarDataGlob2->Draw("E SAME");
928 
929  c->cd(8);
930  hGJMCGlob2->SetLineColor(kRed);
931  hGJMCGlob2->SetFillColor(kRed);
932  hGJMCGlob2->Draw("E4");
933  hGJDataGlob2->Draw("E SAME");
934 
935  c->cd(12);
936  hTYMCGlob2->SetLineColor(kRed);
937  hTYMCGlob2->SetFillColor(kRed);
938  hTYMCGlob2->Draw("E4");
939  hTYDataGlob2->Draw("E SAME");
940  }
941 
942  c->Write();
943  TCanvas dummyCanv("dummy", "dummy");
944  c->Print((psFileName));*/
945 }