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