ATLAS Offline Software
M4MuIntervalFilter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Header for this module
8 
10 #include "CLHEP/Random/RandomEngine.h"
11 
12 // Pt High --> Low
13 namespace {
14  class High2LowByPt {
15  public:
16  bool operator () (HepMC::FourVector t1, HepMC::FourVector t2) {
17  return (t1.perp() > t2.perp());
18  }
19  };
20 } // namespace
21 
22 
23 M4MuIntervalFilter::M4MuIntervalFilter(const std::string& name, ISvcLocator* pSvcLocator)
24  : GenFilter(name, pSvcLocator)
25 {
26  declareProperty("MaxEta", m_maxEta = 5.0);
27  declareProperty("MinPt", m_minPt = 1000);
28  declareProperty("LowM4muProbability", m_prob2low = 1.0);
29  declareProperty("MediumMj4muProbability", m_prob2medium = 0.5);
30  declareProperty("HighM4muProbability", m_prob2high = 0.1);
31  declareProperty("LowM4mu", m_m4mulow=11000);
32  declareProperty("HighM4mu", m_m4muhigh=25000);
33  declareProperty("ApplyReWeighting", m_ApplyReWeighting = true);
34 }
35 
37 
39 
40  CHECK(m_rndmSvc.retrieve());
41 
42  ATH_MSG_DEBUG( "MaxEta " << m_maxEta);
43  ATH_MSG_DEBUG( "MinPt " << m_minPt);
44  ATH_MSG_DEBUG( "LowM4muProbability " << m_prob2low);
45  ATH_MSG_DEBUG( "MediumMj4muProbability " << m_prob2medium);
46  ATH_MSG_DEBUG( "HighM4muProbability " << m_prob2high);
47  ATH_MSG_DEBUG( "LowM4mu " << m_m4mulow);
48  ATH_MSG_DEBUG( "HighM4mu " << m_m4muhigh);
49  ATH_MSG_DEBUG( "ApplyReWeighting " << m_ApplyReWeighting);
50  return StatusCode::SUCCESS;
51 }
52 
54  return StatusCode::SUCCESS;
55 }
56 
58  // Get random number engine
59  const EventContext& ctx = Gaudi::Hive::currentContext();
60  CLHEP::HepRandomEngine* rndm = this->getRandomEngine(name(), ctx);
61  if (!rndm) {
62  ATH_MSG_ERROR("Failed to retrieve random number engine M4MuIntervalFilter");
63  setFilterPassed(false);
64  return StatusCode::FAILURE;
65  }
66 
67  // Find overlap objects
68  std::vector<HepMC::FourVector> MCTruthMuonList;
69 
70  for (McEventCollection::const_iterator itr = events()->begin(); itr != events()->end(); ++itr) {
71  const HepMC::GenEvent* genEvt = (*itr);
72  for (const auto& pitr: *genEvt){
73 
74  // muon
75  if (MC::isMuon(pitr) && MC::isStable(pitr) &&
76  (pitr)->momentum().perp() >= m_minPt &&
77  std::abs((pitr)->momentum().pseudoRapidity()) <= m_maxEta) {
78  HepMC::FourVector tmp((pitr)->momentum().px(), (pitr)->momentum().py(), (pitr)->momentum().pz(), (pitr)->momentum().e());
79  MCTruthMuonList.push_back(tmp);
80  }
81  }
82  }
83 
84  std::sort(MCTruthMuonList.begin(), MCTruthMuonList.end(), High2LowByPt());
85 
86  if(MCTruthMuonList.size()<4){
87  setFilterPassed(false);
88  ATH_MSG_DEBUG("Less than 4 muons. The muon number is " << MCTruthMuonList.size());
89  return StatusCode::SUCCESS;
90  }
91 
92  if(m_ApplyReWeighting) {
93  HepMC::FourVector vec4mu(MCTruthMuonList.at(0).px() + MCTruthMuonList.at(1).px() + MCTruthMuonList.at(2).px() + MCTruthMuonList.at(3).px(),
94  MCTruthMuonList.at(0).py() + MCTruthMuonList.at(1).py() + MCTruthMuonList.at(2).py() + MCTruthMuonList.at(3).py(),
95  MCTruthMuonList.at(0).pz() + MCTruthMuonList.at(1).pz() + MCTruthMuonList.at(2).pz() + MCTruthMuonList.at(3).pz(),
96  MCTruthMuonList.at(0).e() + MCTruthMuonList.at(1).e() + MCTruthMuonList.at(2).e() + MCTruthMuonList.at(3).e() );
97 
98  double m4mu = vec4mu.m();
99  double eventWeight = 1.0;
100  eventWeight = getEventWeight(m4mu);
101  double rnd = rndm->flat();
102  if (1.0/eventWeight < rnd) {
103  setFilterPassed(false);
104  ATH_MSG_DEBUG("Event failed weighting. Weight is " << eventWeight);
105  return StatusCode::SUCCESS;
106  }
107 
108  // Get MC event collection for setting weight
109  const McEventCollection* mecc = 0;
110  if ( evtStore()->retrieve( mecc ).isFailure() || !mecc ){
111  setFilterPassed(false);
112  ATH_MSG_ERROR("Could not retrieve MC Event Collection - weight might not work");
113  return StatusCode::FAILURE;
114  }
115 
116  ATH_MSG_INFO("Event passed. Will weight events " << eventWeight);
117  McEventCollection* mec = const_cast<McEventCollection*> (&(*mecc));
118  for (unsigned int i = 0; i < mec->size(); ++i) {
119  if (!(*mec)[i]) continue;
120  double existingWeight = (*mec)[i]->weights().size()>0 ? (*mec)[i]->weights()[0] : 1.;
121  if ((*mec)[i]->weights().size()>0) {
122  (*mec)[i]->weights()[0] = existingWeight*eventWeight;
123  } else {
124  (*mec)[i]->weights().push_back( eventWeight*existingWeight );
125  }
126  }
127  }
128  // Made it to the end - success!
129  setFilterPassed(true);
130  return StatusCode::SUCCESS;
131 }
132 
133 
135  double weight = 1.0;
136  if (mass < m_m4mulow) {
137  weight /= m_prob2low;
138 
139  } else if (mass > m_m4muhigh) {
140  weight /= m_prob2high;
141 
142  } else {
144  }
145  ATH_MSG_DEBUG("WEIGHTING:: " << mass << "\t" << weight);
146  return weight;
147 }
148 
149 
150 CLHEP::HepRandomEngine* M4MuIntervalFilter::getRandomEngine(const std::string& streamName,
151  const EventContext& ctx) const
152 {
153  ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, streamName);
154  std::string rngName = name()+streamName;
155  rngWrapper->setSeed( rngName, ctx );
156  return rngWrapper->getEngine(ctx);
157 }
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
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
test_pyathena.px
px
Definition: test_pyathena.py:18
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
M4MuIntervalFilter.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
ALFA_EventTPCnv_Dict::t1
std::vector< ALFA_RawDataCollection_p1 > t1
Definition: ALFA_EventTPCnvDict.h:43
M4MuIntervalFilter::getEventWeight
double getEventWeight(double m) const
Definition: M4MuIntervalFilter.cxx:134
M4MuIntervalFilter::m_m4mulow
double m_m4mulow
Definition: M4MuIntervalFilter.h:37
M4MuIntervalFilter::filterEvent
virtual StatusCode filterEvent()
Definition: M4MuIntervalFilter.cxx:57
M4MuIntervalFilter::m_prob2low
double m_prob2low
Definition: M4MuIntervalFilter.h:35
M4MuIntervalFilter::filterInitialize
virtual StatusCode filterInitialize()
Definition: M4MuIntervalFilter.cxx:38
M4MuIntervalFilter::m_maxEta
double m_maxEta
Definition: M4MuIntervalFilter.h:29
python.DataFormatRates.events
events
Definition: DataFormatRates.py:105
dqt_zlumi_pandas.mass
mass
Definition: dqt_zlumi_pandas.py:170
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
M4MuIntervalFilter::m_rndmSvc
ServiceHandle< IAthRNGSvc > m_rndmSvc
Definition: M4MuIntervalFilter.h:32
ParticleGun_EoverP_Config.momentum
momentum
Definition: ParticleGun_EoverP_Config.py:63
lumiFormat.i
int i
Definition: lumiFormat.py:92
M4MuIntervalFilter::getRandomEngine
CLHEP::HepRandomEngine * getRandomEngine(const std::string &streamName, const EventContext &ctx) const
Definition: M4MuIntervalFilter.cxx:150
M4MuIntervalFilter::~M4MuIntervalFilter
virtual ~M4MuIntervalFilter()
Definition: M4MuIntervalFilter.cxx:36
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
M4MuIntervalFilter::m_ApplyReWeighting
bool m_ApplyReWeighting
Definition: M4MuIntervalFilter.h:39
Amg::pz
@ pz
Definition: GeoPrimitives.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
M4MuIntervalFilter::m_prob2high
double m_prob2high
Definition: M4MuIntervalFilter.h:36
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
M4MuIntervalFilter::filterFinalize
virtual StatusCode filterFinalize()
Definition: M4MuIntervalFilter.cxx:53
Amg::py
@ py
Definition: GeoPrimitives.h:39
M4MuIntervalFilter::M4MuIntervalFilter
M4MuIntervalFilter(const std::string &name, ISvcLocator *pSvcLocator)
Definition: M4MuIntervalFilter.cxx:23
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
M4MuIntervalFilter::m_prob2medium
double m_prob2medium
Definition: M4MuIntervalFilter.h:34
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
ALFA_EventTPCnv_Dict::t2
std::vector< ALFA_RawDataContainer_p1 > t2
Definition: ALFA_EventTPCnvDict.h:44
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
M4MuIntervalFilter::m_minPt
double m_minPt
Definition: M4MuIntervalFilter.h:30
M4MuIntervalFilter::m_m4muhigh
double m_m4muhigh
Definition: M4MuIntervalFilter.h:38
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
HepMCHelpers.h
isMuon
bool isMuon(const T &p)
Definition: AtlasPID.h:145