Marlin  1.17.1
 All Classes Namespaces Functions Variables Enumerations Friends Pages
Processor.h
1 #ifndef Processor_h
2 #define Processor_h 1
3 
4 #include "lcio.h"
5 
6 #include "IO/LCRunListener.h"
7 #include "IO/LCEventListener.h"
8 #include "IO/LCReader.h"
9 
10 #include "EVENT/LCEvent.h"
11 #include "EVENT/LCRunHeader.h"
12 
13 #include "StringParameters.h"
14 #include "ProcessorParameter.h"
15 //#include "LogStream.h"
16 #include "marlin/VerbosityLevels.h"
17 
18 //----- Marlin version macros are now defined in MarlinConfig.h generated by cmake -----
19 #include "marlin/MarlinConfig.h"
20 
21 #include "streamlog/streamlog.h"
22 
23 #include <map>
24 #include <memory>
25 
26 // ----- define some useful macros-----------
27 // for backward compatibility - use streamlog_out( MESSAGE ) instead
28 // - this conflicts with a boost header und should not be used by now anywhere ...
29 //#define m_out( VERBOSITY ) streamlog_out( VERBOSITY )
30 //#define m_endl std::endl
31 //----------------------------------------
32 
33 using namespace lcio ;
34 
35 
36 namespace marlin{
37 
38  class ProcessorMgr ;
39  // class ProcessorParameter ;
40  class XMLFixCollTypes ;
41 
42  typedef std::map<std::string, ProcessorParameter* > ProcParamMap ;
43  typedef std::map<std::string, std::string > LCIOTypeMap ;
44 
64  class Processor {
65 
66  friend class ProcessorMgr ;
67  friend class CMProcessor ;
68  friend class XMLFixCollTypes ;
69 
70  private:
71  //prevent users from making (default) copies of processors
72  Processor(const Processor& /*typeName*/) = delete;
73  Processor& operator=(const Processor& /*typeName*/) = delete;
74 
75  public:
76 
77 // /** Possible verbosity levels */
78 // enum{ VERBOSE = 0, DEBUG = 0, MESSAGE = 1, WARNING = 2, ERROR = 3, SILENT = 4 };
79 
80 // /** Global variable used to set the verbosity level */
81 // static int Verbosity;
82 
86  Processor(const std::string& typeName) ;
87 
89  virtual ~Processor() ;
90 
91 
95  virtual Processor* newProcessor() = 0 ;
96 
97 
101  virtual void init() { }
102 
106  virtual void processRunHeader( LCRunHeader* ) { }
107 
110  virtual void processEvent( LCEvent * ) { }
111 
116  virtual void check( LCEvent* ) { }
117 
118 
123  virtual void end(){ }
124 
125 
128  virtual const std::string & type() const { return _typeName ; }
129 
132  virtual const std::string & name() const { return _processorName ; }
133 
136  virtual const std::string & logLevelName() const { return _logLevelName ; }
137 
138 
141  virtual std::shared_ptr<StringParameters> parameters() { return _parameters ; }
142 
143 
146  virtual void printDescription() ;
147 
150  virtual void printDescriptionXML(std::ostream& stream=std::cout) ;
151 
152 
155  template <class T>
157 
158 
159  if( streamlog::out.template write<T>() ) {
160 
161 
162  typedef ProcParamMap::iterator PMI ;
163 
164  streamlog::out() << std::endl
165  << "---- " << name() <<" - parameters: " << std::endl ;
166 
167 
168  for( PMI i = _map.begin() ; i != _map.end() ; i ++ ) {
169 
170  if( ! i->second->isOptional() || i->second->valueSet() ){
171  streamlog::out.template write<T>() ;
172  streamlog::out() << "\t" << i->second->name()
173  << ": " << i->second->value()
174  << std::endl ;
175  }
176  }
177 
178  streamlog::out.template write<T>() ;
179  streamlog::out() << "-------------------------------------------------"
180  << std::endl ;
181 
182  }
183  }
184 
187  void printParameters() ;
188 
189 
190 
193  const std::string& description() { return _description ; }
194 
195 
198  bool isFirstEvent() { return _isFirstEvent ; } ;
199 
202  std::string getLCIOInType( const std::string& colName ) ;
203 
206  std::string getLCIOOutType( const std::string& colName ) ;
207 
212  bool isInputCollectionName( const std::string& parameterName ) ;
213 
214 
216  bool isOutputCollectionName( const std::string& parameterName ) ;
217 
218 
219 // /** Helper function returns the ProcessorParameter for the given name
220 // */
221 // ProcessorParameter* getProcessorParameter( const std::string name) ;
222 
224  virtual void setParameters( std::shared_ptr<StringParameters> parameters) ;
225 
227  virtual void setName( const std::string & processorName) { _processorName = processorName ; }
228 
229  protected:
230 
235  void setReturnValue( bool val) ;
236 
241  void setReturnValue( const std::string& name, bool val ) ;
242 
243 
253  template<class T>
254  void registerProcessorParameter(const std::string& parameterName,
255  const std::string& parameterDescription,
256  T& parameter,
257  const T& defaultVal,
258  int setSize=0 ) {
259  checkForExistingParameter( parameterName );
260  _map[ parameterName ] = new ProcessorParameter_t<T>( parameterName , parameterDescription,
261  parameter, defaultVal,
262  false , setSize) ;
263  }
264 
268  void registerInputCollection(const std::string& collectionType,
269  const std::string& parameterName,
270  const std::string& parameterDescription,
271  std::string& parameter,
272  const std::string& defaultVal,
273  int setSize=0 ) {
274 
275  setLCIOInType( parameterName , collectionType ) ;
276  registerProcessorParameter( parameterName, parameterDescription, parameter, defaultVal, setSize ) ;
277  }
278 
282  void registerOutputCollection(const std::string& collectionType,
283  const std::string& parameterName,
284  const std::string& parameterDescription,
285  std::string& parameter,
286  const std::string& defaultVal,
287  int setSize=0 ) {
288 
289  setLCIOOutType( parameterName , collectionType ) ;
290  registerProcessorParameter( parameterName, parameterDescription, parameter, defaultVal, setSize ) ;
291  }
292 
296  void registerInputCollections(const std::string& collectionType,
297  const std::string& parameterName,
298  const std::string& parameterDescription,
299  StringVec& parameter,
300  const StringVec& defaultVal,
301  int setSize=0 ) {
302 
303  setLCIOInType( parameterName , collectionType ) ;
304  registerProcessorParameter( parameterName, parameterDescription, parameter, defaultVal, setSize ) ;
305  }
306 
313  template<class T>
314  void registerOptionalParameter(const std::string& parameterName,
315  const std::string& parameterDescription,
316  T& parameter,
317  const T& defaultVal,
318  int setSize=0 ) {
319 
320  checkForExistingParameter( parameterName );
321  _map[ parameterName ] = new ProcessorParameter_t<T>( parameterName , parameterDescription,
322  parameter, defaultVal,
323  true , setSize) ;
324  }
325 
328  bool parameterSet( const std::string& name ) ;
329 
335  void checkForExistingParameter( const std::string& parameterName ) {
336  auto paraIt = _map.find( parameterName );
337  if (paraIt != _map.end() ) {
338  std::stringstream errorMessage;
339  errorMessage << "Parameter " << parameterName
340  << " already defined for processor "
341  << this->type()
342  << std::endl;
343  throw std::logic_error( errorMessage.str() );
344  }
345  }
346 
363  template <class T>
364  void message( const std::string& m ) const {
365 
366 
367  if( streamlog::out.template write<T>() )
368  streamlog::out() << m << std::endl ;
369 
370  }
371 
372 
388  template <class T>
389  inline void message( const std::basic_ostream<char, std::char_traits<char> >& m) const {
390 
391  if( T::active ){ // allow the compiler to optimize this away ...
392 
393  try{
394  const std::stringstream& mess = dynamic_cast<const std::stringstream&>( m ) ;
395 
396  this->template message<T>( mess.str() ) ;
397 
398  }
399  catch( std::bad_cast ) {}
400  }
401  }
402 
406  std::stringstream& log() const ;
407 
408 
409  private: // called by ProcessorMgr
410 
412  virtual void setProcessorParameters( std::shared_ptr<StringParameters> processorParameters) {
413  setParameters( processorParameters ) ;
414  }
415 
417  virtual void updateParameters();
418 
421  virtual void baseInit() ;
422 
424  void setFirstEvent( bool firstEvent ) { _isFirstEvent = firstEvent ; }
425 
426  // called internally
427 
433  void setLCIOInType(const std::string& colName, const std::string& lcioInType) ;
434 
435 
436 
440  void setLCIOOutType(const std::string& collectionName, const std::string& lcioOutType) ;
441 
442 
444  const ProcParamMap& procMap() { return _map ; }
445 
446 
447 
448  protected:
449 
452  std::string _description="";
453  std::string _typeName="";
454  std::string _processorName="";
455  std::shared_ptr<StringParameters> _parameters{};
456 
457  ProcParamMap _map{};
458  bool _isFirstEvent = false;
459  LCIOTypeMap _inTypeMap{};
460  LCIOTypeMap _outTypeMap{};
461 
462  std::string _logLevelName{};
463 
464  private:
465  mutable std::stringstream* _str=NULL;
466 
467  Processor() ;
468 
469  };
470 
471 } // end namespace marlin
472 
473 #endif
virtual void check(LCEvent *)
Called for every event - right after processEvent() has been called for this processor.
Definition: Processor.h:116
void message(const std::basic_ostream< char, std::char_traits< char > > &m) const
Same as message(const std::string&amp; message) except that it allows the output of more complex messages...
Definition: Processor.h:389
void registerProcessorParameter(const std::string &parameterName, const std::string &parameterDescription, T &parameter, const T &defaultVal, int setSize=0)
Register a steering variable for this processor - call in constructor of processor.
Definition: Processor.h:254
virtual void processRunHeader(LCRunHeader *)
Called for every run, e.g.
Definition: Processor.h:106
This singleton class contains an instance of every available marlin processor type.
Definition: CMProcessor.h:21
void registerOutputCollection(const std::string &collectionType, const std::string &parameterName, const std::string &parameterDescription, std::string &parameter, const std::string &defaultVal, int setSize=0)
Specialization of registerProcessorParameter() for a parameter that defines an output collection - ca...
Definition: Processor.h:282
void message(const std::string &m) const
Print message according to verbosity level of the templated parameter (one of DEBUG, MESSAGE, WARNING, ERROR ) and the global parameter &quot;Verbosity&quot;.
Definition: Processor.h:364
virtual const std::string & name() const
Return name of this processor.
Definition: Processor.h:132
void registerInputCollection(const std::string &collectionType, const std::string &parameterName, const std::string &parameterDescription, std::string &parameter, const std::string &defaultVal, int setSize=0)
Specialization of registerProcessorParameter() for a parameter that defines an input collection - can...
Definition: Processor.h:268
virtual const std::string & type() const
Return type name for the processor (as set in constructor).
Definition: Processor.h:128
Templated implementation of ProcessorParameter - automatically created by Processor::registerProcesso...
Definition: ProcessorParameter.h:106
virtual void init()
Called at the begin of the job before anything is read.
Definition: Processor.h:101
void printParameters()
Print the parameters and their values depending on the given verbosity level.
Definition: Processor.h:156
virtual std::shared_ptr< StringParameters > parameters()
Return parameters defined for this Processor.
Definition: Processor.h:141
void registerOptionalParameter(const std::string &parameterName, const std::string &parameterDescription, T &parameter, const T &defaultVal, int setSize=0)
Same as registerProcessorParameter except that the parameter is optional.
Definition: Processor.h:314
const std::string & description()
Description of processor.
Definition: Processor.h:193
void registerInputCollections(const std::string &collectionType, const std::string &parameterName, const std::string &parameterDescription, StringVec &parameter, const StringVec &defaultVal, int setSize=0)
Specialization of registerProcessorParameter() for a parameter that defines one or several input coll...
Definition: Processor.h:296
virtual void setName(const std::string &processorName)
Set processor name.
Definition: Processor.h:227
virtual const std::string & logLevelName() const
Return name of the local verbosity level of this processor - &quot;&quot; if not set.
Definition: Processor.h:136
bool isFirstEvent()
True if first event in processEvent(evt) - use this e.g.
Definition: Processor.h:198
Processor manager singleton class.
Definition: ProcessorMgr.h:36
void checkForExistingParameter(const std::string &parameterName)
Tests whether the parameter has been registered before.
Definition: Processor.h:335
virtual void processEvent(LCEvent *)
Called for every event - the working horse.
Definition: Processor.h:110
Base class for Marlin processors.
Definition: Processor.h:64
Internal helper class that creates a new xml steering file with the parameter attributes lcioInType a...
Definition: XMLFixCollTypes.h:25
virtual void end()
Called after data processing for clean up in the inverse order of the init() method so that resources...
Definition: Processor.h:123