00001
00002 #include <iostream>
00003 #include <sstream>
00004 #include <cmath>
00005 #include <stdio.h>
00006
00007 #include "HepMC/GenEvent.h"
00008 #include "HepMC/IO_GenEvent.h"
00009 #include "HepMC/GenParticle.h"
00010 #include "HepMC/GenVertex.h"
00011 #include "HepMC/IO_AsciiParticles.h"
00012 #include "HepMC/SimpleVector.h"
00013 #include "HepPDT/ParticleData.hh"
00014 #include "CLHEP/Vector/LorentzVector.h"
00015 #include "fastjet/ClusterSequence.hh"
00016 #include "fastjet/SISConePlugin.hh"
00017
00018
00019 #include "TH1.h"
00020 #include "TH2.h"
00021 #include "TFile.h"
00022 #include "TMath.h"
00023 #include "TLorentzVector.h"
00024
00025 #include "../include/baseAnalysis.h"
00026
00027
00028 #include <cassert>
00029
00030 using namespace std;
00031
00032
00033
00034
00035 baseAnalysis::baseAnalysis()
00036 {
00037 m_plugin = 0;
00038 m_jetDef = 0;
00039 m_clust_seq = 0;
00040
00041
00042 m_coneRadius = 0.;
00043 m_overlapThreshold = 0.;
00044 m_jet_ptmin = 0.;
00045 m_lepton_ptmin = 0.;
00046 m_DeltaR_lepton_track = 0.;
00047 m_Jetfinder_enabled = false;
00048
00049
00050 m_max_eta = 0.;
00051 m_min_pt = 0.;
00052
00053
00054 exMissTruth = 0.;
00055 eyMissTruth = 0.;
00056 etMissTruth = 0.;
00057 etsumMissTruth = 0.;
00058
00059 m_NanNum = 0;
00060 }
00061
00062
00063
00064
00065 baseAnalysis::~baseAnalysis()
00066 {
00067 cout << "baseAnalysis: Total number of particles with NaN (Not a Number) params: " << m_NanNum << endl;
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 }
00101
00102
00103 int baseAnalysis::Init( double , double )
00104 {
00105
00106 cout << "baseAnalysis: WARNING: You have called the dummy function Init()." << endl;
00107 return 0;
00108 }
00109
00110
00111 int baseAnalysis::Process( HepMC::GenEvent* )
00112 {
00113
00114 cout << "baseAnalysis: WARNING: You have called the dummy function Process()." << endl;
00115 return 0;
00116 }
00117
00118
00119 vector< TH1D* > baseAnalysis::averagedHistograms()
00120 {
00121 return m_histVector;
00122 }
00123
00124
00125 vector<TH1D*>& baseAnalysis::Histograms()
00126 {
00127 return m_histVector;
00128 }
00129
00130
00131
00132
00133 int baseAnalysis::InitJetFinder( double coneRadius, double overlapThreshold, double jet_ptmin,
00134 double lepton_ptmin, double DeltaR_lepton_track, int jetalgorithm)
00135 {
00136
00137
00138 if(m_Jetfinder_enabled){
00139
00140 if(m_jetDef) {
00141 delete m_jetDef;
00142 m_jetDef=0;
00143 }
00144
00145 if(m_plugin){
00146 delete m_plugin;
00147 m_plugin=0;
00148 }
00149 }
00150
00151
00152 m_coneRadius = coneRadius;
00153 m_overlapThreshold = overlapThreshold;
00154 m_jet_ptmin = jet_ptmin;
00155 m_lepton_ptmin = lepton_ptmin;
00156 m_DeltaR_lepton_track = DeltaR_lepton_track;
00157
00158 if( jetalgorithm == 1 ) {
00159 m_plugin = new fastjet::SISConePlugin(m_coneRadius, m_overlapThreshold);
00160 m_jetDef = new fastjet::JetDefinition(m_plugin);
00161 } else if ( jetalgorithm == 2 ) {
00162 m_plugin = 0;
00163 m_jetDef = new fastjet::JetDefinition( fastjet::antikt_algorithm, m_coneRadius );
00164
00165 cout << "baseAnalysis: jet definition: " << endl;
00166 cout << m_jetDef->description() << endl;
00167
00168 } else {
00169 m_jetDef = 0;
00170 }
00171
00172
00173
00174
00175 if(!m_jetDef) return false;
00176
00177 m_Jetfinder_enabled=true;
00178 return true;
00179
00180 }
00181
00182
00183
00184
00185 int baseAnalysis::FindJetableParticles( HepMC::GenEvent* hepmcevt )
00186 {
00187
00188
00189 if( !m_Jetfinder_enabled || !m_jetDef ) {
00190 cerr << "baseAnalysis: FastJet Algorithm not initialized!" << endl;
00191 return false;
00192 }
00193
00194 CLHEP::HepLorentzVector lv_leadingJet(0,0,0,0);
00195
00196 for ( HepMC::GenEvent::particle_const_iterator p = hepmcevt->particles_begin();
00197 p != hepmcevt->particles_end(); ++p ){
00198
00199
00200
00201 int pid = (*p)->pdg_id();
00202
00203
00204 if ( !( ( (*p)->status() % 1000 ) == 1 ) ) continue;
00205 if ( (*p)->end_vertex() ) continue;
00206
00207
00208
00209 if( IsNeutrino( pid ) ||
00210 IsNeutron( pid ) ) continue;
00211
00212
00213
00214 double deltaPhi = 0.0;
00215 double deltaEta = 0.0;
00216 double DeltaR = 9999.0;
00217
00218
00219 double DeltaRCut = m_DeltaR_lepton_track;
00220 double LeptonpTCut = m_lepton_ptmin;
00221
00222 if(IsElectron(pid) || IsMuon(pid)) {
00223 int lepton_barcode = (*p)->barcode();
00224 HepMC::GenParticle *lepton = hepmcevt->barcode_to_particle(lepton_barcode);
00225
00226
00227 if(fabs((*lepton).momentum().perp()) > LeptonpTCut) continue;
00228
00229
00230 for ( HepMC::GenEvent::particle_const_iterator q = hepmcevt->particles_begin();
00231 q != hepmcevt->particles_end(); ++q ){
00232 if(lepton_barcode==(*q)->barcode()) continue;
00233
00234
00235 deltaPhi = (*p)->momentum().phi()-(*q)->momentum().phi();
00236
00237 if( deltaPhi > TMath::Pi() || deltaPhi == TMath::Pi() )
00238 {
00239 deltaPhi = deltaPhi - 2 * TMath::Pi() ;
00240 }
00241
00242 if( deltaPhi < - TMath::Pi() || deltaPhi == - TMath::Pi() )
00243 {
00244 deltaPhi = deltaPhi + 2 * TMath::Pi();
00245 }
00246
00247 deltaEta = (*p)->momentum().eta()-(*q)->momentum().eta();
00248 double DeltaRnew = sqrt( pow(deltaPhi,2) + pow(deltaEta,2));
00249
00250 if(DeltaRnew < DeltaR) DeltaR = DeltaRnew;
00251 }
00252 if(DeltaR > DeltaRCut) continue;
00253 }
00254
00255
00256
00257
00258
00259 if (!(*p)->production_vertex()) continue;
00260
00261 if ((*p)->end_vertex()) continue;
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 double eta = (*p)->momentum().eta();
00272 if(std::abs(eta) > m_max_eta ) continue;
00273
00274
00275 double pt = (*p)->momentum().perp();
00276 if(pt < m_min_pt) continue;
00277
00278 double px = (*p)->momentum().x();
00279 double py = (*p)->momentum().y();
00280 double pz = (*p)->momentum().z();
00281 double pe = (*p)->momentum().e();
00282
00283
00284 if ( isnan( px ) || isnan( py ) || isnan( pz ) || isnan( pe ) ||
00285 isinf( px ) || isinf( py ) || isinf( pz ) || isinf( pe ) ) {
00286 cout << "baseAnalysis: NaN (Not A Number) found in the event record! Particle will be skipped." << endl;
00287 ++m_NanNum;
00288 continue;
00289 }
00290
00291
00292 fastjet::PseudoJet tempVec;
00293 tempVec = fastjet::PseudoJet(px,py,pz,pe);
00294 m_input_particles.push_back(tempVec);
00295 }
00296 return true;
00297 }
00298
00299
00300 int baseAnalysis::FindJet(HepMC::GenEvent* hepmcevt)
00301 {
00302 if (!m_Jetfinder_enabled || !m_jetDef) {
00303 cerr << "baseAnalysis: FastJet Algorithm not initialized!" << endl;
00304 return false;
00305 }
00306
00307
00308 if (m_input_particles.empty()) {
00309 if ( !FindJetableParticles(hepmcevt) ) {
00310 return false;
00311 }
00312 }
00313
00314 if ( !m_input_particles.empty() ) {
00315 m_clust_seq = new fastjet::ClusterSequence( m_input_particles, *m_jetDef );
00316 } else {
00317 m_clust_seq = 0;
00318 }
00319
00320 if (m_input_particles.empty() || m_clust_seq==0) {
00321 return false;
00322 }
00323
00324
00325 m_inclusive_jets = sorted_by_pt( m_clust_seq->inclusive_jets( m_jet_ptmin ) );
00326
00327 return true;
00328 }
00329
00330
00331 vector< fastjet::PseudoJet > baseAnalysis::GetJet( HepMC::GenEvent *hepMcEvt )
00332 {
00333 if( m_inclusive_jets.empty() ) {
00334 FindJet( hepMcEvt );
00335 }
00336
00337 return m_inclusive_jets;
00338 }
00339
00340
00341 void baseAnalysis::SetJet( vector< fastjet::PseudoJet > *inclusive_jets )
00342 {
00343 m_inclusive_jets = *inclusive_jets;
00344 }
00345
00346
00347
00348
00349 int baseAnalysis::DeleteJetObject( HepMC::GenEvent* )
00350 {
00351 m_input_particles.clear();
00352 m_inclusive_jets.clear();
00353
00354 delete m_clust_seq;
00355 m_clust_seq = 0;
00356
00357 return true;
00358 }
00359
00360
00361
00362
00363 int baseAnalysis::FindMissingEt(HepMC::GenEvent* hepmcevt)
00364 {
00365 int properStatus = 1;
00366
00367 exMissTruth = 0.0;
00368 eyMissTruth = 0.0;
00369 etMissTruth = 0.0;
00370 etsumMissTruth = 0.0;
00371
00372
00373 for ( HepMC::GenEvent::particle_const_iterator p = hepmcevt->particles_begin(); p != hepmcevt->particles_end(); ++p ) {
00374 int pid = (*p)->pdg_id();
00375
00376
00377 if ( (*p)->status()%1000 != properStatus ) continue;
00378 if ( (*p)->end_vertex() ) continue;
00379
00380
00381 if(fabs((*p)->momentum().eta()) > 2.5) continue;
00382
00383 if((*p)->momentum().perp() < 0.5) continue;
00384
00385 double px = (*p)->momentum().px();
00386 double py = (*p)->momentum().py();
00387 double pt = (*p)->momentum().perp();
00388
00389 if ( IsNeutrino(pid) || abs(pid)==1000039 || abs(pid) ==1000022 || abs(pid) ==39 || abs(pid) ==5100022 ) {
00390 exMissTruth += px;
00391 eyMissTruth += py;
00392 etsumMissTruth += pt;
00393 }
00394 }
00395
00396 etMissTruth = sqrt(exMissTruth*exMissTruth + eyMissTruth*eyMissTruth);
00397
00398 return true;
00399 }
00400
00401
00402
00403
00404 int baseAnalysis::ClearMissingEt( HepMC::GenEvent* )
00405 {
00406 exMissTruth = 0.0;
00407 eyMissTruth = 0.0;
00408 etMissTruth = 0.0;
00409 etsumMissTruth = 0.0;
00410
00411 return true;
00412 }
00413
00414
00415
00416
00417 int baseAnalysis::IsFinalStateParticle(HepMC::GenParticle *p)
00418 {
00419
00420 if ( !( (*p).status()%1000 == 1 ) ) return false;
00421
00422
00423 if ( (*p).end_vertex() ) return false;
00424
00425
00426 if( fabs((*p).momentum().eta()) > 2.5 ) return false;
00427
00428
00429 if( (*p).momentum().perp() < 0.5 ) return false;
00430
00431 return true;
00432 }
00433
00434
00435
00436
00437 int baseAnalysis::ClearEvent( HepMC::GenEvent* hepMcEvt )
00438 {
00439 baseAnalysis::DeleteJetObject( hepMcEvt );
00440 baseAnalysis::ClearMissingEt( hepMcEvt );
00441
00442 return true;
00443 }
00444
00445
00446 int baseAnalysis::setOutputFileName( const string &fileName )
00447 {
00448 m_outputFileName = fileName;
00449 return 0;
00450 }
00451
00452
00453 int baseAnalysis::setOutputRootDir( const string &dirName )
00454 {
00455 m_outputRootDir = dirName;
00456 return 0;
00457 }
00458
00459
00460 bool baseAnalysis::IsNeutrino( int pid )
00461 {
00462 if( abs( pid ) == 12 || abs( pid ) == 14 || abs( pid ) == 16 ) {
00463 return true;
00464 }
00465 return false;
00466 }
00467
00468
00469 bool baseAnalysis::IsGamma(int pid)
00470 {
00471 if( abs( pid ) == 22 ) {
00472 return true;
00473 }
00474 return false;
00475 }
00476
00477
00478 bool baseAnalysis::IsNeutron(int pid)
00479 {
00480 if( abs(pid)==2112 ) return true;
00481 else return false;
00482 }
00483
00484
00485 bool baseAnalysis::IsK0(int pid)
00486 {
00487 if( abs(pid)==130 || abs(pid)==310 || abs(pid)==311 ) return true;
00488 else return false;
00489 }
00490
00491
00492 bool baseAnalysis::IsPi0(int pid)
00493 {
00494 if( abs(pid)==111 || abs(pid)==113 || abs(pid)==221 ) return true;
00495 else return false;
00496 }
00497
00498
00499 bool baseAnalysis::IsElectron(int pid)
00500 {
00501 if( abs(pid)==11 ) return true;
00502 else return false;
00503 }
00504
00505
00506 bool baseAnalysis::IsMuon(int pid)
00507 {
00508 if( abs(pid)==13 ) return true;
00509 else return false;
00510 }
00511
00512
00513 bool baseAnalysis::IsGluon(int pid)
00514 {
00515 if( abs(pid)==21 ) return true;
00516 else return false;
00517 }
00518
00519
00520 bool baseAnalysis::chargedParticle(int pid)
00521 {
00522 if( IsNeutrino(pid) || IsGamma(pid) || IsNeutron(pid) || IsK0(pid) || IsPi0(pid) || IsGluon(pid) ||
00523 abs(pid)== 94 || abs(pid)==1000039 || abs(pid) ==1000022 || abs(pid) ==39 || abs(pid) ==5100022 ) {
00524
00525 return false;
00526 }
00527
00528 return true;
00529 }
00530
00531
00532 int baseAnalysis::setMaxEtaCut(double maxeta)
00533 {
00534 m_max_eta = maxeta;
00535 return 0;
00536 }
00537
00538
00539 int baseAnalysis::setMinPtCut(double minpt)
00540 {
00541 m_min_pt = minpt;
00542 return 0;
00543 }
00544
00545
00546
00547
00548
00549 int baseAnalysis::Finalize()
00550 {
00551
00552
00553
00554
00555
00556 return true;
00557 }
00558
00559
00560
00561
00562
00563 int baseAnalysis::Finalize(TFile* output)
00564 {
00565 TDirectory* dir = output->GetDirectory(m_outputRootDir.c_str());
00566 if (dir) {
00567 dir->cd();
00568 } else {
00569 cout << "The directory " << m_outputRootDir << " will be created" << endl;
00570 dir = output->mkdir(m_outputRootDir.c_str());
00571 dir->cd();
00572 }
00573
00574
00575 for ( vector< TH1D * >::const_iterator iter = m_histVector.begin(); iter!=m_histVector.end(); iter++ )
00576 (*iter)->Write();
00577
00578
00579
00580
00581
00582 return true;
00583 }
00584
00585
00586
00587
00588
00589 TH1D* baseAnalysis::popHisto()
00590 {
00591
00592 if ( m_histVector.empty() ) return 0;
00593
00594
00595 TH1D* temp = m_histVector.back();
00596
00597
00598 m_histVector.pop_back();
00599 return temp;
00600 }
00601
00602
00603
00604
00605
00606
00607
00608 TH1D* baseAnalysis::initHist(const string &name, const string &title, const string &xlabel,
00609 int nrBins, double xmin, double xmax)
00610 {
00611 TDirectory* dir = gDirectory->GetDirectory(("/"+m_outputRootDir).c_str());
00612 if (dir) {
00613 dir->cd();
00614 } else {
00615 gDirectory->cd("/");
00616 dir = gDirectory->mkdir(m_outputRootDir.c_str());
00617 dir->cd();
00618 }
00619
00620
00621
00622 int histCnt = 0;
00623
00624 ostringstream ssHistCnt;
00625 ssHistCnt << histCnt;
00626
00627 TH1D* histo = new TH1D((m_outputRootDir + "_" + ssHistCnt.str() + "_" + name).c_str(), title.c_str(),
00628 nrBins, xmin, xmax);
00629
00630
00631
00632 histo->GetXaxis()->SetTitle(xlabel.c_str());
00633 histo->GetYaxis()->SetTitle("Count");
00634 m_histVector.push_back(histo);
00635
00636
00637
00638 return histo;
00639 }
00640
00641
00642
00643
00644 TH1D* baseAnalysis::initHistVariableBin(const string &name, const string &title, const string &xlabel,
00645 int nbin, Double_t nbinRange[])
00646 {
00647 TDirectory* dir = gDirectory->GetDirectory( ("/"+m_outputRootDir).c_str() );
00648 if (dir) {
00649 dir->cd();
00650 } else {
00651 gDirectory->cd("/");
00652 dir = gDirectory->mkdir(m_outputRootDir.c_str());
00653 dir->cd();
00654 }
00655
00656 TH1D* histoVariableBin = new TH1D(name.c_str(), title.c_str(), nbin, nbinRange);
00657 histoVariableBin->GetXaxis()->SetTitle(xlabel.c_str());
00658 histoVariableBin->GetYaxis()->SetTitle("Count");
00659 m_histVector.push_back(histoVariableBin);
00660
00661 return histoVariableBin;
00662 }
00663
00664
00665
00666
00667
00668
00669 bool baseAnalysis::checkDaughter(HepMC::GenParticle* mother, HepMC::GenParticle* daughter, int maxGenerations)
00670 {
00671
00672 const size_t maxLoops = ((maxGenerations < 0) ? 1000 : maxGenerations);
00673
00674
00675
00676 HepMC::GenVertex* current_vertex = daughter->production_vertex();
00677 HepMC::GenVertex::particle_iterator current_mother;
00678
00679 bool found = false;
00680 size_t loopCnt = 1;
00681
00682
00683
00684
00685 while ((current_vertex) && !(found) && (loopCnt < maxLoops)) {
00686 current_mother = current_vertex->particles_begin(HepMC::parents);
00687
00688 if ((*current_mother) == mother) {
00689 found = true;
00690 } else {
00691 current_vertex = (*current_mother)->production_vertex();
00692 }
00693
00694 ++loopCnt;
00695 }
00696 return found;
00697 }
00698
00699
00700
00701
00702 bool baseAnalysis::trackfromPID(int pid, HepMC::GenParticle* track, int maxGenerations)
00703 {
00704
00705 const size_t maxLoops = ((maxGenerations < 0) ? 1000 : maxGenerations);
00706
00707
00708
00709 HepMC::GenVertex* current_vertex=track->production_vertex();
00710 HepMC::GenVertex::particle_iterator current_mother;
00711
00712 bool found = false;
00713 size_t loopCnt = 1;
00714
00715
00716
00717
00718 while ((current_vertex) && !(found) && (loopCnt < maxLoops)) {
00719 current_mother = current_vertex->particles_begin(HepMC::parents);
00720
00721
00722 if (*current_mother == 0) break;
00723
00724 if ((*current_mother)->pdg_id() == pid) {
00725 found = true;
00726 } else {
00727 current_vertex = (*current_mother)->production_vertex();
00728 }
00729
00730 ++loopCnt;
00731 }
00732
00733 return found;
00734 }
00735
00736
00737
00738
00739 double baseAnalysis::getRapidity( const HepMC::GenEvent::particle_const_iterator &p )
00740 {
00741 double e = (*p)->momentum().e();
00742 double pz = (*p)->momentum().pz();
00743 double rapidity = 0.5 * log((e + pz) / (e - pz));
00744 return rapidity;
00745 }
00746