ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/makeCMS2ClassFiles.C
Revision: 1.2
Committed: Thu May 26 03:34:52 2011 UTC (13 years, 11 months ago) by fgolf
Content type: text/plain
Branch: MAIN
CVS Tags: ss_summer2011approvalV2, ss_summer2011approval
Changes since 1.1: +79 -58 lines
Log Message:
updated to suppress warnings

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