Fork me on GitHub

source: svn/trunk/examples/DelphesCMSFWLite.cpp@ 1170

Last change on this file since 1170 was 1166, checked in by Pavel Demin, 11 years ago

use better name for event counter

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision Date
File size: 6.5 KB
Line 
1#include <algorithm>
2#include <stdexcept>
3#include <iostream>
4#include <sstream>
5#include <memory>
6
7#include <map>
8#include <vector>
9
10#include <stdlib.h>
11#include <signal.h>
12#include <stdio.h>
13
14#include "TROOT.h"
15#include "TApplication.h"
16
17#include "TFile.h"
18#include "TObjArray.h"
19#include "TStopwatch.h"
20#include "TDatabasePDG.h"
21#include "TParticlePDG.h"
22#include "TLorentzVector.h"
23
24#include "modules/Delphes.h"
25#include "classes/DelphesStream.h"
26#include "classes/DelphesClasses.h"
27#include "classes/DelphesFactory.h"
28
29#include "ExRootAnalysis/ExRootTreeWriter.h"
30#include "ExRootAnalysis/ExRootTreeBranch.h"
31#include "ExRootAnalysis/ExRootProgressBar.h"
32
33#include "FWCore/FWLite/interface/AutoLibraryLoader.h"
34
35#include "DataFormats/FWLite/interface/Event.h"
36#include "DataFormats/FWLite/interface/Handle.h"
37#include "DataFormats/HepMCCandidate/interface/GenParticle.h"
38
39using namespace std;
40
41//---------------------------------------------------------------------------
42
43void ConvertInput(fwlite::Event &event, DelphesFactory *factory, TObjArray *allParticleOutputArray, TObjArray *stableParticleOutputArray, TObjArray *partonOutputArray)
44{
45 fwlite::Handle< vector< reco::GenParticle > > handleParticle;
46 vector< reco::GenParticle >::const_iterator itParticle;
47
48 vector< const reco::Candidate * > vectorCandidate;
49 vector< const reco::Candidate * >::iterator itCandidate;
50
51 handleParticle.getByLabel(event, "genParticles");
52
53 Candidate *candidate;
54 TDatabasePDG *pdg;
55 TParticlePDG *pdgParticle;
56 Int_t pdgCode;
57
58 Int_t pid, status;
59 Double_t px, py, pz, e;
60 Double_t x, y, z;
61
62 pdg = TDatabasePDG::Instance();
63
64 for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
65 {
66 vectorCandidate.push_back(&*itParticle);
67 }
68
69 for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
70 {
71 const reco::GenParticle &particle = *itParticle;
72
73 pid = particle.pdgId();
74 status = particle.status();
75 px = particle.px(); py = particle.py(); pz = particle.pz(); e = particle.energy();
76 x = particle.vx(); y = particle.vy(); z = particle.vz();
77
78 candidate = factory->NewCandidate();
79
80 candidate->PID = pid;
81 pdgCode = TMath::Abs(candidate->PID);
82
83 candidate->Status = status;
84
85 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(0));
86 if(itCandidate != vectorCandidate.end()) candidate->D1 = distance(vectorCandidate.begin(), itCandidate);
87
88 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(particle.numberOfDaughters() - 1));
89 if(itCandidate != vectorCandidate.end()) candidate->D2 = distance(vectorCandidate.begin(), itCandidate);
90
91 pdgParticle = pdg->GetParticle(pid);
92 candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
93 candidate->Mass = pdgParticle ? pdgParticle->Mass() : -999.9;
94
95 candidate->Momentum.SetPxPyPzE(px, py, pz, e);
96
97 candidate->Position.SetXYZT(x, y, z, 0.0);
98
99 allParticleOutputArray->Add(candidate);
100
101 if(!pdgParticle) continue;
102
103 if(status == 1)
104 {
105 stableParticleOutputArray->Add(candidate);
106 }
107 else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
108 {
109 partonOutputArray->Add(candidate);
110 }
111 }
112}
113
114//---------------------------------------------------------------------------
115
116static bool interrupted = false;
117
118void SignalHandler(int sig)
119{
120 interrupted = true;
121}
122
123//---------------------------------------------------------------------------
124
125int main(int argc, char *argv[])
126{
127 char appName[] = "DelphesCMSFWLite";
128 stringstream message;
129 TFile *inputFile = 0;
130 TFile *outputFile = 0;
131 TStopwatch eventStopWatch;
132 ExRootTreeWriter *treeWriter = 0;
133 ExRootConfReader *confReader = 0;
134 Delphes *modularDelphes = 0;
135 DelphesFactory *factory = 0;
136 TObjArray *allParticleOutputArray = 0, *stableParticleOutputArray = 0, *partonOutputArray = 0;
137 Int_t i;
138 Long64_t eventCounter, numberOfEvents;
139
140 if(argc < 4)
141 {
142 cout << " Usage: " << appName << " config_file" << " output_file" << " input_file(s)" << endl;
143 cout << " config_file - configuration file in Tcl format," << endl;
144 cout << " output_file - output file in ROOT format," << endl;
145 cout << " input_file(s) - input file(s) in ROOT format." << endl;
146 return 1;
147 }
148
149 signal(SIGINT, SignalHandler);
150
151 gROOT->SetBatch();
152
153 int appargc = 1;
154 char *appargv[] = {appName};
155 TApplication app(appName, &appargc, appargv);
156
157 AutoLibraryLoader::enable();
158
159 try
160 {
161 outputFile = TFile::Open(argv[2], "CREATE");
162
163 if(outputFile == NULL)
164 {
165 message << "can't open " << argv[2] << endl;
166 throw runtime_error(message.str());
167 }
168
169 treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
170
171 confReader = new ExRootConfReader;
172 confReader->ReadFile(argv[1]);
173
174 modularDelphes = new Delphes("Delphes");
175 modularDelphes->SetConfReader(confReader);
176 modularDelphes->SetTreeWriter(treeWriter);
177
178 factory = modularDelphes->GetFactory();
179 allParticleOutputArray = modularDelphes->ExportArray("allParticles");
180 stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
181 partonOutputArray = modularDelphes->ExportArray("partons");
182
183 modularDelphes->InitTask();
184
185 for(i = 3; i < argc && !interrupted; ++i)
186 {
187 cout << "** Reading " << argv[i] << endl;
188
189 inputFile = TFile::Open(argv[i]);
190
191 if(inputFile == NULL)
192 {
193 message << "can't open " << argv[i] << endl;
194 throw runtime_error(message.str());
195 }
196
197 fwlite::Event event(inputFile);
198
199 numberOfEvents = event.size();
200
201 if(numberOfEvents <= 0) continue;
202
203 ExRootProgressBar progressBar(numberOfEvents - 1);
204
205 // Loop over all objects
206 eventCounter = 0;
207 modularDelphes->Clear();
208 treeWriter->Clear();
209 for(event.toBegin(); !event.atEnd() && !interrupted; ++event)
210 {
211 ConvertInput(event, factory, allParticleOutputArray, stableParticleOutputArray, partonOutputArray);
212 modularDelphes->ProcessTask();
213
214 treeWriter->Fill();
215
216 modularDelphes->Clear();
217 treeWriter->Clear();
218
219 progressBar.Update(eventCounter);
220 ++eventCounter;
221 }
222 progressBar.Finish();
223
224 inputFile->Close();
225 }
226
227 modularDelphes->FinishTask();
228 treeWriter->Write();
229
230 cout << "** Exiting..." << endl;
231
232 delete modularDelphes;
233 delete confReader;
234 delete treeWriter;
235 delete outputFile;
236
237 return 0;
238 }
239 catch(runtime_error &e)
240 {
241 if(treeWriter) delete treeWriter;
242 if(outputFile) delete outputFile;
243 cerr << "** ERROR: " << e.what() << endl;
244 return 1;
245 }
246}
Note: See TracBrowser for help on using the repository browser.