+\r
+//\r
+// Author: G. Balbastre\r
+// Modified by K. Read\r
+//\r
+// Usage: \r
+// After previously using mymerger.C to merge each PYHIA hard bin,\r
+// do the following to download one file per pt bin:\r
+// aliensh; cp histoss* file: \r
+// Then, adjust binlist below to reflect which histograms are non-empty.\r
+// Then, locally:\r
+// root -b -x MergeFileInBins.C\r
+//\r
+// Note: This macro overwrites the downloaded histograms with the same names.\r
+// It may be appropriate to make a backup of the histograms before running\r
+// this macro.\r
+//\r
+\r
+void MergeFileInBins()\r
+{\r
+ char name[128] ;\r
+ TFile * mfile;\r
+ TFile * sfile;\r
+ TList* list;\r
+ TList* newlist;\r
+ TString binlist = "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15";\r
+\r
+ if (binlist.Length()) {\r
+ arr = binlist.Tokenize(" ");\r
+ TObjString *os;\r
+ TIter next(arr);\r
+ while ((os=(TObjString*)next())) {\r
+ printf("Rescaling histosscaled-merged%s.root\n",os->GetString().Data());\r
+ \r
+ //Rescale histograms\r
+ sprintf(name,"histosscaled-merged%s.root",os->GetString().Data());\r
+ mfile = new TFile(name,"read");\r
+ list = (TList*) mfile->Get("histosscaled");\r
+ mfile->Close();\r
+ \r
+ TObject * h ; \r
+ Int_t split = ((TH1F*) list->FindObject("hCount"))->GetEntries();\r
+ cout<<"scale with factor "<<split<< " histograms "<<list->GetEntries()<<endl;\r
+ newlist = new TList();\r
+ sprintf(name,"%s2",list->GetName());\r
+ newlist->SetName(name);\r
+\r
+ for(Int_t iter = 0; iter < list->GetEntries(); iter++){\r
+ h = list->At(iter);\r
+ if(h && (h->GetName()!="hCount")){\r
+ if ( !strncmp(h->ClassName(),"TH",2) ) {\r
+ char name[128] ; \r
+ sprintf(name, "%s", h->GetName()) ; \r
+ //cout<<iter<<" histo scaled : "<<name<<endl;\r
+ TH1 * hout = dynamic_cast<TH1*> (h->Clone(name)) ; \r
+ //if(fSumw2) hout->Sumw2();\r
+ hout->Scale(1./split) ; \r
+ newlist->Add(hout) ; \r
+ }\r
+ } \r
+ }\r
+\r
+ sprintf(name,"histosscaled-merged%s.root",os->GetString().Data());\r
+ sfile = new TFile(name,"recreate");\r
+ newlist->Write();\r
+ sfile->Close(); \r
+ cout<<name<<" has been recreated "<<endl;\r
+ }//while\r
+ }\r
+\r
+\r
+ //Merge all histos\r
+\r
+ cout<<"Merge all bins "<<endl;\r
+\r
+ TFileMerger m;\r
+ sprintf(name,"TOTALhistosscaled.root");\r
+\r
+ if (binlist.Length()) {\r
+ m.OutputFile(name);\r
+ arr = binlist.Tokenize(" ");\r
+ TObjString *os;\r
+ TIter next(arr);\r
+ while ((os=(TObjString*)next())) {\r
+ printf("Adding bin histosscaled-merged%s.root\n",os->GetString().Data());\r
+ sprintf(name,"histosscaled-merged%s.root",os->GetString().Data());\r
+ m.AddFile(name);\r
+ }\r
+ m.Merge();\r
+ }\r
+\r
+}\r