ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/rf/makeCMS2ClassFiles.C
Revision: 1.1
Committed: Wed Dec 23 17:33:25 2009 UTC (15 years, 4 months ago) by benhoob
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
*** empty log message ***

File Contents

# Content
1 // Original author: Puneeth Kalavase (UCSB)
2 //
3 /*ROOT macro to make CMS2.h and ScanChain.C files for basic analysis
4 of CMS2 ntuples. Usage:
5
6 [kalavase@stau ~/rootmacros]$ root
7 root [0] .L makeCMS2ClassFiles.C++
8 //
9 // the second and third arguments are optional
10 //
11 // if we are paranoid, each float, vector<float> and
12 // vector<vector<float> > is checked for NaNs and infs
13 //
14 // The classname is CMS2 by default
15 //
16 root [1] makeCMS2ClassFiles("merged_ntuple.root",true,"classname")
17
18 */
19
20
21 #include "TBranch.h"
22 #include "TString.h"
23 #include "TFile.h"
24 #include "TTree.h"
25 #include <iostream>
26 #include <fstream>
27 #include <set>
28 #include <algorithm>
29 #include <vector>
30 #include "Math/LorentzVector.h"
31
32 using namespace std;
33 ofstream headerf;
34 ofstream codef;
35 ofstream branchfile;
36
37 void makeHeaderFile(TFile *f, bool paranoid, string Classname) {
38
39
40
41 headerf << "// -*- C++ -*-" << endl;
42 headerf << "#ifndef " << Classname << "_H" << endl;
43 headerf << "#define " << Classname << "_H" << endl;
44 headerf << "#include \"Math/LorentzVector.h\"" << endl;
45 headerf << "#include \"Math/Point3D.h\"" << endl;
46 headerf << "#include \"TMath.h\"" << endl;
47 headerf << "#include \"TBranch.h\"" << endl;
48 headerf << "#include \"TTree.h\"" << endl;
49 headerf << "#include \"TH1F.h\"" << endl;
50 headerf << "#include \"TFile.h\"" << endl;
51 headerf << "#include <vector> " << endl << endl;
52 if (paranoid)
53 headerf << "#define PARANOIA" << endl << endl;
54 headerf << "using namespace std; " << endl;
55 headerf << "class " << Classname << " {" << endl;
56 headerf << "private: " << endl;
57 headerf << "protected: " << endl;
58 headerf << "\tunsigned int index;" << endl;
59 TTree *ev = (TTree*)f->Get("Events");
60 TList *fullarray = ev->GetListOfAliases();
61 TList *aliasarray = new TList();
62
63 //for(Int_t i = 0; i < aliasarray->GetSize(); ++i) {
64 for(Int_t i = 0; i < fullarray->GetSize(); ++i) {
65 TString aliasname(fullarray->At(i)->GetName());
66 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
67 TString branchname(branch->GetName());
68 TString branchtitle(branch->GetTitle());
69 if(!branchname.BeginsWith("int") &&
70 !branchname.BeginsWith("uint") &&
71 !branchname.BeginsWith("float") &&
72 !branchname.BeginsWith("double") &&
73 !branchtitle.EndsWith("/F") &&
74 !branchtitle.EndsWith("/I") &&
75 !branchtitle.EndsWith("/i") &&
76 !branchtitle.BeginsWith("TString"))
77 continue;
78 aliasarray->Add(fullarray->At(i));
79 }
80
81
82 for(Int_t i = 0; i< aliasarray->GetSize(); ++i) {
83
84 //Class name is blank for a int of float
85 TString aliasname(aliasarray->At(i)->GetName());
86 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
87
88 TString classname = branch->GetClassName();
89 TString title = branch->GetTitle();
90 if ( classname.Contains("vector") ) {
91 if(classname.Contains("edm::Wrapper<") ) {
92 classname = classname(0,classname.Length()-2);
93 classname.ReplaceAll("edm::Wrapper<","");
94 headerf << "\t" << classname << " " << aliasname << "_;" << endl;
95 } else {
96 headerf << "\t" << classname << " *" << aliasname << "_;" << endl;
97 }
98 } else {
99
100 if(classname != "" ) { //LorentzVector
101 if(classname.Contains("edm::Wrapper<") ) {
102 classname = classname(0,classname.Length()-1);
103 classname.ReplaceAll("edm::Wrapper<","");
104 headerf << "\t" << classname << " " << aliasname << "_;" << endl;
105 } else {
106 headerf << "\t" << classname << " *" << aliasname << "_;" << endl;
107 }
108 } else {
109 if(title.EndsWith("/i"))
110 headerf << "\tunsigned int" << "\t" << aliasname << "_;" << endl;
111 if(title.EndsWith("/F"))
112 headerf << "\tfloat" << "\t" << aliasname << "_;" << endl;
113 if(title.EndsWith("/I"))
114 headerf << "\tint" << "\t" << aliasname << "_;" << endl;
115 }
116 }
117 headerf << "\tTBranch *" << Form("%s_branch",aliasname.Data()) << ";" << endl;
118 headerf << "\tbool " << Form("%s_isLoaded",aliasname.Data()) << ";" << endl;
119 }
120
121
122 headerf << "public: " << endl;
123 headerf << "int ScanChain(class TChain* chain, int nEvents=-1, std::string skimFilePrefix=\"\");" << endl;
124 headerf << "void Init(TTree *tree) {" << endl;
125
126
127 // SetBranchAddresses for LorentzVectors
128 for(Int_t i = 0; i< aliasarray->GetSize(); i++) {
129 TString aliasname(aliasarray->At(i)->GetName());
130 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
131 TString classname = branch->GetClassName();
132 if ( !classname.Contains("vector<vector") ) {
133 if ( classname.Contains("Lorentz") || classname.Contains("PositionVector") ) {
134 headerf << "\t" << Form("%s_branch",aliasname.Data()) << " = 0;" << endl;
135 headerf << "\t" << "if (tree->GetAlias(\"" << aliasname << "\") != 0) {" << endl;
136 headerf << "\t\t" << Form("%s_branch",aliasname.Data()) << " = tree->GetBranch(tree->GetAlias(\"" << aliasname << "\"));" << endl;
137 headerf << "\t\t" << Form("%s_branch",aliasname.Data()) << "->SetAddress(&" << aliasname << "_);" << endl << "\t}" << endl;
138 headerf << "\t" << "if(" << Form("%s_branch",aliasname.Data()) << " == 0 ) {" << endl;
139 headerf << "\t" << "cout << \"Branch " << aliasname.Data() << " does not exist.\" << endl;" << endl;
140 headerf << "\t" << "}" << endl;
141 }
142 }
143 }
144
145
146 // SetBranchAddresses for everything else
147 headerf << " tree->SetMakeClass(1);" << endl;
148 for(Int_t i = 0; i< aliasarray->GetSize(); i++) {
149 TString aliasname(aliasarray->At(i)->GetName());
150 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
151 TString classname = branch->GetClassName();
152 if ( ! (classname.Contains("Lorentz") || classname.Contains("PositionVector")) || classname.Contains("vector<vector") ) {
153 headerf << "\t" << Form("%s_branch",aliasname.Data()) << " = 0;" << endl;
154 headerf << "\t" << "if (tree->GetAlias(\"" << aliasname << "\") != 0) {" << endl;
155 headerf << "\t\t" << Form("%s_branch",aliasname.Data()) << " = tree->GetBranch(tree->GetAlias(\"" << aliasname << "\"));" << endl;
156 headerf << "\t\t" << Form("%s_branch",aliasname.Data()) << "->SetAddress(&" << aliasname << "_);" << endl << "\t}" << endl;
157 headerf << "\t" << "if(" << Form("%s_branch",aliasname.Data()) << " == 0 ) {" << endl;
158 headerf << "\t" << "cout << \"Branch " << aliasname.Data() << " does not exist.\" << endl;" << endl;
159 headerf << "\t" << "}" << endl;
160 }
161 }
162
163 headerf << " tree->SetMakeClass(0);" << endl;
164 headerf << "}" << endl;
165
166 // GetEntry
167 headerf << "void GetEntry(unsigned int idx) " << endl;
168 headerf << "\t// this only marks branches as not loaded, saving a lot of time" << endl << "\t{" << endl;
169 headerf << "\t\tindex = idx;" << endl;
170 for(Int_t i = 0; i< aliasarray->GetSize(); i++) {
171 TString aliasname(aliasarray->At(i)->GetName());
172 headerf << "\t\t" << Form("%s_isLoaded",aliasname.Data()) << " = false;" << endl;
173 }
174 headerf << "\t}" << endl << endl;
175
176 // LoadAllBranches
177 headerf << "void LoadAllBranches() " << endl;
178 headerf << "\t// load all branches" << endl << "{" << endl;
179 for(Int_t i = 0; i< aliasarray->GetSize(); i++) {
180 TString aliasname(aliasarray->At(i)->GetName());
181 headerf << "\t" << "if (" << aliasname.Data() << "_branch != 0) " << Form("%s();",aliasname.Data()) << endl;
182 }
183 headerf << "}" << endl << endl;
184
185 // accessor functions
186 for (Int_t i = 0; i< aliasarray->GetSize(); i++) {
187 TString aliasname(aliasarray->At(i)->GetName());
188 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
189 TString classname = branch->GetClassName();
190 TString title = branch->GetTitle();
191 bool isSkimmedNtuple = false;
192 if(!classname.Contains("edm::Wrapper<") &&
193 (classname.Contains("vector") || classname.Contains("LorentzVector") ) )
194 isSkimmedNtuple = true;
195 if ( classname.Contains("vector") ) {
196 if(classname.Contains("edm::Wrapper<") ) {
197 classname = classname(0,classname.Length()-2);
198 classname.ReplaceAll("edm::Wrapper<","");
199 }
200 headerf << "\t" << classname << " &" << aliasname << "()" << endl;
201 } else {
202 if(classname.Contains("edm::Wrapper<") ) {
203 classname = classname(0,classname.Length()-1);
204 classname.ReplaceAll("edm::Wrapper<","");
205 }
206 if(classname != "" ) {
207 headerf << "\t" << classname << " &" << aliasname << "()" << endl;
208 } else {
209 if(title.EndsWith("/i"))
210 headerf << "\tunsigned int &" << aliasname << "()" << endl;
211 if(title.EndsWith("/F"))
212 headerf << "\tfloat &" << aliasname << "()" << endl;
213 if(title.EndsWith("/I"))
214 headerf << "\tint &" << aliasname << "()" << endl;
215 }
216 }
217 aliasname = aliasarray->At(i)->GetName();
218 headerf << "\t{" << endl;
219 headerf << "\t\t" << "if (not " << Form("%s_isLoaded) {",aliasname.Data()) << endl;
220 headerf << "\t\t\t" << "if (" << Form("%s_branch",aliasname.Data()) << " != 0) {" << endl;
221 headerf << "\t\t\t\t" << Form("%s_branch",aliasname.Data()) << "->GetEntry(index);" << endl;
222 if (paranoid) {
223 headerf << "\t\t\t\t#ifdef PARANOIA" << endl;
224 if (classname == "vector<vector<float> >") {
225 if(isSkimmedNtuple) {
226 headerf << "\t\t\t\t" << "for (vector<vector<float> >::const_iterator i = "
227 << aliasname << "_->begin(); i != "<< aliasname << "_->end(); ++i) {" << endl;
228 } else {
229 headerf << "\t\t\t\t" << "for (vector<vector<float> >::const_iterator i = "
230 << aliasname << "_.begin(); i != "<< aliasname << "_.end(); ++i) {" << endl;
231 }
232 headerf << "\t\t\t\t\t" << "for (vector<float>::const_iterator j = i->begin(); "
233 "j != i->end(); ++j) {" << endl;
234 headerf << "\t\t\t\t\t\t" << "if (not isfinite(*j)) {" << endl;
235 headerf << "\t\t\t\t\t\t\t" << "printf(\"branch " << Form("%s_branch",aliasname.Data())
236 << " contains a bad float: %f\\n\", *j);" << endl << "\t\t\t\t\t\t\t" << "exit(1);"
237 << endl;
238 headerf << "\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}" << endl;
239 } else if (classname == "vector<float>") {
240 if(isSkimmedNtuple) {
241 headerf << "\t\t\t\t" << "for (vector<float>::const_iterator i = "
242 << aliasname << "_->begin(); i != "<< aliasname << "_->end(); ++i) {" << endl;
243 } else {
244 headerf << "\t\t\t\t" << "for (vector<float>::const_iterator i = "
245 << aliasname << "_.begin(); i != "<< aliasname << "_.end(); ++i) {" << endl;
246 }
247 headerf << "\t\t\t\t\t" << "if (not isfinite(*i)) {" << endl;
248 headerf << "\t\t\t\t\t\t" << "printf(\"branch " << Form("%s_branch",aliasname.Data())
249 << " contains a bad float: %f\\n\", *i);" << endl << "\t\t\t\t\t\t" << "exit(1);"
250 << endl;
251 headerf << "\t\t\t\t\t}\n\t\t\t\t}" << endl;
252 } else if (classname == "float") {
253 headerf << "\t\t\t\t" << "if (not isfinite(" << aliasname << "_)) {" << endl;
254 headerf << "\t\t\t\t\t" << "printf(\"branch " << Form("%s_branch",aliasname.Data())
255 << " contains a bad float: %f\\n\", " << aliasname << "_);" << endl
256 << "\t\t\t\t\t" << "exit(1);"
257 << endl;
258 headerf << "\t\t\t\t}" << endl;
259 } else if (classname.BeginsWith("vector<vector<ROOT::Math::LorentzVector")) {
260 if(isSkimmedNtuple) {
261 headerf << "\t\t\t\t" << "for (" << classname.Data() <<"::const_iterator i = "
262 << aliasname << "_->begin(); i != "<< aliasname << "_->end(); ++i) {" << endl;
263 } else {
264 headerf << "\t\t\t\t" << "for (" << classname.Data() <<"::const_iterator i = "
265 << aliasname << "_.begin(); i != "<< aliasname << "_.end(); ++i) {" << endl;
266 }
267 // this is a slightly hacky way to get rid of the outer vector< > ...
268 std::string str = classname.Data() + 7;
269 str[str.length() - 2] = 0;
270 headerf << "\t\t\t\t\t" << "for (" << str.c_str() << "::const_iterator j = i->begin(); "
271 "j != i->end(); ++j) {" << endl;
272 headerf << "\t\t\t\t\t\t" << "int e;" << endl;
273 headerf << "\t\t\t\t\t\t" << "frexp(j->pt(), &e);" << endl;
274 headerf << "\t\t\t\t\t\t" << "if (not isfinite(j->pt()) || e > 30) {" << endl;
275 headerf << "\t\t\t\t\t\t\t" << "printf(\"branch " << Form("%s_branch",aliasname.Data())
276 << " contains a bad float: %f\\n\", j->pt());" << endl << "\t\t\t\t\t\t\t" << "exit(1);"
277 << endl;
278 headerf << "\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}" << endl;
279 } else if (classname.BeginsWith("vector<ROOT::Math::LorentzVector")) {
280 if(isSkimmedNtuple) {
281 headerf << "\t\t\t\t" << "for (" << classname.Data() << "::const_iterator i = "
282 << aliasname << "_->begin(); i != "<< aliasname << "_->end(); ++i) {" << endl;
283 } else {
284 headerf << "\t\t\t\t" << "for (" << classname.Data() << "::const_iterator i = "
285 << aliasname << "_.begin(); i != "<< aliasname << "_.end(); ++i) {" << endl;
286 }
287 headerf << "\t\t\t\t\t" << "int e;" << endl;
288 headerf << "\t\t\t\t\t" << "frexp(i->pt(), &e);" << endl;
289 headerf << "\t\t\t\t\t" << "if (not isfinite(i->pt()) || e > 30) {" << endl;
290 headerf << "\t\t\t\t\t\t" << "printf(\"branch " << Form("%s_branch",aliasname.Data())
291 << " contains a bad float: %f\\n\", i->pt());" << endl << "\t\t\t\t\t\t" << "exit(1);"
292 << endl;
293 headerf << "\t\t\t\t\t}\n\t\t\t\t}" << endl;
294 } else if (classname.BeginsWith("ROOT::Math::LorentzVector")) {
295 headerf << "\t\t\t\t" << "int e;" << endl;
296 if(isSkimmedNtuple) {
297 headerf << "\t\t\t\t" << "frexp(" << aliasname << "_->pt(), &e);" << endl;
298 headerf << "\t\t\t\t" << "if (not isfinite(" << aliasname << "_->pt()) || e > 30) {" << endl;
299 headerf << "\t\t\t\t\t" << "printf(\"branch " << Form("%s_branch",aliasname.Data())
300 << " contains a bad float: %f\\n\", " << aliasname << "_->pt());" << endl
301 << "\t\t\t\t\t" << "exit(1);"
302 << endl;
303 } else {
304 headerf << "\t\t\t\t" << "frexp(" << aliasname << "_.pt(), &e);" << endl;
305 headerf << "\t\t\t\t" << "if (not isfinite(" << aliasname << "_.pt()) || e > 30) {" << endl;
306 headerf << "\t\t\t\t\t" << "printf(\"branch " << Form("%s_branch",aliasname.Data())
307 << " contains a bad float: %f\\n\", " << aliasname << "_.pt());" << endl
308 << "\t\t\t\t\t" << "exit(1);"
309 << endl;
310 }
311 headerf << "\t\t\t\t}" << endl;
312 }
313 headerf << "\t\t\t\t#endif // #ifdef PARANOIA" << endl;
314 }
315 headerf << "\t\t\t" << "} else { " << endl;
316 headerf << "\t\t\t\t" << "printf(\"branch " << Form("%s_branch",aliasname.Data())
317 << " does not exist!\\n\");" << endl;
318 headerf << "\t\t\t\t" << "exit(1);" << endl << "\t\t\t}" << endl;
319 headerf << "\t\t\t" << Form("%s_isLoaded",aliasname.Data()) << " = true;" << endl;
320 headerf << "\t\t" << "}" << endl;
321 if(isSkimmedNtuple) {
322 headerf << "\t\t" << "return *" << aliasname << "_;" << endl << "\t}" << endl;
323 }
324 else {
325 headerf << "\t\t" << "return " << aliasname << "_;" << endl << "\t}" << endl;
326 }
327 }
328
329 bool haveHLTInfo = false;
330 bool haveL1Info = false;
331 bool haveHLT8E29Info = false;
332 for(int i = 0; i < aliasarray->GetSize(); i++) {
333 TString aliasname(aliasarray->At(i)->GetName());
334 if(aliasname=="hlt_trigNames")
335 haveHLTInfo = true;
336 if(aliasname=="l1_trigNames")
337 haveL1Info = true;
338 if(aliasname=="hlt8e29_trigNames")
339 haveHLT8E29Info = true;
340 }
341
342 if(haveHLTInfo) {
343 //functions to return whether or not trigger fired - HLT
344 headerf << "\t" << "bool passHLTTrigger(TString trigName) {" << endl;
345 headerf << "\t\t" << "int trigIndx;" << endl;
346 headerf << "\t\t" << "vector<TString>::const_iterator begin_it = hlt_trigNames().begin();" << endl;
347 headerf << "\t\t" << "vector<TString>::const_iterator end_it = hlt_trigNames().end();" << endl;
348 headerf << "\t\t" << "vector<TString>::const_iterator found_it = find(begin_it, end_it, trigName);" << endl;
349 headerf << "\t\t" << "if(found_it != end_it)" << endl;
350 headerf << "\t\t\t" << "trigIndx = found_it - begin_it;" << endl;
351 headerf << "\t\t" << "else {" << endl;
352 headerf << "\t\t\t" << "cout << \"Cannot find Trigger \" << trigName << endl; " << endl;
353 headerf << "\t\t\t" << "return 0;" << endl;
354 headerf << "\t\t" << "}" << endl << endl;
355 //get the list of branches that hold the HLT bitmasks
356 //store in a set 'cause its automatically sorted
357 set<TString> s_HLTbitmasks;
358 set<TString> s_L1bitmasks;
359 for(int j = 0; j < aliasarray->GetSize(); j++) {
360 TString aliasname(aliasarray->At(j)->GetName());
361 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
362 TString classname = branch->GetClassName();
363 if(aliasname.Contains("hlt_bits") && classname.Contains("int")) {
364 s_HLTbitmasks.insert(aliasname);
365 }
366
367 }
368 int i = 0;
369 for(set<TString>::const_iterator s_it = s_HLTbitmasks.begin();
370 s_it != s_HLTbitmasks.end(); s_it++, i++) {
371
372 if(i==0) {
373 headerf << "\t\t" << "if(trigIndx <= 31) {" << endl;
374 headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
375 headerf << "\t\t\t" << "bitmask <<= trigIndx;" << endl;
376 headerf << "\t\t\t" << "return " << *s_it << "() & bitmask;" << endl;
377 headerf << "\t\t" << "}" << endl;
378 continue;
379 }
380 headerf << "\t\t" << "if(trigIndx >= " << Form("%d && trigIndx <= %d", 32*i, 32*i+31) << ") {" << endl;
381 headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
382 headerf << "\t\t\t" << "bitmask <<= (trigIndx - " << Form("%d",32*i) << "); " << endl;
383 headerf << "\t\t\t" << "return " << *s_it << "() & bitmask;" << endl;
384 headerf << "\t\t" << "}" << endl;
385 }
386 headerf << "\t" << "return 0;" << endl;
387 headerf << "\t" << "}" << endl;
388 }//if(haveHLTInfo)
389
390 if(haveHLT8E29Info) {
391 //functions to return whether or not trigger fired - HLT
392 headerf << "\t" << "bool passHLT8E29Trigger(TString trigName) {" << endl;
393 headerf << "\t\t" << "int trigIndx;" << endl;
394 headerf << "\t\t" << "vector<TString>::const_iterator begin_it = hlt8e29_trigNames().begin();" << endl;
395 headerf << "\t\t" << "vector<TString>::const_iterator end_it = hlt8e29_trigNames().end();" << endl;
396 headerf << "\t\t" << "vector<TString>::const_iterator found_it = find(begin_it, end_it, trigName);" << endl;
397 headerf << "\t\t" << "if(found_it != end_it)" << endl;
398 headerf << "\t\t\t" << "trigIndx = found_it - begin_it;" << endl;
399 headerf << "\t\t" << "else {" << endl;
400 headerf << "\t\t\t" << "cout << \"Cannot find Trigger \" << trigName << endl; " << endl;
401 headerf << "\t\t\t" << "return 0;" << endl;
402 headerf << "\t\t" << "}" << endl << endl;
403 //get the list of branches that hold the HLT bitmasks
404 //store in a set 'cause its automatically sorted
405 set<TString> s_HLTbitmasks;
406 set<TString> s_L1bitmasks;
407 for(int j = 0; j < aliasarray->GetSize(); j++) {
408 TString aliasname(aliasarray->At(j)->GetName());
409 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
410 TString classname = branch->GetClassName();
411 if(aliasname.Contains("hlt8e29_bits") && classname.Contains("int")) {
412 s_HLTbitmasks.insert(aliasname);
413 }
414
415 }
416 int i = 0;
417 for(set<TString>::const_iterator s_it = s_HLTbitmasks.begin();
418 s_it != s_HLTbitmasks.end(); s_it++, i++) {
419
420 if(i==0) {
421 headerf << "\t\t" << "if(trigIndx <= 31) {" << endl;
422 headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
423 headerf << "\t\t\t" << "bitmask <<= trigIndx;" << endl;
424 headerf << "\t\t\t" << "return " << *s_it << "() & bitmask;" << endl;
425 headerf << "\t\t" << "}" << endl;
426 continue;
427 }
428 headerf << "\t\t" << "if(trigIndx >= " << Form("%d && trigIndx <= %d", 32*i, 32*i+31) << ") {" << endl;
429 headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
430 headerf << "\t\t\t" << "bitmask <<= (trigIndx - " << Form("%d",32*i) << "); " << endl;
431 headerf << "\t\t\t" << "return " << *s_it << "() & bitmask;" << endl;
432 headerf << "\t\t" << "}" << endl;
433 }
434 headerf << "\t" << "return 0;" << endl;
435 headerf << "\t" << "}" << endl;
436 }//if(haveHLT8E29Info)
437
438
439 if(haveL1Info) {
440 //functions to return whether or not trigger fired - L1
441 headerf << "\t" << "bool passL1Trigger(TString trigName) {" << endl;
442 headerf << "\t\t" << "int trigIndx;" << endl;
443 headerf << "\t\t" << "vector<TString>::const_iterator begin_it = l1_trigNames().begin();" << endl;
444 headerf << "\t\t" << "vector<TString>::const_iterator end_it = l1_trigNames().end();" << endl;
445 headerf << "\t\t" << "vector<TString>::const_iterator found_it = find(begin_it, end_it, trigName);" << endl;
446 headerf << "\t\t" << "if(found_it != end_it)" << endl;
447 headerf << "\t\t\t" << "trigIndx = found_it - begin_it;" << endl;
448 headerf << "\t\t" << "else {" << endl;
449 headerf << "\t\t\t" << "cout << \"Cannot find Trigger \" << trigName << endl; " << endl;
450 headerf << "\t\t\t" << "return 0;" << endl;
451 headerf << "\t\t" << "}" << endl << endl;
452 //get the list of branches that hold the L1 bitmasks
453 //store in a set 'cause its automatically sorted
454 set<TString> s_L1bitmasks;
455 for(int j = 0; j < aliasarray->GetSize(); j++) {
456 TString aliasname(aliasarray->At(j)->GetName());
457 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
458 TString classname = branch->GetClassName();
459 if(aliasname.Contains("l1_bits") && classname.Contains("int")) {
460 s_L1bitmasks.insert(aliasname);
461 }
462
463 }
464 int i = 0;
465 for(set<TString>::const_iterator s_it = s_L1bitmasks.begin();
466 s_it != s_L1bitmasks.end(); s_it++, i++) {
467
468 if(i==0) {
469 headerf << "\t\t" << "if(trigIndx <= 31) {" << endl;
470 headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
471 headerf << "\t\t\t" << "bitmask <<= trigIndx;" << endl;
472 headerf << "\t\t\t" << "return " << *s_it << "() & bitmask;" << endl;
473 headerf << "\t\t" << "}" << endl;
474 continue;
475 }
476 headerf << "\t\t" << "if(trigIndx >= " << Form("%d && trigIndx <= %d", 32*i, 32*i+31) << ") {" << endl;
477 headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
478 headerf << "\t\t\t" << "bitmask <<= (trigIndx - " << Form("%d",32*i) << "); " << endl;
479 headerf << "\t\t\t" << "return " << *s_it << "() & bitmask;" << endl;
480 headerf << "\t\t" << "}" << endl;
481 }
482 headerf << "\t" << "return 0;" << endl;
483 headerf << "\t" << "}" << endl;
484 }//if(haveL1Info)
485
486 headerf << "};" << endl << endl;
487
488 headerf << "#ifndef __CINT__" << endl;
489 headerf << "extern " << Classname << " cms2;" << endl;
490 headerf << "#endif" << endl << endl;
491
492 // Create namespace that can be used to access the extern'd cms2
493 // object methods without having to type cms2. everywhere.
494 // Does not include cms2.Init and cms2.GetEntry because I think
495 // it is healthy to leave those methods as they are
496 headerf << "namespace tas {" << endl;
497 for (Int_t i = 0; i< aliasarray->GetSize(); i++) {
498 TString aliasname(aliasarray->At(i)->GetName());
499 TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
500 TString classname = branch->GetClassName();
501 TString title = branch->GetTitle();
502 if ( classname.Contains("vector") ) {
503 if(classname.Contains("edm::Wrapper") ) {
504 classname = classname(0,classname.Length()-2);
505 classname.ReplaceAll("edm::Wrapper<","");
506 }
507 headerf << "\t" << classname << " &" << aliasname << "()";
508 } else {
509 if(classname.Contains("edm::Wrapper<") ) {
510 classname = classname(0,classname.Length()-1);
511 classname.ReplaceAll("edm::Wrapper<","");
512 }
513 if(classname != "" ) {
514 headerf << "\t" << classname << " &" << aliasname << "()";
515 } else {
516 if(title.EndsWith("/i"))
517 headerf << "\tunsigned int &" << aliasname << "()";
518 if(title.EndsWith("/F"))
519 headerf << "\tfloat &" << aliasname << "()";
520 if(title.EndsWith("/I"))
521 headerf << "\tint &" << aliasname << "()";
522 }
523 }
524 headerf << " { return cms2." << aliasname << "(); }" << endl;
525 }
526 if(haveHLTInfo) {
527 //functions to return whether or not trigger fired - HLT
528 headerf << "\t" << "static bool passHLTTrigger(TString trigName) { return cms2.passHLTTrigger(trigName); }" << endl;
529 }//if(haveHLTInfo)
530 if(haveHLT8E29Info) {
531 //functions to return whether or not trigger fired - HLT
532 headerf << "\t" << "static bool passHLT8E29Trigger(TString trigName) { return cms2.passHLT8E29Trigger(trigName); }" << endl;
533 }//if(haveHLT8E29Info)
534 if(haveL1Info) {
535 //functions to return whether or not trigger fired - L1
536 headerf << "\t" << "static bool passL1Trigger(TString trigName) { return cms2.passL1Trigger(trigName); }" << endl;
537 }//if(haveL1Info)
538
539 }
540
541
542 void makeSrcFile(TFile *f, bool paranoid, std::string Classname, std::string branchNamesFile) {
543
544 TTree *ev = (TTree*)f->Get("Events");
545
546 codef << "/* Usage:" << endl;
547 codef << " root [0] .L ScanChain.C++" << endl;
548 codef << " root [1] TFile *_file0 = TFile::Open(\"merged_ntuple.root\")" << endl;
549 codef << " root [2] TChain *chain = new TChain(\"Events\")" << endl;
550 codef << " root [3] chain->Add(\"merged_ntuple.root\")" << endl;
551 codef << endl;
552 codef << " There are several places where one may create " << Classname << " cms2" << endl;
553 codef << " It can be done here (in a doAll.C script), i.e.:" << endl;
554 codef << endl;
555 codef << " root [4] " << Classname << " cms2 " << endl;
556 codef << endl;
557 codef << " It can be done in the source as is done below, or it can be" << endl;
558 codef << " ascertained by including CORE/CMS2.cc as is commented out" << endl;
559 codef << " below. They are all the same, and everything will work so" << endl;
560 codef << " long as it is created somewhere globally." << endl;
561 codef << endl;
562 codef << " root [5] ScanChain(chain)" << endl;
563 codef << "*/" << endl;
564 codef << "#include <iostream>" << endl;
565 codef << "#include <vector>" << endl;
566 codef << "" << endl;
567 codef << "#include \"TChain.h\"" << endl;
568 codef << "#include \"TFile.h\"" << endl;
569 codef << "#include \"TDirectory.h\"" << endl;
570 codef << "#include \"TROOT.h\"" << endl;
571 codef << "" << endl;
572 codef << "#include \"" + Classname+".h\"" << endl;
573 if(branchNamesFile!="")
574 codef << "#include \"branches.h\"" << endl;
575 codef << Classname << " cms2;" << endl;
576 codef << "/*" << endl;
577
578 codef << "#include \"CORE/CMS2.cc\"" << endl;
579 codef << "#include \"CORE/selections.cc\"" << endl;
580 codef << "#include \"CORE/utilities.cc\"" << endl;
581 codef << "*/" << endl;
582 codef << endl;
583 codef << "using namespace tas;" << endl;
584 codef << endl;
585
586 codef << "int ScanChain( TChain* chain, int nEvents = -1, std::string skimFilePrefix=\"\") {" << endl;
587 codef << "" << endl;
588 codef << " TObjArray *listOfFiles = chain->GetListOfFiles();" << endl;
589 codef << "" << endl;
590 codef << " unsigned int nEventsChain=0;" << endl;
591 codef << " if(nEvents==-1) " << endl << " nEvents = chain->GetEntries();" << endl;
592 codef << " nEventsChain = nEvents;" << endl;
593
594 codef << " unsigned int nEventsTotal = 0;" << endl;
595 if(branchNamesFile!="")
596 codef << " InitSkimmedTree(skimFilePrefix);" << endl;
597 codef << " TDirectory *rootdir = gDirectory->GetDirectory(\"Rint:\");" << endl << endl;
598 codef << " TH1F *samplehisto = new TH1F(\"samplehisto\", \"Example histogram\", 200,0,200);" << endl;
599 codef << " samplehisto->SetDirectory(rootdir);" << endl;
600
601 codef << " // file loop" << endl;
602 codef << " TIter fileIter(listOfFiles);" << endl;
603 codef << " TFile *currentFile = 0;" << endl;
604 codef << " while ( currentFile = (TFile*)fileIter.Next() ) {" << endl;
605 codef << " TFile f(currentFile->GetTitle());" << endl;
606 codef << " TTree *tree = (TTree*)f.Get(\"Events\");" << endl;
607 codef << " cms2.Init(tree);" << endl;
608 codef << " " << endl;
609 codef << " //Event Loop" << endl;
610 codef << " unsigned int nEvents = tree->GetEntries();" << endl;
611 codef << " for( unsigned int event = 0; event < nEvents; ++event) {" << endl;
612 codef << " cms2.GetEntry(event);" << endl;
613 codef << " ++nEventsTotal;" << endl;
614 //codef << " std::cout << \"els size: \" << els_p4().size() << \" \";" << endl;
615 //codef << " std::cout << \"mus size: \" << mus_p4().size() << std::endl;" << endl << endl;
616 //codef << " for (unsigned int mus = 0; " << endl;
617 //codef << " mus < mus_p4().size(); mus++) " << endl << endl;
618 //codef << " samplehisto->Fill(mus_p4().at(mus).Pt());" << endl << endl;
619 //codef << " for (unsigned int hyp = 0;" << endl;
620 //codef << " hyp < hyp_jets_p4().size();" << endl;
621 //codef << " ++hyp) {" << endl;
622 //codef << " std::cout << \"hyp: \" << hyp << \"jet corrections:\";" << endl;
623 //codef << " for ( unsigned int jet = 0;" << endl;
624 //codef << " jet < hyp_jets_p4()[hyp].size();" << endl;
625 //codef << " ++jet ) {" << endl;
626 //codef << " std::cout << \" \" << hyp_jets_p4()[hyp][jet].pt();" << endl;
627 //codef << " }" << endl;
628 //codef << " std::cout << endl;" << endl;
629 //codef << " }" << endl;
630 // codef << " if ( hyp_jets_p4().size() == 0 ) {" << endl;
631 //codef << " std::cout << \"no hypothesis!\" << std::endl;" << endl;
632 //codef << " }" << endl;
633 codef << " }" << endl;
634 codef << " }" << endl;
635 codef << "" << endl;
636 codef << " if ( nEventsChain != nEventsTotal ) {" << endl;
637 codef << " std::cout << \"ERROR: number of events from files is not equal to total number of events\" << std::endl;" << endl;
638 codef << " }" << endl;
639 codef << "" << endl;
640 if(branchNamesFile!="") {
641 codef << " outFile_->cd();" << endl;
642 codef << " outTree_->Write();" << endl;
643 codef << " outFile_->Close();" << endl;
644 }
645 codef << " samplehisto->Draw();" << endl;
646 codef << " return 0;" << endl;
647 codef << "}" << endl;
648
649 }
650
651
652
653 void makeBranchFile(std::string branchNamesFile) {
654
655 ifstream branchesF(branchNamesFile.c_str());
656 vector<TString> v_datatypes;
657 vector<TString> v_varNames;
658 while(!branchesF.eof()) {
659 string temp;
660 getline(branchesF, temp);
661 TString line(temp);
662 // do not use commented lines
663 if(line.BeginsWith("//"))
664 continue;
665 vector<TString> v_line;
666 TIter objIt((TObjArray*)line.Tokenize(" "));
667 TObject *obj=NULL;
668 while(obj = (TObject*)objIt.Next()) {
669 if(obj==NULL)
670 continue;
671 v_line.push_back(obj->GetName());
672 }
673
674 if(v_line.size() == 0)
675 continue;
676 TString varName;
677 //loop over v_line until you get to the first element thats not a space
678 for(vector<TString>::iterator it = v_line.begin();
679 it != v_line.end(); it++) {
680 if( *it != " " ) {
681 varName = *it;
682 }
683 }
684
685 v_varNames.push_back(varName.Strip()); //paranoid....strips trailing spaces
686 TString datatype("");
687 for(unsigned int i = 0; i < v_line.size()-1; i++) {
688 TString temp = v_line[i];
689 if(temp.Contains("vector") && !temp.Contains("std::"))
690 temp.ReplaceAll("vector", "std::vector");
691 if(temp.Contains("LorentzVector") && !temp.Contains("ROOT::Math::LorentzVector"))
692 temp.ReplaceAll("LorentzVector", "ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double> >");
693 temp.ReplaceAll(">>", "> >");
694 temp.ReplaceAll(">>>", "> > >");
695 if(i!=0)
696 datatype = datatype+" " + temp;
697 else
698 datatype = datatype+temp;
699 }
700 v_datatypes.push_back(datatype);
701
702 }
703 branchfile.open("branches.h");
704 branchfile << "#ifndef BRANCHES_H" << endl << "#define BRANCHES_H" << endl;
705 branchfile << "#include <vector>" << endl;
706 branchfile << "#include \"TFile.h\"" << endl;
707 branchfile << "#include \"TTree.h\"" << endl << endl << endl << endl;
708
709 for(unsigned int i = 0; i < v_datatypes.size(); i++) {
710 TString temp(v_varNames.at(i));
711 if(v_datatypes.at(i).Contains("vector") || v_datatypes.at(i).Contains("LorentzVector")) {
712 branchfile << v_datatypes.at(i) << " *"
713 << temp.ReplaceAll("_","")+"_;" << endl;
714 continue;
715 }
716 branchfile << v_datatypes.at(i) << " "
717 << temp.ReplaceAll("_","")+"_;" << endl;
718 }
719
720
721 branchfile << "TFile *outFile_;" << endl;
722 branchfile << "TTree *outTree_;" << endl;
723
724 //now declare the branches and set aliases in the InitSkimmedTree function
725 branchfile << "void InitSkimmedTree(std::string skimFilePrefix=\"\") {\n\n";
726 branchfile << " if(skimFilePrefix != \"\")" << endl;
727 branchfile << " outFile_ = TFile::Open(string(skimFilePrefix + \"_skimmednTuple.root\").c_str(),\"RECREATE\");\n";
728 branchfile << " else outFile_ = TFile::Open(\"skimmednTuple.root\", \"RECREATE\");\n";
729 branchfile << " outFile_->cd();" << endl;
730 branchfile << " outTree_ = new TTree(\"Events\", \"\");\n\n";
731 branchfile << " //book the branches\n";
732 for(unsigned int i = 0; i < v_datatypes.size(); i++) {
733 TString varName = v_varNames[i];
734 varName = varName.ReplaceAll("_", "") + "_";
735 TString varType = v_datatypes[i];
736 if(varType.BeginsWith("std::vector")
737 || varType.BeginsWith("ROOT::Math") ) {
738 TString prefix = "";
739 if(varType.BeginsWith("std::vector<float>") )
740 prefix = "floats";
741 if(varType.BeginsWith("std::vector<int>") )
742 prefix = "ints";
743 if(varType.BeginsWith("std::vector<unsigned int>") )
744 prefix = "uints";
745 if(varType.BeginsWith("ROOT::Math::LorentzVector<") )
746 prefix = "doubleROOTMathPxPyPzE4DROOTMathLorentzVector";
747 if(varType.BeginsWith("std::vector<ROOT::Math::LorentzVector<") )
748 prefix = "doubleROOTMathPxPyPzE4DROOTMathLorentzVectors";
749 if(varType.Contains("std::vector<std::vector<ROOT::Math::LorentzVector<") )
750 prefix = "doubleROOTMathPxPyPzE4DROOTMathLorentzVectorss";
751 branchfile << " outTree_->Branch(\"" << prefix + "_" +varName << "\", \""
752 << varType << "\", &" << varName << ");" << endl;
753 branchfile << " outTree_->SetAlias(\"" << v_varNames[i] << "\", "
754 << "\"" << prefix+"_"+varName << "\");" << endl;
755 continue;
756 }
757 if(varType=="float" || varType == "Float_t") {
758 branchfile << " outTree_->Branch(\"" << varName << "\", &" << varName;
759 branchfile << ", \"" << varName + "/F\");" << endl;
760 branchfile << " outTree_->SetAlias(\"" << v_varNames[i] << "\", "
761 << "\"" << varName << "\");" << endl;
762 continue;
763 }
764 if(varType=="unsigned int" || varType == "UInt_t") {
765 branchfile << " outTree_->Branch(\"" << varName << "\", &" << varName;
766 branchfile << ", \"" << varName + "/i\");" << endl;
767 branchfile << " outTree_->SetAlias(\"" << v_varNames[i] << "\", "
768 << "\"" << varName << "\");" << endl;
769 continue;
770 }
771 if(varType=="int" || varType == "Int_t") {
772 branchfile << " outTree_->Branch(\"" << varName << "\", &" << varName;
773 branchfile << ", \"" << varName + "/I\");" << endl;
774 branchfile << " outTree_->SetAlias(\"" << v_varNames[i] << "\", "
775 << "\"" << varName << "\");" << endl;
776 continue;
777 }
778 }
779 branchfile << "} " << endl;
780 branchfile << "#endif" << endl;
781
782 branchfile.close();
783 }
784
785
786 void makeCMS2ClassFiles (std::string fname, bool paranoid = true,
787 std::string branchNamesFile="", std::string className = "CMS2") {
788
789 using namespace std;
790
791 TFile *f = TFile::Open( fname.c_str() );
792 if(f->IsZombie()) {
793 cout << "File is not a valid root file, or root file is corruped" << endl;
794 cout << "Exiting..." << endl;
795 }
796 //class is CMS2 by default
797 //std::string Classname = className=="" ? "CMS2" : className;
798
799 headerf.open((className+".h").c_str());
800 codef.open("ScanChain.C");
801
802 makeHeaderFile(f, paranoid, className);
803 makeSrcFile(f, paranoid, className, branchNamesFile);
804 if(branchNamesFile!="")
805 makeBranchFile(branchNamesFile);
806
807 headerf << "}" << endl;
808 headerf << "#endif" << endl;
809 headerf.close();
810
811
812 codef.close();
813 f->Close();
814 }
815