ATLAS Offline Software
McEventWeight.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 // ReweightUtils includes
7 
9 
11 
12 McEventWeight::McEventWeight( const std::string& name )
13  : WeightToolBase( name ),
14  m_McEventWeightIndex(0),
15  m_eventInfoName("EventInfo") {
16  declareProperty( "McEventWeightIndex", m_McEventWeightIndex = 0);
17  declareProperty("EventInfoName", m_eventInfoName = "EventInfo");
18  declareProperty("UseTruthEvents",m_useTruthEvents=false,"To solve ATLASSIM-2989 we have to switch to using TruthEvents for weights");
19 }
20 
21 
22 // returns: the value that was calculated from the xAOD::IParticle composite built event
23 // This is the implementation of the interface
24 double McEventWeight::evaluate( const xAOD::IParticle* /*part*/ ) const
25 {
26  ATH_MSG_DEBUG ("Evaluating " << name() << "...");
27 
28  // Get the proper weight (from EventInfo)
29  return this->getWeight();
30 }
31 
32 double McEventWeight::computeWeight(const xAOD::EventInfo* evtInfo) const {
33  // don't do anything for data
35  ATH_MSG_DEBUG (" Returning weight=1. for data.");
36  return 1.;
37  }
38 
39 
40  if(m_useTruthEvents) {
41  //access TruthEvents container instead
42  const xAOD::TruthEventContainer* te = 0;
43  if( evtStore()->retrieve( te, "TruthEvents" ).isFailure() ) {
44  ATH_MSG_ERROR("TruthEvents could not be retrieved. Throwing exception");
45  throw std::runtime_error("McEventWeight ASG Tool: TruthEvents could not be retrieved");
46  }
47  if( te->size() == 0 ) {
48  ATH_MSG_ERROR("TruthEvents has no events in it. Throwing exception");
49  throw std::runtime_error("McEventWeight ASG Tool: TruthEvents has no events in it");
50  }
51  if( te->at(0)->weights().size() <= m_McEventWeightIndex) {
52  ATH_MSG_ERROR("TruthEvents first event has " << te->at(0)->weights().size() << " weights, but "
53  << m_McEventWeightIndex << " weight requested. Throwing exception");
54  throw std::runtime_error("McEventWeight ASG Tool: TruthEvents has too few weights.");
55  }
56  const double weight = te->at(0)->weights()[m_McEventWeightIndex];
57  ATH_MSG_DEBUG ("Got weight in McEventWeight::computeWeight() = " << weight << ".");
58  return weight;
59  }
60 
61  const double weight = evtInfo->mcEventWeight(m_McEventWeightIndex);
62  ATH_MSG_DEBUG ("Got weight in McEventWeight::computeWeight() = " << weight << ".");
63  return weight;
64 }
65 
66 double McEventWeight::getWeight() const {
67  //Retrieveing eventInfo
68  const xAOD::EventInfo* evtInfo;
69  StatusCode sc = evtStore()->retrieve( evtInfo, m_eventInfoName );
70  if(sc.isFailure() || !evtInfo) {
71  ATH_MSG_ERROR (" EventInfo could not be retrieved !!");
72  return 0.;
73  }
74 
75  double weight = this->computeWeight(evtInfo);
76  ATH_MSG_VERBOSE (" " << name() << " returning weight= " << weight << ".");
77  return weight;
78 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
CompositeParticle.h
McEventWeight::evaluate
virtual double evaluate(const xAOD::IParticle *part) const override
returns: the value that was calculated from the xAOD::IParticle (composite built event object for ins...
Definition: McEventWeight.cxx:24
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
McEventWeight::computeWeight
double computeWeight(const xAOD::EventInfo *) const
Definition: McEventWeight.cxx:32
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
xAOD::EventInfo_v1::IS_SIMULATION
@ IS_SIMULATION
true: simulation, false: data
Definition: EventInfo_v1.h:151
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
McEventWeight::m_eventInfoName
std::string m_eventInfoName
Definition: McEventWeight.h:45
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
McEventWeight::McEventWeight
McEventWeight(const std::string &name)
Create a proper constructor for Athena.
Definition: McEventWeight.cxx:12
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
McEventWeight.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
McEventWeight::m_useTruthEvents
bool m_useTruthEvents
Definition: McEventWeight.h:46
WeightToolBase
Definition: WeightToolBase.h:29
xAOD::EventInfo_v1::mcEventWeight
float mcEventWeight(size_t i=0) const
The weight of one specific MC event used in the simulation.
Definition: EventInfo_v1.cxx:203
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
McEventWeight::getWeight
virtual double getWeight() const override
returns: the value that was calculated from the usual Athena storegate
Definition: McEventWeight.cxx:66
TruthEventContainer.h
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
xAOD::EventInfo_v1::eventType
bool eventType(EventType type) const
Check for one particular bitmask value.
McEventWeight::m_McEventWeightIndex
unsigned int m_McEventWeightIndex
Definition: McEventWeight.h:43