ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/dhidas/Tools/StackAll/src/StackAll.cc
Revision: 1.1
Committed: Thu Dec 1 18:33:53 2011 UTC (13 years, 5 months ago) by dhidas
Content type: text/plain
Branch: MAIN
Branch point for: dhidas
Log Message:
Initial revision

File Contents

# User Rev Content
1 dhidas 1.1 #include "StackAll.h"
2    
3     #include <iostream>
4     #include <fstream>
5     #include <sstream>
6     #include <string>
7     #include <algorithm>
8    
9     #include "TKey.h"
10     #include "TH1F.h"
11     #include "TH1D.h"
12     #include "TH2F.h"
13     #include "TH2D.h"
14     #include "TCanvas.h"
15     #include "TLegend.h"
16     #include "TFrame.h"
17    
18     StackAll::StackAll (TString const InputFileName, TString const OutFileName)
19     {
20     ReadInputFile(InputFileName);
21     OpenOutFile(OutFileName);
22     }
23    
24    
25    
26    
27     StackAll::~StackAll ()
28     {
29     CloseOutFile();
30     }
31    
32    
33    
34    
35     void StackAll::ReadInputFile (TString const InFileName)
36     {
37     // Open file
38     std::ifstream InFile(InFileName.Data());
39     if (!InFile.is_open()) {
40     std::cerr << "ERROR: cannot open input file; " << InFileName << std::endl;
41     exit (1);
42     }
43    
44    
45     // Loop over lines in the input file. If the line begins with a % skip it
46     // or if it is blank skip it. Don't start with a space or tab
47     for ( std::string OneLine; std::getline(InFile, OneLine); ) {
48     if (OneLine == "" || OneLine.at(0) == '%' || OneLine.at(0) == ' ' || OneLine.at(0) == '\t') {
49     continue;
50     }
51    
52     std::cout << "ReadInputFile: " << OneLine << std::endl;
53     std::istringstream LineStream;
54     LineStream.str(OneLine);
55    
56     FileProp* ThisFile = new FileProp;
57     LineStream >> ThisFile->Name
58     >> ThisFile->Color
59     >> ThisFile->IsData
60     >> ThisFile->Scale
61     >> ThisFile->FileName;
62    
63     ThisFile->File = new TFile(ThisFile->FileName, "read");
64    
65    
66     // Check file is open
67     if (!ThisFile->File->IsOpen()) {
68     std::cerr << "ERROR: cannot open file: " << ThisFile->FileName << std::endl;
69     exit(1);
70     }
71    
72     fFiles.push_back(ThisFile);
73     fPropMap.insert( std::pair<TString, FileProp*>(ThisFile->Name, ThisFile) );
74     }
75    
76     return;
77     }
78    
79    
80    
81    
82     void StackAll::OpenOutFile (TString const Name)
83     {
84     fOutFile = new TFile(Name, "recreate");
85     if (!fOutFile->IsOpen()) {
86     std::cerr << "ERROR: cannot open output file: " << Name << std::endl;
87     exit(1);
88     }
89    
90     return;
91     }
92    
93    
94    
95    
96     void StackAll::CloseOutFile ()
97     {
98     fOutFile->Write();
99     fOutFile->Close();
100     return;
101     }
102    
103    
104    
105    
106     void StackAll::Run ()
107     {
108     for (size_t i = 0; i != fFiles.size(); ++i) {
109     this->ReadDirectory( (TDirectoryFile*) fFiles[i]->File, fFiles[i]);
110     }
111    
112     this->MakeAllStacks();
113    
114     return;
115     }
116    
117    
118    
119    
120     void StackAll::MakeAllStacks ()
121     {
122     for (std::map< TString, std::map<TString, std::map<TString, std::map<TString, void *> > > >::iterator iDir = fHistMap.begin(); iDir != fHistMap.end(); ++iDir) {
123     TString const Dir = iDir->first;
124     for (std::map< TString, std::map<TString, std::map<TString, void *> > >::iterator iType = iDir->second.begin(); iType != iDir->second.end(); ++iType) {
125     TString const Type = iType->first;
126     for (std::map< TString, std::map<TString, void *> >::iterator iName = iType->second.begin(); iName != iType->second.end(); ++iName) {
127     TString const Name = iName->first;
128    
129     // Make the Legend
130     TLegend Legend(0.6, 0.7, 0.9, 0.9, "");
131     Legend.SetNColumns(2);
132     Legend.SetFillColor(0);
133    
134     // For summing the stack
135     double StackSum = 0.0;
136    
137     for (std::map< TString, void *>::iterator iProc = iName->second.begin(); iProc != iName->second.end(); ++iProc) {
138     TString const Proc = iProc->first;
139    
140     printf("Stacking: %s %s %s %s\n", Dir.Data(), Type.Data(), Name.Data(), Proc.Data());
141    
142     // Make a new Stack
143     if (iProc == iName->second.begin()) {
144     fStackMap[Dir][Type][Name] = new THStack(Name, Name);
145     }
146    
147     // Fill the stack!
148    
149     FileProp* FP = fPropMap.find(Proc)->second;
150     std::cout << "IsData: " << FP->IsData << " Proc: " << Proc << std::endl;
151    
152     // This is because it works...
153     ((TH1*) iProc->second)->SetLineColor( FP->Color );
154     ((TH1*) iProc->second)->SetFillColor( FP->Color );
155     std::cout << "Entries: " << ((TH1*) iProc->second)->GetEntries() << std::endl;
156    
157     // Let's do some scaling if need be
158     if (FP->Scale > 0) {
159     ((TH1*) iProc->second)->Scale(FP->Scale);
160     } else if (FP->Scale < 0) {
161     ((TH1*) iProc->second)->Scale(-FP->Scale/ ((TH1*) iProc->second)->Integral() );
162     } else {
163     // Do nothing! ie zero means no scaling
164     }
165     std::cout << "Entries: " << ((TH1*) iProc->second)->GetEntries() << std::endl;
166    
167     double ThisHistIntegral = 0.0;
168     if (FP->IsData == 0) {
169     Legend.AddEntry((TH1*) iProc->second, Proc, "f");
170     if (Type.BeginsWith("TH1F")) {
171     fStackMap[Dir][Type][Name]->Add( (TH1F*) iProc->second, "hist");
172     ThisHistIntegral = ((TH1F*) iProc->second)->Integral();
173     } else if (Type.BeginsWith("TH1D")) {
174     fStackMap[Dir][Type][Name]->Add( (TH1D*) iProc->second, "hist");
175     ThisHistIntegral = ((TH1D*) iProc->second)->Integral();
176     } else if (Type.BeginsWith("TH2F")) {
177     fStackMap[Dir][Type][Name]->Add( (TH2F*) iProc->second, "hist");
178     ThisHistIntegral = ((TH2F*) iProc->second)->Integral();
179     } else if (Type.BeginsWith("TH2D")) {
180     fStackMap[Dir][Type][Name]->Add( (TH2D*) iProc->second, "hist");
181     ThisHistIntegral = ((TH2D*) iProc->second)->Integral();
182     }
183     printf("Integral: %15s %12.4f %s\n", Proc.Data(), ThisHistIntegral, (Dir+"/"+Name).Data());
184     StackSum += ThisHistIntegral;
185     }
186    
187     }
188     printf("Integral: %15s %12.4f %s\n", "StackSum", StackSum, (Dir+"/"+Name).Data());
189    
190     GetOrMakeDir(Dir)->cd();
191     TCanvas Canvas(Name, Name);
192     Canvas.cd();
193     fStackMap[Dir][Type][Name]->Draw();
194    
195     for (std::map< TString, void *>::iterator iProc = iName->second.begin(); iProc != iName->second.end(); ++iProc) {
196     TString const Proc = iProc->first;
197     FileProp* FP = fPropMap.find(Proc)->second;
198    
199     // Set the marker size
200     ( (TH1*) iProc->second)->SetMarkerStyle(8);
201     ( (TH1*) iProc->second)->SetMarkerSize(0.7);
202     ( (TH1*) iProc->second)->SetLineWidth(2.0);
203    
204     double ThisHistIntegral = 0.0;
205     if (FP->IsData == 1) {
206     Legend.AddEntry((TH1*) iProc->second, Proc, "lp");
207     if (Type.BeginsWith("TH1F")) {
208     ( (TH1F*) iProc->second)->Draw("epsame");
209     ThisHistIntegral = ((TH1F*) iProc->second)->Integral();
210     } else if (Type.BeginsWith("TH1D")) {
211     ( (TH2D*) iProc->second)->Draw("epsame");
212     ThisHistIntegral = ((TH1D*) iProc->second)->Integral();
213     } else if (Type.BeginsWith("TH2F")) {
214     ( (TH1F*) iProc->second)->Draw("epsame");
215     ThisHistIntegral = ((TH2F*) iProc->second)->Integral();
216     } else if (Type.BeginsWith("TH2D")) {
217     ( (TH2D*) iProc->second)->Draw("epsame");
218     ThisHistIntegral = ((TH2D*) iProc->second)->Integral();
219     }
220     printf("Integral: %15s %12.4f %s\n", Proc.Data(), ThisHistIntegral, (Dir+"/"+Name).Data());
221     }
222    
223     }
224    
225     Legend.Draw("same");
226     Canvas.SetBorderMode(0);
227     Canvas.SetHighLightColor(0);
228     Canvas.SetFillColor(0);
229     //Canvas.GetPad(0)->GetFrame()->SetBorderMode(0);
230     Canvas.Write();
231     TString SaveName = Dir;
232     SaveName.Remove(0,1);
233     SaveName.ReplaceAll('/', "_");
234     Canvas.SaveAs(SaveName+"_"+Name+".eps");
235     //fStackMap[Dir][Type][Name]->Write();
236     }
237     }
238     }
239    
240    
241     return;
242     }
243    
244    
245    
246    
247     void StackAll::ReadDirectory (TDirectoryFile* Dir, FileProp const* Prop)
248     {
249     TIter MyIter(Dir->GetListOfKeys());
250     TKey* Key;
251     while ( (Key = (TKey*) MyIter()) ) {
252     TObject *Object = (TObject*) Key->ReadObj();
253     std::cout << "ClassName: " << Object->ClassName()
254     << " Name: " << Object->GetName() << std::endl;
255    
256     if (Object == 0x0) {
257     continue;
258     }
259    
260     TString const MyClassName = Object->ClassName();
261     TString const PathName = Dir->GetPath();
262     TString const DirName = PathName(PathName.First(":") + 1, PathName.Length() - PathName.First(":") - 1);
263    
264     if (MyClassName.BeginsWith("TH1F")) {
265     AddToHistMap( (TH1F*) Object, DirName, Prop);
266     } else if (MyClassName.BeginsWith("TH1D")) {
267     AddToHistMap( (TH1D*) Object, DirName, Prop);
268     } else if (MyClassName.BeginsWith("TH2F")) {
269     AddToHistMap( (TH2F*) Object, DirName, Prop);
270     } else if (MyClassName.BeginsWith("TH2D")) {
271     AddToHistMap( (TH2D*) Object, DirName, Prop);
272     }
273    
274     if (TString(Object->ClassName()).BeginsWith("TDirectory")) {
275     this->ReadDirectory( (TDirectoryFile*) Object, Prop);
276     }
277     }
278    
279     return;
280     }
281    
282    
283    
284    
285     template <typename T>
286     void StackAll::AddToHistMap (T* Hist, TString const& Dir, FileProp const* Prop)
287     {
288     TString const Proc = Prop->Name;
289     std::cout << "Proc: " << Proc << std::endl;
290     if (fHistMap[Dir][Hist->ClassName()][Hist->GetName()].find(Proc) == fHistMap[Dir][Hist->ClassName()][Hist->GetName()].end()) {
291     fHistMap[Dir][Hist->ClassName()][Hist->GetName()][Proc] = (T*) Hist->Clone();
292     ( (T*) fHistMap[Dir][Hist->ClassName()][Hist->GetName()][Proc])->SetDirectory(0x0);
293     } else {
294     ( (T*) fHistMap[Dir][Hist->ClassName()][Hist->GetName()][Proc])->Add(Hist);
295     }
296    
297     return;
298     }
299    
300    
301    
302    
303     TDirectoryFile* StackAll::GetOrMakeDir (TString const& DirName)
304     {
305     std::cout << "DirName: " << DirName << std::endl;
306    
307     TDirectoryFile* ThisDir = (TDirectoryFile*) fOutFile->GetDirectory(DirName);
308     if (ThisDir) {
309     return ThisDir;
310     }
311    
312     TDirectoryFile* BackDir = GetOrMakeDir( DirName(0, DirName.Last('/')));
313     if (BackDir) {
314     return (TDirectoryFile*) BackDir->mkdir( DirName(DirName.Last('/')+1, DirName.Length()-DirName.Last('/')-1).Data() );
315     }
316    
317     return GetOrMakeDir( DirName(0, DirName.Last('/')) );
318     }
319