00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "AtlfastAlgs/StandardNtupleMaker.h"
00015 #include "AtlfastAlgs/JetRecalibrator.h"
00016
00017 #include "AtlfastUtils/HeaderPrinter.h"
00018 #include "AtlfastUtils/HepMC_helper/IsStatusxx.h"
00019 #include "AtlfastUtils/HepMC_helper/All.h"
00020
00021 #include "AtlfastEvent/ReconstructedParticle.h"
00022 #include "AtlfastEvent/CollectionDefs.h"
00023 #include "AtlfastEvent/Jet.h"
00024 #include "AtlfastEvent/EventHeader.h"
00025
00026 #include "GaudiKernel/PropertyMgr.h"
00027 #include "GaudiKernel/INTupleSvc.h"
00028 #include "GaudiKernel/ISvcLocator.h"
00029 #include "GaudiKernel/IDataProviderSvc.h"
00030 #include "GaudiKernel/MsgStream.h"
00031 #include "GaudiKernel/NTuple.h"
00032 #include "GaudiKernel/SmartDataPtr.h"
00033
00034
00035 #include "GeneratorObjects/McEventCollection.h"
00036 #include "HepMC/GenEvent.h"
00037
00038
00039 #include "GaudiKernel/ObjectList.h"
00040
00041
00042
00043 #include <cmath>
00044 #include <pair.h>
00045
00050 namespace Atlfast {
00051
00052 StandardNtupleMaker::StandardNtupleMaker(const std::string& name, ISvcLocator* pSvcLocator)
00053 : Algorithm(name, pSvcLocator) {
00054
00055 m_atlfastEventLocation = "/Event/Atlfast";
00056 m_jetLocation = "/Event/AtlfastJets";
00057 m_jetBLocation = "/Event/AtlfastBJets";
00058 m_electronLocation = "/Event/AtlfastIsolatedElectrons";
00059 m_isolatedMuonLocation = "/Event/AtlfastIsolatedMuons";
00060 m_nonIsolatedMuonLocation = "/Event/AtlfastNonIsolatedMuons";
00061 m_photonLocation = "/Event/AtlfastIsolatedPhotons";
00062
00063 m_mcTruthLocation = "/Event/McEventCollection";
00064 m_bPhysicsLocation = "/Event/AtlfastBPhysics";
00065
00066 m_eventHeaderLocation = "/Event/AtlfastEventHeader";
00067
00068 declareProperty( "AtlfastEventLocation", m_atlfastEventLocation );
00069 declareProperty( "JetLocation", m_jetLocation );
00070 declareProperty( "JetBLocation", m_jetBLocation );
00071 declareProperty( "ElectronLocation", m_electronLocation );
00072 declareProperty( "IsolatedMuonLocation", m_isolatedMuonLocation );
00073 declareProperty( "NonIsolatedMuonLocation", m_nonIsolatedMuonLocation );
00074 declareProperty( "PhotonLocation", m_photonLocation );
00075
00076 declareProperty( "McTruthLocation", m_mcTruthLocation );
00077 declareProperty( "BPhysicsLocation", m_bPhysicsLocation );
00078
00079 declareProperty( "EventHeaderLocation", m_eventHeaderLocation );
00080
00081
00082 }
00083
00084 StandardNtupleMaker::~StandardNtupleMaker(){
00085 delete m_tesIO;
00086 delete m_jetCal;
00087 }
00088
00089
00090
00091 StatusCode StandardNtupleMaker::initialize() {
00092 MsgStream log(msgSvc(), name());
00093 log << MSG::DEBUG << "Initialising SNM" << endreq;
00094 StatusCode status;
00095
00096 m_nEvents = 0;
00097
00098 log << MSG::DEBUG << "Ntuple call 51" << endreq;
00099 NTuple::Directory* col = 0;
00100 log << MSG::DEBUG << "About to open ntuple file" << endreq;
00101 NTupleFilePtr file1(ntupleSvc(), "/NTUPLES/FILE1");
00102 if ( file1 ) {
00103 log << MSG::DEBUG << "Opened ntuple file" << endreq;
00104 if ( !(col=ntupleSvc()->createDirectory("/NTUPLES/FILE1/Atlfast")) ) {
00105 log << MSG::ERROR << "Cannot create directory /NTUPLES/FILE1/Atlfast"
00106 << endreq;
00107 return StatusCode::FAILURE;
00108 }else{
00109 log << MSG::DEBUG << "Created ntuple directory" << endreq;
00110 }
00111 }
00112 else {
00113 log << MSG::ERROR << "Cannot access file /NTUPLES/FILE1" << endreq;
00114 return StatusCode::FAILURE;
00115 }
00116
00117 NTuplePtr nt1(ntupleSvc(), "/NTUPLES/FILE1/Atlfast/51");
00118 if ( !nt1 ) {
00119 nt1 = ntupleSvc()->book (col, 51, CLID_ColumnWiseTuple, "Atlfast51");
00120 if ( nt1 ) {
00121 log << MSG::DEBUG << "booked ntuple " << endreq;
00122
00123
00124 status = nt1->addItem ("PLEPTONS/NELE" , m_nele, 0, 12 );
00125 status = nt1->addItem ("PLEPTONS/KFELE" , m_nele, m_codeele);
00126 status = nt1->addItem ("PLEPTONS/PXELE" , m_nele, m_pxele);
00127 status = nt1->addItem ("PLEPTONS/PYELE" , m_nele, m_pyele);
00128 status = nt1->addItem ("PLEPTONS/PZELE" , m_nele, m_pzele);
00129 status = nt1->addItem ("PLEPTONS/EEELE" , m_nele, m_eeele);
00130
00131 status = nt1->addItem ("PPHOTONS/NPHO" , m_npho, 0, 12 );
00132 status = nt1->addItem ("PPHOTONS/KFPHO" , m_npho, m_codepho);
00133 status = nt1->addItem ("PPHOTONS/PXPHO" , m_npho, m_pxpho);
00134 status = nt1->addItem ("PPHOTONS/PYPHO" , m_npho, m_pypho);
00135 status = nt1->addItem ("PPHOTONS/PZPHO" , m_npho, m_pzpho);
00136 status = nt1->addItem ("PPHOTONS/EEPHO" , m_npho, m_eepho);
00137
00138 status = nt1->addItem ("PLEPTONS/NMUO" , m_nmuo, 0, 12 );
00139 status = nt1->addItem ("PLEPTONS/KFMUO" , m_nmuo, m_codemuo);
00140 status = nt1->addItem ("PLEPTONS/PXMUO" , m_nmuo, m_pxmuo);
00141 status = nt1->addItem ("PLEPTONS/PYMUO" , m_nmuo, m_pymuo);
00142 status = nt1->addItem ("PLEPTONS/PZMUO" , m_nmuo, m_pzmuo);
00143 status = nt1->addItem ("PLEPTONS/EEMUO" , m_nmuo, m_eemuo);
00144
00145 status = nt1->addItem ("PMUXS/NMUX" , m_nmux, 0, 12 );
00146 status = nt1->addItem ("PMUXS/KFMUX" , m_nmux, m_codemux);
00147 status = nt1->addItem ("PMUXS/PXMUX" , m_nmux, m_pxmux);
00148 status = nt1->addItem ("PMUXS/PYMUX" , m_nmux, m_pymux);
00149 status = nt1->addItem ("PMUXS/PZMUX" , m_nmux, m_pzmux);
00150 status = nt1->addItem ("PMUXS/EEMUX" , m_nmux, m_eemux);
00151
00152 status = nt1->addItem ("PPJETS/NJET" , m_njet, 0, 40 );
00153 status = nt1->addItem ("PPJETS/KFJET" , m_njet, m_codejet);
00154 status = nt1->addItem ("PPJETS/PXJET" , m_njet, m_pxjet);
00155 status = nt1->addItem ("PPJETS/PYJET" , m_njet, m_pyjet);
00156 status = nt1->addItem ("PPJETS/PZJET" , m_njet, m_pzjet);
00157 status = nt1->addItem ("PPJETS/EEJET" , m_njet, m_eejet);
00158 status = nt1->addItem ("PPJETS/PTcalo" , m_njet, m_ptcalo);
00159 status = nt1->addItem ("PPJETS/PTbjet" , m_njet, m_ptbjet);
00160 status = nt1->addItem ("PPJETS/PTujet" , m_njet, m_ptujet);
00161
00162 status = nt1->addItem ("PPJETS/NJETB" , m_njetB, 0, 40 );
00163 status = nt1->addItem ("PPJETS/KFJETB" , m_njetB, m_codejetB);
00164 status = nt1->addItem ("PPJETS/PXJETB" , m_njetB, m_pxjetB);
00165 status = nt1->addItem ("PPJETS/PYJETB" , m_njetB, m_pyjetB);
00166 status = nt1->addItem ("PPJETS/PZJETB" , m_njetB, m_pzjetB);
00167 status = nt1->addItem ("PPJETS/EEJETB" , m_njetB, m_eejetB);
00168
00169
00170 status = nt1->addItem ("PHISTORY/NPART" , m_npart, 0, 40 );
00171 status = nt1->addItem ("PHISTORY/KPPART" , m_npart, m_kppart);
00172 status = nt1->addItem ("PHISTORY/KSPART" , m_npart, m_kspart);
00173 status = nt1->addItem ("PHISTORY/KFPART" , m_npart, m_kfpart);
00174 status = nt1->addItem ("PHISTORY/KPMOTH" , m_npart, m_kpmoth);
00175 status = nt1->addItem ("PHISTORY/KFMOTH" , m_npart, m_kfmoth);
00176 status = nt1->addItem ("PHISTORY/PXPART" , m_npart, m_pxpart);
00177 status = nt1->addItem ("PHISTORY/PYPART" , m_npart, m_pypart);
00178 status = nt1->addItem ("PHISTORY/PZPART" , m_npart, m_pzpart);
00179 status = nt1->addItem ("PHISTORY/EEPART" , m_npart, m_eepart);
00180
00181
00182
00183
00184
00185
00186
00187
00188 status = nt1->addItem ("INFO/ISUB" , m_isub);
00189 status = nt1->addItem ("INFO/JETB" , m_njetb);
00190 status = nt1->addItem ("INFO/JETC" , m_njetc);
00191 status = nt1->addItem ("INFO/JETTAU" , m_njettau);
00192 status = nt1->addItem ("PMISSING/PXMISS" , m_pxmiss);
00193 status = nt1->addItem ("PMISSING/PYMISS" , m_pymiss);
00194 status = nt1->addItem ("PMISSING/PXNUE" , m_pxnue);
00195 status = nt1->addItem ("PMISSING/PYNUE" , m_pynue);
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 }
00243 else {
00244 return StatusCode::FAILURE;
00245 }
00246 }
00247
00248 m_jetCal = new JetRecalibrator();
00249 log<<MSG::DEBUG<<"returning sith status"<< status<<endreq;
00250
00251 m_tesIO = new TesIO();
00252
00253 HeaderPrinter hp("Atlfast StandardNtuleMaker:", log);
00254 hp.add("MC location ", m_mcTruthLocation);
00255 hp.add("Electron Location ", m_electronLocation);
00256 hp.add("Photon Location ", m_photonLocation);
00257 hp.add("Isolated Muon Location ", m_isolatedMuonLocation);
00258 hp.add("Non-Isolated Muon Location ", m_nonIsolatedMuonLocation);
00259 hp.add("Jet Location ", m_jetLocation);
00260 hp.add("EventHeaderLocation ", m_eventHeaderLocation );
00261 hp.print();
00262
00263
00264
00265 return StatusCode::SUCCESS;
00266 }
00267
00268
00269 StatusCode StandardNtupleMaker::execute() {
00270
00271 MsgStream log(msgSvc(), name());
00272 log << MSG::DEBUG << "****Start execute()****" << endreq;
00273
00274
00275 if( m_nEvents % 50 == 0) {
00276 log << MSG::INFO << " processing event number "
00277 << m_nEvents << endreq;
00278 }
00279
00280 m_nEvents++;
00281
00282 StatusCode status;
00283
00284
00285
00286
00287 std::vector<ReconstructedParticle*> rp;
00288 if(!m_tesIO->copy<ReconstructedParticleCollection>(rp,m_electronLocation)){
00289 log <<MSG::DEBUG << "Could not find Electrons"<<endreq;
00290 }
00291 log << MSG::DEBUG << "processing Electrons: " <<rp.size()<< endreq;
00292
00293 std::vector<ReconstructedParticle*>::const_iterator irp = rp.begin();
00294
00295 m_nele = 0;
00296 for (; irp != rp.end(); ++irp) {
00297 m_codeele[m_nele] = (*irp)->pdg_id();
00298 m_pxele[m_nele] = (*irp)->momentum().px();
00299 m_pyele[m_nele] = (*irp)->momentum().py();
00300 m_pzele[m_nele] = (*irp)->momentum().pz();
00301 m_eeele[m_nele] = (*irp)->momentum().e();
00302 if (++m_nele == m_nele->range().distance() ) {
00303 log << MSG::WARNING << "Electron list truncated in Ntuple" << endreq;
00304 break;
00305 }
00306 }
00307
00308
00309
00310
00311
00312 rp.clear();
00313 if(!m_tesIO->copy <ReconstructedParticleCollection>(rp, m_photonLocation)){
00314 log <<MSG::DEBUG << "Could not find Photons"<<endreq;
00315 }
00316 log << MSG::DEBUG << "processing Photons: " <<rp.size()<< endreq;
00317
00318 irp = rp.begin();
00319
00320 m_npho = 0;
00321 for (; irp != rp.end(); ++irp) {
00322 m_codepho[m_npho] = (*irp)->pdg_id();
00323 m_pxpho[m_npho] = (*irp)->momentum().px();
00324 m_pypho[m_npho] = (*irp)->momentum().py();
00325 m_pzpho[m_npho] = (*irp)->momentum().pz();
00326 m_eepho[m_npho] = (*irp)->momentum().e();
00327 if (++m_npho == m_npho->range().distance() ) {
00328 log << MSG::WARNING << "Photon list truncated in Ntuple" << endreq;
00329 break;
00330 }
00331 }
00332
00333
00334
00335
00336
00337 rp.clear();
00338 if(!m_tesIO->copy
00339 <ReconstructedParticleCollection>(rp, m_isolatedMuonLocation)){
00340 log <<MSG::DEBUG << "Could not find Isolated Muons"<<endreq;
00341 }
00342 log << MSG::DEBUG << "Isolated Muons: " <<rp.size()<< endreq;
00343
00344 irp = rp.begin();
00345
00346 m_nmuo = 0;
00347
00348 for (; irp != rp.end(); ++irp) {
00349 m_codemuo[m_nmuo] = (*irp)->pdg_id();
00350 m_pxmuo[m_nmuo] = (*irp)->momentum().px();
00351 m_pymuo[m_nmuo] = (*irp)->momentum().py();
00352 m_pzmuo[m_nmuo] = (*irp)->momentum().pz();
00353 m_eemuo[m_nmuo] = (*irp)->momentum().e();
00354 if (++m_nmuo == m_nmuo->range().distance() ) {
00355 log << MSG::WARNING
00356 << "Isolated Muon list truncated in Ntuple" << endreq;
00357 break;
00358 }
00359 }
00360
00361
00362
00363
00364
00365 rp.clear();
00366 if(!m_tesIO->copy
00367 <ReconstructedParticleCollection >(rp, m_nonIsolatedMuonLocation)){
00368 log <<MSG::DEBUG << "Could not find non Isolated muons"<<endreq;
00369 }
00370 log << MSG::DEBUG << "NonIsolated Muons: " <<rp.size()<< endreq;
00371 irp = rp.begin();
00372
00373 m_nmux = 0;
00374
00375 for (; irp != rp.end(); ++irp) {
00376 m_codemux[m_nmux] = (*irp)->pdg_id();
00377 m_pxmux[m_nmux] = (*irp)->momentum().px();
00378 m_pymux[m_nmux] = (*irp)->momentum().py();
00379 m_pzmux[m_nmux] = (*irp)->momentum().pz();
00380 m_eemux[m_nmux] = (*irp)->momentum().e();
00381 if (++m_nmux == m_nmux->range().distance() ) {
00382 log << MSG::WARNING
00383 << "Non-isolated Muon list truncated in Ntuple" << endreq;
00384 break;
00385 }
00386 }
00387
00388
00389
00390
00391
00392 std::vector<Jet*> jv;
00393 if(!m_tesIO->copy<JetCollection >(jv, m_jetLocation)){
00394 log <<MSG::DEBUG << "Could not find Jets"<<endreq;
00395 }
00396 log << MSG::DEBUG << "Processing Jets: " <<jv.size()<< endreq;
00397
00398 std::vector<Jet*>::const_iterator ijv = jv.begin();
00399
00400 m_njet = 0;
00401
00402 for (; ijv != jv.end(); ++ijv) {
00403 m_codejet[m_njet]= (*ijv)->pdg_id();
00404 m_pxjet[m_njet] = (*ijv)->px();
00405 m_pyjet[m_njet] = (*ijv)->py();
00406 m_pzjet[m_njet] = (*ijv)->pz();
00407 m_eejet[m_njet] = (*ijv)->e();
00408 m_ptcalo[m_njet] = (*ijv)->pT();
00409 m_ptbjet[m_njet] = m_jetCal->bCalibrate( (*ijv)->pT() );
00410 m_ptujet[m_njet] = m_jetCal->uCalibrate( (*ijv)->pT() );
00411 if ( ++m_njet == m_njet->range().distance() ) break;
00412 }
00413 log << MSG::DEBUG << " Jets in ntuple" << endreq;
00414
00415
00416
00417
00418
00419
00420 std::vector<Jet*> jvb;
00421 if(!m_tesIO->copy<JetCollection >(jvb, m_jetBLocation)){
00422 log <<MSG::DEBUG << "Could not find AtlfastB Jets"<<endreq;
00423 }
00424 log << MSG::DEBUG << "Processing AtlfastB Jets: " <<jvb.size()<< endreq;
00425
00426 ijv = jvb.begin();
00427
00428 m_njetB = 0;
00429
00430 for (; ijv != jvb.end(); ++ijv) {
00431 m_codejetB[m_njetB]= (*ijv)->pdg_id();
00432 m_pxjetB[m_njetB] = (*ijv)->px();
00433 m_pyjetB[m_njetB] = (*ijv)->py();
00434 m_pzjetB[m_njetB] = (*ijv)->pz();
00435 m_eejetB[m_njetB] = (*ijv)->e();
00436 if ( ++m_njetB == m_njetB->range().distance() ) break;
00437 }
00438 log << MSG::DEBUG << " AtlfastB Jets in ntuple" << endreq;
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 MCparticleCollection mc;
00451
00452
00453 HepMC_helper::All* all= new HepMC_helper::All;
00454 TesIoStat stat = m_tesIO->getMC(mc, all);
00455 delete all;
00456
00457 if(stat){
00458 log << MSG::DEBUG << " found MC truth" << endreq;
00459 }else{
00460 log << MSG::ERROR << " MC truth not found in TES" << endreq;
00461 }
00462
00463 MCparticleCollection::const_iterator imc = mc.begin();
00464 m_npart=0;
00465
00466 HepMC_helper::IsStatusxx isf(3);
00467
00468 for(;imc != mc.end(); ++imc){
00469 if(isf(*imc)){
00470 m_kppart[m_npart] = (*imc)->barcode();
00471 m_kspart[m_npart] = (*imc)->status()%100;
00472 m_kfpart[m_npart] = (*imc)->pdg_id();
00473 std::pair<int, int> parentCodes = getParentCodes(imc);
00474 m_kfmoth[m_npart] = parentCodes.first;
00475 m_kpmoth[m_npart] = parentCodes.second;
00476
00477 m_pxpart[m_npart] = (*imc)->momentum().x();
00478 m_pypart[m_npart] = (*imc)->momentum().y();
00479 m_pzpart[m_npart] = (*imc)->momentum().z();
00480 m_eepart[m_npart] = (*imc)->momentum().e();
00481 ++m_npart;
00482 }
00483
00484 if ( m_npart == m_npart->range().distance() ) break;
00485
00486
00487
00488
00489
00490
00491 }
00492
00493 log << MSG::DEBUG << " Particles in ntuple" << endreq;
00494
00495 const DataHandle<EventHeader> theEventHeader;
00496 if(!(m_tesIO->getDH(theEventHeader))){
00497 log << MSG::ERROR<<"Could not find the event header in the TES"<<endreq;
00498 }
00499
00500 if(theEventHeader.isValid()){
00501 m_isub = 0;
00502 m_njetb = theEventHeader->nBJets();
00503 m_njetc = theEventHeader->nCJets();
00504 m_njettau= theEventHeader->nTauJets();
00505 m_pxmiss = theEventHeader->pMiss().x();
00506 m_pymiss = theEventHeader->pMiss().y();
00507 m_pxnue = theEventHeader->pEscaped().x();
00508 m_pynue = theEventHeader->pEscaped().y();
00509 } else {
00510 log << MSG::ERROR << " Invalid EventHeader data handle" << endreq;
00511 }
00512
00513 log << MSG::DEBUG << " EventHeader quantities are in ntuple" << endreq;
00514
00515
00516
00517 log << MSG::DEBUG << "Writing ntuple part 51" << endreq;
00518 status = ntupleSvc()->writeRecord("/NTUPLES/FILE1/Atlfast/51");
00519 if ( !status.isSuccess() ) log <<MSG::ERROR
00520 <<"Cannot fill id 51"
00521 <<endreq;
00522 log << MSG::DEBUG << "Done all the ntuple writing" << endreq;
00523 return StatusCode::SUCCESS;
00524 }
00525 std::pair<int, int>
00526 StandardNtupleMaker::getParentCodes(MCparticleCollection::const_iterator&
00527 imc){
00528
00529 HepMC::GenVertex* gv = (*imc)->production_vertex();
00530 if ( gv == 0 ){ return std::pair<int, int>(0,0);}
00531
00532 if ( gv->particles_in_size() == 0 ) { return std::pair<int, int>(0,0); }
00533
00534 HepMC::GenVertex::particle_iterator
00535 pi = gv->particles_begin(HepMC::parents);
00536 if( *pi == 0 ){ return std::pair<int, int>(0,0); }
00537
00538
00539 return std::pair<int, int>( (*pi)->pdg_id(), (*pi)->barcode() );
00540 }
00541
00542
00543
00544 StatusCode StandardNtupleMaker::finalize() {
00545
00546 MsgStream log(msgSvc(), name());
00547 log << MSG::INFO << " Number of events processed = " << m_nEvents << endreq;
00548
00549 return StatusCode::SUCCESS;
00550 }
00551
00552
00553 }
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566