Fork me on GitHub

source: svn/trunk/readers/DelphesPythia8.cpp@ 1180

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

add DelphesPythia8

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision Date
File size: 6.4 KB
RevLine 
[1178]1#include <stdexcept>
2#include <iostream>
3#include <sstream>
4
5#include <signal.h>
6
7#include "pythia->h"
8
9#include "TROOT.h"
10#include "TApplication.h"
11
12#include "TFile.h"
13#include "TObjArray.h"
14#include "TStopwatch.h"
15#include "TDatabasePDG.h"
16#include "TParticlePDG.h"
17#include "TLorentzVector.h"
18
19#include "modules/Delphes.h"
20#include "classes/DelphesClasses.h"
21#include "classes/DelphesFactory.h"
22
23#include "ExRootAnalysis/ExRootTreeWriter.h"
24#include "ExRootAnalysis/ExRootTreeBranch.h"
25#include "ExRootAnalysis/ExRootProgressBar.h"
26
27using namespace std;
28
29//---------------------------------------------------------------------------
30
31void ConvertInput(Pythia8::Pythia *pythia, DelphesFactory *factory, TObjArray *allParticleOutputArray, TObjArray *stableParticleOutputArray, TObjArray *partonOutputArray)
32{
33 int i;
34
35 Candidate *candidate;
36 TDatabasePDG *pdg;
37 TParticlePDG *pdgParticle;
38 Int_t pdgCode;
39
40 Int_t pid, status;
41 Double_t px, py, pz, e;
42 Double_t x, y, z;
43
44 pdg = TDatabasePDG::Instance();
45
46 for(i = 0; i < pythia->event.size(); ++i)
47 {
48 Pythia8::Particle &particle = pythia->event[i];
49
50 pid = particle.id();
51 status = particle.status();
52 m1 = particle.mother1(); m2 = particle.mother2();
53 d1 = particle.daughter1(); d2 = particle.daughter2();
54 px = particle.px(); py = particle.py(); pz = particle.pz(); e = particle.e();
55 x = particle.xProd(); y = particle.yProd(); z = particle.zProd(); t = particle.tProd();
56
57 candidate = factory->NewCandidate();
58
59 candidate->PID = pid;
60 pdgCode = TMath::Abs(candidate->PID);
61
62 candidate->Status = status;
63
64 candidate->M1 = m1 - 1;
65 candidate->M2 = m2 - 1;
66
67 candidate->D1 = d1 - 1;
68 candidate->D2 = d2 - 1;
69
70 pdgParticle = pdg->GetParticle(pid);
71 candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
72 candidate->Mass = pdgParticle ? pdgParticle->Mass() : -999.9;
73
74 candidate->Momentum.SetPxPyPzE(px, py, pz, e);
75
76 candidate->Position.SetXYZT(x, y, z, 0.0);
77
78 allParticleOutputArray->Add(candidate);
79
80 if(!pdgParticle) continue;
81
82 if(status == 1)
83 {
84 stableParticleOutputArray->Add(candidate);
85 }
86 else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
87 {
88 partonOutputArray->Add(candidate);
89 }
90 }
91}
92
93//---------------------------------------------------------------------------
94
95static bool interrupted = false;
96
97void SignalHandler(int sig)
98{
99 interrupted = true;
100}
101
102//---------------------------------------------------------------------------
103
104int main(int argc, char *argv[])
105{
106 char appName[] = "DelphesPythia8";
107 stringstream message;
108 FILE *inputFile = 0;
109 TFile *outputFile = 0;
110 TStopwatch readStopWatch, procStopWatch;
111 ExRootTreeWriter *treeWriter = 0;
112 ExRootTreeBranch *branchEvent = 0;
113 ExRootConfReader *confReader = 0;
114 Delphes *modularDelphes = 0;
115 DelphesFactory *factory = 0;
116 TObjArray *stableParticleOutputArray = 0, *allParticleOutputArray = 0, *partonOutputArray = 0;
117 Int_t i;
118 Long64_t eventCounter, errorCounter;
119 Long64_t numberOfEvents, timesAllowErrors;
120
121 Pythia8::Pythia *pythia = 0;
122
123 if(argc != 4)
124 {
125 cout << " Usage: " << appName << " config_file" << " pythia_card" << " output_file" << endl;
126 cout << " config_file - configuration file in Tcl format," << endl;
127 cout << " pythia_card - Pythia8 configuration file," << endl;
128 cout << " output_file - output file in ROOT format." << endl;
129 return 1;
130 }
131
132 signal(SIGINT, SignalHandler);
133
134 gROOT->SetBatch();
135
136 int appargc = 1;
137 char *appargv[] = {appName};
138 TApplication app(appName, &appargc, appargv);
139
140 try
141 {
142 outputFile = TFile::Open(argv[3], "CREATE");
143
144 if(outputFile == NULL)
145 {
146 message << "can't create output file " << argv[3];
147 throw runtime_error(message.str());
148 }
149
150 treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
151
152 branchEvent = treeWriter->NewBranch("Event", LHEFEvent::Class());
153
154 confReader = new ExRootConfReader;
155 confReader->ReadFile(argv[1]);
156
157 modularDelphes = new Delphes("Delphes");
158 modularDelphes->SetConfReader(confReader);
159 modularDelphes->SetTreeWriter(treeWriter);
160
161 factory = modularDelphes->GetFactory();
162 allParticleOutputArray = modularDelphes->ExportArray("allParticles");
163 stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
164 partonOutputArray = modularDelphes->ExportArray("partons");
165
166 modularDelphes->InitTask();
167
168 // Initialize pythia
169 pythia = new Pythia8::Pythia;
170
171 if(pythia == NULL)
172 {
173 throw runtime_error("can't create Pythia instance");
174 }
175
176 // Read in commands from configuration file
177 pythia->readFile(argv[2]);
178
179 // Extract settings to be used in the main program
180 numberOfEvents = pythia->mode("Main:numberOfEvents");
181 timesAllowErrors = pythia->mode("Main:timesAllowErrors");
182
183 pythia->init();
184
185 ExRootProgressBar progressBar(numberOfEvents - 1);
186
187 // Loop over all events
188 errorCounter = 0;
189 treeWriter->Clear();
190 modularDelphes->Clear();
191 readStopWatch.Start();
192 for(eventCounter = 0; eventCounter < numberOfEvents && !interrupted; ++eventCounter)
193 {
194 if(!pythia->next())
195 {
196 // If failure because reached end of file then exit event loop
197 if (pythia->info.atEndOfFile())
198 {
199 cerr << "Aborted since reached end of Les Houches Event File" << endl;
200 break;
201 }
202
203 // First few failures write off as "acceptable" errors, then quit
204 if (++errorCounter < timesAllowErrors) continue;
205 cerr << "Event generation aborted prematurely, owing to error!" << endl;
206 break;
207 }
208
209 readStopWatch.Stop();
210
211 procStopWatch.Start();
212 ConvertInput(pythia, factory, allParticleOutputArray, stableParticleOutputArray, partonOutputArray);
213 modularDelphes->ProcessTask();
214 procStopWatch.Stop();
215
216 treeWriter->Fill();
217
218 treeWriter->Clear();
219 modularDelphes->Clear();
220
221 readStopWatch.Start();
222 progressBar.Update(eventCounter);
223 }
224
225 progressBar.Finish();
226
227 pythia->statistics();
228
229 modularDelphes->FinishTask();
230 treeWriter->Write();
231
232 cout << "** Exiting..." << endl;
233
234 delete pythia;
235 delete modularDelphes;
236 delete confReader;
237 delete treeWriter;
238 delete outputFile;
239
240 return 0;
241 }
242 catch(runtime_error &e)
243 {
244 if(treeWriter) delete treeWriter;
245 if(outputFile) delete outputFile;
246 cerr << "** ERROR: " << e.what() << endl;
247 return 1;
248 }
249}
Note: See TracBrowser for help on using the repository browser.