Fork me on GitHub

source: svn/trunk/readers/DelphesProMC.cpp@ 1174

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

move DelphesCMSFWLite.cpp and DelphesProMC.cpp to readers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision Date
File size: 6.8 KB
Line 
1#include <stdexcept>
2#include <iostream>
3#include <sstream>
4#include <memory>
5
6#include <map>
7
8#include <stdlib.h>
9#include <signal.h>
10#include <stdio.h>
11
12#include "TROOT.h"
13#include "TApplication.h"
14
15#include "TFile.h"
16#include "TObjArray.h"
17#include "TStopwatch.h"
18#include "TDatabasePDG.h"
19#include "TParticlePDG.h"
20#include "TLorentzVector.h"
21
22#include "modules/Delphes.h"
23#include "classes/DelphesStream.h"
24#include "classes/DelphesClasses.h"
25#include "classes/DelphesFactory.h"
26
27#include "ExRootAnalysis/ExRootTreeWriter.h"
28#include "ExRootAnalysis/ExRootTreeBranch.h"
29#include "ExRootAnalysis/ExRootProgressBar.h"
30
31#include "ProMC/ProMC.pb.h"
32#include "ProMC/ProMCBook.h"
33#include "ProMC/ProMCHeader.pb.h"
34
35using namespace std;
36
37//---------------------------------------------------------------------------
38
39void ConvertInput(ProMCEvent &event, ExRootTreeBranch *branch, DelphesFactory *factory, TObjArray *allParticleOutputArray, TObjArray *stableParticleOutputArray, TObjArray *partonOutputArray)
40{
41 Int_t i;
42
43 ProMCEvent_Event *mutableEvent;
44 ProMCEvent_Particles *mutableParticles;
45
46 HepMCEvent *element;
47 Candidate *candidate;
48 TDatabasePDG *pdg;
49 TParticlePDG *pdgParticle;
50 Int_t pdgCode;
51
52 Int_t pid, status;
53 Double_t px, py, pz, e, mass;
54 Double_t x, y, z;
55
56 pdg = TDatabasePDG::Instance();
57
58 // event information
59 mutableEvent = event.mutable_event();
60
61 element = static_cast<HepMCEvent *>(branch->NewEntry());
62
63 element->Number = mutableEvent->number();
64
65 element->ProcessID = mutableEvent->process_id();
66 element->MPI = mutableEvent->mpi();
67 element->Weight = mutableEvent->weight();
68 element->Scale = mutableEvent->scale();
69 element->AlphaQED = mutableEvent->alpha_qed();
70 element->AlphaQCD = mutableEvent->alpha_qcd();
71
72 element->ID1 = mutableEvent->id1();
73 element->ID2 = mutableEvent->id2();
74 element->X1 = mutableEvent->x1();
75 element->X2 = mutableEvent->x2();
76 element->ScalePDF = mutableEvent->scale_pdf();
77 element->PDF1 = mutableEvent->pdf1();
78 element->PDF2 = mutableEvent->pdf2();
79
80 element->ReadTime = 0.0;
81 element->ProcTime = 0.0;
82
83 mutableParticles = event.mutable_particles();
84
85 for(i = 0; i < mutableParticles->pdg_id_size(); ++i)
86 {
87 pid = mutableParticles->pdg_id(i);
88 status = mutableParticles->status(i);
89 px = mutableParticles->px(i); py = mutableParticles->py(i); pz = mutableParticles->pz(i); mass = mutableParticles->mass(i);
90 x = mutableParticles->x(i); y = mutableParticles->y(i); z = mutableParticles->z(i);
91 e = TMath::Sqrt(px*px + py*py + pz*pz + mass*mass);
92
93 candidate = factory->NewCandidate();
94
95 candidate->PID = pid;
96 pdgCode = TMath::Abs(candidate->PID);
97
98 candidate->Status = status;
99
100 candidate->M1 = mutableParticles->mother1(i);
101 candidate->M2 = mutableParticles->mother2(i);
102
103 candidate->D1 = mutableParticles->daughter1(i);
104 candidate->D2 = mutableParticles->daughter2(i);
105
106 pdgParticle = pdg->GetParticle(pid);
107 candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
108 candidate->Mass = pdgParticle ? pdgParticle->Mass() : -999.9;
109
110 candidate->Momentum.SetPxPyPzE(px, py, pz, e);
111
112 candidate->Position.SetXYZT(x, y, z, 0.0);
113
114 allParticleOutputArray->Add(candidate);
115
116 if(!pdgParticle) continue;
117
118 if(status == 1)
119 {
120 stableParticleOutputArray->Add(candidate);
121 }
122 else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
123 {
124 partonOutputArray->Add(candidate);
125 }
126 }
127}
128
129//---------------------------------------------------------------------------
130
131static bool interrupted = false;
132
133void SignalHandler(int sig)
134{
135 interrupted = true;
136}
137
138//---------------------------------------------------------------------------
139
140int main(int argc, char *argv[])
141{
142 char appName[] = "DelphesCMSFWLite";
143 stringstream message;
144 ProMCBook *inputFile = 0;
145 TFile *outputFile = 0;
146 ExRootTreeWriter *treeWriter = 0;
147 ExRootTreeBranch *branchEvent = 0;
148 ExRootConfReader *confReader = 0;
149 Delphes *modularDelphes = 0;
150 DelphesFactory *factory = 0;
151 TObjArray *allParticleOutputArray = 0, *stableParticleOutputArray = 0, *partonOutputArray = 0;
152 Int_t i;
153 Long64_t entry, allEntries;
154
155 if(argc < 4)
156 {
157 cout << " Usage: " << appName << " config_file" << " output_file" << " input_file(s)" << endl;
158 cout << " config_file - configuration file in Tcl format," << endl;
159 cout << " output_file - output file in ROOT format," << endl;
160 cout << " input_file(s) - input file(s) in ProMC format." << endl;
161 return 1;
162 }
163
164 signal(SIGINT, SignalHandler);
165
166 gROOT->SetBatch();
167
168 int appargc = 1;
169 char *appargv[] = {appName};
170 TApplication app(appName, &appargc, appargv);
171
172 try
173 {
174 outputFile = TFile::Open(argv[2], "CREATE");
175
176 if(outputFile == NULL)
177 {
178 message << "can't open " << argv[2] << endl;
179 throw runtime_error(message.str());
180 }
181
182 treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
183
184 branchEvent = treeWriter->NewBranch("Event", HepMCEvent::Class());
185
186 confReader = new ExRootConfReader;
187 confReader->ReadFile(argv[1]);
188
189 modularDelphes = new Delphes("Delphes");
190 modularDelphes->SetConfReader(confReader);
191 modularDelphes->SetTreeWriter(treeWriter);
192
193 factory = modularDelphes->GetFactory();
194 allParticleOutputArray = modularDelphes->ExportArray("allParticles");
195 stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
196 partonOutputArray = modularDelphes->ExportArray("partons");
197
198 modularDelphes->InitTask();
199
200 for(i = 3; i < argc && !interrupted; ++i)
201 {
202 cout << "** Reading " << argv[i] << endl;
203
204 inputFile = new ProMCBook(argv[i], "r");
205
206 if(inputFile == NULL)
207 {
208 message << "can't open " << argv[i] << endl;
209 throw runtime_error(message.str());
210 }
211
212 allEntries = inputFile->getEvents();;
213
214 if(allEntries <= 0) continue;
215
216 ExRootProgressBar progressBar(allEntries - 1);
217
218 // Loop over all objects
219 modularDelphes->Clear();
220 treeWriter->Clear();
221 for(entry = 0; entry < allEntries && !interrupted; ++entry)
222 {
223 if(inputFile->next() != 0) continue;
224 ProMCEvent event = inputFile->get();
225
226 ConvertInput(event, branchEvent, factory, allParticleOutputArray, stableParticleOutputArray, partonOutputArray);
227 modularDelphes->ProcessTask();
228
229 treeWriter->Fill();
230
231 modularDelphes->Clear();
232 treeWriter->Clear();
233
234 progressBar.Update(entry);
235 }
236 progressBar.Finish();
237
238 inputFile->close();
239 delete inputFile;
240 }
241
242 modularDelphes->FinishTask();
243 treeWriter->Write();
244
245 cout << "** Exiting..." << endl;
246
247 delete modularDelphes;
248 delete confReader;
249 delete treeWriter;
250 delete outputFile;
251
252 return 0;
253 }
254 catch(runtime_error &e)
255 {
256 if(treeWriter) delete treeWriter;
257 if(outputFile) delete outputFile;
258 cerr << "** ERROR: " << e.what() << endl;
259 return 1;
260 }
261}
Note: See TracBrowser for help on using the repository browser.