[1157] | 1 | #include <algorithm>
|
---|
[834] | 2 | #include <stdexcept>
|
---|
| 3 | #include <iostream>
|
---|
| 4 | #include <sstream>
|
---|
[976] | 5 | #include <memory>
|
---|
[834] | 6 |
|
---|
| 7 | #include <map>
|
---|
[1157] | 8 | #include <vector>
|
---|
[834] | 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 |
|
---|
| 39 | using namespace std;
|
---|
| 40 |
|
---|
| 41 | //---------------------------------------------------------------------------
|
---|
| 42 |
|
---|
[975] | 43 | void ConvertInput(fwlite::Event &event, DelphesFactory *factory, TObjArray *allParticleOutputArray, TObjArray *stableParticleOutputArray, TObjArray *partonOutputArray)
|
---|
[834] | 44 | {
|
---|
[835] | 45 | fwlite::Handle< vector< reco::GenParticle > > handleParticle;
|
---|
[1157] | 46 | vector< reco::GenParticle >::const_iterator itParticle;
|
---|
| 47 |
|
---|
| 48 | vector< const reco::Candidate * > vectorCandidate;
|
---|
| 49 | vector< const reco::Candidate * >::iterator itCandidate;
|
---|
| 50 |
|
---|
[834] | 51 | handleParticle.getByLabel(event, "genParticles");
|
---|
[960] | 52 |
|
---|
[834] | 53 | Candidate *candidate;
|
---|
| 54 | TDatabasePDG *pdg;
|
---|
| 55 | TParticlePDG *pdgParticle;
|
---|
[1051] | 56 | Int_t pdgCode;
|
---|
| 57 |
|
---|
[834] | 58 | Int_t pid, status;
|
---|
| 59 | Double_t px, py, pz, e;
|
---|
| 60 | Double_t x, y, z;
|
---|
| 61 |
|
---|
[835] | 62 | pdg = TDatabasePDG::Instance();
|
---|
[960] | 63 |
|
---|
[1157] | 64 | for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
|
---|
[834] | 65 | {
|
---|
[1157] | 66 | vectorCandidate.push_back(&*itParticle);
|
---|
| 67 | }
|
---|
[834] | 68 |
|
---|
[1157] | 69 | for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
|
---|
| 70 | {
|
---|
| 71 | const reco::GenParticle &particle = *itParticle;
|
---|
| 72 |
|
---|
[1134] | 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();
|
---|
[960] | 77 |
|
---|
[1134] | 78 | candidate = factory->NewCandidate();
|
---|
[834] | 79 |
|
---|
[1134] | 80 | candidate->PID = pid;
|
---|
| 81 | pdgCode = TMath::Abs(candidate->PID);
|
---|
[834] | 82 |
|
---|
[1134] | 83 | candidate->Status = status;
|
---|
[834] | 84 |
|
---|
[1157] | 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 |
|
---|
[1134] | 91 | pdgParticle = pdg->GetParticle(pid);
|
---|
| 92 | candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
|
---|
| 93 | candidate->Mass = pdgParticle ? pdgParticle->Mass() : -999.9;
|
---|
[960] | 94 |
|
---|
[1134] | 95 | candidate->Momentum.SetPxPyPzE(px, py, pz, e);
|
---|
[834] | 96 |
|
---|
[1134] | 97 | candidate->Position.SetXYZT(x, y, z, 0.0);
|
---|
[834] | 98 |
|
---|
[1134] | 99 | allParticleOutputArray->Add(candidate);
|
---|
[834] | 100 |
|
---|
[1147] | 101 | if(!pdgParticle) continue;
|
---|
[834] | 102 |
|
---|
[1134] | 103 | if(status == 1)
|
---|
| 104 | {
|
---|
| 105 | stableParticleOutputArray->Add(candidate);
|
---|
[834] | 106 | }
|
---|
[1134] | 107 | else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
|
---|
| 108 | {
|
---|
| 109 | partonOutputArray->Add(candidate);
|
---|
| 110 | }
|
---|
[834] | 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | //---------------------------------------------------------------------------
|
---|
| 115 |
|
---|
| 116 | static bool interrupted = false;
|
---|
| 117 |
|
---|
| 118 | void SignalHandler(int sig)
|
---|
| 119 | {
|
---|
[960] | 120 | interrupted = true;
|
---|
[834] | 121 | }
|
---|
| 122 |
|
---|
| 123 | //---------------------------------------------------------------------------
|
---|
| 124 |
|
---|
| 125 | int 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;
|
---|
[975] | 136 | TObjArray *allParticleOutputArray = 0, *stableParticleOutputArray = 0, *partonOutputArray = 0;
|
---|
[834] | 137 | Int_t i;
|
---|
[835] | 138 | Long64_t entry, allEntries;
|
---|
[834] | 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);
|
---|
[835] | 156 |
|
---|
| 157 | AutoLibraryLoader::enable();
|
---|
| 158 |
|
---|
[834] | 159 | try
|
---|
| 160 | {
|
---|
[977] | 161 | outputFile = TFile::Open(argv[2], "CREATE");
|
---|
[834] | 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]);
|
---|
[960] | 173 |
|
---|
[834] | 174 | modularDelphes = new Delphes("Delphes");
|
---|
| 175 | modularDelphes->SetConfReader(confReader);
|
---|
| 176 | modularDelphes->SetTreeWriter(treeWriter);
|
---|
[960] | 177 |
|
---|
[834] | 178 | factory = modularDelphes->GetFactory();
|
---|
[975] | 179 | allParticleOutputArray = modularDelphes->ExportArray("allParticles");
|
---|
| 180 | stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
|
---|
[834] | 181 | partonOutputArray = modularDelphes->ExportArray("partons");
|
---|
| 182 |
|
---|
| 183 | modularDelphes->InitTask();
|
---|
[960] | 184 |
|
---|
[834] | 185 | for(i = 3; i < argc && !interrupted; ++i)
|
---|
| 186 | {
|
---|
| 187 | cout << "** Reading " << argv[i] << endl;
|
---|
| 188 |
|
---|
[835] | 189 | inputFile = TFile::Open(argv[i]);
|
---|
[834] | 190 |
|
---|
| 191 | if(inputFile == NULL)
|
---|
| 192 | {
|
---|
| 193 | message << "can't open " << argv[i] << endl;
|
---|
| 194 | throw runtime_error(message.str());
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[835] | 197 | fwlite::Event event(inputFile);
|
---|
[834] | 198 |
|
---|
[835] | 199 | allEntries = event.size();
|
---|
| 200 |
|
---|
[843] | 201 | if(allEntries <= 0) continue;
|
---|
[834] | 202 |
|
---|
[843] | 203 | ExRootProgressBar progressBar(allEntries - 1);
|
---|
[834] | 204 |
|
---|
[843] | 205 | // Loop over all objects
|
---|
| 206 | entry = 0;
|
---|
| 207 | modularDelphes->Clear();
|
---|
| 208 | treeWriter->Clear();
|
---|
| 209 | for(event.toBegin(); !event.atEnd() && !interrupted; ++event)
|
---|
| 210 | {
|
---|
[975] | 211 | ConvertInput(event, factory, allParticleOutputArray, stableParticleOutputArray, partonOutputArray);
|
---|
[843] | 212 | modularDelphes->ProcessTask();
|
---|
[834] | 213 |
|
---|
[843] | 214 | treeWriter->Fill();
|
---|
[836] | 215 |
|
---|
[843] | 216 | modularDelphes->Clear();
|
---|
| 217 | treeWriter->Clear();
|
---|
[836] | 218 |
|
---|
[843] | 219 | progressBar.Update(entry);
|
---|
| 220 | ++entry;
|
---|
[834] | 221 | }
|
---|
[843] | 222 | progressBar.Finish();
|
---|
| 223 |
|
---|
| 224 | inputFile->Close();
|
---|
[834] | 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;
|
---|
[960] | 236 |
|
---|
[834] | 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 | }
|
---|