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