Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCProcessor.cc
Go to the documentation of this file.
1 #include "marlin/CCProcessor.h"
2 #include "marlin/CMProcessor.h"
3 #include "marlin/CCCollection.h"
4 #include "marlin/ProcessorMgr.h"
5 
6 using namespace std;
7 
8 namespace marlin{
9 
10  //constructor
11  CCProcessor::CCProcessor( bool status, const string& name, const string& type, std::shared_ptr<StringParameters> p ):
12  _status(status),
13  _error(MAX_ERRORS, false),
14  _name(name),
15  _type(type),
16  _param(0),
17  _proc(0),
18  _errors{},
19  _conditions(),
20  _cols(),
21  _types(),
22  _optParams()
23  {
24 
25  setError( NO_PARAMETERS );
26 
27  //setup Marlin Processor
28  setMarlinProc();
29 
30  //merge the Processor Parameters with the default ones for this type of processor
31  _param=CMProcessor::instance()->mergeParams( type, p );
32 
33  //adds the input/output collections from the string parameters
34  addColsFromParam( _param );
35 
36  //clears all collections related parameters and the processor type
37  clearParameters();
38  }
39 
40  //copy constructor
42  _status(p._status),
43  _error(p._error),
44  _name(p._name),
45  _type(p._type),
46  _param(NULL), //copied later
47  _proc(p._proc),
48  _errors(p._errors),
49  _conditions(p._conditions),
50  _cols(p._cols),
51  _types(p._types),
52  _optParams(p._optParams)
53  {
54 
55  // for( int i=0; i<MAX_ERRORS; i++ ){
56  // _error[i] = false;
57  // _error_desc.push_back( p._error_desc[i] );
58  // }
59 
60  // copied in initialiser list
61  // for( int i=0; i<MAX_ERRORS; i++ ){
62  // if(p._error[i]){
63  // setError(i);
64  // }
65  // }
66 
67  if( p._param != NULL ){
68  p.writeColsToParam();
69  _param= std::make_shared<StringParameters>( *p._param );
71  p.clearParameters();
73  }
74  }
75 
77  for( ssColVecMap::const_iterator q=_cols.begin(); q!=_cols.end(); q++ ){
78  if( q->first != UNAVAILABLE && q->first != DUPLICATE ){
79  for( sColVecMap::const_iterator r=q->second.begin(); r!=q->second.end(); r++ ){
80  for( unsigned int i=0; i<r->second.size(); i++ ){
81  delete r->second[i];
82  }
83  }
84  }
85  }
86  }
87 
89  if( p == NULL ){
91  return;
92  }
93 
95 
96  //check if the Marlin Processor is installed
97  if( isInstalled() ){
98 
99  StringVec keys;
100  _param->getStringKeys( keys );
101 
102  for( unsigned int i=0; i<keys.size(); i++ ){
103  StringVec values;
104  _param->getStringVals( keys[i], values );
105 
106  if( _proc->isInputCollectionName( keys[i] )){
107  for( unsigned int j=0; j<values.size(); j++ ){
108  addCol( INPUT, keys[i], _proc->getLCIOInType( keys[i] ), values[j] );
109  }
110  }
111  else if( _proc->isOutputCollectionName( keys[i] )){
112  for( unsigned int j=0; j<values.size(); j++ ){
113  addCol( OUTPUT, keys[i], _proc->getLCIOOutType( keys[i] ), values[j] );
114  }
115  }
116  }
117  }
118  else{
119  StringVec InCols, OutCols;
120  string name, type;
121 
122  _param->getStringVals( "_marlin.lcioInType", InCols );
123  _param->getStringVals( "_marlin.lcioOutType", OutCols );
124 
125  unsigned int index = 0;
126  while( index < InCols.size() ){
127  name = InCols[ index++ ];
128  type = InCols[ index++ ];
129 
130  StringVec values;
131  _param->getStringVals(name, values); // get corresponding collections
132 
133  for (unsigned int i=0; i<values.size(); i++){
134  addCol( INPUT, name, type, values[i] );
135  }
136  }
137 
138  index = 0;
139  while( index < OutCols.size() ){
140  name = OutCols[index++];
141  type = OutCols[index++];
142 
143  StringVec values;
144  _param->getStringVals(name, values); // get corresponding collections
145 
146  for( unsigned int i=0; i<values.size(); i++ ){
147  addCol( OUTPUT, name, type, values[i] );
148  }
149  }
150  }
151  }
152 
153  void CCProcessor::setConditions( const string& conditions ){
154 
155  if( conditions == "true" ){
156  return;
157  }
158 
159  int brackets=0;
160  unsigned p = 0;
161  unsigned q = 1;
162 
163  while( q < conditions.size() ){
164  if( conditions[ p ] == '(' ){
165  brackets++;
166  while( brackets ){
167  if( conditions[ q ] == '(' ){
168  brackets++;
169  }
170  if( conditions[ q ] == ')' ){
171  brackets--;
172  }
173  q++;
174  }
175  //skip the white space after the brackets
176  q++;
177  }
178  if( conditions[ q ] == '&' ){
179  if( conditions[ p ] == '(' ){
180  //skip brackets
181  _conditions.insert( conditions.substr( p+1, (q-p)-3 ));
182  }
183  else{
184  _conditions.insert( conditions.substr( p, q-1 ));
185  }
186  p = q+3;
187  q = p;
188  }
189  q++;
190  }
191  //make the last split
192  if( conditions[ p ]== '(' ){
193  //skip brackets
194  _conditions.insert( conditions.substr( p+1, (q-p)-4 ));
195  }
196  else{
197  _conditions.insert( conditions.substr( p, q ));
198  }
199  }
200 
201  bool CCProcessor::hasCondition( const string& condition ){
202  if( _conditions.find( condition) != _conditions.end() ){
203  return true;
204  }
205  return false;
206  }
207 
211  return;
212  }
215  }
216 
218  // skip if it does not contain parameters or is not installed
219  if( hasParameters() ){
220  for( sColVecMap::const_iterator p=_cols[INPUT].begin(); p!=_cols[INPUT].end(); p++ ){
221  _param->erase( p->first );
222  }
223 
224  for( sColVecMap::const_iterator p=_cols[OUTPUT].begin(); p!=_cols[OUTPUT].end(); p++ ){
225  _param->erase( p->first );
226  }
227 
228  //erase the processor type from the string parameters
229  _param->erase( "ProcessorType" );
230 
231  //erase the "name, type" list from processor's parameters
232  _param->erase( "_marlin.lcioInType" );
233  _param->erase( "_marlin.lcioOutType" );
234  _param->erase( INPUT );
235  _param->erase( OUTPUT );
236 
237  //if there are any optional parameters active we save the keys into _optParams
238  if( _param->isParameterSet( "Opt_Params_Set" )){
239  StringVec values;
240  _param->getStringVals( "Opt_Params_Set", values );
241  for( unsigned int i=0; i<values.size(); i++ ){
242  _optParams.insert( values[i] );
243  }
244  }
245 
246  //erase the list of optional parameters
247  _param->erase( "Opt_Params_Set" );
248  }
249  }
250 
251  // write IO collections back to marlin processor's parameters
253  StringVec value;
254 
255  // skip if it does not contain parameters or is not installed
256  if( hasParameters() && isInstalled() ){
257 
258  // loop over all collections of this processor
259  // write every collection back to string parameters
260 
261  for( sColVecMap::const_iterator p=_cols[INPUT].begin(); p!=_cols[INPUT].end(); p++ ){
262 
263  value.clear();
264  for(unsigned int i=0; i< p->second.size(); i++){
265  value.push_back( p->second[i]->getValue() );
266  }
267  _param->add( p->first, value );
268  }
269 
270  for( sColVecMap::const_iterator p=_cols[OUTPUT].begin(); p!=_cols[OUTPUT].end(); p++ ){
271 
272  value.clear();
273  for(unsigned int i=0; i< p->second.size(); i++){
274  value.push_back( p->second[i]->getValue() );
275  }
276  _param->add( p->first, value );
277  }
278  }
279  }
280 
282  _status = !_status;
284  }
285 
287 // COLLECTION METHODS
289 
290  void CCProcessor::addCol( const string& iotype, const string& name, const string& type, const string& value ){
291  /*
292  ColVec v = getCols(iotype, name);
293 
294  //test if collection already exists to avoid duplicate collections
295  for( unsigned int i=0; i<v.size(); i++){
296  if( v[i]->getName() == name && v[i]->getType() == type && v[i]->getValue() == value ){
297  //abort if collection already exists
298  return;
299  }
300  }
301  */
302  CCCollection* newCol = new CCCollection(value, type, name, this);
303 
304  //add collection to the map
305  _cols[iotype][name].push_back( newCol );
306 
307  //add collection type to the map
308  _types[iotype][name]=type;
309  }
310 
312  //add collection to the map
313  _cols[ UNAVAILABLE ][ c->getType() ].push_back( c );
314 
315  //set the error
316  setError( COL_ERRORS );
317  }
318 
320  //add collection to the map
321  _cols[ DUPLICATE ][ c->getType() ].push_back( c );
322 
323  //set the error
324  setError( COL_ERRORS );
325  }
326 
327  void CCProcessor::remCol( const string& iotype, const string& name, unsigned int index ){
328  if( index < _cols[iotype][name].size() ){
329  popCol( _cols[iotype][name], _cols[iotype][name][index] );
330  }
331  }
332 
333  //pop a collection out of the given vector
335  //test if the vector is empty
336  if( v.size() == 0 ){
337  return c;
338  }
339 
340  ColVec newVec;
341  for( unsigned int i=0; i<v.size(); i++ ){
342  if( v[i] != c ){
343  newVec.push_back( v[i] );
344  }
345  }
346 
347  v.assign( newVec.begin(), newVec.end() );
348 
349  return c;
350  }
351 
352  ColVec& CCProcessor::getCols( const string& iotype, const string& type_name ){
353  static ColVec newVec;
354 
355  newVec.clear();
356 
357  if( _cols.find( iotype ) != _cols.end() ){
358  for( sColVecMap::const_iterator p=_cols[iotype].begin(); p!=_cols[iotype].end(); p++ ){
359  if( type_name=="ALL_COLLECTIONS" || type_name == p->first ){
360  for( unsigned int i=0; i<p->second.size(); i++ ){
361  newVec.push_back( p->second[i] );
362  }
363  }
364  }
365  }
366 
367  return newVec;
368  }
369 
370  sSet& CCProcessor::getColTypeNames( const string& iotype ){
371  static sSet type_names;
372 
373  type_names.clear();
374 
375  for( sColVecMap::const_iterator p=_cols[ iotype ].begin(); p!=_cols[ iotype ].end(); p++ ){
376  type_names.insert( p->first );
377  }
378  return type_names;
379  }
380 
381  bool CCProcessor::isErrorCol( const string& type, const string& value ){
382  ColVec v = getCols( UNAVAILABLE );
383  v.insert( v.end(), getCols( DUPLICATE ).begin(), getCols( DUPLICATE ).end() );
384 
385  for( unsigned int i=0; i<v.size(); i++ ){
386  if(( v[i]->getValue() == value ) && ( v[i]->getType() == type )){
387  return true;
388  }
389  }
390 
391  return false;
392  }
393 
394 
396 // ERROR METHODS
398 
399  void CCProcessor::setError( int error ){
400  if( _error[ error ] == false ){
401  _error[ error ] = true;
402  _errors.push_back( _error_desc[ error ] );
403  }
404  }
405 
406  void CCProcessor::clearError( int error ){
407  if( _error[ error ] == true ){
408  _error[ error ] = false;
409 
410  StringVec tmp;
411  for( unsigned int i=0; i<_errors.size(); i++ ){
412  if( _errors[i] != _error_desc[ error ] ){
413  tmp.push_back( _errors[i] );
414  }
415  }
416  _errors.clear();
417  _errors=tmp;
418 
419  if( error == COL_ERRORS && (_cols.find(UNAVAILABLE) != _cols.end())){
420  _cols[ UNAVAILABLE ].clear();
421  }
422  if( error == COL_ERRORS && (_cols.find(DUPLICATE) != _cols.end())){
423  _cols[ DUPLICATE ].clear();
424  }
425  }
426  }
427 
429  for( int i=0; i<MAX_ERRORS; i++ ){
430  if( _error[i] == true ){
431  return true;
432  }
433  }
434  return false;
435  }
436 
437  bool CCProcessor::isParamOptional( const string& key ){
438  if( CMProcessor::instance()->isParamOpt( _type, key )){
439  if( _optParams.find( key ) == _optParams.end() ){
440  return true;
441  }
442  }
443  return false;
444  }
445 
446  void CCProcessor::setOptionalParam( const string& key, bool optional ){
447  if( optional ){
448  _optParams.erase( key );
449  }
450  else{
451  _optParams.insert( key );
452  }
453  }
454 
456 
457  stream << " <processor name=\"" << _name << "\" type=\"" << _type << "\">\n";
458  stream << " <!--" << getDescription() << "-->\n";
459 
460  StringVec value;
461  //write I/O collections
462  for( ssColVecMap::const_iterator p=_cols.begin(); p!=_cols.end(); p++ ){
463  if( p->first != UNAVAILABLE && p->first != DUPLICATE ){
464  for( sColVecMap::const_iterator q=p->second.begin(); q!=p->second.end(); q++ ){
465  if( q->second.size() != 0){
466  if( isInstalled() ){
467  stream << " <!--" << CMProcessor::instance()->getParamD( _type, q->first ) << "-->" << std::endl ;
468  }
469  stream << " <parameter name=\"" << q->first << "\" type=\"" << (q->second.size() == 1 ? "string" : "StringVec") << "\" ";
470  stream << p->first << "=\"" << q->second[0]->getType() << "\"> ";
471  value.clear();
472  for( unsigned int i=0; i<q->second.size(); i++ ){
473  stream << q->second[i]->getValue() << " ";
474  }
475  stream << "</parameter>\n";
476  }
477  }
478  }
479  }
480 
481  StringVec keys;
482  if( hasParameters() ){
483  _param->getStringKeys( keys );
484  }
485 
486  //parameters
487  for( unsigned int i=0; i<keys.size(); i++ ){
488 
489  ProcessorParameter* p = CMProcessor::instance()->getParam( _type, keys[i] );
490 
491  //if the parameter is recognized by the marlin processor
492  if( p != NULL ){
493 
494  stream << " <!--" << CMProcessor::instance()->getParamD( _type, keys[i] ) << "-->" << std::endl ;
495 
496  StringVec values;
497  _param->getStringVals( keys[i], values );
498 
499  int ssize=CMProcessor::instance()->getParamSetSize( _type, keys[i] );
500  if( ssize > 1 ){
501  for( int j=0; j<((int)values.size())/ssize; j++ ){
502 
503  stream << " <" << (isParamOptional( keys[i] ) ? "!--" : "") << "parameter name=\"" << keys[i] << "\" ";
504  stream << "type=\"" << p->type() << "\"> ";
505 
506  for( int k=0; k<ssize; k++ ){
507  stream << values[ (j*ssize)+k ] << " ";
508  }
509 
510  stream << "</parameter" << (isParamOptional( keys[i] ) ? "--" : "") << ">\n";
511  }
512  }
513  else{
514  stream << " <" << (isParamOptional( keys[i] ) ? "!--" : "") << "parameter name=\"" << keys[i] << "\" ";
515  stream << "type=\"" << p->type() << "\"";
516 
517  StringVec valuesLocal;
518  _param->getStringVals( keys[i], valuesLocal );
519  stream << (valuesLocal.size() == 1 ? " value=\"" : "> ");
520  for( unsigned int j=0; j<valuesLocal.size(); j++ ){
521  stream << valuesLocal[j] << (valuesLocal.size() > 1 ? " " : "");
522  }
523  stream << (valuesLocal.size() == 1 ? "\"/" : "</parameter") << (isParamOptional( keys[i] ) ? "--" : "") << ">\n";
524  }
525  }
526  //else it's a parameter from an uninstalled processor or an extra parameter from an installed processor
527  else{
528  if( isInstalled() ){
529  stream << " <!-- Sorry, this parameter isn't a default parameter for this processor: description and type lost!! -->" << std::endl ;
530  }
531  stream << " <parameter name=\"" << keys[i] << "\"";
532 
533  StringVec values;
534  //get the values for the given key
535  _param->getStringVals( keys[i], values );
536 
537  stream << ( values.size() == 1 ? " value=\"" : ">" );
538 
539  for( unsigned int j=0; j<values.size(); j++ ){
540  stream << ( values.size() == 1 ? "" : " ") << values[j];
541  }
542  stream << ( values.size() == 1 ? "\"/>\n" : " </parameter>\n" );
543  }
544  }
545  stream << "</processor>\n\n";
546  }
547 
548 } // end namespace marlin
virtual const std::string type()=0
std::string getLCIOOutType(const std::string &colName)
Return the LCIO output type for the collection colName - empty string if colName is not a registered ...
Definition: Processor.cc:250
void addDCol(CCCollection *c)
Adds a duplicate collection to this processor.
Definition: CCProcessor.cc:319
#define NO_PARAMETERS
Definition: CCProcessor.h:12
std::string _name
Definition: CCProcessor.h:167
static CMProcessor * instance()
return the instance of this class
Definition: CMProcessor.cc:11
std::vector< bool > _error
Definition: CCProcessor.h:166
CCProcessor(bool status, const std::string &name, const std::string &type, std::shared_ptr< StringParameters > p)
Definition: CCProcessor.cc:11
std::string getLCIOInType(const std::string &colName)
Return the LCIO input type for the collection colName - empty string if colName is not a registered c...
Definition: Processor.cc:241
bool isErrorCol(const std::string &type, const std::string &value)
Returns true if the given collection is in the unavailable or duplicate list of this processor...
Definition: CCProcessor.cc:381
handles information about LCIO collections needed by MarlinSteerCheck
Definition: CCCollection.h:17
T endl(T...args)
ProcessorParameter * getParam(const std::string &type, const std::string &key)
returns the parameter with the given key of the processor with the given type
Definition: CMProcessor.cc:152
#define NOT_INSTALLED
Definition: CCProcessor.h:13
ssColVecMap _cols
Definition: CCProcessor.h:177
const std::string & getType() const
Definition: CCCollection.h:34
Processor * getProc(const std::string &type)
returns the instance of the processor with the given type
Definition: CMProcessor.cc:92
T end(T...args)
void remCol(const std::string &iotype, const std::string &name, unsigned int index)
Removes collection of the given iotype ( INPUT / OUTPUT ) with the given name at the given index...
Definition: CCProcessor.cc:327
void setError(int error)
Activates an error flag in this processor ( NO_PARAMETERS=0, NOT_INSTALLED=1, COL_ERRORS=2 ) ...
Definition: CCProcessor.cc:399
const StringVec _error_desc
Definition: CCProcessor.h:172
#define MAX_ERRORS
Definition: CCProcessor.h:9
handles information about marlin processors and their collections needed by MarlinSteerCheck ...
Definition: CCProcessor.h:39
sSet & getColTypeNames(const std::string &iotype)
Returns collection&#39;s types/names of a given iotype found in the processor If iotype == INPUT/OUTPUT t...
Definition: CCProcessor.cc:370
const std::string getParamD(const std::string &type, const std::string &key)
returns the description of the parameter with the given key for the processor with the given type ...
Definition: CMProcessor.cc:165
std::string _type
Definition: CCProcessor.h:168
bool isInputCollectionName(const std::string &parameterName)
True if the given parameter defines an LCIO input collection, i.e.
Definition: Processor.cc:259
CCCollection * popCol(ColVec &v, CCCollection *c)
Definition: CCProcessor.cc:334
T push_back(T...args)
STL class.
void clearError(int error)
Clears an error flag in this processor ( NO_PARAMETERS=0, NOT_INSTALLED=1, COL_ERRORS=2 ) ...
Definition: CCProcessor.cc:406
void setConditions(const std::string &conditions)
Sets the processor&#39;s conditions.
Definition: CCProcessor.cc:153
bool isParamOptional(const std::string &key)
Returns true if a parameter is optional (optional means the parameter will be written out as a commen...
Definition: CCProcessor.cc:437
bool isInstalled()
Returns true if the processor is installed.
Definition: CCProcessor.h:65
void setOptionalParam(const std::string &key, bool optional=true)
Sets a parameter as optional (if optional=true parameter is written out as a comment) ...
Definition: CCProcessor.cc:446
T erase(T...args)
void writeToXML(std::ofstream &stream)
Writes this processor to a stream using the XML format.
Definition: CCProcessor.cc:455
bool hasErrors()
Returns true if the processor has errors.
Definition: CCProcessor.cc:428
bool hasCondition(const std::string &condition)
Returns true if the processor is constrained by the given condition.
Definition: CCProcessor.cc:201
bool isOutputCollectionName(const std::string &parameterName)
True if the given parameter defines an LCIO output collection.
Definition: Processor.cc:267
T clear(T...args)
void changeStatus()
Changes the processor status ( ACTIVE-&gt;INACTIVE or INACTIVE-&gt;ACTIVE )
Definition: CCProcessor.cc:281
ColVec & getCols(const std::string &iotype, const std::string &type_name="ALL_COLLECTIONS")
Returns collections of a given iotype ( INPUT, OUTPUT, UNAVAILABLE, DUPLICATE ) for a given name or t...
Definition: CCProcessor.cc:352
void addCol(const std::string &iotype, const std::string &name, const std::string &type, const std::string &value)
Adds a collection of the given iotype ( INPUT / OUTPUT ) with the given name, type and value...
Definition: CCProcessor.cc:290
T insert(T...args)
#define UNAVAILABLE
Definition: CCProcessor.h:17
Class that holds a steering variable for a marlin processor - automatically created by Processor::reg...
T find(T...args)
std::shared_ptr< StringParameters > _param
Definition: CCProcessor.h:169
T size(T...args)
T assign(T...args)
#define COL_ERRORS
Definition: CCProcessor.h:14
T begin(T...args)
Processor * _proc
Definition: CCProcessor.h:170
bool hasParameters()
Returns true if the processor has parameters.
Definition: CCProcessor.h:59
int getParamSetSize(const std::string &type, const std::string &key)
returns the set_size of the parameter with the given key for the processor with the given type ...
Definition: CMProcessor.cc:181
#define DUPLICATE
Definition: CCProcessor.h:18
T substr(T...args)
std::shared_ptr< StringParameters > mergeParams(const std::string &type, std::shared_ptr< StringParameters > sp)
merges the given parameters with the default ones from the processor with the given type ...
Definition: CMProcessor.cc:106
#define INPUT
Definition: CCProcessor.h:15
void addColsFromParam(std::shared_ptr< StringParameters > p)
Definition: CCProcessor.cc:88
#define OUTPUT
Definition: CCProcessor.h:16
const std::string getDescription()
Returns the Description of the processor.
Definition: CCProcessor.h:83
void addUCol(CCCollection *c)
Adds an unavailable collection to this processor.
Definition: CCProcessor.cc:311