[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 <stdexcept>
|
---|
| 21 | #include <iostream>
|
---|
| 22 | #include <sstream>
|
---|
| 23 | #include <memory>
|
---|
| 24 |
|
---|
| 25 | #include <map>
|
---|
[975405a] | 26 | #include <vector>
|
---|
[d7d2da3] | 27 |
|
---|
| 28 | #include <stdlib.h>
|
---|
| 29 | #include <signal.h>
|
---|
| 30 | #include <stdio.h>
|
---|
| 31 |
|
---|
| 32 | #include "TROOT.h"
|
---|
| 33 | #include "TApplication.h"
|
---|
| 34 |
|
---|
| 35 | #include "TFile.h"
|
---|
| 36 | #include "TObjArray.h"
|
---|
| 37 | #include "TStopwatch.h"
|
---|
| 38 | #include "TDatabasePDG.h"
|
---|
| 39 | #include "TParticlePDG.h"
|
---|
| 40 | #include "TLorentzVector.h"
|
---|
| 41 |
|
---|
| 42 | #include "modules/Delphes.h"
|
---|
| 43 | #include "classes/DelphesStream.h"
|
---|
| 44 | #include "classes/DelphesClasses.h"
|
---|
| 45 | #include "classes/DelphesFactory.h"
|
---|
| 46 |
|
---|
| 47 | #include "ExRootAnalysis/ExRootTreeWriter.h"
|
---|
| 48 | #include "ExRootAnalysis/ExRootTreeBranch.h"
|
---|
| 49 | #include "ExRootAnalysis/ExRootProgressBar.h"
|
---|
| 50 |
|
---|
[cd699d0] | 51 | #include "FWCore/FWLite/interface/FWLiteEnabler.h"
|
---|
[d7d2da3] | 52 | #include "DataFormats/FWLite/interface/Event.h"
|
---|
| 53 | #include "DataFormats/FWLite/interface/Handle.h"
|
---|
| 54 | #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
|
---|
[f29758e] | 55 | #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
|
---|
| 56 | #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
|
---|
[3241a0e] | 57 | #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
|
---|
| 58 | #include "SimDataFormats/GeneratorProducts/interface/WeightsInfo.h"
|
---|
[d7d2da3] | 59 |
|
---|
| 60 | using namespace std;
|
---|
| 61 |
|
---|
| 62 | //---------------------------------------------------------------------------
|
---|
| 63 |
|
---|
[f29758e] | 64 | void ConvertInput(fwlite::Event &event, Long64_t eventCounter,
|
---|
[3241a0e] | 65 | ExRootTreeBranch *branchEvent, ExRootTreeBranch *branchRwgt,
|
---|
| 66 | DelphesFactory *factory, TObjArray *allParticleOutputArray,
|
---|
| 67 | TObjArray *stableParticleOutputArray, TObjArray *partonOutputArray)
|
---|
[d7d2da3] | 68 | {
|
---|
[f29758e] | 69 |
|
---|
[480f9ed] | 70 | fwlite::Handle< GenEventInfoProduct > handleGenEventInfo;
|
---|
[3241a0e] | 71 | fwlite::Handle< LHEEventProduct > handleLHEEvent;
|
---|
[d7d2da3] | 72 | fwlite::Handle< vector< reco::GenParticle > > handleParticle;
|
---|
[480f9ed] | 73 |
|
---|
[975405a] | 74 | vector< reco::GenParticle >::const_iterator itParticle;
|
---|
| 75 |
|
---|
| 76 | vector< const reco::Candidate * > vectorCandidate;
|
---|
| 77 | vector< const reco::Candidate * >::iterator itCandidate;
|
---|
| 78 |
|
---|
[f29758e] | 79 | handleGenEventInfo.getByLabel(event, "generator");
|
---|
[480f9ed] | 80 |
|
---|
| 81 | if (!((handleLHEEvent.getBranchNameFor(event, "source")).empty()))
|
---|
| 82 | {
|
---|
| 83 | handleLHEEvent.getByLabel(event, "source");
|
---|
| 84 | }
|
---|
| 85 | else if (!((handleLHEEvent.getBranchNameFor(event, "externalLHEProducer")).empty()))
|
---|
| 86 | {
|
---|
| 87 | handleLHEEvent.getByLabel(event, "externalLHEProducer");
|
---|
| 88 | }
|
---|
| 89 | else
|
---|
| 90 | {
|
---|
| 91 | std::cout<<"Wrong LHEEvent Label! Please, check the input file."<<std::endl;
|
---|
| 92 | exit(-1);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | if (!((handleParticle.getBranchNameFor(event, "genParticles")).empty()))
|
---|
| 96 | {
|
---|
| 97 | handleParticle.getByLabel(event, "genParticles");
|
---|
| 98 | }
|
---|
| 99 | else if (!((handleParticle.getBranchNameFor(event, "prunedGenParticles")).empty()))
|
---|
| 100 | {
|
---|
| 101 | handleParticle.getByLabel(event, "prunedGenParticles");
|
---|
| 102 | }
|
---|
| 103 | else
|
---|
| 104 | {
|
---|
| 105 | std::cout<<"Wrong GenParticle Label! Please, check the input file."<<std::endl;
|
---|
| 106 | exit(-1);
|
---|
| 107 | }
|
---|
[d7d2da3] | 108 |
|
---|
[f29758e] | 109 | HepMCEvent *element;
|
---|
[3241a0e] | 110 | Weight *weight;
|
---|
[d7d2da3] | 111 | Candidate *candidate;
|
---|
| 112 | TDatabasePDG *pdg;
|
---|
| 113 | TParticlePDG *pdgParticle;
|
---|
| 114 | Int_t pdgCode;
|
---|
| 115 |
|
---|
| 116 | Int_t pid, status;
|
---|
[80d4a34] | 117 | Double_t px, py, pz, e, mass;
|
---|
[d7d2da3] | 118 | Double_t x, y, z;
|
---|
| 119 |
|
---|
[3241a0e] | 120 | const vector< gen::WeightsInfo > &vectorWeightsInfo = handleLHEEvent->weights();
|
---|
| 121 | vector< gen::WeightsInfo >::const_iterator itWeightsInfo;
|
---|
| 122 |
|
---|
[f29758e] | 123 | element = static_cast<HepMCEvent *>(branchEvent->NewEntry());
|
---|
| 124 |
|
---|
| 125 | element->Number = eventCounter;
|
---|
| 126 |
|
---|
| 127 | element->ProcessID = handleGenEventInfo->signalProcessID();
|
---|
| 128 | element->MPI = 1;
|
---|
[529fe78] | 129 | element->Weight = handleGenEventInfo->weight();
|
---|
[f29758e] | 130 | element->Scale = handleGenEventInfo->qScale();
|
---|
| 131 | element->AlphaQED = handleGenEventInfo->alphaQED();
|
---|
| 132 | element->AlphaQCD = handleGenEventInfo->alphaQCD();
|
---|
| 133 |
|
---|
| 134 | element->ID1 = 0;
|
---|
| 135 | element->ID2 = 0;
|
---|
| 136 | element->X1 = 0.0;
|
---|
| 137 | element->X2 = 0.0;
|
---|
| 138 | element->ScalePDF = 0.0;
|
---|
| 139 | element->PDF1 = 0.0;
|
---|
| 140 | element->PDF2 = 0.0;
|
---|
| 141 |
|
---|
| 142 | element->ReadTime = 0.0;
|
---|
| 143 | element->ProcTime = 0.0;
|
---|
| 144 |
|
---|
[3241a0e] | 145 | for(itWeightsInfo = vectorWeightsInfo.begin(); itWeightsInfo != vectorWeightsInfo.end(); ++itWeightsInfo)
|
---|
| 146 | {
|
---|
| 147 | weight = static_cast<Weight *>(branchRwgt->NewEntry());
|
---|
| 148 | weight->Weight = itWeightsInfo->wgt;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[d7d2da3] | 151 | pdg = TDatabasePDG::Instance();
|
---|
| 152 |
|
---|
[975405a] | 153 | for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
|
---|
[d7d2da3] | 154 | {
|
---|
[975405a] | 155 | vectorCandidate.push_back(&*itParticle);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
|
---|
| 159 | {
|
---|
| 160 | const reco::GenParticle &particle = *itParticle;
|
---|
[d7d2da3] | 161 |
|
---|
[1e1f73f] | 162 | pid = particle.pdgId();
|
---|
| 163 | status = particle.status();
|
---|
[80d4a34] | 164 | px = particle.px(); py = particle.py(); pz = particle.pz(); e = particle.energy(); mass = particle.mass();
|
---|
[1e1f73f] | 165 | x = particle.vx(); y = particle.vy(); z = particle.vz();
|
---|
[d7d2da3] | 166 |
|
---|
[1e1f73f] | 167 | candidate = factory->NewCandidate();
|
---|
[d7d2da3] | 168 |
|
---|
[1e1f73f] | 169 | candidate->PID = pid;
|
---|
| 170 | pdgCode = TMath::Abs(candidate->PID);
|
---|
[d7d2da3] | 171 |
|
---|
[1e1f73f] | 172 | candidate->Status = status;
|
---|
[d7d2da3] | 173 |
|
---|
[f29758e] | 174 | if(particle.mother())
|
---|
| 175 | {
|
---|
| 176 | itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.mother());
|
---|
| 177 | if(itCandidate != vectorCandidate.end()) candidate->M1 = distance(vectorCandidate.begin(), itCandidate);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[975405a] | 180 | itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(0));
|
---|
| 181 | if(itCandidate != vectorCandidate.end()) candidate->D1 = distance(vectorCandidate.begin(), itCandidate);
|
---|
| 182 |
|
---|
| 183 | itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(particle.numberOfDaughters() - 1));
|
---|
| 184 | if(itCandidate != vectorCandidate.end()) candidate->D2 = distance(vectorCandidate.begin(), itCandidate);
|
---|
| 185 |
|
---|
[1e1f73f] | 186 | pdgParticle = pdg->GetParticle(pid);
|
---|
| 187 | candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
|
---|
[bba4646] | 188 | candidate->Mass = mass;
|
---|
[d7d2da3] | 189 |
|
---|
[1e1f73f] | 190 | candidate->Momentum.SetPxPyPzE(px, py, pz, e);
|
---|
[d7d2da3] | 191 |
|
---|
[0a836f2] | 192 | candidate->Position.SetXYZT(x*10.0, y*10.0, z*10.0, 0.0);
|
---|
[d7d2da3] | 193 |
|
---|
[1e1f73f] | 194 | allParticleOutputArray->Add(candidate);
|
---|
[d7d2da3] | 195 |
|
---|
[2f82259] | 196 | if(!pdgParticle) continue;
|
---|
[d7d2da3] | 197 |
|
---|
[1e1f73f] | 198 | if(status == 1)
|
---|
| 199 | {
|
---|
| 200 | stableParticleOutputArray->Add(candidate);
|
---|
| 201 | }
|
---|
| 202 | else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
|
---|
| 203 | {
|
---|
| 204 | partonOutputArray->Add(candidate);
|
---|
[d7d2da3] | 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | //---------------------------------------------------------------------------
|
---|
| 210 |
|
---|
| 211 | static bool interrupted = false;
|
---|
| 212 |
|
---|
| 213 | void SignalHandler(int sig)
|
---|
| 214 | {
|
---|
| 215 | interrupted = true;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | //---------------------------------------------------------------------------
|
---|
| 219 |
|
---|
| 220 | int main(int argc, char *argv[])
|
---|
| 221 | {
|
---|
| 222 | char appName[] = "DelphesCMSFWLite";
|
---|
| 223 | stringstream message;
|
---|
| 224 | TFile *inputFile = 0;
|
---|
| 225 | TFile *outputFile = 0;
|
---|
| 226 | TStopwatch eventStopWatch;
|
---|
| 227 | ExRootTreeWriter *treeWriter = 0;
|
---|
[3241a0e] | 228 | ExRootTreeBranch *branchEvent = 0, *branchRwgt = 0;
|
---|
[d7d2da3] | 229 | ExRootConfReader *confReader = 0;
|
---|
| 230 | Delphes *modularDelphes = 0;
|
---|
| 231 | DelphesFactory *factory = 0;
|
---|
| 232 | TObjArray *allParticleOutputArray = 0, *stableParticleOutputArray = 0, *partonOutputArray = 0;
|
---|
| 233 | Int_t i;
|
---|
[5ca3d52] | 234 | Long64_t eventCounter, numberOfEvents;
|
---|
[d7d2da3] | 235 |
|
---|
| 236 | if(argc < 4)
|
---|
| 237 | {
|
---|
| 238 | cout << " Usage: " << appName << " config_file" << " output_file" << " input_file(s)" << endl;
|
---|
| 239 | cout << " config_file - configuration file in Tcl format," << endl;
|
---|
| 240 | cout << " output_file - output file in ROOT format," << endl;
|
---|
| 241 | cout << " input_file(s) - input file(s) in ROOT format." << endl;
|
---|
| 242 | return 1;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | signal(SIGINT, SignalHandler);
|
---|
| 246 |
|
---|
| 247 | gROOT->SetBatch();
|
---|
| 248 |
|
---|
| 249 | int appargc = 1;
|
---|
| 250 | char *appargv[] = {appName};
|
---|
| 251 | TApplication app(appName, &appargc, appargv);
|
---|
| 252 |
|
---|
[cd699d0] | 253 | FWLiteEnabler::enable();
|
---|
| 254 |
|
---|
[d7d2da3] | 255 | try
|
---|
| 256 | {
|
---|
| 257 | outputFile = TFile::Open(argv[2], "CREATE");
|
---|
| 258 |
|
---|
| 259 | if(outputFile == NULL)
|
---|
| 260 | {
|
---|
| 261 | message << "can't open " << argv[2] << endl;
|
---|
| 262 | throw runtime_error(message.str());
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
|
---|
| 266 |
|
---|
[f29758e] | 267 | branchEvent = treeWriter->NewBranch("Event", HepMCEvent::Class());
|
---|
[3241a0e] | 268 | branchRwgt = treeWriter->NewBranch("Rwgt", Weight::Class());
|
---|
[f29758e] | 269 |
|
---|
[d7d2da3] | 270 | confReader = new ExRootConfReader;
|
---|
| 271 | confReader->ReadFile(argv[1]);
|
---|
| 272 |
|
---|
| 273 | modularDelphes = new Delphes("Delphes");
|
---|
| 274 | modularDelphes->SetConfReader(confReader);
|
---|
| 275 | modularDelphes->SetTreeWriter(treeWriter);
|
---|
| 276 |
|
---|
| 277 | factory = modularDelphes->GetFactory();
|
---|
| 278 | allParticleOutputArray = modularDelphes->ExportArray("allParticles");
|
---|
| 279 | stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
|
---|
| 280 | partonOutputArray = modularDelphes->ExportArray("partons");
|
---|
| 281 |
|
---|
| 282 | modularDelphes->InitTask();
|
---|
| 283 |
|
---|
| 284 | for(i = 3; i < argc && !interrupted; ++i)
|
---|
| 285 | {
|
---|
| 286 | cout << "** Reading " << argv[i] << endl;
|
---|
| 287 |
|
---|
| 288 | inputFile = TFile::Open(argv[i]);
|
---|
| 289 |
|
---|
| 290 | if(inputFile == NULL)
|
---|
| 291 | {
|
---|
| 292 | message << "can't open " << argv[i] << endl;
|
---|
| 293 | throw runtime_error(message.str());
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | fwlite::Event event(inputFile);
|
---|
| 297 |
|
---|
[5ca3d52] | 298 | numberOfEvents = event.size();
|
---|
[d7d2da3] | 299 |
|
---|
[5ca3d52] | 300 | if(numberOfEvents <= 0) continue;
|
---|
[d7d2da3] | 301 |
|
---|
[a0538b9] | 302 | // ExRootProgressBar progressBar(numberOfEvents - 1);
|
---|
| 303 | ExRootProgressBar progressBar(-1);
|
---|
[d7d2da3] | 304 |
|
---|
| 305 | // Loop over all objects
|
---|
[5ca3d52] | 306 | eventCounter = 0;
|
---|
[d7d2da3] | 307 | modularDelphes->Clear();
|
---|
| 308 | treeWriter->Clear();
|
---|
| 309 | for(event.toBegin(); !event.atEnd() && !interrupted; ++event)
|
---|
| 310 | {
|
---|
[3241a0e] | 311 | ConvertInput(event, eventCounter, branchEvent, branchRwgt, factory,
|
---|
[f29758e] | 312 | allParticleOutputArray, stableParticleOutputArray, partonOutputArray);
|
---|
[d7d2da3] | 313 | modularDelphes->ProcessTask();
|
---|
| 314 |
|
---|
| 315 | treeWriter->Fill();
|
---|
| 316 |
|
---|
| 317 | modularDelphes->Clear();
|
---|
| 318 | treeWriter->Clear();
|
---|
| 319 |
|
---|
[a0538b9] | 320 | progressBar.Update(eventCounter, eventCounter);
|
---|
[5ca3d52] | 321 | ++eventCounter;
|
---|
[d7d2da3] | 322 | }
|
---|
[a0538b9] | 323 |
|
---|
| 324 | progressBar.Update(eventCounter, eventCounter, kTRUE);
|
---|
[d7d2da3] | 325 | progressBar.Finish();
|
---|
| 326 |
|
---|
| 327 | inputFile->Close();
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | modularDelphes->FinishTask();
|
---|
| 331 | treeWriter->Write();
|
---|
| 332 |
|
---|
| 333 | cout << "** Exiting..." << endl;
|
---|
| 334 |
|
---|
| 335 | delete modularDelphes;
|
---|
| 336 | delete confReader;
|
---|
| 337 | delete treeWriter;
|
---|
| 338 | delete outputFile;
|
---|
| 339 |
|
---|
| 340 | return 0;
|
---|
| 341 | }
|
---|
| 342 | catch(runtime_error &e)
|
---|
| 343 | {
|
---|
| 344 | if(treeWriter) delete treeWriter;
|
---|
| 345 | if(outputFile) delete outputFile;
|
---|
| 346 | cerr << "** ERROR: " << e.what() << endl;
|
---|
| 347 | return 1;
|
---|
| 348 | }
|
---|
| 349 | }
|
---|