Fork me on GitHub

source: git/readers/DelphesCMSFWLite.cpp@ af181bf

ImprovedOutputFile Timing
Last change on this file since af181bf was 77e9ae1, checked in by Pavel Demin <pavel-demin@…>, 6 years ago

set Standard to Cpp03 in .clang-format

  • Property mode set to 100644
File size: 13.2 KB
RevLine 
[b443089]1/*
2 * Delphes: a framework for fast simulation of a generic collider experiment
3 * Copyright (C) 2012-2014 Universite catholique de Louvain (UCL), Belgium
[1fa50c2]4 *
[b443089]5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
[1fa50c2]9 *
[b443089]10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
[1fa50c2]14 *
[b443089]15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
[975405a]19#include <algorithm>
[d7d2da3]20#include <iostream>
21#include <memory>
[341014c]22#include <sstream>
23#include <stdexcept>
[d7d2da3]24
25#include <map>
[975405a]26#include <vector>
[d7d2da3]27
28#include <signal.h>
29#include <stdio.h>
[341014c]30#include <stdlib.h>
[d7d2da3]31
32#include "TApplication.h"
[341014c]33#include "TROOT.h"
[d7d2da3]34
[341014c]35#include "TDatabasePDG.h"
[d7d2da3]36#include "TFile.h"
[341014c]37#include "TLorentzVector.h"
[d7d2da3]38#include "TObjArray.h"
39#include "TParticlePDG.h"
[341014c]40#include "TStopwatch.h"
[d7d2da3]41
42#include "classes/DelphesClasses.h"
43#include "classes/DelphesFactory.h"
[341014c]44#include "classes/DelphesStream.h"
45#include "modules/Delphes.h"
[d7d2da3]46
47#include "ExRootAnalysis/ExRootProgressBar.h"
[341014c]48#include "ExRootAnalysis/ExRootTreeBranch.h"
49#include "ExRootAnalysis/ExRootTreeWriter.h"
[d7d2da3]50
51#include "DataFormats/FWLite/interface/Event.h"
52#include "DataFormats/FWLite/interface/Handle.h"
53#include "DataFormats/HepMCCandidate/interface/GenParticle.h"
[386f526]54#include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
[341014c]55#include "FWCore/FWLite/interface/FWLiteEnabler.h"
[f29758e]56#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
[341014c]57#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
[3241a0e]58#include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
59#include "SimDataFormats/GeneratorProducts/interface/WeightsInfo.h"
[d7d2da3]60
61using namespace std;
62
63//---------------------------------------------------------------------------
64
[f29758e]65void ConvertInput(fwlite::Event &event, Long64_t eventCounter,
[2886328]66 ExRootTreeBranch *branchEvent, ExRootTreeBranch *branchWeight,
[5df97ac]67 DelphesFactory *factory, TObjArray *allParticleOutputArray,
68 TObjArray *stableParticleOutputArray, TObjArray *partonOutputArray, Bool_t firstEvent)
[d7d2da3]69{
[f29758e]70
[341014c]71 fwlite::Handle<GenEventInfoProduct> handleGenEventInfo;
72 fwlite::Handle<LHEEventProduct> handleLHEEvent;
[77e9ae1]73 fwlite::Handle<vector<reco::GenParticle> > handleParticle;
74 fwlite::Handle<vector<pat::PackedGenParticle> > handlePackedParticle;
[480f9ed]75
[341014c]76 vector<reco::GenParticle>::const_iterator itParticle;
77 vector<pat::PackedGenParticle>::const_iterator itPackedParticle;
[975405a]78
[341014c]79 vector<const reco::Candidate *> vectorCandidate;
80 vector<const reco::Candidate *>::iterator itCandidate;
[975405a]81
[f29758e]82 handleGenEventInfo.getByLabel(event, "generator");
[480f9ed]83
[5df97ac]84 if(!((handleLHEEvent.getBranchNameFor(event, "source")).empty()))
85 {
[480f9ed]86 handleLHEEvent.getByLabel(event, "source");
87 }
[5df97ac]88 else if(!((handleLHEEvent.getBranchNameFor(event, "externalLHEProducer")).empty()))
[480f9ed]89 {
90 handleLHEEvent.getByLabel(event, "externalLHEProducer");
91 }
[5df97ac]92 else if(firstEvent)
[480f9ed]93 {
[5df97ac]94 cout << "Wrong LHEEvent Label! Please, check the input file." << endl;
[480f9ed]95 }
96
[5df97ac]97 if(!((handleParticle.getBranchNameFor(event, "genParticles")).empty()))
[480f9ed]98 {
99 handleParticle.getByLabel(event, "genParticles");
100 }
[341014c]101 else if(!((handlePackedParticle.getBranchNameFor(event, "packedGenParticles")).empty()) && !((handleParticle.getBranchNameFor(event, "prunedGenParticles")).empty()))
[480f9ed]102 {
[837fa70]103 handleParticle.getByLabel(event, "prunedGenParticles");
[386f526]104 handlePackedParticle.getByLabel(event, "packedGenParticles");
[480f9ed]105 }
106 else
107 {
[341014c]108 std::cout << "Wrong GenParticle Label! Please, check the input file." << std::endl;
[480f9ed]109 exit(-1);
110 }
[d7d2da3]111
[7611cb9]112 Bool_t foundLHE = !((handleLHEEvent.getBranchNameFor(event, "source")).empty()) || !((handleLHEEvent.getBranchNameFor(event, "externalLHEProducer")).empty());
[341014c]113 Bool_t isMiniAOD = !((handlePackedParticle.getBranchNameFor(event, "packedGenParticles")).empty()) && ((handleParticle.getBranchNameFor(event, "genParticles")).empty());
[d7d2da3]114
[f29758e]115 HepMCEvent *element;
[3241a0e]116 Weight *weight;
[d7d2da3]117 Candidate *candidate;
118 TDatabasePDG *pdg;
119 TParticlePDG *pdgParticle;
120 Int_t pdgCode;
121
122 Int_t pid, status;
[80d4a34]123 Double_t px, py, pz, e, mass;
[d7d2da3]124 Double_t x, y, z;
125
[f29758e]126 element = static_cast<HepMCEvent *>(branchEvent->NewEntry());
127
128 element->Number = eventCounter;
129
130 element->ProcessID = handleGenEventInfo->signalProcessID();
131 element->MPI = 1;
[529fe78]132 element->Weight = handleGenEventInfo->weight();
[f29758e]133 element->Scale = handleGenEventInfo->qScale();
134 element->AlphaQED = handleGenEventInfo->alphaQED();
135 element->AlphaQCD = handleGenEventInfo->alphaQCD();
136
137 element->ID1 = 0;
138 element->ID2 = 0;
139 element->X1 = 0.0;
140 element->X2 = 0.0;
141 element->ScalePDF = 0.0;
142 element->PDF1 = 0.0;
143 element->PDF2 = 0.0;
144
145 element->ReadTime = 0.0;
146 element->ProcTime = 0.0;
147
[7611cb9]148 if(foundLHE)
[3241a0e]149 {
[341014c]150 const vector<gen::WeightsInfo> &vectorWeightsInfo = handleLHEEvent->weights();
151 vector<gen::WeightsInfo>::const_iterator itWeightsInfo;
[386f526]152
[7611cb9]153 for(itWeightsInfo = vectorWeightsInfo.begin(); itWeightsInfo != vectorWeightsInfo.end(); ++itWeightsInfo)
154 {
[2886328]155 weight = static_cast<Weight *>(branchWeight->NewEntry());
[7611cb9]156 weight->Weight = itWeightsInfo->wgt;
[5df97ac]157 }
[3241a0e]158 }
159
[d7d2da3]160 pdg = TDatabasePDG::Instance();
161
[837fa70]162 for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
163 {
164 const reco::GenParticle &particle = *itParticle;
[5df97ac]165 if(!isMiniAOD || particle.status() != 1) vectorCandidate.push_back(&*itParticle);
[837fa70]166 }
[d7d2da3]167
[837fa70]168 for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
169 {
170 const reco::GenParticle &particle = *itParticle;
[d7d2da3]171
[837fa70]172 pid = particle.pdgId();
173 status = particle.status();
[5df97ac]174 if(isMiniAOD && particle.status() == 1) continue;
[341014c]175 px = particle.px();
176 py = particle.py();
177 pz = particle.pz();
178 e = particle.energy();
179 mass = particle.mass();
180 x = particle.vx();
181 y = particle.vy();
182 z = particle.vz();
[d7d2da3]183
[837fa70]184 candidate = factory->NewCandidate();
[d7d2da3]185
[837fa70]186 candidate->PID = pid;
187 pdgCode = TMath::Abs(candidate->PID);
[d7d2da3]188
[837fa70]189 candidate->Status = status;
[f29758e]190
[837fa70]191 if(particle.mother())
192 {
193 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.mother());
194 if(itCandidate != vectorCandidate.end()) candidate->M1 = distance(vectorCandidate.begin(), itCandidate);
195 }
[975405a]196
[837fa70]197 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(0));
198 if(itCandidate != vectorCandidate.end()) candidate->D1 = distance(vectorCandidate.begin(), itCandidate);
[975405a]199
[837fa70]200 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(particle.numberOfDaughters() - 1));
201 if(itCandidate != vectorCandidate.end()) candidate->D2 = distance(vectorCandidate.begin(), itCandidate);
[d7d2da3]202
[837fa70]203 pdgParticle = pdg->GetParticle(pid);
[341014c]204 candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge() / 3.0) : -999;
[837fa70]205 candidate->Mass = mass;
[d7d2da3]206
[837fa70]207 candidate->Momentum.SetPxPyPzE(px, py, pz, e);
[d7d2da3]208
[341014c]209 candidate->Position.SetXYZT(x * 10.0, y * 10.0, z * 10.0, 0.0);
[d7d2da3]210
[837fa70]211 allParticleOutputArray->Add(candidate);
[d7d2da3]212
[837fa70]213 if(!pdgParticle) continue;
214
[5df97ac]215 if(status == 1)
[837fa70]216 {
217 // Prevent duplicated particle.
[5df97ac]218 if(!isMiniAOD) stableParticleOutputArray->Add(candidate);
[386f526]219 }
[837fa70]220 else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
[1e1f73f]221 {
[837fa70]222 partonOutputArray->Add(candidate);
[1e1f73f]223 }
[837fa70]224 }
[5df97ac]225
226 if(!isMiniAOD) return;
[837fa70]227 // For MiniAOD sample,
228 // Only status==1 particles are saved to packedGenParticles.
229 for(itPackedParticle = handlePackedParticle->begin(); itPackedParticle != handlePackedParticle->end(); ++itPackedParticle)
230 {
231 vectorCandidate.push_back(&*itPackedParticle);
232 }
[386f526]233
[837fa70]234 for(itPackedParticle = handlePackedParticle->begin(); itPackedParticle != handlePackedParticle->end(); ++itPackedParticle)
235 {
236 const pat::PackedGenParticle &particle = *itPackedParticle;
[386f526]237
[837fa70]238 pid = particle.pdgId();
239 status = particle.status();
[341014c]240 px = particle.px();
241 py = particle.py();
242 pz = particle.pz();
243 e = particle.energy();
244 mass = particle.mass();
245 x = particle.vx();
246 y = particle.vy();
247 z = particle.vz();
[386f526]248
[837fa70]249 candidate = factory->NewCandidate();
[386f526]250
[837fa70]251 candidate->PID = pid;
252 pdgCode = TMath::Abs(candidate->PID);
[386f526]253
[837fa70]254 candidate->Status = status;
[386f526]255
[837fa70]256 if(particle.mother(0))
257 {
258 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.mother(0));
259 if(itCandidate != vectorCandidate.end()) candidate->M1 = distance(vectorCandidate.begin(), itCandidate);
260 }
[386f526]261
[837fa70]262 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(0));
263 if(itCandidate != vectorCandidate.end()) candidate->D1 = distance(vectorCandidate.begin(), itCandidate);
[386f526]264
[837fa70]265 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(particle.numberOfDaughters() - 1));
266 if(itCandidate != vectorCandidate.end()) candidate->D2 = distance(vectorCandidate.begin(), itCandidate);
[386f526]267
[837fa70]268 pdgParticle = pdg->GetParticle(pid);
[341014c]269 candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge() / 3.0) : -999;
[837fa70]270 candidate->Mass = mass;
[386f526]271
[837fa70]272 candidate->Momentum.SetPxPyPzE(px, py, pz, e);
[386f526]273
[341014c]274 candidate->Position.SetXYZT(x * 10.0, y * 10.0, z * 10.0, 0.0);
[386f526]275
[837fa70]276 allParticleOutputArray->Add(candidate);
[386f526]277
[837fa70]278 if(!pdgParticle) continue;
279
280 if(status == 1)
281 {
282 stableParticleOutputArray->Add(candidate);
[d7d2da3]283 }
284 }
285}
286
287//---------------------------------------------------------------------------
288
289static bool interrupted = false;
290
291void SignalHandler(int sig)
292{
293 interrupted = true;
294}
295
296//---------------------------------------------------------------------------
297
298int main(int argc, char *argv[])
299{
300 char appName[] = "DelphesCMSFWLite";
301 stringstream message;
302 TFile *inputFile = 0;
303 TFile *outputFile = 0;
304 TStopwatch eventStopWatch;
305 ExRootTreeWriter *treeWriter = 0;
[2886328]306 ExRootTreeBranch *branchEvent = 0, *branchWeight = 0;
[d7d2da3]307 ExRootConfReader *confReader = 0;
308 Delphes *modularDelphes = 0;
309 DelphesFactory *factory = 0;
310 TObjArray *allParticleOutputArray = 0, *stableParticleOutputArray = 0, *partonOutputArray = 0;
311 Int_t i;
[5ca3d52]312 Long64_t eventCounter, numberOfEvents;
[7611cb9]313 Bool_t firstEvent = kTRUE;
[d7d2da3]314
315 if(argc < 4)
316 {
[341014c]317 cout << " Usage: " << appName << " config_file"
318 << " output_file"
319 << " input_file(s)" << endl;
[d7d2da3]320 cout << " config_file - configuration file in Tcl format," << endl;
321 cout << " output_file - output file in ROOT format," << endl;
322 cout << " input_file(s) - input file(s) in ROOT format." << endl;
323 return 1;
324 }
325
326 signal(SIGINT, SignalHandler);
327
328 gROOT->SetBatch();
329
330 int appargc = 1;
331 char *appargv[] = {appName};
332 TApplication app(appName, &appargc, appargv);
333
[cd699d0]334 FWLiteEnabler::enable();
[386f526]335
[d7d2da3]336 try
337 {
338 outputFile = TFile::Open(argv[2], "CREATE");
339
340 if(outputFile == NULL)
341 {
342 message << "can't open " << argv[2] << endl;
343 throw runtime_error(message.str());
344 }
345
346 treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
347
[f29758e]348 branchEvent = treeWriter->NewBranch("Event", HepMCEvent::Class());
[2886328]349 branchWeight = treeWriter->NewBranch("Weight", Weight::Class());
[f29758e]350
[d7d2da3]351 confReader = new ExRootConfReader;
352 confReader->ReadFile(argv[1]);
353
354 modularDelphes = new Delphes("Delphes");
355 modularDelphes->SetConfReader(confReader);
356 modularDelphes->SetTreeWriter(treeWriter);
357
358 factory = modularDelphes->GetFactory();
359 allParticleOutputArray = modularDelphes->ExportArray("allParticles");
360 stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
361 partonOutputArray = modularDelphes->ExportArray("partons");
362
363 modularDelphes->InitTask();
364
365 for(i = 3; i < argc && !interrupted; ++i)
366 {
367 cout << "** Reading " << argv[i] << endl;
368
369 inputFile = TFile::Open(argv[i]);
370
371 if(inputFile == NULL)
372 {
373 message << "can't open " << argv[i] << endl;
374 throw runtime_error(message.str());
375 }
376
377 fwlite::Event event(inputFile);
378
[5ca3d52]379 numberOfEvents = event.size();
[d7d2da3]380
[5ca3d52]381 if(numberOfEvents <= 0) continue;
[d7d2da3]382
[a0538b9]383 // ExRootProgressBar progressBar(numberOfEvents - 1);
384 ExRootProgressBar progressBar(-1);
[d7d2da3]385
386 // Loop over all objects
[5ca3d52]387 eventCounter = 0;
[d7d2da3]388 modularDelphes->Clear();
389 treeWriter->Clear();
[7611cb9]390
[d7d2da3]391 for(event.toBegin(); !event.atEnd() && !interrupted; ++event)
392 {
[2886328]393 ConvertInput(event, eventCounter, branchEvent, branchWeight, factory,
[5df97ac]394 allParticleOutputArray, stableParticleOutputArray, partonOutputArray, firstEvent);
[d7d2da3]395 modularDelphes->ProcessTask();
[386f526]396
[7611cb9]397 firstEvent = kFALSE;
[d7d2da3]398
399 treeWriter->Fill();
400
401 modularDelphes->Clear();
402 treeWriter->Clear();
403
[a0538b9]404 progressBar.Update(eventCounter, eventCounter);
[5ca3d52]405 ++eventCounter;
[d7d2da3]406 }
[a0538b9]407
408 progressBar.Update(eventCounter, eventCounter, kTRUE);
[d7d2da3]409 progressBar.Finish();
410
411 inputFile->Close();
412 }
413
414 modularDelphes->FinishTask();
415 treeWriter->Write();
416
417 cout << "** Exiting..." << endl;
418
419 delete modularDelphes;
420 delete confReader;
421 delete treeWriter;
422 delete outputFile;
423
424 return 0;
425 }
426 catch(runtime_error &e)
427 {
428 if(treeWriter) delete treeWriter;
429 if(outputFile) delete outputFile;
430 cerr << "** ERROR: " << e.what() << endl;
431 return 1;
432 }
433}
Note: See TracBrowser for help on using the repository browser.