00001 #include "AtlfastAlgs/CBNT_Atlfast.h"
00002 #include "AtlfastAlgs/StandardNtupleMaker.h"
00003
00004 #include "AtlfastUtils/HeaderPrinter.h"
00005 #include "AtlfastUtils/HepMC_helper/IsStatusxx.h"
00006 #include "AtlfastUtils/HepMC_helper/All.h"
00007
00008 #include "AtlfastEvent/ReconstructedParticle.h"
00009 #include "AtlfastEvent/CollectionDefs.h"
00010 #include "AtlfastEvent/Jet.h"
00011 #include "AtlfastEvent/EventHeader.h"
00012 #include "AtlfastEvent/MCweightContainerCollection.h"
00013 #include "AtlfastAlgs/GlobalEventData.h"
00014
00015
00016 #include "GeneratorObjects/McEventCollection.h"
00017 #include "HepMC/GenEvent.h"
00018
00019
00020
00021 #include <cmath>
00022
00023 #include "GaudiKernel/MsgStream.h"
00024 #include "GaudiKernel/ISvcLocator.h"
00025
00026 #include "GaudiKernel/SmartDataPtr.h"
00027 #include "GaudiKernel/SmartDataLocator.h"
00028 #include "GaudiKernel/IDataProviderSvc.h"
00029
00030
00031 #include "GaudiKernel/PropertyMgr.h"
00032
00033 #include "GaudiKernel/INTupleSvc.h"
00034 #include "GaudiKernel/NTuple.h"
00035
00036
00037 #include <string>
00038
00039 namespace Atlfast{
00040
00041
00042 CBNT_Atlfast::CBNT_Atlfast(const std::string& name,
00043 ISvcLocator* pSvcLocator) :
00044 Algorithm(name, pSvcLocator),
00045 m_NtupleLocID("/FILE1/CBNT/3333"),
00046 m_ntPtr(0),
00047 m_tesIO(0)
00048 {
00049 m_atlfastEventLocation = "/Event/Atlfast";
00050 m_jetLocation = "/Event/AtlfastJets";
00051 m_electronLocation = "/Event/AtlfastIsolatedElectrons";
00052 m_isolatedMuonLocation = "/Event/AtlfastIsolatedMuons";
00053 m_nonIsolatedMuonLocation = "/Event/AtlfastNonIsolatedMuons";
00054 m_photonLocation = "/Event/AtlfastIsolatedPhotons";
00055 m_mcTruthLocation = "/Event/McEventCollection";
00056 m_bPhysicsLocation = "/Event/AtlfastBPhysics";
00057 m_eventHeaderLocation = "/Event/AtlfastEventHeader";
00058
00059 m_fillIsolatedElectrons = true;
00060 m_fillIsolatedPhotons = true;
00061 m_fillIsolatedMuons = true;
00062 m_fillNonIsolatedMuons = true;
00063 m_fillJets = true;
00064 m_fillHistory = false;
00065 m_fillEventData = true;
00066
00067 declareProperty( "AtlfastEventLocation", m_atlfastEventLocation );
00068 declareProperty( "JetLocation", m_jetLocation );
00069 declareProperty( "ElectronLocation", m_electronLocation );
00070 declareProperty( "IsolatedMuonLocation", m_isolatedMuonLocation );
00071 declareProperty( "NonIsolatedMuonLocation", m_nonIsolatedMuonLocation );
00072 declareProperty( "PhotonLocation", m_photonLocation );
00073 declareProperty( "McTruthLocation", m_mcTruthLocation );
00074 declareProperty( "BPhysicsLocation", m_bPhysicsLocation );
00075 declareProperty( "EventHeaderLocation", m_eventHeaderLocation );
00076 declareProperty( "NtupleLocID", m_NtupleLocID);
00077 declareProperty( "FillIsolatedElectrons", m_fillIsolatedElectrons);
00078 declareProperty( "FillIsolatedPhotons", m_fillIsolatedPhotons);
00079 declareProperty( "FillIsolatedMuons", m_fillIsolatedMuons);
00080 declareProperty( "FillNonIsolatedMuons", m_fillNonIsolatedMuons);
00081 declareProperty( "FillJets", m_fillJets);
00082 declareProperty( "FillHistory", m_fillHistory);
00083 declareProperty( "FillEventData", m_fillEventData);
00084
00085 }
00086 CBNT_Atlfast::~CBNT_Atlfast(){
00087 if (m_tesIO) delete m_tesIO;
00088 }
00089
00090
00091
00092 StatusCode CBNT_Atlfast::initialize(){
00093
00094 StatusCode sc;
00095
00096 MsgStream log(messageService(), name());
00097
00098
00099 sc = accessNtuple();
00100 if (sc.isFailure()) {
00101 log << MSG::ERROR << "Problem accessing ntuple" << endreq;
00102 return StatusCode::FAILURE;
00103 }
00104
00105 NTuple::Tuple* nt=m_ntPtr;
00106
00107
00108
00109
00110
00111 log << MSG::DEBUG << "declare more variables" << endreq;
00112
00113
00114 if(m_fillIsolatedElectrons){
00115 sc = nt->addItem ("ATLFAST/NELE" , m_nele, 0, 12 );
00116 sc = nt->addItem ("ATLFAST/KFELE" , m_nele, m_codeele);
00117 sc = nt->addItem ("ATLFAST/PXELE" , m_nele, m_pxele);
00118 sc = nt->addItem ("ATLFAST/PYELE" , m_nele, m_pyele);
00119 sc = nt->addItem ("ATLFAST/PZELE" , m_nele, m_pzele);
00120 sc = nt->addItem ("ATLFAST/EEELE" , m_nele, m_eeele);
00121 }
00122
00123 if(m_fillIsolatedPhotons){
00124 sc = nt->addItem ("ATLFAST/NPHO" , m_npho, 0, 12 );
00125 sc = nt->addItem ("ATLFAST/KFPHO" , m_npho, m_codepho);
00126 sc = nt->addItem ("ATLFAST/PXPHO" , m_npho, m_pxpho);
00127 sc = nt->addItem ("ATLFAST/PYPHO" , m_npho, m_pypho);
00128 sc = nt->addItem ("ATLFAST/PZPHO" , m_npho, m_pzpho);
00129 sc = nt->addItem ("ATLFAST/EEPHO" , m_npho, m_eepho);
00130 }
00131
00132 if(m_fillIsolatedMuons){
00133 sc = nt->addItem ("ATLFAST/NMUO" , m_nmuo, 0, 12 );
00134 sc = nt->addItem ("ATLFAST/KFMUO" , m_nmuo, m_codemuo);
00135 sc = nt->addItem ("ATLFAST/PXMUO" , m_nmuo, m_pxmuo);
00136 sc = nt->addItem ("ATLFAST/PYMUO" , m_nmuo, m_pymuo);
00137 sc = nt->addItem ("ATLFAST/PZMUO" , m_nmuo, m_pzmuo);
00138 sc = nt->addItem ("ATLFAST/EEMUO" , m_nmuo, m_eemuo);
00139 }
00140
00141 if(m_fillNonIsolatedMuons){
00142 sc = nt->addItem ("ATLFAST/NMUX" , m_nmux, 0, 12 );
00143 sc = nt->addItem ("ATLFAST/KFMUX" , m_nmux, m_codemux);
00144 sc = nt->addItem ("ATLFAST/PXMUX" , m_nmux, m_pxmux);
00145 sc = nt->addItem ("ATLFAST/PYMUX" , m_nmux, m_pymux);
00146 sc = nt->addItem ("ATLFAST/PZMUX" , m_nmux, m_pzmux);
00147 sc = nt->addItem ("ATLFAST/EEMUX" , m_nmux, m_eemux);
00148 }
00149
00150 if(m_fillJets){
00151 sc = nt->addItem ("ATLFAST/NJET" , m_njet, 0, 40 );
00152 sc = nt->addItem ("ATLFAST/KFJET" , m_njet, m_jetlabel);
00153 sc = nt->addItem ("ATLFAST/PXJET" , m_njet, m_pxjet);
00154 sc = nt->addItem ("ATLFAST/PYJET" , m_njet, m_pyjet);
00155 sc = nt->addItem ("ATLFAST/PZJET" , m_njet, m_pzjet);
00156 sc = nt->addItem ("ATLFAST/EEJET" , m_njet, m_eejet);
00157 sc = nt->addItem ("ATLFAST/BJETDR" , m_njet, m_bjetdR);
00158 sc = nt->addItem ("ATLFAST/CJETDR" , m_njet, m_cjetdR);
00159 sc = nt->addItem ("ATLFAST/TAUJETDR" , m_njet, m_taujetdR);
00160 sc = nt->addItem ("ATLFAST/PTCALO" , m_njet, m_ptcalo);
00161 sc = nt->addItem ("ATLFAST/UCORR" , m_njet, m_ucorr);
00162 sc = nt->addItem ("ATLFAST/BTAG" , m_njet, m_btag);
00163 sc = nt->addItem ("ATLFAST/BCORR" , m_njet, m_bcorr);
00164 sc = nt->addItem ("ATLFAST/TAUTAG" , m_njet, m_tautag);
00165 sc = nt->addItem ("ATLFAST/TAUCORR" , m_njet, m_taucorr);
00166 sc = nt->addItem ("ATLFAST/TAU1PTAG" , m_njet, m_tau1ptag);
00167 sc = nt->addItem ("ATLFAST/TAU3PTAG" , m_njet, m_tau3ptag);
00168 sc = nt->addItem ("ATLFAST/TAU1P3PCORR", m_njet, m_tau1p3pcorr);
00169 }
00170
00171 if(m_fillHistory){
00172 sc = nt->addItem ("ATLFAST/NPART" , m_npart, 0, 40 );
00173 sc = nt->addItem ("ATLFAST/KPPART" , m_npart, m_kppart);
00174 sc = nt->addItem ("ATLFAST/KSPART" , m_npart, m_kspart);
00175 sc = nt->addItem ("ATLFAST/KFPART" , m_npart, m_kfpart);
00176 sc = nt->addItem ("ATLFAST/KPMOTH" , m_npart, m_kpmoth);
00177 sc = nt->addItem ("ATLFAST/KFMOTH" , m_npart, m_kfmoth);
00178 sc = nt->addItem ("ATLFAST/PXPART" , m_npart, m_pxpart);
00179 sc = nt->addItem ("ATLFAST/PYPART" , m_npart, m_pypart);
00180 sc = nt->addItem ("ATLFAST/PZPART" , m_npart, m_pzpart);
00181 sc = nt->addItem ("ATLFAST/EEPART" , m_npart, m_eepart);
00182 }
00183
00184 if(m_fillEventData){
00185 sc = nt->addItem ("ATLFAST/ISUB" , m_isub);
00186 sc = nt->addItem ("ATLFAST/JETB" , m_njetb);
00187 sc = nt->addItem ("ATLFAST/JETC" , m_njetc);
00188 sc = nt->addItem ("ATLFAST/JETTAU" , m_njettau);
00189 sc = nt->addItem ("ATLFAST/PXMISS" , m_pxmiss);
00190 sc = nt->addItem ("ATLFAST/PYMISS" , m_pymiss);
00191 sc = nt->addItem ("ATLFAST/PXNUE" , m_pxnue);
00192 sc = nt->addItem ("ATLFAST/PYNUE" , m_pynue);
00193 sc = nt->addItem ("ATLFAST/SUMET" , m_sumET);
00194 sc = nt->addItem ("ATLFAST/WEIGHT0" , m_mcWeight0);
00195 sc = nt->addItem ("ATLFAST/WEIGHT1" , m_mcWeight1);
00196 sc = nt->addItem ("ATLFAST/WEIGHT2" , m_mcWeight2);
00197 }
00198
00199 if (sc.isFailure()) {
00200 log << MSG::ERROR << "Problem adding item to ntuple" << endreq;
00201 return StatusCode::FAILURE;
00202 }
00203
00204
00205 GlobalEventData* ged = GlobalEventData::Instance();
00206
00207 m_mcLocation = ged -> mcLocation();
00208 m_tesIO = new TesIO(m_mcLocation, ged->justHardScatter());
00209
00210 HeaderPrinter hp("Atlfast CBNT NtupleMaker:", log);
00211 hp.add("MC location ", m_mcTruthLocation);
00212 hp.add("Electron Location ", m_electronLocation);
00213 hp.add("Photon Location ", m_photonLocation);
00214 hp.add("Isolated Muon Location ", m_isolatedMuonLocation);
00215 hp.add("Non-Isolated Muon Location ", m_nonIsolatedMuonLocation);
00216 hp.add("Jet Location ", m_jetLocation);
00217 hp.add("EventHeaderLocation ", m_eventHeaderLocation );
00218 hp.print();
00219 return StatusCode::SUCCESS;
00220
00221 }
00222
00223
00224
00225
00226 StatusCode CBNT_Atlfast::execute(){
00227
00228 MsgStream log(messageService(), name());
00229 log << MSG::DEBUG << "in execute() ..." << endreq;
00230
00231 log << MSG::DEBUG << " -> retrieving all Atlfast stuff"<< endreq;
00232
00233
00234
00235
00236
00237
00238
00239 std::vector<ReconstructedParticle*> rp;
00240 std::vector<ReconstructedParticle*>::const_iterator irp;
00241 if(m_fillIsolatedElectrons){
00242 if(!m_tesIO->copy<ReconstructedParticleCollection>(rp,m_electronLocation)){
00243 log <<MSG::DEBUG << "Could not find Electrons"<<endreq;
00244 }
00245 log << MSG::DEBUG << "processing Electrons: " <<rp.size()<< endreq;
00246
00247 irp = rp.begin();
00248
00249 m_nele = 0;
00250 for (; irp != rp.end(); ++irp) {
00251 m_codeele[m_nele] = (*irp)->pdg_id();
00252 m_pxele[m_nele] = (*irp)->momentum().px();
00253 m_pyele[m_nele] = (*irp)->momentum().py();
00254 m_pzele[m_nele] = (*irp)->momentum().pz();
00255 m_eeele[m_nele] = (*irp)->momentum().e();
00256 if (++m_nele == m_nele->range().distance() ) {
00257 log << MSG::WARNING << "Electron list truncated in Ntuple" << endreq;
00258 break;
00259 }
00260 }
00261 }else{
00262 log << MSG::DEBUG << "Isolated-Electrons switched off!" << endreq;
00263 }
00264
00265
00266
00267 rp.clear();
00268 if(m_fillIsolatedPhotons){
00269 if(!m_tesIO->copy <ReconstructedParticleCollection>(rp, m_photonLocation)){
00270 log <<MSG::DEBUG << "Could not find Photons"<<endreq;
00271 }
00272 log << MSG::DEBUG << "processing Photons: " <<rp.size()<< endreq;
00273
00274 irp = rp.begin();
00275
00276 m_npho = 0;
00277 for (; irp != rp.end(); ++irp) {
00278 m_codepho[m_npho] = (*irp)->pdg_id();
00279 m_pxpho[m_npho] = (*irp)->momentum().px();
00280 m_pypho[m_npho] = (*irp)->momentum().py();
00281 m_pzpho[m_npho] = (*irp)->momentum().pz();
00282 m_eepho[m_npho] = (*irp)->momentum().e();
00283 if (++m_npho == m_npho->range().distance() ) {
00284 log << MSG::WARNING << "Photon list truncated in Ntuple" << endreq;
00285 break;
00286 }
00287 }
00288 }else{
00289 log << MSG::DEBUG << "Isolated-Photons switched off!" << endreq;
00290 }
00291
00292
00293
00294 rp.clear();
00295 if(m_fillIsolatedMuons){
00296 if(!m_tesIO->copy
00297 <ReconstructedParticleCollection>(rp, m_isolatedMuonLocation)){
00298 log <<MSG::DEBUG << "Could not find Isolated Muons"<<endreq;
00299 }
00300 log << MSG::DEBUG << "Isolated Muons: " <<rp.size()<< endreq;
00301
00302 irp = rp.begin();
00303
00304 m_nmuo = 0;
00305 for (; irp != rp.end(); ++irp) {
00306 m_codemuo[m_nmuo] = (*irp)->pdg_id();
00307 m_pxmuo[m_nmuo] = (*irp)->momentum().px();
00308 m_pymuo[m_nmuo] = (*irp)->momentum().py();
00309 m_pzmuo[m_nmuo] = (*irp)->momentum().pz();
00310 m_eemuo[m_nmuo] = (*irp)->momentum().e();
00311 if (++m_nmuo == m_nmuo->range().distance() ) {
00312 log << MSG::WARNING
00313 << "Isolated Muon list truncated in Ntuple" << endreq;
00314 break;
00315 }
00316 }
00317 }else{
00318 log << MSG::DEBUG << "Isolated-Muons switched off!" << endreq;
00319 }
00320
00321
00322
00323
00324 rp.clear();
00325 if(m_fillNonIsolatedMuons){
00326 if(!m_tesIO->copy
00327 <ReconstructedParticleCollection >(rp, m_nonIsolatedMuonLocation)){
00328 log <<MSG::DEBUG << "Could not find non Isolated muons"<<endreq;
00329 }
00330 log << MSG::DEBUG << "NonIsolated Muons: " <<rp.size()<< endreq;
00331 irp = rp.begin();
00332
00333 m_nmux = 0;
00334
00335 for (; irp != rp.end(); ++irp) {
00336 m_codemux[m_nmux] = (*irp)->pdg_id();
00337 m_pxmux[m_nmux] = (*irp)->momentum().px();
00338 m_pymux[m_nmux] = (*irp)->momentum().py();
00339 m_pzmux[m_nmux] = (*irp)->momentum().pz();
00340 m_eemux[m_nmux] = (*irp)->momentum().e();
00341 if (++m_nmux == m_nmux->range().distance() ) {
00342 log << MSG::WARNING
00343 << "Non-isolated Muon list truncated in Ntuple" << endreq;
00344 break;
00345 }
00346 }
00347 }else{
00348 log << MSG::DEBUG << "NonIsolated-Muons switched off!" << endreq;
00349 }
00350
00351
00352
00353
00354 std::vector<Jet*> jv;
00355 std::vector<Jet*>::const_iterator ijv;
00356 if(m_fillJets){
00357 if(!m_tesIO->copy<JetCollection >(jv, m_jetLocation)){
00358 log <<MSG::DEBUG << "Could not find Jets"<<endreq;
00359 }
00360 log << MSG::DEBUG << "Processing Jets: " <<jv.size()<< endreq;
00361
00362 ijv = jv.begin();
00363
00364 m_njet = 0;
00365
00366 for (; ijv != jv.end(); ++ijv) {
00367 m_jetlabel[m_njet] = (*ijv)->pdg_id();
00368 m_pxjet[m_njet] = (*ijv)->px();
00369 m_pyjet[m_njet] = (*ijv)->py();
00370 m_pzjet[m_njet] = (*ijv)->pz();
00371 m_eejet[m_njet] = (*ijv)->e();
00372 m_bjetdR[m_njet] = (*ijv)->getdRbquark();
00373 m_cjetdR[m_njet] = (*ijv)->getdRcquark();
00374 m_taujetdR[m_njet] = (*ijv)->getdRhadtau();
00375 m_ptcalo[m_njet] = (*ijv)->pT();
00376 m_ucorr[m_njet] = (*ijv)->lTagCorrFactor();
00377 m_btag[m_njet] = (*ijv)->isBTagged() ? 1. : 0.;
00378 m_bcorr[m_njet] = (*ijv)->bTagCorrFactor();
00379 m_tautag[m_njet] = (*ijv)->isTauTagged("Standard") ? 1. : 0.;
00380 m_taucorr[m_njet] = (*ijv)->tauTagCorrFactor("Tau");
00381 m_tau1ptag[m_njet] = (*ijv)->isTauTagged("Tau1P3P:1prong") ? 1. : 0.;
00382 m_tau3ptag[m_njet] = (*ijv)->isTauTagged("Tau1P3P:3prong") ? 1. : 0.;
00383 m_tau1p3pcorr[m_njet] = (*ijv)->tauTagCorrFactor("Tau1P3P");
00384 if ( ++m_njet == m_njet->range().distance() ) {break;}
00385 }
00386 log << MSG::DEBUG << " Jets in ntuple" << endreq;
00387 }else{
00388 log << MSG::DEBUG << "Atlfast Jets switched off!" << endreq;
00389 }
00390
00391 if(m_fillHistory){
00392 MCparticleCollection mc;
00393
00394 HepMC_helper::All* all= new HepMC_helper::All;
00395 TesIoStat stat = m_tesIO->getMC(mc, all);
00396 delete all;
00397
00398 if(stat){
00399 log << MSG::DEBUG << " found MC truth" << endreq;
00400 }else{
00401 log << MSG::ERROR << " MC truth not found in TES" << endreq;
00402 }
00403
00404 MCparticleCollection::const_iterator imc = mc.begin();
00405 m_npart=0;
00406
00407 HepMC_helper::IsStatusxx isf(3);
00408
00409 for(;imc != mc.end(); ++imc){
00410 if(isf(*imc)){
00411 m_kppart[m_npart] = (*imc)->barcode();
00412 m_kspart[m_npart] = (*imc)->status()%100;
00413 m_kfpart[m_npart] = (*imc)->pdg_id();
00414 std::pair<int, int> parentCodes = getParentCodes(imc);
00415 m_kfmoth[m_npart] = parentCodes.first;
00416 m_kpmoth[m_npart] = parentCodes.second;
00417 m_pxpart[m_npart] = (*imc)->momentum().x();
00418 m_pypart[m_npart] = (*imc)->momentum().y();
00419 m_pzpart[m_npart] = (*imc)->momentum().z();
00420 m_eepart[m_npart] = (*imc)->momentum().e();
00421 ++m_npart;
00422 }
00423
00424 if ( m_npart == m_npart->range().distance() ) break;
00425 }
00426
00427 log << MSG::DEBUG << " Particles in ntuple" << endreq;
00428 }else{
00429 log << MSG::DEBUG << "Atlfast History switched off!" << endreq;
00430 }
00431
00432 if(m_fillEventData){
00433 const EventHeader* theEventHeader(0);
00434 if(!(m_tesIO->getDH(theEventHeader))){
00435 log << MSG::ERROR<<"Could not find the event header in the TES"<<endreq;
00436 }
00437
00438 if(theEventHeader){
00439 m_isub = 0;
00440 m_njetb = theEventHeader->nBJets();
00441 m_njetc = theEventHeader->nCJets();
00442 m_njettau = theEventHeader->nTauJets();
00443 m_pxmiss = theEventHeader->pMiss().x();
00444 m_pymiss = theEventHeader->pMiss().y();
00445 m_pxnue = theEventHeader->pEscaped().x();
00446 m_pynue = theEventHeader->pEscaped().y();
00447 m_sumET = theEventHeader->sumET();
00448
00449 MCweightContainer weights = theEventHeader->firstFewWeights(3);
00450 m_mcWeight0 = weights[0];
00451 m_mcWeight1 = weights[1];
00452 m_mcWeight2 = weights[2];
00453 } else {
00454 log << MSG::ERROR << " Invalid EventHeader data handle" << endreq;
00455 }
00456
00457 log << MSG::DEBUG << " EventHeader quantities are in ntuple" << endreq;
00458 }else{
00459 log << MSG::DEBUG << "Atlfast History switched off!" << endreq;
00460 }
00461
00462 return StatusCode::SUCCESS;
00463
00464 }
00465
00466 std::pair<int, int>
00467 CBNT_Atlfast::getParentCodes(MCparticleCollection::const_iterator&
00468 imc){
00469
00470 HepMC::GenVertex* gv = (*imc)->production_vertex();
00471 if ( gv == 0 ){ return std::pair<int, int>(0,0);}
00472
00473 if ( gv->particles_in_size() == 0 ) { return std::pair<int, int>(0,0); }
00474
00475 HepMC::GenVertex::particle_iterator
00476 pi = gv->particles_begin(HepMC::parents);
00477 if( *pi == 0 ){ return std::pair<int, int>(0,0); }
00478
00479
00480 return std::pair<int, int>( (*pi)->pdg_id(), (*pi)->barcode() );
00481 }
00482
00483
00484
00485 StatusCode CBNT_Atlfast::finalize() {
00486
00487
00488 return StatusCode::SUCCESS;
00489 }
00490
00491
00492
00493 StatusCode CBNT_Atlfast::accessNtuple() {
00494 MsgStream log(messageService(), name());
00495
00496
00497 NTuplePtr nt(ntupleService(),"/NTUPLES"+m_NtupleLocID);
00498
00499 if (nt)
00500 {
00501 m_ntPtr=nt;
00502 log << MSG::INFO << "Ntuple " <<m_NtupleLocID
00503 << " reaccessed! " << endreq;
00504 return StatusCode::SUCCESS;
00505 } else
00506 {
00507 log << MSG::FATAL << "Cannot reaccess " << m_NtupleLocID << endreq;
00508 return StatusCode::FAILURE;
00509 }
00510 }
00511
00512 }