ATLAS Offline Software
PDFWeight.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 
10 #include "LHAPDF/LHAPDF.h"
11 #include "LHAPDF/PDFSet.h"
12 
14 
15 using namespace LHAPDF;
16 
17 PDFWeight::PDFWeight( const std::string& name )
18  : WeightToolBase( name ),
19  m_eventInfoName("EventInfo") {
20  declareProperty("index", m_index = 0);
21  declareProperty("PDFName", m_pdfName = "CT10");
22  declareProperty("TruthEventContainerName", m_truthEventContainerName = "TruthEvents");
23 
24  m_weight = 0;
25 }
26 
29  if (sc.isFailure()) {
30  return StatusCode::FAILURE;
31  }
32  try {
33  LHAPDF::pathsPrepend("/cvmfs/atlas.cern.ch/repo/sw/Generators/lhapdfsets/current"); // hack!
34  if(m_pdf) delete m_pdf;
35  m_pdf = mkPDF(m_pdfName, m_index);
36  } catch (...) {
37  m_pdf = 0;
38  }
39  return StatusCode::SUCCESS;
40 }
41 
42 // returns: the value that was calculated from the xAOD::IParticle composite built event
43 // This is the implementation of the interface
44 double PDFWeight::evaluate( const xAOD::IParticle* /*part*/ ) const
45 {
46  ATH_MSG_DEBUG ("Evaluating " << name() << "...");
47 
48  // Get the proper weight (from EventInfo)
49  return this->getWeight();
50 }
51 
52 double PDFWeight::computeWeight(const xAOD::EventInfo* evtInfo) const {
53  // don't do anything for data
55  ATH_MSG_DEBUG (" Returning weight=1. for data.");
56  return 1.;
57  }
58  if (!m_pdf)
59  return 1.;
60 
61  int pdf_id1 = 0;
62  int pdf_id2 = 0;
63  int pdg_id1 = 0;
64  int pdg_id2 = 0;
65  float x1 = 0;
66  float x2 = 0;
67  float q = 0;
68  float xf1 = 0;
69  float xf2 = 0;
70 
71  const xAOD::TruthEventContainer* truthEventContainer(nullptr);
72  StatusCode sc = evtStore()->retrieve(truthEventContainer, m_truthEventContainerName);
73  if (sc.isFailure()) { ATH_MSG_ERROR("Could not retrieve TruthEventContainer."); return 0; }
74 
75  float weight = 1.0;
76 
77  for (auto truthEvent : *truthEventContainer) {
78 
79  try {
80  if ( !truthEvent->pdfInfoParameter( pdg_id1, xAOD::TruthEvent::PdfParam::PDGID1 ) ) {
81  ATH_MSG_DEBUG("Could not retrieve PDG id1.");
82  }
83  if ( !truthEvent->pdfInfoParameter( pdg_id2, xAOD::TruthEvent::PdfParam::PDGID2 ) ) {
84  ATH_MSG_DEBUG("Could not retrieve PDG id2.");
85  }
86  if ( !truthEvent->pdfInfoParameter( pdf_id1, xAOD::TruthEvent::PdfParam::PDFID1 ) ) {
87  ATH_MSG_DEBUG("Could not retrieve PDF id1.");
88  }
89  if ( !truthEvent->pdfInfoParameter( pdf_id2, xAOD::TruthEvent::PdfParam::PDFID2 ) ) {
90  ATH_MSG_DEBUG("Could not retrieve PDF id2.");
91  }
92  if ( !truthEvent->pdfInfoParameter( x1, xAOD::TruthEvent::PdfParam::X1 ) ) {
93  ATH_MSG_DEBUG("Could not retrieve x_1.");
94  }
95  if ( !truthEvent->pdfInfoParameter( x2, xAOD::TruthEvent::PdfParam::X2 ) ) {
96  ATH_MSG_DEBUG("Could not retrieve x_2.");
97  }
98  if ( !truthEvent->pdfInfoParameter( q, xAOD::TruthEvent::PdfParam::Q ) ) {
99  ATH_MSG_DEBUG("Could not retrieve Q.");
100  }
101  if ( !truthEvent->pdfInfoParameter( xf1, xAOD::TruthEvent::PdfParam::XF1 ) ) {
102  ATH_MSG_DEBUG("Could not retrieve x_f1.");
103  }
104  if ( !truthEvent->pdfInfoParameter( xf2, xAOD::TruthEvent::PdfParam::XF2 ) ) {
105  ATH_MSG_DEBUG("Could not retrieve x_f2.");
106  }
107  } catch (...) {
108  // set this to debug only because this is a frequent problem in ttbar samples
109  // sending the message in every event will only flood the logs until a newer version is available
110  ATH_MSG_DEBUG("Could not retrieve PDF information. xAODs generated with a version earlier than AtlasProduction-19.2.3.7 might have a bug that causes this. Please check your input xAOD. All PDF weights will be 1.");
111  return 1.0;
112  }
113 
114  if (xf1 == 0 || xf2 == 0) {
115  ATH_MSG_DEBUG("Skipping event, as this TruthEvent is not reliable.");
116  // this can happen for some PDFs which cross zero and go negative at NLO
117  // not sure this is the best to do, so leaving the weight == 0 for now
118  weight = 0;
119  continue;
120  }
121 
122  float reweighted_xf1 = 0;
123  float reweighted_xf2 = 0;
124  try {
125  reweighted_xf1 = m_pdf->xfxQ(pdg_id1, x1, q);
126  reweighted_xf2 = m_pdf->xfxQ(pdg_id2, x2, q);
127  } catch (...) {
128  ATH_MSG_DEBUG("LHAPDF exception.");
129  }
130  weight = reweighted_xf1*reweighted_xf2/(xf1*xf2);
131 
132  break;
133  }
134 
135  return weight;
136 }
137 
138 double PDFWeight::getWeight() const {
139  //Retrieveing eventInfo
140  const xAOD::EventInfo* evtInfo;
141  StatusCode sc = evtStore()->retrieve( evtInfo, m_eventInfoName );
142  if(sc.isFailure() || !evtInfo) {
143  ATH_MSG_ERROR (" EventInfo could not be retrieved !!");
144  return 0.;
145  }
146 
147  double weight = this->computeWeight(evtInfo);
148  ATH_MSG_DEBUG (" " << name() << " returning weight= " << weight << ".");
149  return weight;
150 }
151 
PDFWeight::m_pdfName
std::string m_pdfName
Definition: PDFWeight.h:45
CompositeParticle.h
plotBeamSpotCompare.x1
x1
Definition: plotBeamSpotCompare.py:216
PDFWeight::computeWeight
double computeWeight(const xAOD::EventInfo *) const
Definition: PDFWeight.cxx:52
PDFWeight::m_truthEventContainerName
std::string m_truthEventContainerName
Definition: PDFWeight.h:46
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
WeightToolBase::initialize
virtual StatusCode initialize() override
Usual initialize method of the framework.
Definition: WeightToolBase.cxx:21
plotBeamSpotCompare.x2
x2
Definition: plotBeamSpotCompare.py:218
PDFWeight::PDFWeight
PDFWeight(const std::string &name)
Create a proper constructor for Athena.
Definition: PDFWeight.cxx:17
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
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
PDFWeight::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: PDFWeight.cxx:44
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
PDFWeight::m_eventInfoName
std::string m_eventInfoName
Definition: PDFWeight.h:47
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
PDFWeight::m_index
int m_index
Definition: PDFWeight.h:52
PDFWeight::m_pdf
LHAPDF::PDF * m_pdf
Definition: PDFWeight.h:50
PDFWeight::m_weight
float m_weight
Definition: PDFWeight.h:48
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
PDFWeight.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
PDFWeight::getWeight
virtual double getWeight() const override
returns: the value that was calculated from the usual Athena storegate
Definition: PDFWeight.cxx:138
WeightToolBase
Definition: WeightToolBase.h:29
extractSporadic.q
list q
Definition: extractSporadic.py:98
PDFWeight::initialize
StatusCode initialize() override
Usual initialize method of the framework.
Definition: PDFWeight.cxx:27
LHAPDF
Definition: PDFWeight.h:16
TruthEventContainer.h
xAOD::EventInfo_v1::eventType
bool eventType(EventType type) const
Check for one particular bitmask value.