ATLAS Offline Software
Loading...
Searching...
No Matches
SelectorBase.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5/***********************************************************************
6SelectorBase.h: Base class for selectors, the derived class is a template for
7 SelectorBase, ie class Derived: public SelectorBase<Derived>
8
9
10 Expects an InputKey for the branch with objects to be selected
11 and a SelectedParticlesKey for the output branch
12
13NOTE: Derived class for container XXXContainer must define
14 public:
15 typedef XXXContainer Container;
16 typedef XXX Contained;
17
18 S. Protopopescu 15-Sept-2008
19
20***********************************************************************/
21
22#ifndef SELECTORBASE_H
23#define SELECTORBASE_H
24
26#include "AthenaKernel/Units.h"
27#include "GaudiKernel/MsgStream.h"
28#include "GaudiKernel/AlgTool.h"
29#include "GaudiKernel/ToolHandle.h"
31#include "CLHEP/Units/SystemOfUnits.h"
32#include <string>
33#include <cmath>
34
35template<class Derived>
36class SelectorBase : public AthAlgorithm {
37
38 public:
39 SelectorBase(const std::string& name, ISvcLocator* pSvcLocator);
40 virtual ~SelectorBase(){;}
41 virtual StatusCode initialize();
42 virtual StatusCode userExecute(){return StatusCode::SUCCESS;}
43 StatusCode execute();
44 virtual StatusCode finalize();
45 bool pt_eta_range(double pt, double eta);
46 // Derived class must supply
47 // bool accept(const Contained*);
48 // it should use pt_eta_range(pt,eta)
49 // For each object that this method returns true
50 // a flag will be set to true in a SelectedParticles branch
51 const std::string& getInputKey(){return m_inputKey;}
52
53 protected:
54
55 std::string m_inputKey;
56 std::string m_inselectedKey;
57 std::string m_selectedKey;
58 std::string m_inlinksKey;
59 std::string m_linksKey;
60 bool m_all;
62 bool m_inSel;
66 double m_ptmin;
67 double m_ptmax;
68 double m_etamin;
69 double m_etamax;
72};
73 //
74 // Templated implementation must go here...
75 //
76
77template<class Derived>
78 SelectorBase<Derived>::SelectorBase(const std::string& name, ISvcLocator* pSvcLocator)
79 : AthAlgorithm(name, pSvcLocator),
80 m_gensel(false),
81 m_genlink(false)
82{
83 declareProperty("InputKey", m_inputKey = "NONE");
84 declareProperty("InSelectedParticlesKey", m_inselectedKey = "NONE");
85 declareProperty("OutSelectedParticlesKey", m_selectedKey = "NONE");
86 declareProperty("InParticleLinksKey", m_inlinksKey = "NONE");
87 declareProperty("OutParticleLinksKey", m_linksKey = "NONE");
88 declareProperty("PtMin",m_ptmin = 15.0*CLHEP::GeV);
89 declareProperty("PtMax",m_ptmax = 10000.0*CLHEP::GeV);
90 declareProperty("EtaMax",m_etamax = 2.5);
91 declareProperty("EtaMin",m_etamin = -2.5);
92 declareProperty("AbsEtaMax",m_absetamax = 2.5);
93 declareProperty("AbsEtaMin",m_absetamin =0);
94 declareProperty("SelectAll",m_all =false);
95 m_nogood=false;
96 m_inSel=false;
97 m_inLink=false;
98}
99template<class Derived>
101{
102 m_genlink=true;
103 m_gensel=true;
104 ATH_MSG_INFO("Initializing Selector " <<name() );
105 if(m_inputKey==std::string("NONE")) m_nogood=true;
106 if(m_selectedKey==std::string("NONE")) m_gensel=false;
107 if(m_inselectedKey!=std::string("NONE")) m_inSel=true;
108 if(m_linksKey==std::string("NONE") ) m_genlink=false;
109 if(m_inlinksKey!=std::string("NONE")) m_inLink=true;
110
111 ATH_MSG_INFO( "InputKey="<<m_inputKey.c_str()<<", OutSelectedParticlesKey="<<m_selectedKey.c_str()
112 <<", InSelectedParticlesKey="<<m_inselectedKey.c_str()
113 <<", InParticlesLinksKey="<<m_inlinksKey.c_str()
114 <<", OutParticleLinksKey="<<m_linksKey.c_str() );
115 ATH_MSG_INFO(", PtMin="<<m_ptmin
116 << MSG::INFO<<", PtMax="<<m_ptmax
117 << MSG::INFO<<", EtaMin="<<m_etamin
118 << MSG::INFO<<", EtaMax="<<m_etamax
119 << MSG::INFO<<", AbsEtaMin="<<m_absetamin
120 << MSG::INFO<<", AbsEtaMax="<<m_absetamax
121 << MSG::INFO<<", SelectAll="<<m_all );
122 if(m_all) ATH_MSG_INFO(" All items will be accepted" );
123
125 if(m_nogood){
126 ATH_MSG_FATAL( "Must supply names for InputKey, OutSelectedParticlesKey and/or OutParticleLinksKey" );
127 }
129 if(m_nogood){
130 ATH_MSG_FATAL( "Cannot have both InSelectedParticlesKey and InParticlesLinksKey, must chose one" );
131 }
132
133 ATH_MSG_INFO("Done Initializing " <<name() );
134 return StatusCode::SUCCESS;
135}
136
137template<class Derived>
139{
140 typedef typename Derived::Container Container;
141 //typedef typename Derived::Contained Contained;
142 typedef typename Derived::Links Links;
143
144 ATH_MSG_DEBUG(std::endl );
145 ATH_MSG_DEBUG("Executing Selector " <<name() );
147 const Container* particles = 0;
148 const SelectedParticles* inselparts=0;
149 const Links* inlinks=0;
150 if(m_inSel){
151 ATH_CHECK( evtStore()->retrieve(inselparts,m_inselectedKey) );
152 }
153 if(m_inLink){
154 ATH_CHECK( evtStore()->retrieve(inlinks,m_inlinksKey) );
155 }
156
157 ATH_CHECK( evtStore()->retrieve(particles,m_inputKey) );
158 int npart=particles->size();
159 ATH_MSG_DEBUG(" >>>> got Container, no. of particles="<<npart );
160
161 SelectedParticles* selparts=0;
162 if(m_gensel){
163 // SelectedParticles option
164 selparts=new SelectedParticles();
165 ATH_MSG_DEBUG(" >>>> Generate SelectedParticles" );
166 ATH_CHECK(evtStore()->record(selparts,m_selectedKey) );
167 typename Container::const_iterator ipItr = particles->begin();
168 typename Container::const_iterator ipEnd = particles->end();
169 int npart=particles->size();
170 selparts->SetMaxBits(npart);
171 unsigned ipart=0;
172 if(!m_inLink){
173 if(m_inSel) ATH_MSG_DEBUG(" using InSelectedParticles("<<m_inselectedKey
174 <<"), no. particles="<<inselparts->numGood()
175 <<" <<<" );
176 else ATH_MSG_DEBUG(" looping over whole Container <<<" );
177 for(; ipItr != ipEnd; ++ipItr) {
178 if(m_inSel && !inselparts->isGood(ipart)){
179 ATH_MSG_DEBUG(" rejected ipart="<<ipart );
180 ipart++;
181 continue;
182 }
183 if( m_all || static_cast<Derived*>(this)->accept(*ipItr)){
184 selparts->SetBit(ipart);
185 ATH_MSG_DEBUG(" accepted ipart="<<ipart );
186 }else ATH_MSG_DEBUG(" rejected ipart="<<ipart );
187 ipart++;
188 }
189 }else{
190 // loop over input links
191 typename Links::const_iterator ilItr = inlinks->begin();
192 typename Links::const_iterator ilEnd = inlinks->end();
193 ATH_MSG_DEBUG(" using input ParticleLinks ("<<m_inlinksKey<<"), no. of particles="
194 <<inlinks->size()<<" <<<<<" );
195 int ipart=0;
196 for(; ilItr != ilEnd; ++ilItr) {
197 ipart=(*ilItr).index();
198 //const Contained* cpart=(*inlinks)[ipart];
199 //Contained* part=const_cast<Contained *>(cpart);
200 if( m_all || static_cast<Derived*>(this)->accept(*ilItr) ){
201 selparts->SetBit(ipart);
202 ATH_MSG_DEBUG(" accepted ipart="<<ipart );
203 }else ATH_MSG_DEBUG(" rejected ipart="<<ipart );
204 }
205 }
206 ATH_MSG_DEBUG(" SelectedParticles bits: "<<selparts->displayBits() );
207 } // end of SelectedParticles option
208
209 // ParticleLinks option
210 if(m_genlink){
211 ATH_MSG_DEBUG(" >>>> Generate ParticleLinks" );
212 Links* links=new Links();
213 ATH_CHECK( evtStore()->record(links,m_linksKey) );
214
215 if(m_gensel){
216 // selection already done, selparts filled
217 typename Container::const_iterator ipItr = particles->begin();
218 typename Container::const_iterator ipEnd = particles->end();
219 unsigned ipart=0;
220 for(; ipItr != ipEnd; ++ipItr) {
221 if(selparts->isGood(ipart)){
222 ElementLink<Container> el(*particles,ipart);
223 links->push_back(el);
224 }
225 ipart++;
226 }
227 return StatusCode::SUCCESS;
228 }
229 if(m_inLink){
230 typename Links::const_iterator ilItr = inlinks->begin();
231 typename Links::const_iterator ilEnd = inlinks->end();
232 ATH_MSG_DEBUG(" using input ParticleLinks ("<<m_inlinksKey<<"), no. of particles="
233 <<inlinks->size()<<" <<<<<" );
234 unsigned ipart=0;
235 for(; ilItr != ilEnd; ++ilItr) {
236 ipart=(*ilItr).index();
237 //const Contained* cpart=(*inlinks)[ipart];
238 //Contained* part=const_cast<Contained *>(cpart);
239 if( m_all || static_cast<Derived*>(this)->accept(*ilItr) ){
240 links->push_back(*ilItr);
241 ATH_MSG_DEBUG(" accepted ipart="<<ipart );
242 }else ATH_MSG_DEBUG(" rejected ipart="<<ipart );
243 }
244 }else{
245 if(m_inSel) ATH_MSG_DEBUG(" using InSelectedParticles("<<m_inselectedKey
246 <<"), no. particles="<<inselparts->numGood()
247 <<" <<<" );
248 else ATH_MSG_DEBUG(" looping over whole Container <<<" );
249 typename Container::const_iterator ipItr = particles->begin();
250 typename Container::const_iterator ipEnd = particles->end();
251 unsigned ipart=0;
252 for(; ipItr != ipEnd; ++ipItr) {
253 if(m_inSel && !inselparts->isGood(ipart)){
254 ATH_MSG_DEBUG(" rejected ipart="<<ipart );
255 ipart++;
256 continue;
257 }
258 if( m_all || static_cast<Derived*>(this)->accept(*ipItr) ){
259 ElementLink<Container> el(*particles,ipart);
260 links->push_back(el);
261 ATH_MSG_DEBUG(" accepted ipart="<<ipart<<", pt="<<(*el)->pt()/Athena::Units::GeV );
262 }else ATH_MSG_DEBUG(" rejected ipart="<<ipart );
263 ipart++;
264 }
265 }
266 } // end of ParticleLinks option
267 return StatusCode::SUCCESS;
268}
269
270template<class Derived>
272
273 ATH_MSG_INFO( "Finalizing Selector " << name() );
274
275 return StatusCode::SUCCESS;
276}
277
278template<class Derived>
280 if(pt<m_ptmin) return false;
281 if(pt>m_ptmax) return false;
282 if(fabs(eta)>m_absetamax) return false;
283 if(fabs(eta)<m_absetamin) return false;
284 if(eta>m_etamax) return false;
285 if(eta<m_etamin) return false;
286 return true;
287 }
288#endif
Scalar eta() const
pseudorapidity method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
Wrapper to avoid constant divisions when using units.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
storage of the time histories of all the cells
void SetMaxBits(unsigned maxbits)
void SetBit(unsigned i, bool good=true)
std::string displayBits() const
bool isGood(unsigned i) const
unsigned numGood() const
SelectorBase(const std::string &name, ISvcLocator *pSvcLocator)
virtual StatusCode finalize()
double m_absetamax
std::string m_linksKey
std::string m_inputKey
std::string m_inlinksKey
double m_etamin
std::string m_inselectedKey
double m_etamax
virtual StatusCode initialize()
double m_absetamin
bool pt_eta_range(double pt, double eta)
StatusCode execute()
virtual ~SelectorBase()
virtual StatusCode userExecute()
std::string m_selectedKey
const std::string & getInputKey()