LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PIDHandler.cc
Go to the documentation of this file.
1 #include "UTIL/PIDHandler.h"
2 #include "Exceptions.h"
3 
5 #include "IMPL/ClusterImpl.h"
6 
7 #include <sstream>
8 
9 using namespace IMPL;
10 using namespace EVENT;
11 
12 namespace UTIL{
13 
14 
15  enum ObjectType {
16  Cluster = 1 ,
18  } ;
19 
20 
21  PIDHandler::PIDHandler( LCCollection* col ) :
22  _col( col) ,
23  _cpm( "PIDAlgorithmTypeName", "PIDAlgorithmTypeID" , col ),
24  _type(-1) ,
25  _maxID(-1) {
26 
27  init( col ) ;
28  }
29 
30 
32  _col( 0 ) , // not needed
33  _cpm( "PIDAlgorithmTypeName", "PIDAlgorithmTypeID" , col ),
34  _type(-1) ,
35  _maxID(-1) {
36 
37  init( col ) ;
38  }
39 
40 
41  void PIDHandler::init( const LCCollection* col ) {
42 
43 
44  // ---- get the information on existing ParticleID objects ----------
45 
46 
47  for( CPM::map_type::iterator it = _cpm.map().begin() ; it != _cpm.map().end() ; ++it) {
48 
49  int id = it->second ;
50 
51  const std::string& aName = it->first ;
52 
53  if( id > _maxID )
54  _maxID = id ;
55 
56  // ensure id is unique for collection
57  PNM::iterator nit = _pNames.find( id ) ;
58 
59  if( nit != _pNames.end() ){
60 
61  std::stringstream sstr ;
62 
63  sstr << " PIDHandler::PIDHandler() - duplicate algorithm type IDs: "
64  << aName << " [" << id << "] " ;
65 
66  throw Exception( sstr.str() ) ;
67 
68  }
69 
70  // get parameter names
71 
72  StringVec pNames ;
73 
74  col->getParameters().getStringVals( std::string( "ParameterNames_" + aName ) , pNames ) ;
75 
76  _pNames[ id ] = pNames ; // save a copy of parameter names for algorithm with id
77 
78  _ids.push_back( id ) ;
79 
80  // save inverse map for lookup algoName from algoID
81 
82  _cpmInv.insert( std::make_pair( id , aName ) ) ;
83 
84  }
85 
86  //------------------------------------------------
87 
88  // determine LCCollection type
89  std::string colType = col->getTypeName() ;
90 
91  // arghhh ...
92  if( colType == LCIO::RECONSTRUCTEDPARTICLE ) {
93 
95 
96  } else if( colType == LCIO::CLUSTER ){
97 
98  _type = Cluster ;
99 
100  } else {
101 
102  throw Exception( "PIDHandler::PIDHandler() "
103  " collection type is neither Cluster nor ReconstructedParticle ") ;
104  }
105  }
106 
107 
108 
109 
111 
112 
113  if (_col != 0 ) {
114  // save the collection parameters
115 
116  for( PNM::iterator pnm = _pNames.begin() ; pnm != _pNames.end() ; ++pnm) {
117 
118  int id = pnm->first ;
119 
120 
121  _col->parameters().setValues( std::string( "ParameterNames_" + _cpmInv[id] ) ,
122  pnm->second ) ;
123  }
124  }
125  }
126 
127  int PIDHandler::addAlgorithm( const std::string& algoName, const StringVec& pNames ) {
128 
129  CPM::map_type& map = _cpm.map() ;
130 
131  CPM::map_type::iterator it = map.find( algoName ) ;
132 
133  if( it != map.end() ){
134 
135  std::stringstream sstr ;
136 
137  sstr << " PIDHandler::addAlgorithm() - duplicate algorithm name: "
138  << algoName ;
139 
140  throw Exception( sstr.str() ) ;
141  }
142 
143  int id = nextID() ;
144 
145  map.insert( std::make_pair( algoName , id ) ) ;
146 
147  // inverse map:
148  _cpmInv.insert( std::make_pair( id , algoName ) ) ;
149 
150  // save parameter names
151  _pNames[ id ] = pNames ;
152 
153  _ids.push_back( id ) ;
154 
155  return id ;
156  }
157 
158  int PIDHandler::getAlgorithmID( const std::string& algoName ) {
159 
160  CPM::map_type::iterator it = _cpm.map().find( algoName ) ;
161 
162  if( it == _cpm.map().end() ){
163 
164  throw UnknownAlgorithm( algoName ) ;
165  }
166 
167  return it->second ;
168  }
169 
171 
172  CPMINV::iterator it = _cpmInv.find( algoID ) ;
173 
174  if( it == _cpmInv.end() ){
175 
176  std::stringstream ss ; ss << algoID ;
177  throw UnknownAlgorithm( ss.str().c_str() ) ;
178  }
179 
180  return it->second ;
181  }
182 
183 
184 
185  int PIDHandler::getParameterIndex( int algorithmID, const std::string& name ) {
186 
187 
188  PNM::iterator nit = _pNames.find( algorithmID ) ;
189 
190  if( nit == _pNames.end() ){
191 
192  std::stringstream ss ; ss << algorithmID ;
193  throw UnknownAlgorithm( ss.str().c_str() ) ;
194  }
195  // brute force search:
196 
197  const StringVec& names = nit->second ;
198 
199  unsigned n = names.size() ;
200 
201  for(unsigned i=0 ; i<n ; ++i){
202 
203  if( names[i] == name )
204 
205  return i ;
206  }
207 
208  return -1 ; // or better throw sth. ?
209  }
210 
211 
213 
214  PNM::iterator nit = _pNames.find( id ) ;
215 
216  if( nit == _pNames.end() ){
217 
218  std::stringstream ss ; ss << id ;
219  throw UnknownAlgorithm( ss.str().c_str() ) ;
220  }
221 
222  return nit->second ;
223  }
224 
226 
227  return _ids ;
228  }
229 
230 
231 
233 
234  PNM::iterator nit = _pNames.find( id ) ;
235 
236  if( nit == _pNames.end() ){
237 
238  std::stringstream ss ; ss << id ;
239  throw UnknownAlgorithm( ss.str().c_str() ) ;
240  }
241 
242  ParticleID* pid = 0 ;
243 
244 // const ParticleIDVec* idv = 0 ;
245 // if( _type == ReconstructedParticle ){
246 
247 // idv = &( static_cast< ReconstructedParticleImpl* >(p)->getParticleIDs() ) ;
248 // }
249 // else if( _type == Cluster ){
250 
251 // idv = &( static_cast< ClusterImpl* >(p)->getParticleIDs() ) ;
252 // }
253 // const ParticleIDVec& pidV = *idv ;
254 
255  const ParticleIDVec& pidV = p->getParticleIDs() ;
256 
257  unsigned nPid = pidV.size() ;
258 
259  for(unsigned i=0; i<nPid; ++i ) {
260 
261  if( pidV[i]->getAlgorithmType() == id ) {
262 
263  pid = pidV[i] ;
264  break ;
265 
266  }
267  }
268 
269  if( pid == 0 ) {
270  std::stringstream err ;
271  err << "pid object not found in particle for algorithmId: "<< id ;
272  throw UnknownAlgorithm( err.str() ) ;
273  }
274 
275  p->setParticleIDUsed( pid ) ;
276 
277  }
278 
279 
281 
282  PNM::iterator nit = _pNames.find( id ) ;
283 
284  if( nit == _pNames.end() ){
285 
286  std::stringstream ss ; ss << id ;
287  throw UnknownAlgorithm( ss.str().c_str() ) ;
288  }
289 
290 
291  const ParticleIDVec* idv = 0 ;
292 
293  if( _type == ReconstructedParticle ){
294 
295  idv = &( static_cast< ReconstructedParticleImpl* >(p)->getParticleIDs() ) ;
296  }
297  else if( _type == Cluster ){
298 
299  idv = &( static_cast< ClusterImpl* >(p)->getParticleIDs() ) ;
300  }
301  else{
302 
303  throw Exception("PIDHandler::getParticleID LCObject is neither ReconstructedParticleImpl nor ClusterImpl !") ;
304  }
305 
306  const ParticleIDVec& pidV = *idv ;
307 
308  ParticleIDVec pidVID ;
309 
310  unsigned nPid = pidV.size() ;
311 
312  for(unsigned i=0; i<nPid; ++i ) {
313 
314  if( pidV[i]->getAlgorithmType() == id )
315 
316  pidVID.push_back( pidV[i] ) ;
317 
318  }
319 
320  return pidVID ;
321  }
322 
324 
325  PNM::iterator nit = _pNames.find( id ) ;
326 
327  if( nit == _pNames.end() ){
328 
329  std::stringstream ss ; ss << id ;
330  throw UnknownAlgorithm( ss.str().c_str() ) ;
331  }
332 
333 
334  const ParticleIDVec* idv = 0 ;
335 
336  if( _type == ReconstructedParticle ){
337 
338  idv = &( static_cast< ReconstructedParticleImpl* >(p)->getParticleIDs() ) ;
339  }
340  else if( _type == Cluster ){
341 
342  idv = &( static_cast< ClusterImpl* >(p)->getParticleIDs() ) ;
343  }
344  else{
345 
346  throw Exception("PIDHandler::getParticleID LCObject is neither ReconstructedParticleImpl nor ClusterImpl !") ;
347  }
348 
349  const ParticleIDVec& pidV = *idv ;
350 
351  unsigned nPid = pidV.size() ;
352 
353  for(unsigned i=0; i<nPid; ++i ) {
354 
355  if( pidV[i]->getAlgorithmType() == id )
356 
357  return *pidV[i] ;
358 
359  }
360 
361  // nothing found - return a dummy ParticleID object
362 
363  static const ParticleIDImpl dummyPID ;
364  return dummyPID ;
365  }
366 
368  int userType,
369  int PDG,
370  float likelihood,
371  int id,
372  const FloatVec& params ) {
373 
374 
375  PNM::iterator nit = _pNames.find( id ) ;
376 
377  if( nit == _pNames.end() ){
378 
379  std::stringstream ss ; ss << id ;
380  throw UnknownAlgorithm( ss.str().c_str() ) ;
381  }
382 
383 
384  // ---- check paramaters size -----
385  unsigned nParam = params.size() ;
386 
387  if( nParam != _pNames[ id ].size() ) {
388 
389  std::stringstream sstr ;
390 
391  sstr << " PIDHandler::setParticleID() - wrong parmeter size specified: "
392  << nParam << " - expected " << _pNames[ id ].size() ;
393 
394  throw Exception( sstr.str() ) ;
395 
396  }
397 
398  ParticleIDImpl* pid = 0 ;
399 
400  const ParticleIDVec* idv = 0 ;
401 
402  if( _type == ReconstructedParticle ){
403 
404  idv = &( static_cast< ReconstructedParticleImpl* >(p)->getParticleIDs() ) ;
405  }
406  else if( _type == Cluster ){
407 
408  idv = &( static_cast< ClusterImpl* >(p)->getParticleIDs() ) ;
409  }
410  else{
411 
412  throw Exception("PIDHandler::setParticleID LCObject is neither ReconstructedParticleImpl nor ClusterImpl !") ;
413  }
414 
415  const ParticleIDVec& pidV = *idv ;
416 
417  unsigned nPid = pidV.size() ;
418 
419  for(unsigned i=0; i<nPid; ++i ) {
420 
421  if( pidV[i]->getAlgorithmType() == id ) {
422 
423  pid = static_cast<ParticleIDImpl*>( pidV[i] ) ;
424  break ;
425 
426  }
427  }
428 
429  // if nothing found we create a new object
430 
431  bool isNewPID = false ;
432 
433  if( pid == 0 ) {
434 
435  pid = new ParticleIDImpl ;
436 
437  isNewPID = true ;
438 
439  }
440 
441  // ---- now set the PID data ------------
442 
443  pid->setLikelihood( likelihood ) ;
444 
445  pid->setType( userType ) ;
446 
447  pid->setPDG( PDG ) ;
448 
449  pid->setAlgorithmType( id ) ;
450 
451  FloatVec& ps = pid->parameters() ;
452 
453  ps.resize( nParam ) ;
454 
455  for(unsigned k=0; k< nParam ; k++){
456 
457  ps[ k ] = params[ k ] ;
458  }
459 
460  // ----------------------------------------
461 
462 
463  if( isNewPID ) { // need to add it to the particle/cluster
464 
465  if( _type == ReconstructedParticle ){
466 
467  static_cast< ReconstructedParticleImpl* >(p)->addParticleID( pid ) ;
468  }
469  else if( _type == Cluster ){
470 
471  static_cast< ClusterImpl* >(p)->addParticleID( pid ) ;
472  }
473  }
474 
475  }
476 }
The generic object that is held in an LCCollection.
Definition: LCObject.h:30
Base exception class for LCIO - all other exceptions extend this.
Definition: Exceptions.h:21
void setType(int type)
void setAlgorithmType(int algorithmType)
int getParameterIndex(int algorithmID, const std::string &pName)
The index of parameter pName for the algorithm with algorithmID - throws UnknownAlgoritm.
Definition: PIDHandler.cc:185
void init(const EVENT::LCCollection *col)
Definition: PIDHandler.cc:41
Exception for unknown algorithms.
Definition: PIDHandler.h:136
ObjectType
Definition: PIDHandler.cc:15
void setParticleIDUsed(IMPL::ReconstructedParticleImpl *particle, int algorithmID)
Set the particleID algorithm that is used for this particle&#39;s kinematic variables.
Definition: PIDHandler.cc:232
PIDHandler()=default
Implementation of ParticleID.
const EVENT::StringVec & getParameterNames(int algorithmID)
The names of parameters for the algorithm with algorithmID - throws UnknownAlgoritm.
Definition: PIDHandler.cc:212
T end(T...args)
EVENT::IntVec _ids
Definition: PIDHandler.h:129
const std::string & getAlgorithmName(int algoID)
Return the name of the algorithm with id - throws UnknownAlgorithm.
Definition: PIDHandler.cc:170
T resize(T...args)
STL class.
T push_back(T...args)
const EVENT::IntVec & getAlgorithmIDs()
Return the IDs of all known Algorithms.
Definition: PIDHandler.cc:225
virtual LCParameters & parameters()=0
Parameters defined for this collection.
int addAlgorithm(const std::string &algoName, const EVENT::StringVec &parameterNames)
Add a new algorithm and return the corresponding algorithm id.
Definition: PIDHandler.cc:127
The LCIO cluster.
Definition: Cluster.h:30
T str(T...args)
T make_pair(T...args)
virtual void setValues(const std::string &key, const IntVec &values)=0
Set integer values for the given key.
virtual const std::string & getTypeName() const =0
Returns the type name of the collection - valid names are defined in LCIO.
map_type & map()
The std::map&lt; std::string, int &gt;.
The LCIO reconstructedParticle.
T insert(T...args)
Persistent interface for LCIO ParticleIDs.
Definition: ParticleID.h:28
T find(T...args)
T size(T...args)
int getAlgorithmID(const std::string &algoName)
Return the typeID of algorithm algoName - throws UnknownAlgorithm.
Definition: PIDHandler.cc:158
virtual StringVec & getStringVals(const std::string &key, StringVec &values) const =0
Adds all string values for the given key to values.
void setParticleID(EVENT::LCObject *p, int userType, int PDG, float likelihood, int algorithmID, const EVENT::FloatVec &params)
Set the particleID information for this particle (or cluster) - throws UnknownAlgorithm.
Definition: PIDHandler.cc:367
EVENT::ParticleIDVec getParticleIDs(EVENT::LCObject *p, int id)
Return all PID objects for a given algorithm - ordered with decreasing likelihood - throws UnknownAlg...
Definition: PIDHandler.cc:280
EVENT::LCCollection * _col
Definition: PIDHandler.h:123
The generic collection used in LCIO.
Definition: LCCollection.h:29
void setLikelihood(float logL)
T begin(T...args)
virtual const EVENT::ParticleIDVec & getParticleIDs() const
The particle Id&#39;s sorted by their likelihood.
const EVENT::ParticleID & getParticleID(EVENT::LCObject *particle, int algorithmID)
Return the (first) ParticleID object for the given algorithm and particle (or cluster) - throws Unkno...
Definition: PIDHandler.cc:323
void setPDG(int pdg)
void setParticleIDUsed(EVENT::ParticleID *pid)
virtual const LCParameters & getParameters() const =0
Parameters defined for this collection.
~PIDHandler()
Update the collection parameter when going out of scope.
Definition: PIDHandler.cc:110
virtual EVENT::FloatVec & parameters()
Access to parameters associated with this hypothesis.
Implementation of Cluster.
Definition: ClusterImpl.h:24
Implementation of ReconstructedParticle.