15 |
|
showLegend = false; |
16 |
|
logY = true; |
17 |
|
addTrackJets = false; |
18 |
+ |
addEventInfo = false; |
19 |
|
verbose = true; |
20 |
|
|
21 |
|
globalCuts=""; |
26 |
|
//Fill object PlotTool with Chains constructed from files from given source |
27 |
|
|
28 |
|
int PlotTool::init(string fileName, string dirPath, string treeName, |
29 |
< |
string fileLabel) { |
30 |
< |
this->New(this->GetEntries() ); |
31 |
< |
int currChain = this->GetEntries() - 1; |
29 |
> |
string fileLabel) { |
30 |
> |
this->New(this->GetEntries() ); |
31 |
> |
int currChain = this->GetEntries() - 1; |
32 |
> |
|
33 |
> |
if (currChain < 0) |
34 |
> |
return currChain; |
35 |
> |
|
36 |
> |
TStopwatch timer; |
37 |
> |
timer.Start(); |
38 |
> |
|
39 |
> |
|
40 |
> |
fileNames.clear(); |
41 |
> |
|
42 |
> |
//check if alternative label is different from default(searchString) |
43 |
> |
if (fileLabel=="") { |
44 |
> |
fileLabel=fileName; |
45 |
> |
} |
46 |
> |
((TChain*) this->At(currChain))->SetName(fileLabel.c_str()); |
47 |
> |
|
48 |
> |
TList *files = new TList(); |
49 |
> |
TSystemFile* sysFile = 0; |
50 |
> |
if(fileName.find(".") != string::npos) { |
51 |
> |
ifstream f(fileName.c_str()); |
52 |
> |
if( ! f.is_open() ) return -1; |
53 |
> |
string line; |
54 |
> |
|
55 |
> |
while (!f.eof()) { |
56 |
> |
getline(f,line); |
57 |
> |
sysFile = new TSystemFile(line.c_str(),dirPath.c_str()); |
58 |
> |
files->Add(sysFile); |
59 |
> |
} |
60 |
> |
|
61 |
> |
|
62 |
> |
} else { |
63 |
> |
TSystemDirectory dir("sourceDir", dirPath.c_str()); |
64 |
> |
files = dir.GetListOfFiles(); |
65 |
> |
} |
66 |
> |
if (files->GetEntries()>0) { |
67 |
> |
TIter next(files); |
68 |
> |
TSystemFile *file; |
69 |
> |
TString fname; |
70 |
> |
string filePath; |
71 |
> |
|
72 |
> |
if(verbose && fileName.find(".") == string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: " |
73 |
> |
<<fileName.c_str()<<endl; |
74 |
> |
if(verbose && fileName.find(".") != string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search lines with .root in: " |
75 |
> |
<<fileName.c_str()<<endl; |
76 |
> |
|
77 |
> |
|
78 |
> |
while ((file=(TSystemFile*)next())) { |
79 |
> |
fname = file->GetName(); |
80 |
> |
if (!fname.EndsWith(".root")) |
81 |
> |
continue; |
82 |
> |
if (!fname.Contains(fileName.c_str()) && fileName.find(".") == string::npos ) |
83 |
> |
continue; |
84 |
> |
|
85 |
> |
filePath = dirPath; |
86 |
> |
filePath += fname.Data(); |
87 |
> |
//fwlite::ChainEvent to lop over events, jets, etc |
88 |
> |
fileNames.push_back(filePath.c_str()); |
89 |
> |
|
90 |
> |
if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1, |
91 |
> |
treeName.c_str()) ) |
92 |
> |
if(verbose) cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl; |
93 |
> |
else |
94 |
> |
return -1; |
95 |
> |
|
96 |
> |
} |
97 |
> |
} else |
98 |
> |
return -1; |
99 |
> |
|
100 |
> |
for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) { |
101 |
> |
|
102 |
> |
string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName()); |
103 |
> |
|
104 |
> |
string branch_alias = s; |
105 |
> |
string branch_name = s; |
106 |
> |
if (s.find(".", s.size()-1) != string::npos) |
107 |
> |
branch_name += "obj"; |
108 |
> |
|
109 |
> |
size_t a = s.find("_"); |
110 |
> |
if (a != string::npos) { |
111 |
> |
size_t b = s.find("_", a+1); |
112 |
> |
if (b != string::npos) { |
113 |
> |
size_t c = s.find("_", b+1); |
114 |
> |
if (c != string::npos) { |
115 |
> |
string _prod =s.substr(0,a); |
116 |
> |
string _label =s.substr(a+1, b-a-1); |
117 |
> |
string _instance =s.substr(b+1, c-b-1); |
118 |
> |
branch_alias = _label; |
119 |
> |
if(_instance.length() > 0 ) { |
120 |
> |
branch_alias += "_"; |
121 |
> |
branch_alias += _instance; |
122 |
> |
((TChain*) this->At(currChain))->SetAlias(_instance.c_str(), |
123 |
> |
branch_name.c_str()); |
124 |
> |
} |
125 |
> |
string branch_alias_full = _prod + "_" + branch_alias; |
126 |
> |
((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(), |
127 |
> |
branch_name.c_str()); |
128 |
> |
} |
129 |
> |
} |
130 |
> |
} |
131 |
|
|
132 |
< |
if (currChain < 0) |
133 |
< |
return currChain; |
132 |
> |
((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(), |
133 |
> |
branch_name.c_str()); |
134 |
|
|
135 |
< |
TStopwatch timer; |
36 |
< |
timer.Start(); |
135 |
> |
} |
136 |
|
|
137 |
+ |
if( addEventInfo || addTrackJets) { |
138 |
|
|
139 |
< |
fileNames.clear(); |
139 |
> |
if(verbose) cout<<"calculating additional variables..."<<endl; |
140 |
|
|
141 |
< |
//check if alternative label is different from default(searchString) |
142 |
< |
if (fileLabel=="") { |
143 |
< |
fileLabel=fileName; |
144 |
< |
} |
145 |
< |
((TChain*) this->At(currChain))->SetName(fileLabel.c_str()); |
141 |
> |
//make file for tree friends (adding additional, computed branches) |
142 |
> |
string friendFileName("/scratch/hh/current/cms/user/"); |
143 |
> |
friendFileName += gSystem->GetUserInfo()->fUser; |
144 |
> |
friendFileName += "/temp/", |
145 |
> |
friendFileName += fileName; |
146 |
> |
friendFileName += ".root"; |
147 |
> |
TFile *f = new TFile(friendFileName.c_str(),"recreate"); |
148 |
|
|
149 |
< |
TList *files = new TList(); |
48 |
< |
TSystemFile* sysFile = 0; |
49 |
< |
if(fileName.find(".") != string::npos) { |
50 |
< |
ifstream f(fileName.c_str()); |
51 |
< |
if( ! f.is_open() ) return -1; |
52 |
< |
string line; |
53 |
< |
|
54 |
< |
while (!f.eof()) { |
55 |
< |
getline(f,line); |
56 |
< |
sysFile = new TSystemFile(line.c_str(),dirPath.c_str()); |
57 |
< |
files->Add(sysFile); |
58 |
< |
} |
149 |
> |
if( f->IsZombie() ) return -1; |
150 |
|
|
151 |
+ |
string friendTreeName("friendTree"); |
152 |
+ |
//char number = currChain; |
153 |
+ |
//friendTreeName += number; |
154 |
+ |
//TTree *friendTree = new TTree("friendTree","friendTree"); |
155 |
+ |
TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str()); |
156 |
+ |
fwlite::ChainEvent ev(fileNames); |
157 |
+ |
int _event, _run, _lumi; |
158 |
+ |
int nJetsKT; |
159 |
+ |
TrackJetKT = new float [100]; |
160 |
|
|
161 |
< |
} else { |
162 |
< |
TSystemDirectory dir("sourceDir", dirPath.c_str()); |
63 |
< |
files = dir.GetListOfFiles(); |
64 |
< |
} |
65 |
< |
if (files->GetEntries()>0) { |
66 |
< |
TIter next(files); |
67 |
< |
TSystemFile *file; |
68 |
< |
TString fname; |
69 |
< |
string filePath; |
70 |
< |
|
71 |
< |
if(verbose && fileName.find(".") == string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: " |
72 |
< |
<<fileName.c_str()<<endl; |
73 |
< |
if(verbose && fileName.find(".") != string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search lines with .root in: " |
74 |
< |
<<fileName.c_str()<<endl; |
75 |
< |
|
76 |
< |
|
77 |
< |
while ((file=(TSystemFile*)next())) { |
78 |
< |
fname = file->GetName(); |
79 |
< |
if (!fname.EndsWith(".root")) |
80 |
< |
continue; |
81 |
< |
if (!fname.Contains(fileName.c_str()) && fileName.find(".") == string::npos ) |
82 |
< |
continue; |
83 |
< |
|
84 |
< |
filePath = dirPath; |
85 |
< |
filePath += fname.Data(); |
86 |
< |
//fwlite::ChainEvent to lop over events, jets, etc |
87 |
< |
fileNames.push_back(filePath.c_str()); |
88 |
< |
|
89 |
< |
if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1, |
90 |
< |
treeName.c_str()) ) |
91 |
< |
if(verbose) cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl; |
92 |
< |
else |
93 |
< |
return -1; |
161 |
> |
if( addEventInfo ) { |
162 |
> |
|
163 |
|
|
164 |
< |
} |
165 |
< |
} else |
166 |
< |
return -1; |
164 |
> |
friendTree->Branch("event", &_event, "event/I"); |
165 |
> |
friendTree->Branch("run", &_run, "run/I"); |
166 |
> |
friendTree->Branch("lumi", &_lumi, "lumi/I"); |
167 |
|
|
168 |
< |
for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) { |
168 |
> |
} |
169 |
|
|
170 |
< |
string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName()); |
170 |
> |
if( addTrackJets ) { |
171 |
|
|
103 |
– |
string branch_alias = s; |
104 |
– |
string branch_name = s; |
105 |
– |
if (s.find(".", s.size()-1) != string::npos) |
106 |
– |
branch_name += "obj"; |
107 |
– |
|
108 |
– |
size_t a = s.find("_"); |
109 |
– |
if (a != string::npos) { |
110 |
– |
size_t b = s.find("_", a+1); |
111 |
– |
if (b != string::npos) { |
112 |
– |
size_t c = s.find("_", b+1); |
113 |
– |
if (c != string::npos) { |
114 |
– |
string _prod =s.substr(0,a); |
115 |
– |
string _label =s.substr(a+1, b-a-1); |
116 |
– |
string _instance =s.substr(b+1, c-b-1); |
117 |
– |
branch_alias = _label; |
118 |
– |
if(_instance.length() > 0 ) { |
119 |
– |
branch_alias += "_"; |
120 |
– |
branch_alias += _instance; |
121 |
– |
((TChain*) this->At(currChain))->SetAlias(_instance.c_str(), |
122 |
– |
branch_name.c_str()); |
123 |
– |
} |
124 |
– |
string branch_alias_full = _prod + "_" + branch_alias; |
125 |
– |
((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(), |
126 |
– |
branch_name.c_str()); |
127 |
– |
} |
128 |
– |
} |
129 |
– |
} |
172 |
|
|
173 |
< |
((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(), |
174 |
< |
branch_name.c_str()); |
173 |
> |
friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I"); |
174 |
> |
friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F"); |
175 |
> |
} |
176 |
|
|
134 |
– |
} |
177 |
|
|
178 |
< |
// add branch with track Jets |
178 |
> |
int tenth = ev.size() / 10; |
179 |
> |
int eventNum =0; |
180 |
> |
for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) { |
181 |
|
|
138 |
– |
if( addTrackJets ) { |
182 |
|
|
183 |
< |
//make file for tree friends (adding additional, computed branches) |
141 |
< |
string friendFileName("/scratch/hh/current/cms/user/"); |
142 |
< |
friendFileName += gSystem->GetUserInfo()->fUser; |
143 |
< |
friendFileName += "/temp/", |
144 |
< |
friendFileName += fileName; |
145 |
< |
friendFileName += ".root"; |
146 |
< |
TFile *f = new TFile(friendFileName.c_str(),"recreate"); |
147 |
< |
|
148 |
< |
if( f->IsZombie() ) return -1; |
149 |
< |
|
150 |
< |
string friendTreeName("friendTree"); |
151 |
< |
//char number = currChain; |
152 |
< |
//friendTreeName += number; |
153 |
< |
//TTree *friendTree = new TTree("friendTree","friendTree"); |
154 |
< |
TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str()); |
155 |
< |
|
156 |
< |
int nJetsKT; |
157 |
< |
TrackJetKT = new float [100]; |
158 |
< |
fwlite::ChainEvent ev(fileNames); |
159 |
< |
|
160 |
< |
friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I"); |
161 |
< |
friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F"); |
162 |
< |
|
163 |
< |
if(verbose) cout<<"calculating additional variables..."<<endl; |
164 |
< |
|
165 |
< |
|
166 |
< |
|
167 |
< |
int tenth = ev.size() / 10; |
168 |
< |
int eventNum =0; |
169 |
< |
for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) { |
170 |
< |
|
171 |
< |
fwlite::Handle<reco::CaloJetCollection> jets; |
172 |
< |
jets.getByLabel(ev, "antikt5CaloJets"); |
173 |
< |
|
174 |
< |
fwlite::Handle<reco::TrackCollection> tracks; |
175 |
< |
tracks.getByLabel(ev, "generalTracks"); |
176 |
< |
|
177 |
< |
if (!jets.isValid()) |
178 |
< |
continue; |
179 |
< |
if (!tracks.isValid()) |
180 |
< |
continue; |
181 |
< |
double trackSum; |
182 |
< |
nJetsKT = 0; |
183 |
< |
for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet |
184 |
< |
!=jets->end(); jet++) { |
185 |
< |
trackSum = 0; |
186 |
< |
for (reco::TrackCollection::const_iterator track = tracks->begin(); track |
187 |
< |
!=tracks->end(); track++) { |
188 |
< |
if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi()) |
189 |
< |
< 0.5) |
190 |
< |
trackSum += track->pt(); |
191 |
< |
} |
192 |
< |
TrackJetKT[nJetsKT] = trackSum; |
193 |
< |
nJetsKT++; |
194 |
< |
} |
195 |
< |
friendTree->Fill(); |
183 |
> |
if( addEventInfo ) { |
184 |
|
|
185 |
< |
if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl; |
185 |
> |
_event = ev.id().event(); |
186 |
> |
_run = ev.id().run(); |
187 |
> |
_lumi = ev.luminosityBlock(); |
188 |
|
|
189 |
+ |
} |
190 |
+ |
|
191 |
+ |
if( addTrackJets ) { |
192 |
+ |
|
193 |
+ |
fwlite::Handle<reco::CaloJetCollection> jets; |
194 |
+ |
jets.getByLabel(ev, "antikt5CaloJets"); |
195 |
+ |
|
196 |
+ |
fwlite::Handle<reco::TrackCollection> tracks; |
197 |
+ |
tracks.getByLabel(ev, "generalTracks"); |
198 |
+ |
|
199 |
+ |
if (!jets.isValid()) |
200 |
+ |
continue; |
201 |
+ |
if (!tracks.isValid()) |
202 |
+ |
continue; |
203 |
+ |
double trackSum; |
204 |
+ |
nJetsKT = 0; |
205 |
+ |
for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet |
206 |
+ |
!=jets->end(); jet++) { |
207 |
+ |
trackSum = 0; |
208 |
+ |
for (reco::TrackCollection::const_iterator track = tracks->begin(); track |
209 |
+ |
!=tracks->end(); track++) { |
210 |
+ |
if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi()) |
211 |
+ |
< 0.5) |
212 |
+ |
trackSum += track->pt(); |
213 |
|
} |
214 |
< |
f->cd(); |
215 |
< |
friendTree->Write(); |
202 |
< |
f->Close(); |
203 |
< |
((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(), |
204 |
< |
friendFileName.c_str()); |
214 |
> |
TrackJetKT[nJetsKT] = trackSum; |
215 |
> |
nJetsKT++; |
216 |
|
} |
217 |
+ |
} |
218 |
+ |
friendTree->Fill(); |
219 |
+ |
|
220 |
+ |
if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl; |
221 |
+ |
|
222 |
+ |
} |
223 |
+ |
f->cd(); |
224 |
+ |
friendTree->Write(); |
225 |
+ |
f->Close(); |
226 |
+ |
((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(), |
227 |
+ |
friendFileName.c_str()); |
228 |
+ |
|
229 |
+ |
|
230 |
+ |
|
231 |
+ |
} |
232 |
+ |
|
233 |
|
|
234 |
< |
timer.Stop(); |
235 |
< |
if(verbose) timer.Print(); |
234 |
> |
timer.Stop(); |
235 |
> |
if(verbose) timer.Print(); |
236 |
|
|
237 |
< |
return this->GetEntries(); |
237 |
> |
return this->GetEntries(); |
238 |
|
|
239 |
|
} |
240 |
|
//------------------------------------------------------------------ |
344 |
|
|
345 |
|
if(verbose && draw_err > 0) { |
346 |
|
timer.Stop(); |
347 |
< |
cout<<"Done: Selected "<<draw_err<<" of total "<< currN <<" events."<<endl; |
347 |
> |
cout<<"Done: Selected "<<draw_err<<" objects in "<< currN <<" processed events."<<endl; |
348 |
|
timer.Print(); |
349 |
|
} |
350 |
|
return draw_err; |