forked from CMUCMS/CommonCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduceSimpleTree.cc
More file actions
321 lines (262 loc) · 10.3 KB
/
Copy pathproduceSimpleTree.cc
File metadata and controls
321 lines (262 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "SimpleEventProducer.h"
#include "Utilities.h"
#include "HLT.h"
#include <iostream>
#include <stdexcept>
#include "TFile.h"
#include "TChain.h"
#include "TH1.h"
#include "TSystem.h"
class SimpleTreeProducer {
public:
SimpleTreeProducer();
~SimpleTreeProducer() {}
bool initialize(char const*, TString const& = "", bool = true, bool = true, bool = false, bool = false);
bool run(char const*);
bool finalize();
void setThrow(bool _val) { throw_ = _val; }
private:
TFile* outputFile_;
TTree* evtTree_;
TTree* selectedObjTree_;
TTree* allObjTree_;
susy::SimpleEventProducer eventProducer_;
bool fillTriggerEvent_;
bool throw_;
};
SimpleTreeProducer::SimpleTreeProducer() :
outputFile_(0),
evtTree_(0),
selectedObjTree_(0),
allObjTree_(0),
eventProducer_(),
fillTriggerEvent_(false),
throw_(false)
{
}
bool
SimpleTreeProducer::initialize(char const* _outputDir, TString const& _puScenario/* = ""*/, bool _fillSelected/* = true*/, bool _fillAll/* = true*/, bool _fillPF/* = false*/, bool _fillTriggerEvent/* = false*/)
{
/* DEFINE OUTPUT TREES */
TString outputName(_outputDir);
if(!outputName.Contains(".root")) outputName += "/simpleTree.root";
outputFile_ = TFile::Open(outputName, "recreate");
if(!outputFile_ || outputFile_->IsZombie()){
std::cerr << "Output " << outputName << " not opened" << std::endl;
delete outputFile_;
if(throw_) throw std::runtime_error("IOError");
else return false;
}
outputFile_->cd();
evtTree_ = new TTree("eventVars", "Event variables");
evtTree_->SetAutoSave(10000000);
if(_fillSelected){
selectedObjTree_ = new TTree("selectedObjects", "Selected objects");
selectedObjTree_->SetAutoSave(10000000);
}
if(_fillAll){
allObjTree_ = new TTree("allObjects", "All objects");
allObjTree_->SetAutoSave(10000000);
}
fillTriggerEvent_ = _fillTriggerEvent;
/* SETUP EVENT PRODUCER */
eventProducer_.setSavePF(_fillPF);
for(unsigned iT(0); iT != nHLTPaths; ++iT)
eventProducer_.addHLTPath(hltPaths[iT]);
if(fillTriggerEvent_){
for(unsigned iF(0); iF != nHLTEventFilters; ++iF)
eventProducer_.addHLTEventFilter(hltEventFilters[iF]);
for(unsigned iF(0); iF != nHLTPhotonFilters; ++iF)
eventProducer_.addHLTPhotonFilter(hltPhotonFilters[iF]);
for(unsigned iF(0); iF != nHLTElectronFilters; ++iF)
eventProducer_.addHLTElectronFilter(hltElectronFilters[iF]);
for(unsigned iF(0); iF != nHLTMuonFilters; ++iF)
eventProducer_.addHLTMuonFilter(hltMuonFilters[iF]);
}
eventProducer_.setPhotonId(susy::PhLoose12);
eventProducer_.setElectronId(susy::ElMedium12);
eventProducer_.setMuonId(susy::MuTight12);
eventProducer_.setJetId(susy::JtLoose);
TH1* puWeights(0);
if(_puScenario.Length() > 0){
std::cout << "Running in MC mode with PU scenario " << _puScenario << std::endl;
/* DEFINE LIST OF MC PARAMETERS TO INCLUDE */
eventProducer_.addGridParam("ptHat");
if(_puScenario != "None"){
TFile* puWeightSource(TFile::Open(TString(gSystem->DirName(__FILE__)) + "/puReweighting_2012.root"));
if(!puWeightSource || puWeightSource->IsZombie() || !(puWeights = dynamic_cast<TH1*>(puWeightSource->Get("weight" + _puScenario)))){
std::cerr << "PU weights for scenario " << _puScenario << " not found" << std::endl;
if(throw_) throw std::invalid_argument("PU weights");
else return false;
}
}
}
else{
std::cout << "Running in real-data mode as no PU scenario was specified." << std::endl;
std::cout << "If this is a MC sample, check the dataset name and rerun the function with a valid scenario name (e.g. S10, PU50)." << std::endl;
}
/* INITIALIZE EVENT PRODUCER */
eventProducer_.initialize(evtTree_, selectedObjTree_, allObjTree_, puWeights);
return true;
}
bool
SimpleTreeProducer::run(char const* _sourcePath)
{
TObjArray* sourcePaths(TString(_sourcePath).Tokenize(","));
if(sourcePaths->GetEntries() == 0){
delete sourcePaths;
std::cerr << "No source name provided" << std::endl;
if(throw_) throw std::invalid_argument("Input");
else return false;
}
TString susyTreePath(sourcePaths->At(0)->GetName());
TString susyTriggerPath;
if(fillTriggerEvent_ && sourcePaths->GetEntries() == 2) susyTriggerPath = sourcePaths->At(1)->GetName();
delete sourcePaths;
TChain input("susyTree");
input.Add(susyTreePath);
if(input.GetEntries() == 0) return false;
TChain triggerInput("triggerEventTree");
if(susyTriggerPath.Length() != 0) triggerInput.Add(susyTriggerPath);
/* DISABLE UNUSED INPUT BRANCHES TO SPEED UP THE PROCESSING */
input.SetBranchStatus("*", 0);
input.SetBranchStatus("runNumber", 1);
input.SetBranchStatus("luminosityBlockNumber", 1);
input.SetBranchStatus("eventNumber", 1);
input.SetBranchStatus("metFilter*", 1);
input.SetBranchStatus("hlt*", 1);
input.SetBranchStatus("pfParticles*", 1);
input.SetBranchStatus("met_pfType01CorrectedMet*", 1);
input.SetBranchStatus("beamSpot*", 1);
if(input.GetBranch("pu")) input.SetBranchStatus("pu*", 1);
if(input.GetBranch("genParticles")) input.SetBranchStatus("genParticles*", 1);
if(input.GetBranch("met_genMetTrue.")) input.SetBranchStatus("met_genMetTrue*", 1);
if(input.GetBranch("gridParams_ptHat")) input.SetBranchStatus("gridParams*", 1);
susy::ObjectTree::setBranchStatus(input); // set status = 1 for photon-, electron-, muon-, jet-, and vertex-related branches
/* SET INPUT BRANCH ADDRESS TO EVENT OBJECT */
susy::Event event;
susy::TriggerEvent triggerEvent;
if(fillTriggerEvent_){
if(susyTriggerPath.Length() != 0)
triggerEvent.bindTree(&input, &triggerInput);
else
triggerEvent.bindTree(&input, "susyEvents", "susyTriggers");
}
event.setInput(input);
if(event.getEntry(0) <= 0){
std::cerr << "Input is empty or corrupted" << std::endl;
delete outputFile_;
if(throw_) throw std::runtime_error("IOError");
else return false;
}
/* EVENT LOOP */
bool failed(false);
long iEntry(0);
int nRead(0);
while((nRead = event.getEntry(iEntry++)) != 0){
if(nRead < 0){
std::cerr << "Input corrupted at entry " << iEntry - 1 << std::endl;
if(throw_) throw std::runtime_error("IOError");
else return false;
}
if(iEntry % 10000 == 1) std::cout << "Processing event " << iEntry - 1 << "..." << std::endl;
try{
if(fillTriggerEvent_) eventProducer_.extractTriggerObjects(triggerEvent);
eventProducer_.produce(event);
}
catch(std::exception& e){
std::cerr << "Exception caught:" << std::endl;
std::cerr << e.what() << std::endl;
std::cerr << "Run " << event.runNumber << ", Lumi " << event.luminosityBlockNumber << ", Event " << event.eventNumber << " in " << std::endl;
std::cerr << input.GetCurrentFile()->GetName() << std::endl;
susy::Exception* susyExcept(dynamic_cast<susy::Exception*>(&e));
if(susyExcept){
std::cerr << "This was an exception of category " << susyExcept->categoryName() << std::endl;
switch(susyExcept->category){
case susy::Exception::kEventAnomaly:
std::cerr << "Skipping event.." << std::endl;
continue;
case susy::Exception::kObjectAnomaly:
case susy::Exception::kIOError:
case susy::Exception::kFormatError:
default:
break;
}
}
failed = true;
break;
}
evtTree_->Fill();
if(selectedObjTree_) selectedObjTree_->Fill();
if(allObjTree_) allObjTree_->Fill();
}
triggerEvent.reset();
event.releaseTrees();
std::cout << "Processed " << iEntry - 1 << " Events." << std::endl;
if(failed){
if(throw_) throw std::runtime_error("produceSimpleTree");
else return false;
}
return true;
}
bool
SimpleTreeProducer::finalize()
{
/* CLEANUP & FINALZE */
outputFile_->cd();
outputFile_->Write();
delete outputFile_;
return true;
}
void
produceSimpleTree(TString const& _sourceName, TString const& _outputName, TString const& _puScenario = "", bool _fillSelected = true, bool _fillAll = true, bool _fillPF = false, bool _fillTriggerEvent = false)
{
SimpleTreeProducer producer;
producer.setThrow(true);
producer.initialize(_outputName, _puScenario, _fillSelected, _fillAll, _fillPF, _fillTriggerEvent) &&
producer.run(_sourceName) &&
producer.finalize();
}
void
produceSimpleTree(TString const& _puScenario, bool _fillSelected, bool _fillAll, bool _fillPF, bool _fillTriggerEvent, TObjArray* _urls, TObjArray* _outputName)
{
SimpleTreeProducer producer;
producer.setThrow(true);
if(!producer.initialize(_outputName->At(0)->GetName(), _puScenario, _fillSelected, _fillAll, _fillPF, _fillTriggerEvent)) return;
for(int iS(0); iS != _urls->GetEntries(); ++iS)
if(!producer.run(_urls->At(iS)->GetName())) return;
producer.finalize();
}
void
produceSimpleTree(TString const& _puScenario, TObjArray* _urls, TObjArray* _outputName)
{
produceSimpleTree(_puScenario, true, true, true, false, _urls, _outputName);
}
void
produceSimpleTree(TString const& _puScenario, bool _fillSelected, bool _fillAll, bool _fillPF, bool _fillTriggerEvent, TString const& _dataset, TObjArray* _inputLines, TObjArray* _outputDir)
{
// For "externalList" mode of dcmu job scheduler
// The list must have the grid point name in the (N+1)n-th rows (N=files per point, n=0,1,...) and the file names in the folloing N rows
// example: TChiwg.list
// TChiwg_1000_1000
// TChiwg_1000_1000_10_4_2zc.root
// ...
// Then the submit command is
// submitDCMUJobs.py -n 51 -J TChiwg -x "$PWD/TChiwg.list" -s 'output_directory' -z /store/RA3Ntuples/... produceSimpleTree.cc
TObjArray outputName;
outputName.SetOwner(true);
TString gridPoint(_inputLines->At(0)->GetName());
TString outputFileName(_outputDir->At(0)->GetName());
outputFileName += "/" + gridPoint + ".root";
outputName.Add(new TObjString(outputFileName));
TObjArray urls;
urls.SetOwner(true);
for(int iL(1); iL != _inputLines->GetEntries(); ++iL)
urls.Add(new TObjString(_dataset + "/" + _inputLines->At(iL)->GetName()));
produceSimpleTree(_puScenario, _fillSelected, _fillAll, _fillPF, _fillTriggerEvent, &urls, &outputName);
}
void
produceSimpleTree(TString const& _puScenario, TString const& _dataset, TObjArray* _inputLines, TObjArray* _outputDir)
{
produceSimpleTree(_puScenario, true, true, true, false, _dataset, _inputLines, _outputDir);
}