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