ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/UHHAnalysis/SFramePlotter/FileParser.cxx
Revision: 1.4
Committed: Tue May 14 13:14:27 2013 UTC (11 years, 11 months ago) by rkogler
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +2 -1 lines
Log Message:
added normalisation uncertainty for different processes

File Contents

# Content
1 #include <iostream>
2 #include <stdio.h>
3 #include <sys/stat.h>
4 #include <TSystem.h>
5 #include <TObjString.h>
6 #include <TKey.h>
7 #include <TDirectory.h>
8 #include <TClass.h>
9 #include <TMath.h>
10
11 #include "FileParser.h"
12 #include "SHist.h"
13
14 using namespace std;
15
16 FileParser::FileParser()
17 {
18 m_file = NULL;
19 m_hists = NULL;
20 debug = false;
21 m_do_cumulative = true;
22 }
23
24 FileParser::~FileParser()
25 {
26 if (m_file){
27 CloseFile();
28 }
29 }
30
31 void FileParser::CloseFile()
32 {
33 if (m_file){
34 m_file->Close();
35 delete m_file;
36 m_file = NULL;
37 }
38 }
39
40 bool FileParser::FileExists(TString filename)
41 {
42 struct stat buf;
43 if (stat(filename.Data(), &buf) != -1)
44 {
45 return true;
46 }
47 return false;
48 }
49
50
51 void FileParser::OpenFile(TString fname, TString cyclename)
52 {
53 // open the root files with names given in the TObjArray
54
55 if (m_file != NULL){
56 cerr << "FileParser::OpenFile: Can not open new file, since file "
57 << m_file->GetName() << " is still in memory. Abort." << endl;
58 exit(EXIT_FAILURE);
59 }
60
61 if (cyclename.Sizeof()!=0){
62 TString Prefix(cyclename);
63 Prefix.Append(".");
64 fname.Prepend(Prefix);
65 }
66
67 // check if name consists of a wildcard, if so use hadd to combine histograms
68 if (fname.Contains("*")){
69 TString target(fname);
70 target.ReplaceAll("*","");
71
72 // check if target exists, delete if yes
73 if (FileExists(target)){
74 if (debug) cout << "Target exists, removing file " << target << endl;
75 remove(target);
76 }
77
78 TString command = "hadd " + target + " " + fname;
79 gSystem->Exec(command);
80 fname = target;
81 }
82
83 if (debug) cout << "Opening file with name " << fname << "..." << endl;
84 m_file = new TFile(fname, "READ");
85 if (debug){
86 cout << "... success! pointer = " << m_file << endl;
87 cout << "name = " << m_file << endl;
88 cout << " is open? " << m_file->IsOpen() << endl;
89 m_file->ls();
90 }
91
92 if (!m_file->IsOpen()) {
93 cout << endl << "FileParser: File " << fname << " does not exist!!!" << endl;
94 exit(EXIT_FAILURE);
95 } else { // success!
96 cout << "FileParser: Successfully opened file " << fname << endl;
97 }
98
99 StoreProcessName(fname);
100
101 // create a new TObjArray to store all histograms
102 m_hists = new TObjArray();
103
104 return;
105 }
106
107 void FileParser::StoreProcessName(TString name)
108 {
109
110 TObjArray* pieces = name.Tokenize(".");
111 for (int i=0; i<pieces->GetEntries(); ++i){
112 TString piece = ((TObjString*)pieces->At(i))->GetString();
113 if (piece.CompareTo("root")==0){
114 m_process = ((TObjString*)pieces->At(i-1))->GetString();
115 if (debug) cout << "Process in file = " << m_process << endl;
116 }
117 }
118 }
119
120 TObjArray* FileParser::FindSubdirs()
121 {
122 // find all subdirectories (former histogram collections) in the open file
123 // returns a TObjArray with the names of the subdirectories
124
125 m_file->cd();
126 TObjArray* dirnames = new TObjArray();
127 TKey *key;
128 TIter nextkey( gDirectory->GetListOfKeys() );
129 while ( (key = (TKey*)nextkey())) {
130 TObject *obj = key->ReadObj();
131 if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) { // found a subdirectory!
132 TString dirname(((TDirectory*) obj)->GetName());
133 dirnames->Add(new TObjString(dirname));
134 if (debug) cout << "Found directory " << dirname << endl;
135 }
136 }
137 return dirnames;
138
139 }
140
141 void FileParser::BrowseFile()
142 {
143
144 if (!m_file){
145 cerr << "FileParser::BrowseFile: No file open. Abort." << endl;
146 exit(EXIT_FAILURE);
147 }
148 TObjArray* dirs = FindSubdirs();
149
150 // loop over all directories and get the histograms
151 for (Int_t i=0; i<dirs->GetEntries(); ++i){
152
153 TString dirname = ((TObjString*)dirs->At(i))->GetString();
154 if (debug) cout << "Getting all histograms from directory " << dirname << endl;
155
156 m_file->cd();
157 gDirectory->Cd(dirname);
158
159 // loop over all histograms in the directory and store them
160 TKey *key;
161 TIter nextkey( gDirectory->GetListOfKeys() );
162 while ( (key = (TKey*)nextkey())) {
163
164 TObject *obj = key->ReadObj();
165
166 if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
167
168 // histogram found
169 TH1* thist = (TH1*) obj;
170 if (m_do_cumulative) MakeCumulativeHist(thist);
171 TH1* rebinned = Rebin(thist, dirname);
172 SHist* shist = NULL;
173 if (rebinned){
174 shist = new SHist(rebinned);
175 } else {
176 shist = new SHist(thist);
177 }
178 shist->SetProcessName(m_process);
179 shist->SetDir(dirname);
180 if (debug) cout << "Adding hist " << shist->GetHist()->GetName()
181 << " (process = " << m_process << ")" << endl;
182 m_hists->Add(shist);
183 }
184
185 delete obj;
186
187 }
188
189 }
190
191 return;
192
193 }
194
195 void FileParser::MakeCumulativeHist(TH1* hist)
196 {
197 for (Int_t i=1; i<hist->GetNbinsX()+1; ++i){
198 Double_t sum = 0;
199 Double_t sumw2 = 0;
200 for (int j=i; j<hist->GetNbinsX()+1; ++j){
201 sum += hist->GetBinContent(j);
202 sumw2 += hist->GetSumw2()->At(j);
203 }
204 hist->SetBinContent(i, sum);
205 hist->SetBinError(i, TMath::Sqrt(sumw2));
206 }
207
208 }
209
210
211 TH1* FileParser::Rebin(TH1* hist, TString dirname)
212 {
213 TString name(hist->GetName());
214 TString title(hist->GetTitle());
215 if (name.CompareTo("pT")==0 && dirname.Contains("Electron") && title.Contains("electron")){
216 //Double_t binsx[] = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 160, 170, 180, 190, 200, 220, 240, 260, 280, 300, 350, 400, 500};
217 Double_t binsx[] = {30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 160, 170, 180, 190, 200, 220, 240, 260, 280, 300, 350, 400, 500};
218 name.Append("_rebin_lx");
219 TH1* rebinned = hist->Rebin(37, name, binsx);
220 rebinned->SetTitle("electron P_{T} [GeV]");
221 return rebinned;
222 } else {
223 return NULL;
224 }
225
226 }
227
228 void FileParser::SetInfo(TString legname, double weight, int colour, int marker, float unc)
229 {
230
231 for (int i=0; i<m_hists->GetEntries(); ++i){
232 SHist* sh = (SHist*)m_hists->At(i);
233 sh->SetLegName(legname);
234 sh->SetWeight(weight);
235 sh->SetUnc(unc);
236 if (weight>0) sh->GetHist()->Scale(weight);
237 sh->GetHist()->SetMarkerColor(colour);
238 sh->GetHist()->SetLineColor(colour);
239
240 if (marker > 1 ){
241 sh->SetDrawMarker(true);
242 sh->GetHist()->SetMarkerStyle(marker);
243 } else {
244 sh->SetDrawMarker(false);
245 sh->GetHist()->SetMarkerStyle(0);
246 sh->GetHist()->SetMarkerSize(0);
247 sh->GetHist()->SetLineWidth(2);
248 }
249
250 // histogram is transparent if marker < 0
251 if (marker < 0 ){
252 // change line style
253 if (marker==-1) sh->GetHist()->SetLineStyle(kDashed);
254 if (marker==-2) sh->GetHist()->SetLineStyle(kDotted);
255 if (marker==-3) sh->GetHist()->SetLineStyle(kDashDotted);
256 if (marker==-4) sh->GetHist()->SetLineStyle(kDashDotted);
257 }
258 }
259 }