ATLAS Offline Software
testAthenaPhotonAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // PhotonEfficiencyCorrection includes
6 #include "testAthenaPhotonAlg.h"
7 
11 #include "xAODCore/ShallowCopy.h"
12 
14 
15 testAthenaPhotonAlg::testAthenaPhotonAlg( const std::string& name, ISvcLocator* pSvcLocator ) : AthAlgorithm( name, pSvcLocator ), m_photonSF("AsgPhotonEfficiencyCorrectionTool/AsgPhotonEfficiencyCorrectionTool",this){
16 
17  //declareProperty( "Property", m_nProperty ); //example property declaration
18  declareProperty( "PhotonEfficiencyCorrectionTool", m_photonSF );
19 }
20 //
22 
23 
25  ATH_MSG_INFO ("Initializing " << name() << "...");
26  // Retrieve the tool
27  ATH_CHECK( m_photonSF.retrieve() );
28  return StatusCode::SUCCESS;
29 }
30 
32  ATH_MSG_INFO ("Finalizing " << name() << "...");
33  // Clean up
34  ATH_CHECK( m_photonSF.release() );
35  return StatusCode::SUCCESS;
36 }
37 
39  ATH_MSG_DEBUG ("Executing " << name() << "...");
40 
41 //----------------------------
42  // Event information
43  //---------------------------
44  const xAOD::EventInfo* eventInfo = nullptr; //NOTE: Everything that comes from the storegate direct from the input files is const!
45 
46  // ask the event store to retrieve the xAOD EventInfo container
47  //ATH_CHECK( evtStore()->retrieve( eventInfo, "EventInfo") ); // the second argument ("EventInfo") is the key name
48  ATH_CHECK( evtStore()->retrieve( eventInfo) );
49  // if there is only one container of that type in the xAOD (as with the EventInfo container), you do not need to pass
50  // the key name, the default will be taken as the only key name in the xAOD
51 
52  // check if data or MC
53  bool isMC = true;
54  if(!eventInfo->eventType(xAOD::EventInfo::IS_SIMULATION ) ){
55  isMC = false;
56  }
57  if(!isMC) ATH_MSG_ERROR("This is data, no scale factors should be used on the data!");
58 
59  //---------
60  // photons
61  //---------
62  const xAOD::PhotonContainer* photons = nullptr;
63  ATH_CHECK( evtStore()->retrieve( photons, "Photons") );
64  ATH_MSG_DEBUG("Found "<<photons->size() <<" photons in event, itterate....");
65 
66 // Let's create a shallow copy of the const photon container, and decorate it with the obtained SF
67 auto inContShallowCopy = xAOD::shallowCopyContainer( *photons );
68 
69 //creates a new photon container to hold the subset as well as the needed auxiliary container
72  // You need to tell the photon container in which auxiliary container it should write its member variables
73  myphotons->setStore( myphotonsAux ); //gives it a new associated aux container
74 
75  // Also record to storegate: you must record both the container and the auxcontainer.
76  // Note that storegate takes ownership of these objects, i.e., you must not try to delete them yourself.
77  ATH_CHECK( evtStore()->record(myphotons, "MyPhotons" ) );
78  ATH_CHECK( evtStore()->record(myphotonsAux, "MyPhotonsAux" ) );
79 
80 // Loop over all Photons in the shallow-copy container, decorate it with SF, and store in new xAOD file, in addition print out all SF (with get* function)
81 for ( xAOD::Photon* ph : *(inContShallowCopy.first) ) {
82 
83  //if ( std::abs(ph->eta()) > 2.37 || ph->pt()<15000. ) continue;
84  //if (std::abs(ph->eta())>=1.37 && std::abs(ph->eta())<=1.52) continue;
85  ATH_MSG_DEBUG( " photon pt = " << ph->pt() <<", photon eta = " << ph->eta() );
86 
87  double SF, SFerr; // for photon SF and the apropriate error
88  if(!m_photonSF->getEfficiencyScaleFactor(*ph,SF)){
89  ATH_MSG_WARNING( "Couldn't get photon scale factor!" );
90  continue;
91  }
92  if(!m_photonSF->getEfficiencyScaleFactorError(*ph,SFerr)){
93  ATH_MSG_WARNING( "Couldn't get photon scale factor uncertainty!" );
94  continue;
95  }
96  ATH_MSG_DEBUG( " photon SF = " << SF <<", photon SF sys error = " << SFerr );
97 
98  // applyEfficiencyScaleFactor - decorate the object (*photon)
99  if ( m_photonSF->applyEfficiencyScaleFactor(*ph) == CP::CorrectionCode::Error ) {
100  ATH_MSG_ERROR("PhotonEfficiencyCorrectionTool reported a CP::CorrectionCode::Error");
101  return StatusCode::FAILURE;
102  }
103 
104  myphotons->push_back(ph); // add it to a new photon container which will be writed to a new file
105 } // and loop on photons
106 
107  return StatusCode::SUCCESS;
108 }
109 
110 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ShallowCopy.h
testAthenaPhotonAlg::finalize
virtual StatusCode finalize()
Definition: testAthenaPhotonAlg.cxx:31
testAthenaPhotonAlg::testAthenaPhotonAlg
testAthenaPhotonAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: testAthenaPhotonAlg.cxx:15
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TRT_PAI_gasdata::SF
const float SF[NF]
Cross sections for Fluor.
Definition: TRT_PAI_gasdata.h:285
xAOD::PhotonAuxContainer
PhotonAuxContainer_v3 PhotonAuxContainer
Definition of the current photon auxiliary container.
Definition: PhotonAuxContainer.h:22
IAsgPhotonEfficiencyCorrectionTool.h
xAOD::EventInfo_v1::IS_SIMULATION
@ IS_SIMULATION
true: simulation, false: data
Definition: EventInfo_v1.h:151
testAthenaPhotonAlg::~testAthenaPhotonAlg
virtual ~testAthenaPhotonAlg()
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
CP::CorrectionCode::Error
@ Error
Some error happened during the object correction.
Definition: CorrectionCode.h:36
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
testAthenaPhotonAlg::m_photonSF
ToolHandle< IAsgPhotonEfficiencyCorrectionTool > m_photonSF
The tool handle to our photon efficiency correction.
Definition: testAthenaPhotonAlg.h:25
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
AthAlgorithm
Definition: AthAlgorithm.h:47
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
xAOD::PhotonContainer
PhotonContainer_v1 PhotonContainer
Definition of the current "photon container version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/PhotonContainer.h:17
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
testAthenaPhotonAlg::initialize
virtual StatusCode initialize()
Definition: testAthenaPhotonAlg.cxx:24
PhotonAuxContainer.h
xAOD::PhotonAuxContainer_v3
Auxiliary store for offline photons.
Definition: PhotonAuxContainer_v3.h:36
xAOD::Photon_v1
Definition: Photon_v1.h:37
testAthenaPhotonAlg.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
EventInfoRead.isMC
isMC
Definition: EventInfoRead.py:11
testAthenaPhotonAlg::execute
virtual StatusCode execute()
Definition: testAthenaPhotonAlg.cxx:38
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
PhotonContainer.h
xAOD::EventInfo_v1::eventType
bool eventType(EventType type) const
Check for one particular bitmask value.