ATLAS Offline Software
xAODM4MuIntervalFilter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Header for this module
7 
9 #include "CLHEP/Random/RandomEngine.h"
11 
12 
13 xAODM4MuIntervalFilter::xAODM4MuIntervalFilter(const std::string& name, ISvcLocator* pSvcLocator)
14  : GenFilter(name, pSvcLocator)
15 {
16 
17 }
18 
20 
21  CHECK(m_rndmSvc.retrieve());
22 
23  ATH_MSG_DEBUG( "MaxEta " << m_maxEta);
24  ATH_MSG_DEBUG( "MinPt " << m_minPt);
25  ATH_MSG_DEBUG( "LowM4muProbability " << m_prob2low);
26  ATH_MSG_DEBUG( "MediumMj4muProbability " << m_prob2medium);
27  ATH_MSG_DEBUG( "HighM4muProbability " << m_prob2high);
28  ATH_MSG_DEBUG( "LowM4mu " << m_m4mulow);
29  ATH_MSG_DEBUG( "HighM4mu " << m_m4muhigh);
30  ATH_MSG_DEBUG( "ApplyReWeighting " << m_ApplyReWeighting);
31  return StatusCode::SUCCESS;
32 }
33 
35  return StatusCode::SUCCESS;
36 }
37 
39  // Get random number engine
40  const EventContext& ctx = Gaudi::Hive::currentContext();
41  CLHEP::HepRandomEngine* rndm = this->getRandomEngine(name(), ctx);
42  if (!rndm) {
43  ATH_MSG_ERROR("Failed to retrieve random number engine xAODM4MuIntervalFilter");
44  setFilterPassed(false);
45  return StatusCode::FAILURE;
46  }
47 
48  // Find overlap objects
49  std::vector<HepMC::FourVector> MCTruthMuonList;
50 
51 // Retrieve TruthGen container from xAOD Gen slimmer, contains all particles witout barcode_zero and
52 // duplicated barcode ones
53  const xAOD::TruthParticleContainer* xTruthParticleContainer;
54  if (evtStore()->retrieve(xTruthParticleContainer, "TruthGen").isFailure()) {
55  ATH_MSG_ERROR("No TruthParticle collection with name " << "TruthGen" << " found in StoreGate!");
56  return StatusCode::FAILURE;
57  }
58 
59  unsigned int nPart = xTruthParticleContainer->size();
60  for (unsigned int iPart = 0; iPart < nPart; ++iPart) {
61  const xAOD::TruthParticle* pitr = (*xTruthParticleContainer)[iPart];
62 
63  // muon
64  if (std::abs((pitr)->pdgId()) == 13 && MC::isStable(pitr) &&
65  (pitr)->pt() >= m_minPt &&
66  std::abs((pitr)->eta()) <= m_maxEta) {
67  HepMC::FourVector tmp((pitr)->px(), (pitr)->py(), (pitr)->pz(), (pitr)->e());
68  MCTruthMuonList.push_back(tmp);
69 
70  }
71  } // end loop over Particles
72 
73 
74  std::sort(MCTruthMuonList.begin(), MCTruthMuonList.end(), High2LowByPt());
75 
76  if(MCTruthMuonList.size()<4){
77  setFilterPassed(false);
78  ATH_MSG_DEBUG("Less than 4 muons. The muon number is " << MCTruthMuonList.size());
79  return StatusCode::SUCCESS;
80  }
81 
82  if(m_ApplyReWeighting) {
83  HepMC::FourVector vec4mu(MCTruthMuonList.at(0).px() + MCTruthMuonList.at(1).px() + MCTruthMuonList.at(2).px() + MCTruthMuonList.at(3).px(),
84  MCTruthMuonList.at(0).py() + MCTruthMuonList.at(1).py() + MCTruthMuonList.at(2).py() + MCTruthMuonList.at(3).py(),
85  MCTruthMuonList.at(0).pz() + MCTruthMuonList.at(1).pz() + MCTruthMuonList.at(2).pz() + MCTruthMuonList.at(3).pz(),
86  MCTruthMuonList.at(0).e() + MCTruthMuonList.at(1).e() + MCTruthMuonList.at(2).e() + MCTruthMuonList.at(3).e() );
87 
88  double m4mu = vec4mu.m();
89  double eventWeight = 1.0;
90  eventWeight = getEventWeight(m4mu);
91  double rnd = rndm->flat();
92  if (1.0/eventWeight < rnd) {
93  setFilterPassed(false);
94  ATH_MSG_DEBUG("Event failed weighting. Weight is " << eventWeight);
95  return StatusCode::SUCCESS;
96  }
97 
98  // Get MC event collection for setting weight
99  const McEventCollection* mecc = 0;
100  if ( evtStore()->retrieve( mecc ).isFailure() || !mecc ){
101  setFilterPassed(false);
102  ATH_MSG_ERROR("Could not retrieve MC Event Collection - weight might not work");
103  return StatusCode::FAILURE;
104  }
105 
106  ATH_MSG_INFO("Event passed. Will weight events " << eventWeight);
107  McEventCollection* mec = const_cast<McEventCollection*> (&(*mecc));
108  for (unsigned int i = 0; i < mec->size(); ++i) {
109  if (!(*mec)[i]) continue;
110  double existingWeight = (*mec)[i]->weights().size()>0 ? (*mec)[i]->weights()[0] : 1.;
111  if ((*mec)[i]->weights().size()>0) {
112  for (unsigned int iw = 0; iw < (*mec)[i]->weights().size(); ++iw) {
113  double existWeight = (*mec)[i]->weights()[iw];
114  (*mec)[i]->weights()[iw] = existWeight*eventWeight;
115  }
116 // (*mec)[i]->weights()[0] = existingWeight*eventWeight;
117  } else {
118  (*mec)[i]->weights().push_back( eventWeight*existingWeight );
119  }
120 
121 #ifdef HEPMC3
122  (*mec)[i]->add_attribute("filterWeight", std::make_shared<HepMC3::DoubleAttribute>(eventWeight));
123 #endif
124 
125  }
126  }
127  // Made it to the end - success!
128  setFilterPassed(true);
129  return StatusCode::SUCCESS;
130 }
131 
133  double weight = 1.0;
134  if (mass < m_m4mulow) {
135  weight /= m_prob2low;
136 
137  } else if (mass > m_m4muhigh) {
138  weight /= m_prob2high;
139 
140  } else {
142  }
143  ATH_MSG_DEBUG("WEIGHTING:: " << mass << "\t" << weight);
144  return weight;
145 }
146 
147 
148 CLHEP::HepRandomEngine* xAODM4MuIntervalFilter::getRandomEngine(const std::string& streamName,
149  const EventContext& ctx) const
150 {
151  ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, streamName);
152  std::string rngName = name()+streamName;
153  rngWrapper->setSeed( rngName, ctx );
154  return rngWrapper->getEngine(ctx);
155 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ATHRNG::RNGWrapper::setSeed
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition: RNGWrapper.h:169
xAODM4MuIntervalFilter::filterInitialize
virtual StatusCode filterInitialize() override
Definition: xAODM4MuIntervalFilter.cxx:19
test_pyathena.px
px
Definition: test_pyathena.py:18
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAODM4MuIntervalFilter::m_rndmSvc
ServiceHandle< IAthRNGSvc > m_rndmSvc
Definition: xAODM4MuIntervalFilter.h:50
xAODM4MuIntervalFilter::filterFinalize
virtual StatusCode filterFinalize() override
Definition: xAODM4MuIntervalFilter.cxx:34
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:79
test_pyathena.pt
pt
Definition: test_pyathena.py:11
xAODM4MuIntervalFilter::m_prob2high
Gaudi::Property< double > m_prob2high
Definition: xAODM4MuIntervalFilter.h:54
PowhegPy8EG_H2a.pdgId
dictionary pdgId
Definition: PowhegPy8EG_H2a.py:128
dqt_zlumi_pandas.mass
mass
Definition: dqt_zlumi_pandas.py:170
xAODM4MuIntervalFilter::m_m4muhigh
Gaudi::Property< double > m_m4muhigh
Definition: xAODM4MuIntervalFilter.h:56
xAODM4MuIntervalFilter::m_maxEta
Gaudi::Property< double > m_maxEta
Definition: xAODM4MuIntervalFilter.h:47
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
GenFilter
Base class for event generator filtering modules.
Definition: GenFilter.h:30
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
xAODM4MuIntervalFilter::m_m4mulow
Gaudi::Property< double > m_m4mulow
Definition: xAODM4MuIntervalFilter.h:55
lumiFormat.i
int i
Definition: lumiFormat.py:92
xAODM4MuIntervalFilter::m_ApplyReWeighting
Gaudi::Property< bool > m_ApplyReWeighting
Definition: xAODM4MuIntervalFilter.h:57
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
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
xAODM4MuIntervalFilter::m_prob2low
Gaudi::Property< double > m_prob2low
Definition: xAODM4MuIntervalFilter.h:52
Amg::pz
@ pz
Definition: GeoPrimitives.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAODM4MuIntervalFilter::xAODM4MuIntervalFilter
xAODM4MuIntervalFilter(const std::string &name, ISvcLocator *pSvcLocator)
Definition: xAODM4MuIntervalFilter.cxx:13
xAODM4MuIntervalFilter::getEventWeight
double getEventWeight(double m) const
Definition: xAODM4MuIntervalFilter.cxx:132
Amg::py
@ py
Definition: GeoPrimitives.h:39
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ATHRNG::RNGWrapper
A wrapper class for event-slot-local random engines.
Definition: RNGWrapper.h:56
xAODM4MuIntervalFilter.h
ATHRNG::RNGWrapper::getEngine
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition: RNGWrapper.h:134
RNGWrapper.h
AthenaPoolExample_Copy.streamName
string streamName
Definition: AthenaPoolExample_Copy.py:39
MC::isStable
bool isStable(const T &p)
Definition: HepMCHelpers.h:30
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
xAODM4MuIntervalFilter::m_minPt
Gaudi::Property< double > m_minPt
Definition: xAODM4MuIntervalFilter.h:48
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
xAODM4MuIntervalFilter::getRandomEngine
CLHEP::HepRandomEngine * getRandomEngine(const std::string &streamName, const EventContext &ctx) const
Definition: xAODM4MuIntervalFilter.cxx:148
xAODM4MuIntervalFilter::m_prob2medium
Gaudi::Property< double > m_prob2medium
Definition: xAODM4MuIntervalFilter.h:53
HepMCHelpers.h
xAODM4MuIntervalFilter::filterEvent
virtual StatusCode filterEvent() override
Definition: xAODM4MuIntervalFilter.cxx:38