ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/makeCMS2ClassFiles.C
Revision: 1.1
Committed: Sun Aug 29 18:25:19 2010 UTC (14 years, 8 months ago) by fgolf
Content type: text/plain
Branch: MAIN
CVS Tags: synchMay2011v1, ss20May2011, ss2010_10_06, fkwsynch3pbv2, fkw_3pb_samesign_synch
Log Message:
some tools useful for a variety of studies

File Contents

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