#include #include #include #include #include #include #include #include #include "TROOT.h" #include "TApplication.h" #include "TFile.h" #include "TClonesArray.h" #include "classes/DelphesClasses.h" #include "ExRootAnalysis/ExRootTreeReader.h" #include "ExRootAnalysis/ExRootProgressBar.h" using namespace std; /* LHC Olympics format discription from http://www.jthaler.net/olympicswiki/doku.php?id=lhc_olympics:data_file_format * The first column of each row is just a counter that labels the object. * The event begins with a row labelled "0"; this row contains the event number and the triggering information. The last row of the event is always the missing transverse momentum (MET). * The second column of each row gives the type of object being listed [0, 1, 2, 3, 4, 6 = photon, electron, muon, hadronically-decaying tau, jet, missing transverse energy]. * The next three columns give the pseudorapidity, the azimuthal angle, and the transverse momentum of the object. * The sixth column gives the invariant mass of the object. * The seventh column gives the number of tracks associated with the object; in the case of a lepton, this number is multiplied by the charge of the lepton. * The eighth column is 1 or 2 for a jet that has been "tagged" as containing a b-quark (actually a heavy flavor tag that sometimes indicates c-quarks), otherwise it is 0. For muons, the integer part of this number is the identity of the jet (see column 1) that is closest ot this muon in Delta R. * The ninth column is the ratio of the hadronic versus electromagnetic energy deposited in the calorimeter cells associated with the object. For muons to the left of the decimal point is the summed pT in a R=0.4 cone (excluding the muon). To the right of the decimal point is etrat, which is a percentage between .00 and .99. It is the ratio of the transverse energy in a 3x3 grid surrounding the muon to the pT of the muon. */ class LHCOWriter { public: LHCOWriter(ExRootTreeReader *treeReader, FILE *outputFile); ~LHCOWriter(); void ProcessEvent(); private: void Reset(); void Write(); void AnalyseEvent(); void AnalysePhotons(); void AnalyseElectrons(); void AnalyseMuons(); void AnalyseTauJets(); void AnalyseJets(); void AnalyseMissingET(); enum {kIntParamSize = 2, kDblParamSize = 9}; Int_t fIntParam[kIntParamSize]; Double_t fDblParam[kDblParamSize]; Long64_t fTriggerWord, fEventNumber; ExRootTreeReader *fTreeReader; FILE *fOutputFile; TClonesArray *fBranchEvent; TClonesArray *fBranchPhoton; TClonesArray *fBranchElectron; TClonesArray *fBranchMuon; TClonesArray *fBranchTauJet; TClonesArray *fBranchJet; TClonesArray *fBranchMissingET; TIterator *fItPhoton; TIterator *fItElectron; TIterator *fItMuon; TIterator *fItTauJet; TIterator *fItJet; }; //------------------------------------------------------------------------------ LHCOWriter::LHCOWriter(ExRootTreeReader *treeReader, FILE *outputFile) : fTriggerWord(0), fEventNumber(1), fTreeReader(0), fOutputFile(0) { fTreeReader = treeReader; fOutputFile = outputFile; // information about reconstructed event fBranchEvent = fTreeReader->UseBranch("Event"); // reconstructed photons fBranchPhoton = fTreeReader->UseBranch("Photon"); fItPhoton = fBranchPhoton->MakeIterator(); // reconstructed electrons fBranchElectron = fTreeReader->UseBranch("Electron"); fItElectron = fBranchElectron->MakeIterator(); // reconstructed muons fBranchMuon = fTreeReader->UseBranch("Muon"); fItMuon = fBranchMuon->MakeIterator(); // reconstructed hadronically-decaying tau leptons fBranchTauJet = fTreeReader->UseBranch("TauJet"); fItTauJet = fBranchTauJet->MakeIterator(); // reconstructed jets fBranchJet = fTreeReader->UseBranch("Jet"); fItJet = fBranchJet->MakeIterator(); // missing transverse energy fBranchMissingET = fTreeReader->UseBranch("MissingET"); } //------------------------------------------------------------------------------ LHCOWriter::~LHCOWriter() { } //--------------------------------------------------------------------------- void LHCOWriter::ProcessEvent() { fIntParam[0] = 0; AnalyseEvent(); AnalysePhotons(); AnalyseElectrons(); AnalyseMuons(); AnalyseTauJets(); AnalyseJets(); AnalyseMissingET(); } //--------------------------------------------------------------------------- void LHCOWriter::Reset() { int i; for(i = 1; i < kIntParamSize; ++i) { fIntParam[i] = 0; } for(i = 0; i < kDblParamSize; ++i) { fDblParam[i] = 0.0; } } //--------------------------------------------------------------------------- void LHCOWriter::Write() { fprintf(fOutputFile, "%d\t%d\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n", fIntParam[0], fIntParam[1], fDblParam[0], fDblParam[1], fDblParam[2], fDblParam[3], fDblParam[4], fDblParam[5], fDblParam[6], fDblParam[7], fDblParam[8]); ++fIntParam[0]; } //--------------------------------------------------------------------------- void LHCOWriter::AnalyseEvent() { Event *element; element = static_cast(fBranchEvent->At(0)); fprintf(fOutputFile, "0\t%lld\t0\n", element->Number); ++fIntParam[0]; } //--------------------------------------------------------------------------- void LHCOWriter::AnalysePhotons() { Photon *element; fItPhoton->Reset(); while((element = static_cast(fItPhoton->Next()))) { Reset(); fIntParam[1] = 0; fDblParam[0] = element->Eta; fDblParam[1] = element->Phi; fDblParam[2] = element->PT; fDblParam[6] = 0.0; // element->EhadOverEem; Write(); } } //--------------------------------------------------------------------------- void LHCOWriter::AnalyseElectrons() { Electron *element; fItElectron->Reset(); while((element = static_cast(fItElectron->Next()))) { Reset(); fIntParam[1] = 1; fDblParam[0] = element->Eta; fDblParam[1] = element->Phi; fDblParam[2] = element->PT; fDblParam[4] = 0.0; // element->Ntrk * element->Charge; fDblParam[6] = 0.0; // element->EhadOverEem; Write(); } } //--------------------------------------------------------------------------- void LHCOWriter::AnalyseMuons() { Muon *element; fItMuon->Reset(); while((element = static_cast(fItMuon->Next()))) { Reset(); fIntParam[1] = 2; fDblParam[0] = element->Eta; fDblParam[1] = element->Phi; fDblParam[2] = element->PT; fDblParam[4] = 0.0; // element->Ntrk * element->Charge; fDblParam[5] = 0.0; // element->JetIndex; fDblParam[6] = 0.0; // element->PTiso + element->ETiso; Write(); } } //--------------------------------------------------------------------------- void LHCOWriter::AnalyseTauJets() { TauJet *element; fItTauJet->Reset(); while((element = static_cast(fItTauJet->Next()))) { Reset(); fIntParam[1] = 3; fDblParam[0] = element->Eta; fDblParam[1] = element->Phi; fDblParam[2] = element->PT; fDblParam[4] = 0.0; // element->Ntrk * element->Charge; fDblParam[6] = 0.0; // element->EhadOverEem; Write(); } } //--------------------------------------------------------------------------- void LHCOWriter::AnalyseJets() { Jet *element; fItJet->Reset(); while((element = static_cast(fItJet->Next()))) { Reset(); fIntParam[1] = 4; fDblParam[0] = element->Eta; fDblParam[1] = element->Phi; fDblParam[2] = element->PT; fDblParam[3] = element->Mass; fDblParam[4] = element->Ntrk; fDblParam[5] = element->BTag; fDblParam[6] = 0.0; // element->EhadOverEem; Write(); } } //--------------------------------------------------------------------------- void LHCOWriter::AnalyseMissingET() { MissingET *element; element = static_cast(fBranchMissingET->At(0)); Reset(); fIntParam[1] = 6; fDblParam[1] = element->Phi; fDblParam[2] = element->MET; Write(); } //--------------------------------------------------------------------------- static bool interrupted = false; void SignalHandler(int sig) { interrupted = true; } //--------------------------------------------------------------------------- int main(int argc, char *argv[]) { char appName[] = "root2lhco"; stringstream message; FILE *outputFile = 0; TChain *inputChain = 0; LHCOWriter *writer = 0; ExRootTreeReader *treeReader = 0; Long64_t entry, allEntries; if(argc < 2 || argc > 3) { cout << " Usage: " << appName << " input_file" << " [output_file]" << endl; cout << " input_file - input file in ROOT format," << endl; cout << " output_file - output file in LHCO format," << endl; cout << " with no output_file, or when output_file is -, write to standard output." << endl; return 1; } signal(SIGINT, SignalHandler); gROOT->SetBatch(); int appargc = 1; char *appargv[] = {appName}; TApplication app(appName, &appargc, appargv); try { cout << "** Reading " << argv[1] << endl; inputChain = new TChain("Delphes"); inputChain->Add(argv[1]); ExRootTreeReader *treeReader = new ExRootTreeReader(inputChain); if(argc == 2 || strcmp(argv[2], "-") == 0) { outputFile = stdout; } else { outputFile = fopen(argv[2], "w"); if(outputFile == NULL) { message << "can't open " << argv[2]; throw runtime_error(message.str()); } } allEntries = treeReader->GetEntries(); cerr << "** Input file contains " << allEntries << " events" << endl; if(allEntries > 0) { // Create LHC Olympics converter: writer = new LHCOWriter(treeReader, outputFile); ExRootProgressBar progressBar(allEntries - 1); // Loop over all events for(entry = 0; entry < allEntries && !interrupted; ++entry) { if(!treeReader->ReadEntry(entry)) { cout << "** ERROR: cannot read event " << entry << endl; break; } writer->ProcessEvent(); progressBar.Update(entry); } progressBar.Finish(); delete writer; } cout << "** Exiting..." << endl; if(outputFile != stdout) fclose(outputFile); delete treeReader; delete inputChain; return 0; } catch(runtime_error &e) { if(writer) delete writer; if(treeReader) delete treeReader; if(inputChain) delete inputChain; cerr << "** ERROR: " << e.what() << endl; return 1; } }