[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 |
|
---|
[5150e39] | 19 | #include <stdexcept>
|
---|
| 20 | #include <iostream>
|
---|
| 21 | #include <sstream>
|
---|
[cd616b9] | 22 | #include <string>
|
---|
[5150e39] | 23 |
|
---|
| 24 | #include <signal.h>
|
---|
| 25 |
|
---|
[280667b] | 26 | #include "Pythia.h"
|
---|
[5150e39] | 27 |
|
---|
| 28 | #include "TROOT.h"
|
---|
| 29 | #include "TApplication.h"
|
---|
| 30 |
|
---|
| 31 | #include "TFile.h"
|
---|
| 32 | #include "TObjArray.h"
|
---|
| 33 | #include "TStopwatch.h"
|
---|
| 34 | #include "TDatabasePDG.h"
|
---|
| 35 | #include "TParticlePDG.h"
|
---|
| 36 | #include "TLorentzVector.h"
|
---|
| 37 |
|
---|
| 38 | #include "modules/Delphes.h"
|
---|
| 39 | #include "classes/DelphesClasses.h"
|
---|
| 40 | #include "classes/DelphesFactory.h"
|
---|
[cd616b9] | 41 | #include "classes/DelphesLHEFReader.h"
|
---|
[5150e39] | 42 |
|
---|
| 43 | #include "ExRootAnalysis/ExRootTreeWriter.h"
|
---|
| 44 | #include "ExRootAnalysis/ExRootTreeBranch.h"
|
---|
| 45 | #include "ExRootAnalysis/ExRootProgressBar.h"
|
---|
| 46 |
|
---|
| 47 | using namespace std;
|
---|
| 48 |
|
---|
| 49 | //---------------------------------------------------------------------------
|
---|
| 50 |
|
---|
[b94aacf] | 51 | void ConvertInput(Long64_t eventCounter, Pythia8::Pythia *pythia,
|
---|
| 52 | ExRootTreeBranch *branch, DelphesFactory *factory,
|
---|
| 53 | TObjArray *allParticleOutputArray, TObjArray *stableParticleOutputArray, TObjArray *partonOutputArray,
|
---|
| 54 | TStopwatch *readStopWatch, TStopwatch *procStopWatch)
|
---|
[5150e39] | 55 | {
|
---|
| 56 | int i;
|
---|
| 57 |
|
---|
[ad78364] | 58 | HepMCEvent *element;
|
---|
[5150e39] | 59 | Candidate *candidate;
|
---|
| 60 | TDatabasePDG *pdg;
|
---|
| 61 | TParticlePDG *pdgParticle;
|
---|
| 62 | Int_t pdgCode;
|
---|
| 63 |
|
---|
| 64 | Int_t pid, status;
|
---|
[80d4a34] | 65 | Double_t px, py, pz, e, mass;
|
---|
[474eb76] | 66 | Double_t x, y, z, t;
|
---|
[5150e39] | 67 |
|
---|
[b94aacf] | 68 | // event information
|
---|
| 69 | element = static_cast<HepMCEvent *>(branch->NewEntry());
|
---|
| 70 |
|
---|
| 71 | element->Number = eventCounter;
|
---|
| 72 |
|
---|
| 73 | element->ProcessID = pythia->info.code();
|
---|
| 74 | element->MPI = 1;
|
---|
| 75 | element->Weight = pythia->info.weight();
|
---|
| 76 | element->Scale = pythia->info.QRen();
|
---|
| 77 | element->AlphaQED = pythia->info.alphaEM();
|
---|
| 78 | element->AlphaQCD = pythia->info.alphaS();
|
---|
| 79 |
|
---|
[ad78364] | 80 | element->ID1 = pythia->info.id1();
|
---|
| 81 | element->ID2 = pythia->info.id2();
|
---|
| 82 | element->X1 = pythia->info.x1();
|
---|
| 83 | element->X2 = pythia->info.x2();
|
---|
[b94aacf] | 84 | element->ScalePDF = pythia->info.QFac();
|
---|
| 85 | element->PDF1 = pythia->info.pdf1();
|
---|
| 86 | element->PDF2 = pythia->info.pdf2();
|
---|
| 87 |
|
---|
| 88 | element->ReadTime = readStopWatch->RealTime();
|
---|
| 89 | element->ProcTime = procStopWatch->RealTime();
|
---|
| 90 |
|
---|
[5150e39] | 91 | pdg = TDatabasePDG::Instance();
|
---|
| 92 |
|
---|
[d8b1858] | 93 | for(i = 1; i < pythia->event.size(); ++i)
|
---|
[5150e39] | 94 | {
|
---|
| 95 | Pythia8::Particle &particle = pythia->event[i];
|
---|
| 96 |
|
---|
| 97 | pid = particle.id();
|
---|
[3e276d4] | 98 | status = particle.statusHepMC();
|
---|
[80d4a34] | 99 | px = particle.px(); py = particle.py(); pz = particle.pz(); e = particle.e(); mass = particle.m();
|
---|
[5150e39] | 100 | x = particle.xProd(); y = particle.yProd(); z = particle.zProd(); t = particle.tProd();
|
---|
| 101 |
|
---|
| 102 | candidate = factory->NewCandidate();
|
---|
| 103 |
|
---|
| 104 | candidate->PID = pid;
|
---|
| 105 | pdgCode = TMath::Abs(candidate->PID);
|
---|
| 106 |
|
---|
| 107 | candidate->Status = status;
|
---|
| 108 |
|
---|
[474eb76] | 109 | candidate->M1 = particle.mother1() - 1;
|
---|
| 110 | candidate->M2 = particle.mother2() - 1;
|
---|
[5150e39] | 111 |
|
---|
[474eb76] | 112 | candidate->D1 = particle.daughter1() - 1;
|
---|
| 113 | candidate->D2 = particle.daughter2() - 1;
|
---|
[5150e39] | 114 |
|
---|
| 115 | pdgParticle = pdg->GetParticle(pid);
|
---|
| 116 | candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
|
---|
[bba4646] | 117 | candidate->Mass = mass;
|
---|
[5150e39] | 118 |
|
---|
| 119 | candidate->Momentum.SetPxPyPzE(px, py, pz, e);
|
---|
| 120 |
|
---|
[474eb76] | 121 | candidate->Position.SetXYZT(x, y, z, t);
|
---|
[5150e39] | 122 |
|
---|
| 123 | allParticleOutputArray->Add(candidate);
|
---|
| 124 |
|
---|
| 125 | if(!pdgParticle) continue;
|
---|
| 126 |
|
---|
| 127 | if(status == 1)
|
---|
| 128 | {
|
---|
| 129 | stableParticleOutputArray->Add(candidate);
|
---|
| 130 | }
|
---|
| 131 | else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
|
---|
| 132 | {
|
---|
| 133 | partonOutputArray->Add(candidate);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | //---------------------------------------------------------------------------
|
---|
| 139 |
|
---|
| 140 | static bool interrupted = false;
|
---|
| 141 |
|
---|
| 142 | void SignalHandler(int sig)
|
---|
| 143 | {
|
---|
| 144 | interrupted = true;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | //---------------------------------------------------------------------------
|
---|
| 148 |
|
---|
| 149 | int main(int argc, char *argv[])
|
---|
| 150 | {
|
---|
| 151 | char appName[] = "DelphesPythia8";
|
---|
| 152 | stringstream message;
|
---|
[cd616b9] | 153 | FILE *inputFile = 0;
|
---|
[5150e39] | 154 | TFile *outputFile = 0;
|
---|
| 155 | TStopwatch readStopWatch, procStopWatch;
|
---|
| 156 | ExRootTreeWriter *treeWriter = 0;
|
---|
| 157 | ExRootTreeBranch *branchEvent = 0;
|
---|
[cd616b9] | 158 | ExRootTreeBranch *branchEventLHEF = 0, *branchRwgtLHEF = 0;
|
---|
[5150e39] | 159 | ExRootConfReader *confReader = 0;
|
---|
| 160 | Delphes *modularDelphes = 0;
|
---|
| 161 | DelphesFactory *factory = 0;
|
---|
| 162 | TObjArray *stableParticleOutputArray = 0, *allParticleOutputArray = 0, *partonOutputArray = 0;
|
---|
[cd616b9] | 163 | TObjArray *stableParticleOutputArrayLHEF = 0, *allParticleOutputArrayLHEF = 0, *partonOutputArrayLHEF = 0;
|
---|
| 164 | DelphesLHEFReader *reader = 0;
|
---|
[5150e39] | 165 | Long64_t eventCounter, errorCounter;
|
---|
| 166 | Long64_t numberOfEvents, timesAllowErrors;
|
---|
| 167 |
|
---|
| 168 | Pythia8::Pythia *pythia = 0;
|
---|
| 169 |
|
---|
| 170 | if(argc != 4)
|
---|
| 171 | {
|
---|
| 172 | cout << " Usage: " << appName << " config_file" << " pythia_card" << " output_file" << endl;
|
---|
| 173 | cout << " config_file - configuration file in Tcl format," << endl;
|
---|
| 174 | cout << " pythia_card - Pythia8 configuration file," << endl;
|
---|
| 175 | cout << " output_file - output file in ROOT format." << endl;
|
---|
| 176 | return 1;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | signal(SIGINT, SignalHandler);
|
---|
| 180 |
|
---|
| 181 | gROOT->SetBatch();
|
---|
| 182 |
|
---|
| 183 | int appargc = 1;
|
---|
| 184 | char *appargv[] = {appName};
|
---|
| 185 | TApplication app(appName, &appargc, appargv);
|
---|
| 186 |
|
---|
| 187 | try
|
---|
| 188 | {
|
---|
| 189 | outputFile = TFile::Open(argv[3], "CREATE");
|
---|
| 190 |
|
---|
| 191 | if(outputFile == NULL)
|
---|
| 192 | {
|
---|
| 193 | message << "can't create output file " << argv[3];
|
---|
| 194 | throw runtime_error(message.str());
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
|
---|
| 198 |
|
---|
[b94aacf] | 199 | branchEvent = treeWriter->NewBranch("Event", HepMCEvent::Class());
|
---|
[5150e39] | 200 |
|
---|
| 201 | confReader = new ExRootConfReader;
|
---|
| 202 | confReader->ReadFile(argv[1]);
|
---|
| 203 |
|
---|
| 204 | modularDelphes = new Delphes("Delphes");
|
---|
| 205 | modularDelphes->SetConfReader(confReader);
|
---|
| 206 | modularDelphes->SetTreeWriter(treeWriter);
|
---|
| 207 |
|
---|
| 208 | factory = modularDelphes->GetFactory();
|
---|
| 209 | allParticleOutputArray = modularDelphes->ExportArray("allParticles");
|
---|
| 210 | stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
|
---|
| 211 | partonOutputArray = modularDelphes->ExportArray("partons");
|
---|
| 212 |
|
---|
| 213 | modularDelphes->InitTask();
|
---|
| 214 |
|
---|
[cd616b9] | 215 | // Initialize Pythia
|
---|
[5150e39] | 216 | pythia = new Pythia8::Pythia;
|
---|
| 217 |
|
---|
| 218 | if(pythia == NULL)
|
---|
| 219 | {
|
---|
| 220 | throw runtime_error("can't create Pythia instance");
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | // Read in commands from configuration file
|
---|
| 224 | pythia->readFile(argv[2]);
|
---|
| 225 |
|
---|
| 226 | // Extract settings to be used in the main program
|
---|
| 227 | numberOfEvents = pythia->mode("Main:numberOfEvents");
|
---|
| 228 | timesAllowErrors = pythia->mode("Main:timesAllowErrors");
|
---|
| 229 |
|
---|
[cd616b9] | 230 | inputFile = fopen(pythia->word("Beams:LHEF").c_str(), "r");
|
---|
| 231 | if(inputFile)
|
---|
| 232 | {
|
---|
| 233 | reader = new DelphesLHEFReader;
|
---|
| 234 | reader->SetInputFile(inputFile);
|
---|
| 235 |
|
---|
| 236 | branchEventLHEF = treeWriter->NewBranch("EventLHEF", LHEFEvent::Class());
|
---|
| 237 | branchRwgtLHEF = treeWriter->NewBranch("RwgtLHEF", Weight::Class());
|
---|
| 238 |
|
---|
| 239 | allParticleOutputArrayLHEF = modularDelphes->ExportArray("allParticlesLHEF");
|
---|
| 240 | stableParticleOutputArrayLHEF = modularDelphes->ExportArray("stableParticlesLHEF");
|
---|
| 241 | partonOutputArrayLHEF = modularDelphes->ExportArray("partonsLHEF");
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[5150e39] | 244 | pythia->init();
|
---|
| 245 |
|
---|
[a0538b9] | 246 | // ExRootProgressBar progressBar(numberOfEvents - 1);
|
---|
| 247 | ExRootProgressBar progressBar(-1);
|
---|
[5150e39] | 248 |
|
---|
| 249 | // Loop over all events
|
---|
| 250 | errorCounter = 0;
|
---|
| 251 | treeWriter->Clear();
|
---|
| 252 | modularDelphes->Clear();
|
---|
| 253 | readStopWatch.Start();
|
---|
| 254 | for(eventCounter = 0; eventCounter < numberOfEvents && !interrupted; ++eventCounter)
|
---|
| 255 | {
|
---|
[cd616b9] | 256 | while(reader && reader->ReadBlock(factory, allParticleOutputArrayLHEF,
|
---|
| 257 | stableParticleOutputArrayLHEF, partonOutputArrayLHEF) && !reader->EventReady());
|
---|
| 258 |
|
---|
[5150e39] | 259 | if(!pythia->next())
|
---|
| 260 | {
|
---|
| 261 | // If failure because reached end of file then exit event loop
|
---|
[cd616b9] | 262 | if(pythia->info.atEndOfFile())
|
---|
[5150e39] | 263 | {
|
---|
| 264 | cerr << "Aborted since reached end of Les Houches Event File" << endl;
|
---|
| 265 | break;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | // First few failures write off as "acceptable" errors, then quit
|
---|
[cd616b9] | 269 | if(++errorCounter > timesAllowErrors)
|
---|
| 270 | {
|
---|
| 271 | cerr << "Event generation aborted prematurely, owing to error!" << endl;
|
---|
| 272 | break;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | modularDelphes->Clear();
|
---|
| 276 | reader->Clear();
|
---|
| 277 | continue;
|
---|
[5150e39] | 278 | }
|
---|
| 279 |
|
---|
| 280 | readStopWatch.Stop();
|
---|
| 281 |
|
---|
| 282 | procStopWatch.Start();
|
---|
[b94aacf] | 283 | ConvertInput(eventCounter, pythia, branchEvent, factory,
|
---|
| 284 | allParticleOutputArray, stableParticleOutputArray, partonOutputArray,
|
---|
| 285 | &readStopWatch, &procStopWatch);
|
---|
[5150e39] | 286 | modularDelphes->ProcessTask();
|
---|
| 287 | procStopWatch.Stop();
|
---|
| 288 |
|
---|
[cd616b9] | 289 | if(reader)
|
---|
| 290 | {
|
---|
| 291 | reader->AnalyzeEvent(branchEventLHEF, eventCounter, &readStopWatch, &procStopWatch);
|
---|
| 292 | reader->AnalyzeRwgt(branchRwgtLHEF);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[5150e39] | 295 | treeWriter->Fill();
|
---|
| 296 |
|
---|
| 297 | treeWriter->Clear();
|
---|
| 298 | modularDelphes->Clear();
|
---|
[cd616b9] | 299 | if(reader) reader->Clear();
|
---|
[5150e39] | 300 |
|
---|
| 301 | readStopWatch.Start();
|
---|
[a0538b9] | 302 | progressBar.Update(eventCounter, eventCounter);
|
---|
[5150e39] | 303 | }
|
---|
| 304 |
|
---|
[a0538b9] | 305 | progressBar.Update(eventCounter, eventCounter, kTRUE);
|
---|
[5150e39] | 306 | progressBar.Finish();
|
---|
| 307 |
|
---|
[3e276d4] | 308 | pythia->stat();
|
---|
[5150e39] | 309 |
|
---|
| 310 | modularDelphes->FinishTask();
|
---|
| 311 | treeWriter->Write();
|
---|
| 312 |
|
---|
| 313 | cout << "** Exiting..." << endl;
|
---|
| 314 |
|
---|
[cd616b9] | 315 | delete reader;
|
---|
[5150e39] | 316 | delete pythia;
|
---|
| 317 | delete modularDelphes;
|
---|
| 318 | delete confReader;
|
---|
| 319 | delete treeWriter;
|
---|
| 320 | delete outputFile;
|
---|
| 321 |
|
---|
| 322 | return 0;
|
---|
| 323 | }
|
---|
| 324 | catch(runtime_error &e)
|
---|
| 325 | {
|
---|
| 326 | if(treeWriter) delete treeWriter;
|
---|
| 327 | if(outputFile) delete outputFile;
|
---|
| 328 | cerr << "** ERROR: " << e.what() << endl;
|
---|
| 329 | return 1;
|
---|
| 330 | }
|
---|
| 331 | }
|
---|