ATLAS Offline Software
xAODTauFilter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
6 #include "CLHEP/Random/RandomEngine.h"
9 
10 #include "TMath.h"
11 #include <vector>
12 
13 
14 xAODTauFilter::xAODTauFilter( const std::string& name, ISvcLocator* pSvcLocator)
15  : GenFilter( name,pSvcLocator ),
16  m_eventse(0), m_eventsmu(0), m_eventshad(0),
17  m_eventseacc(0), m_eventsmuacc(0), m_eventshadacc(0)
18 {
19  declareProperty( "Ntaus", m_Ntau = 1 );
20  declareProperty( "MaxNtaus", m_maxNtau = 100 );
21  declareProperty( "EtaMaxe", m_etaMaxe = 2.5 );
22  declareProperty( "EtaMaxmu", m_etaMaxmu = 2.5 );
23  declareProperty( "EtaMaxhad", m_etaMaxhad = 2.5 );
24 
25  declareProperty( "Ptcute", m_pTmine = 12000.0 );
26  declareProperty( "Ptcutmu", m_pTminmu = 12000.0 );
27  declareProperty( "Ptcuthad", m_pTminhad = 12000.0 );
28 
29  // new options:
30  declareProperty( "UseNewOptions", m_NewOpt = false );
31  declareProperty( "UseMaxNTaus", m_useMaxNTaus = false );
32  declareProperty( "Nhadtaus", m_Nhadtau = 0 );
33  declareProperty( "MaxNhadtaus", m_maxNhadtau = 100 );
34  declareProperty( "Nleptaus", m_Nleptau = 0 );
35  declareProperty( "MaxNleptaus", m_maxNleptau = 100 );
36  declareProperty( "EtaMaxlep", m_etaMaxlep = 2.6 );
37  declareProperty( "Ptcutlep", m_pTminlep = 7000.0 );
38  declareProperty( "Ptcutlep_lead", m_pTminlep_lead = 7000.0);
39  declareProperty( "Ptcuthad_lead", m_pTminhad_lead = 12000.0 );
40  declareProperty( "ReverseFilter", m_ReverseFilter = false);
41 
42  declareProperty( "HasTightRegion", m_HasTightRegion = false);
43  declareProperty( "LooseRejectionFactor", m_LooseRejectionFactor = 1);
44  declareProperty( "Ptcutlep_tight", m_pTminlep_tight = 7000.0 );
45  declareProperty( "Ptcutlep_tight_lead", m_pTminlep_tight_lead = 7000.0 );
46  declareProperty( "Ptcuthad_tight", m_pTminhad_tight = 12000.0 );
47  declareProperty( "Ptcuthad_tight_lead", m_pTminhad_tight_lead = 12000.0 );
48 
49  declareProperty( "filterEventNumber", m_filterEventNumber = 0 );
50 }
51 
52 
54  m_eventse = 0;
55  m_eventsmu = 0;
56  m_eventshad = 0;
57  m_eventseacc = 0;
58  m_eventsmuacc = 0;
59  m_eventshadacc = 0;
60 
61  for(int i=0; i<6; i++) {
62  m_events[i] = 0; m_events_sel[i] = 0;
63  }
64 
65  CHECK(m_rndmSvc.retrieve());
66 
67  return StatusCode::SUCCESS;
68 }
69 
70 
72  if(m_NewOpt) {
73  ATH_MSG_INFO("Sum of Events total: " << m_events[0] << " , selected: " << m_events_sel[0]);
74  ATH_MSG_INFO("Sum of Events with pos. weights total: " << m_events[1] << " , selected: " << m_events_sel[1]);
75  ATH_MSG_INFO("Sum of Events with neg. weights total: " << m_events[2] << " , selected: " << m_events_sel[2]);
76  ATH_MSG_INFO("Sum of weights total: " << m_events[3] << " , selected: " << m_events_sel[3]);
77  ATH_MSG_INFO("Sum of pos. weights total: " << m_events[4] << " , selected: " << m_events_sel[4]);
78  ATH_MSG_INFO("Sum of neg. weights total: " << m_events[5] << " , selected: " << m_events_sel[5]);
79  }
80  else {
81  ATH_MSG_INFO(" , e: " << m_eventse << " , mu: " << m_eventsmu << " , had: " << m_eventshad <<
82  " , eacc: " << m_eventseacc << " , muacc: " << m_eventsmuacc << " , hadacc: " << m_eventshadacc);
83  }
84 
85  return StatusCode::SUCCESS;
86 }
87 
88 
90  // Get random number engine
91  CLHEP::HepRandomEngine* rndm{};
92  if(m_HasTightRegion) {
93  const EventContext& ctx = Gaudi::Hive::currentContext();
94  rndm = this->getRandomEngine(name(), ctx);
95  if (!rndm) {
96  ATH_MSG_ERROR("Failed to retrieve random number engine for xAODTauFilter");
97  setFilterPassed(false);
98  return StatusCode::SUCCESS;
99  }
100  }
101 
102  CLHEP::HepLorentzVector mom_tauprod; // will contain the momentum of the products of the tau decay
103  CLHEP::HepLorentzVector tauvis;
104  CLHEP::HepLorentzVector nutau;
105  int ntau = 0;
106 
107  double ptlep_max = 0;
108  double pthad_max = 0;
109  int ntaulep = 0;
110  int ntauhad = 0;
111  int ntaulep_tight = 0;
112  int ntauhad_tight = 0;
113  double weight = 1;
114 
115 
116  const xAOD::TruthParticleContainer* vtruth = nullptr;
117  ATH_CHECK( evtStore()->retrieve( vtruth, "TruthTaus" ) );
118 
119  //get the weight of the event from McEventCollection
120  setFilterPassed(false);
121  for(const HepMC::GenEvent* genEvt : *events_const()) {
122  int eventNumber = genEvt->event_number();
123  if(m_filterEventNumber==1 && (eventNumber%2)==0) {
124  setFilterPassed(false);
125  return StatusCode::SUCCESS;
126  }
127  else if(m_filterEventNumber==2 && (eventNumber%2)==1) {
128  setFilterPassed(false);
129  return StatusCode::SUCCESS;
130  }
131 
132  auto wgtsC = genEvt->weights();
133  weight = wgtsC.size() > 0 ? wgtsC[0] : 1;
134  }
135 
136 
137  for (const auto * truthtau : *vtruth) {
138  // Look for the first physical tau
139  if (MC::isTau(truthtau) && MC::isPhysical(truthtau)) {
140  const xAOD::TruthParticle* tau = truthtau;
141  ATH_MSG_DEBUG("found tau " << tau);
142  ATH_MSG_DEBUG("pT\t\teta\tphi\tid");
143  ATH_MSG_DEBUG(tau->pt() << "\t" <<
144  tau->abseta() << "\t" <<
145  tau->phi() << "\t" <<
146  tau->pdgId() << "\t");
147 
148 // tau type:
149 // 1- decays into electron
150 // 2- decays into muon
151 // 11 - decaus into tauon
152 // 0 - unset
153 
154  static const SG::ConstAccessor<int> tauTypeAcc ("tauType");
155  int tauType = tauTypeAcc (*tau);
156 
157  if (tauType == 11) {
158  ATH_MSG_DEBUG("tau has a tau as daughter - skipping");
159  continue;
160  }
161 
162  static const SG::ConstAccessor<CLHEP::HepLorentzVector> nuVectorAcc ("nuVector");
163  nutau = nuVectorAcc (*tau);
164  ATH_MSG_DEBUG("pT\t\teta\tphi\tlh");
165  ATH_MSG_DEBUG(nutau.perp() << " nutau \t" << nutau.eta() << "\t" << nutau.phi() << "\t" << tauType);
166 
167  tauvis.setPx(tau->px()-nutau.px());
168  tauvis.setPy(tau->py()-nutau.py());
169  tauvis.setPz(tau->pz()-nutau.pz());
170  tauvis.setE(tau->e()-nutau.e());
171 
172  ATH_MSG_DEBUG(tauvis.perp() << " tauvis \t" << tauvis.eta() << "\t" << tauvis.phi() << "\t" << tauType);
173  if ( tauType == 1 ) {
174  if(!m_NewOpt) {
175  m_eventse++;
176  if ( tauvis.perp() < m_pTmine ) continue;
177  if ( std::abs( tauvis.eta() ) > m_etaMaxe ) continue;
178  ntau++;
179  m_eventseacc++;
180  }
181  else {
182  if ( tauvis.perp() < m_pTminlep ) continue;
183  if ( std::abs( tauvis.eta() ) > m_etaMaxlep ) continue;
184  ntaulep++;
185  if ( tauvis.perp() >= ptlep_max ) ptlep_max = tauvis.perp();
186  if ( tauvis.perp() >= m_pTminlep_tight ) ntaulep_tight++;
187  }
188  }
189 
190  else if ( tauType == 2 ) {
191  if(!m_NewOpt) {
192  m_eventsmu++;
193  if ( tauvis.perp() < m_pTminmu ) continue;
194  if ( std::abs( tauvis.eta() ) > m_etaMaxmu ) continue;
195  ntau++;
196  m_eventsmuacc++;
197  }
198  else {
199  if ( tauvis.perp() < m_pTminlep ) continue;
200  if ( std::abs( tauvis.eta() ) > m_etaMaxlep ) continue;
201  ntaulep++;
202  if ( tauvis.perp() >= ptlep_max ) ptlep_max = tauvis.perp();
203  if ( tauvis.perp() >= m_pTminlep_tight ) ntaulep_tight++;
204  }
205  }
206 
207  else if ( tauType == 0 ) {
208  m_eventshad++;
209  if ( tauvis.perp() < m_pTminhad ) continue;
210  if ( std::abs( tauvis.eta() ) > m_etaMaxhad ) continue;
211  ntau++;
212  m_eventshadacc++;
213  ntauhad++;
214  if ( tauvis.perp() >= pthad_max ) pthad_max = tauvis.perp();
215  if ( tauvis.perp() >= m_pTminhad_tight ) ntauhad_tight++;
216  }
217 
218  else {
219  ATH_MSG_DEBUG("Could not find a tauType! Something went wrong.");
220  std::cout << std::endl << std::endl <<"************ COULD NOT FIND A TAU TYPE *****************" << std::endl << std::endl;
221  }
222  }
223  }
224 
225  bool pass1 = ( ntaulep + ntauhad >= m_Ntau //For use with UseNewOptions
226  && ntaulep >= m_Nleptau
227  && ntauhad >= m_Nhadtau
228  && (ntaulep<2 || ptlep_max>=m_pTminlep_lead)
229  && (ntauhad<2 || pthad_max>=m_pTminhad_lead)
230  );
231  bool pass2 = ( ntaulep_tight + ntauhad_tight >= m_Ntau //For use with UseNewOptions + HssTightRegion
232  && ntaulep_tight >= m_Nleptau
233  && ntauhad_tight >= m_Nhadtau
234  && (ntaulep_tight<2 || ptlep_max>=m_pTminlep_tight_lead)
235  && (ntauhad_tight<2 || pthad_max>=m_pTminhad_tight_lead)
236  );
237 
238  bool pass3 = (ntaulep + ntauhad >= m_Ntau //For use with UseNewOptions + UseMaxNTaus
239  && ntaulep + ntauhad <= m_maxNtau
240  && ntaulep >= m_Nleptau
241  && ntauhad >= m_Nhadtau
242  && ntaulep <= m_maxNleptau
243  && ntauhad <= m_maxNhadtau
244  );
245 
246  bool pass = false; //Initializing the pass variable that will be checked to se if the filter is passed.
247 
248  if (m_NewOpt) pass = pass1;
249  if (m_useMaxNTaus) pass = pass3;
250 
251 
252  double extra_weight = 1;
253  if(m_NewOpt && m_HasTightRegion) {
254  if (pass2) {
255  pass = pass2;
256  extra_weight = 1/m_LooseRejectionFactor;
257  weight *= extra_weight;
258  }
259  else if (pass1) {
260  double rnd = rndm->flat(); // a random number between (0,1)
261  if(rnd > 1/m_LooseRejectionFactor) pass = false;
262  else pass = true;
263  }
264  else pass = false;
265  }
266 
267  m_events[0]++;
268  m_events[3] += weight;
269  if(weight>=0) {
270  m_events[1]++;
271  m_events[4] += weight;
272  }
273  else {
274  m_events[2]++;
275  m_events[5] += weight;
276  }
277 
278  if(pass) {
279  m_events_sel[0]++;
280  m_events_sel[3] += weight;
281  if(weight>=0) {
282  m_events_sel[1]++;
283  m_events_sel[4] += weight;
284  }
285  else {
286  m_events_sel[2]++;
287  m_events_sel[5] += weight;
288  }
289  }
290 
291  if(m_NewOpt && m_HasTightRegion) {
292  // Get MC event collection for setting weight
293  const McEventCollection* mecc = 0;
294  if ( evtStore()->retrieve( mecc ).isFailure() || !mecc ){
295  setFilterPassed(false);
296  ATH_MSG_ERROR("Could not retrieve MC Event Collection - weight might not work");
297  return StatusCode::SUCCESS;
298  }
299 
300  // Event passed. Will weight events
301  McEventCollection* mec = const_cast<McEventCollection*> (&(*mecc));
302  for (unsigned int i = 0; i < mec->size(); ++i) {
303  if (!(*mec)[i]) continue;
304  double existingWeight = (*mec)[i]->weights().size()>0 ? (*mec)[i]->weights()[0] : 1.;
305  if ((*mec)[i]->weights().size()>0) {
306  for (unsigned int iw = 0; iw < (*mec)[i]->weights().size(); ++iw) {
307  double existWeight = (*mec)[i]->weights()[iw];
308  (*mec)[i]->weights()[iw] = existWeight*extra_weight;
309  }
310 // in case modification of a nominal weight is only needed uncomment the line below
311 // (*mec)[i]->weights()[0] = existingWeight*extra_weight;
312  } else {
313  (*mec)[i]->weights().push_back( existingWeight*extra_weight );
314  }
315 
316 #ifdef HEPMC3
317  (*mec)[i]->add_attribute("filterWeight", std::make_shared<HepMC3::DoubleAttribute>(extra_weight));
318 #endif
319 
320  }
321  }
322 
323  if(!m_NewOpt) pass = (ntau >= m_Ntau); //If UseNewOptions is not enabled, only look at total number of taus present.
324 
325  if (m_ReverseFilter) pass = !pass; //If reverse filter is active, flip the truth value of pass
326 
327  setFilterPassed(pass);
328 
329  return StatusCode::SUCCESS;
330 }
331 
332 
333 CLHEP::HepRandomEngine* xAODTauFilter::getRandomEngine(const std::string& streamName,
334  const EventContext& ctx) const
335 {
336  ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, streamName);
337  std::string rngName = name()+streamName;
338  rngWrapper->setSeed( rngName, ctx );
339  return rngWrapper->getEngine(ctx);
340 }
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
xAODTauFilter::m_etaMaxhad
double m_etaMaxhad
Definition: xAODTauFilter.h:41
xAODTauFilter::filterInitialize
StatusCode filterInitialize()
Definition: xAODTauFilter.cxx:53
xAODTauFilter.h
xAODTauFilter::m_pTminhad_lead
double m_pTminhad_lead
Definition: xAODTauFilter.h:54
xAODTauFilter::m_maxNhadtau
int m_maxNhadtau
Definition: xAODTauFilter.h:67
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAOD::TruthParticle_v1::pz
float pz() const
The z component of the particle's momentum.
xAODTauFilter::m_pTminlep_tight
double m_pTminlep_tight
Definition: xAODTauFilter.h:58
xAODTauFilter::m_rndmSvc
ServiceHandle< IAthRNGSvc > m_rndmSvc
Definition: xAODTauFilter.h:36
xAODTauFilter::filterEvent
StatusCode filterEvent()
Definition: xAODTauFilter.cxx:89
GenBase::events_const
const McEventCollection * events_const() const
Access the current event's McEventCollection (const)
Definition: GenBase.h:96
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
xAODTauFilter::m_pTminhad_tight
double m_pTminhad_tight
Definition: xAODTauFilter.h:60
xAODTauFilter::m_pTminlep_tight_lead
double m_pTminlep_tight_lead
Definition: xAODTauFilter.h:59
xAODTauFilter::m_eventsmu
double m_eventsmu
Definition: xAODTauFilter.h:77
xAOD::TruthParticle_v1::px
float px() const
The x component of the particle's momentum.
xAOD::TruthParticle_v1::py
float py() const
The y component of the particle's momentum.
SG::ConstAccessor< int >
xAODTauFilter::m_pTminhad_tight_lead
double m_pTminhad_tight_lead
Definition: xAODTauFilter.h:61
xAODTauFilter::m_useMaxNTaus
bool m_useMaxNTaus
Definition: xAODTauFilter.h:65
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
plotBeamSpotCompare.pass2
bool pass2
Definition: plotBeamSpotCompare.py:269
MC::isPhysical
bool isPhysical(const T &p)
Definition: HepMCHelpers.h:32
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
xAODTauFilter::m_Ntau
int m_Ntau
Definition: xAODTauFilter.h:38
xAODTauFilter::m_etaMaxmu
double m_etaMaxmu
Definition: xAODTauFilter.h:40
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
xAOD::TruthParticle_v1::e
virtual double e() const override final
The total energy of the particle.
plotBeamSpotCompare.pass1
bool pass1
Definition: plotBeamSpotCompare.py:231
xAODTauFilter::m_maxNtau
int m_maxNtau
Definition: xAODTauFilter.h:66
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAODTauFilter::m_pTminlep_lead
double m_pTminlep_lead
Definition: xAODTauFilter.h:53
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
xAODTauFilter::getRandomEngine
CLHEP::HepRandomEngine * getRandomEngine(const std::string &streamName, const EventContext &ctx) const
Definition: xAODTauFilter.cxx:333
xAODTauFilter::m_events_sel
double m_events_sel[6]
Definition: xAODTauFilter.h:74
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
xAODTauFilter::m_pTminlep
double m_pTminlep
Definition: xAODTauFilter.h:52
xAOD::eventNumber
eventNumber
Definition: EventInfo_v1.cxx:124
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAODTauFilter::m_eventse
double m_eventse
Definition: xAODTauFilter.h:76
isTau
bool isTau(const T &p)
Definition: AtlasPID.h:148
xAODTauFilter::m_pTminmu
double m_pTminmu
Definition: xAODTauFilter.h:44
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
xAODTauFilter::m_NewOpt
bool m_NewOpt
Definition: xAODTauFilter.h:48
xAODTauFilter::m_maxNleptau
int m_maxNleptau
Definition: xAODTauFilter.h:68
xAODTauFilter::m_Nhadtau
int m_Nhadtau
Definition: xAODTauFilter.h:50
xAODTauFilter::m_eventshadacc
double m_eventshadacc
Definition: xAODTauFilter.h:82
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
xAODTauFilter::m_events
double m_events[6]
Definition: xAODTauFilter.h:73
xAODTauFilter::m_pTminhad
double m_pTminhad
Definition: xAODTauFilter.h:45
AthenaPoolExample_Copy.streamName
string streamName
Definition: AthenaPoolExample_Copy.py:39
xAODTauFilter::m_filterEventNumber
int m_filterEventNumber
Definition: xAODTauFilter.h:62
xAODTauFilter::m_ReverseFilter
bool m_ReverseFilter
Definition: xAODTauFilter.h:55
xAODTauFilter::m_LooseRejectionFactor
double m_LooseRejectionFactor
Definition: xAODTauFilter.h:57
xAOD::TruthParticle_v1::phi
virtual double phi() const override final
The azimuthal angle ( ) of the particle.
Definition: TruthParticle_v1.cxx:181
xAODTauFilter::m_eventsmuacc
double m_eventsmuacc
Definition: xAODTauFilter.h:81
xAODTauFilter::m_HasTightRegion
bool m_HasTightRegion
Definition: xAODTauFilter.h:56
xAODTauFilter::m_Nleptau
int m_Nleptau
Definition: xAODTauFilter.h:49
xAOD::TruthParticle_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: TruthParticle_v1.cxx:166
xAODTauFilter::filterFinalize
StatusCode filterFinalize()
Definition: xAODTauFilter.cxx:71
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
xAODTauFilter::m_pTmine
double m_pTmine
Definition: xAODTauFilter.h:43
xAOD::TruthParticle_v1::pdgId
int pdgId() const
PDG ID code.
xAODTauFilter::m_eventshad
double m_eventshad
Definition: xAODTauFilter.h:78
xAODTauFilter::xAODTauFilter
xAODTauFilter(const std::string &name, ISvcLocator *pSvcLocator)
Definition: xAODTauFilter.cxx:14
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
xAODTauFilter::m_eventseacc
double m_eventseacc
Definition: xAODTauFilter.h:80
HepMCHelpers.h
xAOD::TruthParticle_v1::abseta
double abseta() const
The absolute pseudorapidity ( ) of the particle.
Definition: TruthParticle_v1.cxx:218
xAODTauFilter::m_etaMaxe
double m_etaMaxe
Definition: xAODTauFilter.h:39
xAODTauFilter::m_etaMaxlep
double m_etaMaxlep
Definition: xAODTauFilter.h:51