00001 #include <iostream>
00002 #include <sstream>
00003 #include <cmath>
00004 #include <stdio.h>
00005 #include "HepMC/GenEvent.h"
00006 #include "HepMC/IO_GenEvent.h"
00007 #include "HepMC/GenParticle.h"
00008 #include "HepMC/GenVertex.h"
00009 #include "HepMC/IO_AsciiParticles.h"
00010 #include "HepMC/SimpleVector.h"
00011 #include "HepPDT/ParticleData.hh"
00012 #include "CLHEP/Vector/LorentzVector.h"
00013
00014 #include "fastjet/ClusterSequence.hh"
00015 #include "fastjet/SISConePlugin.hh"
00016
00017 using namespace std;
00018
00019
00020 #include "TH1.h"
00021 #include "TH2.h"
00022 #include "TFile.h"
00023 #include "TMath.h"
00024 #include "TLorentzVector.h"
00025
00026
00027 #include "../include/baseAnalysis.h"
00028
00029
00030
00031
00032
00033
00034 baseAnalysis::baseAnalysis()
00035 {
00036 m_Jetfinder_enabled=false;
00037 }
00038
00039
00040
00041
00042 baseAnalysis::~baseAnalysis()
00043 {
00044 for (std::vector<TH1D*>::const_iterator i(m_histVector.begin()); i != m_histVector.end(); ++i) {
00045 delete (*i);
00046 }
00047 m_histVector.resize(0);
00048
00049
00050
00051
00052
00053
00054 }
00055
00056
00057
00058
00059 int baseAnalysis::InitJetFinder(double coneRadius, double overlapThreshold, double jet_ptmin, double lepton_ptmin, double DeltaR_lepton_track)
00060 {
00061
00062 if(m_Jetfinder_enabled){
00063
00064 if(m_jetDef) {
00065 delete m_jetDef;
00066 m_jetDef=0;
00067 }
00068
00069 if(m_plugin){
00070 delete m_plugin;
00071 m_plugin=0;
00072 }
00073 }
00074
00075
00076 m_coneRadius = coneRadius;
00077 m_overlapThreshold = overlapThreshold;
00078 m_jet_ptmin = jet_ptmin;
00079 m_lepton_ptmin = lepton_ptmin;
00080 m_DeltaR_lepton_track = DeltaR_lepton_track;
00081
00082 m_plugin = new fastjet::SISConePlugin(m_coneRadius, m_overlapThreshold);
00083 m_jetDef = new fastjet::JetDefinition(m_plugin);
00084
00085 if(!m_jetDef) return false;
00086
00087 m_Jetfinder_enabled=true;
00088 return true;
00089
00090 }
00091
00092
00093
00094
00095 int baseAnalysis::FindJetableParticles(HepMC::GenEvent* hepmcevt)
00096 {
00097
00098
00099
00100 if(!m_Jetfinder_enabled || !m_jetDef){
00101 std::cout << "baseAnalysis: FastJet Algorithm not initialized!" << std::endl;
00102 return false;
00103 }
00104 CLHEP::HepLorentzVector lv_leadingJet(0,0,0,0);
00105
00106 for ( HepMC::GenEvent::particle_const_iterator p =
00107 hepmcevt->particles_begin(); p != hepmcevt->particles_end(); ++p ){
00108
00109
00110 int pid = (*p)->pdg_id();
00111
00112
00113
00114 if (!((*p)->status() == 1)) continue;
00115
00116
00117
00118 if(IsNeutrino(pid) || IsNeutron(pid)) continue;
00119
00120
00121
00122 double deltaPhi = 0;
00123 double deltaEta = 0;
00124 double DeltaR = 9999;
00125
00126
00127 double DeltaRCut = m_DeltaR_lepton_track;
00128 double LeptonpTCut = m_lepton_ptmin;
00129
00130 if(IsElectron(pid) || IsMuon(pid)) {
00131 int lepton_barcode = (*p)->barcode();
00132 HepMC::GenParticle *lepton = hepmcevt->barcode_to_particle(lepton_barcode);
00133
00134
00135 if(fabs((*lepton).momentum().perp()) > LeptonpTCut) continue;
00136
00137
00138 for ( HepMC::GenEvent::particle_const_iterator q = hepmcevt->particles_begin();
00139 q != hepmcevt->particles_end(); ++q ){
00140 if(lepton_barcode==(*q)->barcode()) continue;
00141
00142
00143 deltaPhi = (*p)->momentum().phi()-(*q)->momentum().phi();
00144
00145 if( deltaPhi > TMath::Pi() || deltaPhi == TMath::Pi() )
00146 {
00147 deltaPhi = deltaPhi - 2 * TMath::Pi() ;
00148 }
00149
00150 if( deltaPhi < - TMath::Pi() || deltaPhi == - TMath::Pi() )
00151 {
00152 deltaPhi = deltaPhi + 2 * TMath::Pi();
00153 }
00154
00155 deltaEta = (*p)->momentum().eta()-(*q)->momentum().eta();
00156 double DeltaRnew = sqrt( pow(deltaPhi,2) + pow(deltaEta,2));
00157
00158 if(DeltaRnew < DeltaR) DeltaR = DeltaRnew;
00159 }
00160 if(DeltaR > DeltaRCut) continue;
00161 }
00162
00163
00164
00165
00166
00167 if (!(*p)->production_vertex()) continue;
00168
00169 if ((*p)->end_vertex()) continue;
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 double eta = (*p)->momentum().eta();
00180 if(std::abs(eta) > m_max_eta ) continue;
00181
00182
00183 double pt = (*p)->momentum().perp();
00184 if(pt < m_min_pt) continue;
00185
00186 double px = (*p)->momentum().x();
00187 double py = (*p)->momentum().y();
00188 double pz = (*p)->momentum().z();
00189 double pe = (*p)->momentum().e();
00190
00191
00192
00193 fastjet::PseudoJet tempVec;
00194 tempVec = fastjet::PseudoJet(px,py,pz,pe);
00195 m_input_particles.push_back(tempVec);
00196 }
00197 return true;
00198 }
00199
00200
00201
00202
00203 int baseAnalysis::FindJet(HepMC::GenEvent* hepmcevt) {
00204
00205 if(!m_Jetfinder_enabled || !m_jetDef){
00206 std::cout << "baseAnalysis: FastJet Algorithm not initialized!" << std::endl;
00207 return false;
00208 }
00209
00210
00211 if(m_input_particles.empty()) {
00212 int input = FindJetableParticles(hepmcevt);
00213 if(!input)
00214 return false;
00215 }
00216
00217 if(m_input_particles.size())
00218 m_clust_seq = new fastjet::ClusterSequence(m_input_particles,*m_jetDef);
00219
00220 if(m_input_particles.empty() || m_clust_seq==0)
00221 return false;
00222
00223
00224 m_inclusive_jets= sorted_by_pt(m_clust_seq->inclusive_jets(m_jet_ptmin));
00225
00226 return true;
00227 }
00228
00229 vector<fastjet::PseudoJet> baseAnalysis::GetJet(HepMC::GenEvent* hepmcevt) {
00230
00231 if(m_inclusive_jets.empty()) {
00232 int inputJet = FindJet(hepmcevt);
00233
00234 }
00235 return m_inclusive_jets;
00236
00237 }
00238
00239
00240
00241
00242 int baseAnalysis::DeleteJetObject(HepMC::GenEvent* hepmcevt) {
00243
00244
00245
00246 m_input_particles.clear();
00247 m_inclusive_jets.clear();
00248 delete m_clust_seq;
00249 m_clust_seq=0;
00250
00251 return true;
00252 }
00253
00254
00255
00256
00257 int baseAnalysis::FindMissingEt(HepMC::GenEvent* hepmcevt) {
00258
00259 int properStatus = 1;
00260
00261 exMissTruth = 0.0;
00262 eyMissTruth = 0.0;
00263 etMissTruth = 0.0;
00264 etsumMissTruth = 0.0;
00265
00266
00267 for ( HepMC::GenEvent::particle_const_iterator p = hepmcevt->particles_begin(); p != hepmcevt->particles_end(); ++p ){
00268
00269 int pid = (*p)->pdg_id();
00270
00271
00272 if ( (*p)->status() != properStatus ) continue;
00273
00274
00275 if(fabs((*p)->momentum().eta()) > 2.5) continue;
00276
00277 if((*p)->momentum().perp() < 0.5) continue;
00278
00279 double px = (*p)->momentum().px();
00280 double py = (*p)->momentum().py();
00281 double pt = (*p)->momentum().perp();
00282
00283 if ( IsNeutrino(pid) || abs(pid)==1000039 || abs(pid) ==1000022 || abs(pid) ==39 || abs(pid) ==5100022 ) {
00284 exMissTruth += px;
00285 eyMissTruth += py;
00286 etsumMissTruth += pt;
00287 }
00288 }
00289
00290 etMissTruth = sqrt(exMissTruth*exMissTruth + eyMissTruth*eyMissTruth);
00291
00292 return true;
00293 }
00294
00295
00296
00297
00298 int baseAnalysis::ClearMissingEt(HepMC::GenEvent* hepmcevt) {
00299 exMissTruth = 0.0;
00300 eyMissTruth = 0.0;
00301 etMissTruth = 0.0;
00302 etsumMissTruth = 0.0;
00303
00304 return true;
00305 }
00306
00307
00308
00309
00310 int baseAnalysis::IsFinalStateParticle(HepMC::GenParticle *p) {
00311
00312
00313 if (!((*p).status() == 1)) return false;
00314
00315
00316 if ((*p).end_vertex()) return false;
00317
00318
00319 if(fabs((*p).momentum().eta()) > 2.5) return false;
00320
00321 if((*p).momentum().perp() < 0.5) return false;
00322
00323 return true;
00324 }
00325
00326
00327
00328
00329
00330 int baseAnalysis::ClearEvent(HepMC::GenEvent* hepmcevt) {
00331
00332 baseAnalysis::DeleteJetObject(hepmcevt);
00333 baseAnalysis::ClearMissingEt(hepmcevt);
00334
00335 return true;
00336 }
00337
00338
00339
00340
00341
00342 int baseAnalysis::Finalize()
00343 {
00344
00345 TFile f(m_outputFileName.c_str(),"RECREATE");
00346 Finalize(&f);
00347 f.Close();
00348
00349 return true;
00350 }
00351
00352
00353
00354
00355
00356 int baseAnalysis::Finalize(TFile* output)
00357 {
00358 TDirectory* dir = output->GetDirectory(m_outputRootDir.c_str());
00359 if (dir) {
00360 dir->cd();
00361 } else {
00362 std::cout << "The directory " << m_outputRootDir << " will be created" << std::endl;
00363 dir = output->mkdir(m_outputRootDir.c_str());
00364 dir->cd();
00365 }
00366
00367
00368 for ( vector< TH1D * >::const_iterator iter = m_histVector.begin(); iter!=m_histVector.end(); iter++ )
00369 (*iter)->Write();
00370
00371
00372
00373
00374
00375 return true;
00376 }
00377
00378
00379
00380
00381
00382 TH1D* baseAnalysis::popHisto() {
00383
00384
00385 if (m_histVector.empty()) return 0;
00386
00387
00388 TH1D* temp = m_histVector.back();
00389 std::cout<<"m_histVector.back()"<< temp;
00390
00391 m_histVector.pop_back();
00392 return temp;
00393 }
00394
00395
00396 const std::string intToString(const int i) {
00397 std::ostringstream s;
00398 s << i;
00399 return s.str();
00400 }
00401
00402
00403
00404
00405
00406
00407
00408 TH1D* baseAnalysis::initHist(string name, string title, string xlabel, int nrBins, double xmin, double xmax)
00409 {
00410 TDirectory* dir = gDirectory->GetDirectory(("/"+m_outputRootDir).c_str());
00411 if (dir) {
00412 dir->cd();
00413 } else {
00414 gDirectory->cd("/");
00415 dir = gDirectory->mkdir(m_outputRootDir.c_str());
00416 dir->cd();
00417 }
00418
00419
00420 TH1D* histo=new TH1D((m_outputRootDir+"_"+intToString(m_histCounter[m_outputRootDir]) + "_" + name).c_str(),title.c_str(),nrBins,xmin,xmax);
00421 histo->GetXaxis()->SetTitle(xlabel.c_str());
00422 histo->GetYaxis()->SetTitle("Count");
00423 baseAnalysis::m_histVector.push_back(histo);
00424
00425 ++m_histCounter[m_outputRootDir];
00426
00427 return histo;
00428 }
00429
00430
00431
00432
00433 TH1D* baseAnalysis::initHistVariableBin(string name, string title, string xlabel, int nbin, Double_t nbinRange[])
00434 {
00435 TDirectory* dir = gDirectory->GetDirectory(("/"+m_outputRootDir).c_str());
00436 if (dir) {
00437 dir->cd();
00438 } else {
00439 gDirectory->cd("/");
00440 dir = gDirectory->mkdir(m_outputRootDir.c_str());
00441 dir->cd();
00442 }
00443
00444 TH1D* histoVariableBin=new TH1D(name.c_str(),title.c_str(),nbin,nbinRange);
00445 histoVariableBin->GetXaxis()->SetTitle(xlabel.c_str());
00446 histoVariableBin->GetYaxis()->SetTitle("Count");
00447 baseAnalysis::m_histVector.push_back(histoVariableBin);
00448
00449 return histoVariableBin;
00450 }
00451
00452
00453
00454
00455
00456
00457 bool baseAnalysis::checkDaughter(HepMC::GenParticle* mother, HepMC::GenParticle* daughter, int maxGenerations)
00458 {
00459 bool found=false;
00460 int maxLoops=maxGenerations;
00461 int loop=1;
00462
00463
00464 if(maxGenerations<0) maxLoops=1000;
00465
00466
00467
00468 HepMC::GenVertex* current_vertex=daughter->production_vertex();
00469 HepMC::GenVertex::particle_iterator current_mother;
00470
00471
00472
00473
00474 while((current_vertex) && !(found) && (loop<maxLoops))
00475 {
00476 current_mother = current_vertex->particles_begin(HepMC::parents);
00477
00478 if((*current_mother)==mother)
00479 found=true;
00480 else
00481 current_vertex=(*current_mother)->production_vertex();
00482
00483 loop++;
00484 }
00485 return found;
00486 }
00487
00488
00489
00490 bool baseAnalysis::trackfromPID(int pid, HepMC::GenParticle* track, int maxGenerations)
00491 {
00492 bool found=false;
00493 int maxLoops=maxGenerations;
00494 int loop=1;
00495
00496
00497 if(maxGenerations<0) maxLoops=1000;
00498
00499
00500
00501 HepMC::GenVertex* current_vertex=track->production_vertex();
00502 HepMC::GenVertex::particle_iterator current_mother;
00503
00504
00505
00506
00507 while((current_vertex) && !(found) && (loop<maxLoops))
00508 {
00509 current_mother = current_vertex->particles_begin(HepMC::parents);
00510
00511
00512 if(*current_mother==0)break;
00513
00514 if((*current_mother)->pdg_id()==pid)
00515 found=true;
00516 else
00517 current_vertex=(*current_mother)->production_vertex();
00518
00519 loop++;
00520 }
00521 return found;
00522 }