Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XMLFixCollTypes.cc
Go to the documentation of this file.
1 
3 #include "marlin/Exceptions.h"
4 #include "marlin/tinyxml.h"
5 #include "marlin/ProcessorMgr.h"
6 
7 #include <algorithm>
8 #include <set>
9 
10 #include <sstream>
11 
12 namespace marlin{
13 
14  // open steering file with processor names
16  _doc(NULL), _fileName( fileName ) {
17  }
18 
20  }
21 
22  void XMLFixCollTypes::parse(const std::string& outFileName ){
23 
24 
25  _doc = new TiXmlDocument ;
26  bool loadOkay = _doc->LoadFile(_fileName ) ;
27 
28  if( !loadOkay ) {
29 
30  std::stringstream str ;
31 
32  str << "XMLFixCollTypes::parse error in file [" << _fileName
33  << ", row: " << _doc->ErrorRow() << ", col: " << _doc->ErrorCol() << "] : "
34  << _doc->ErrorDesc() ;
35 
36  throw ParseException( str.str() ) ;
37  }
38 
39  // TiXmlHandle docHandle( _doc );
40 
41  TiXmlElement* root = _doc->RootElement();
42  if( root==0 ){
43 
44  throw ParseException(std::string( "XMLFixCollTypes::parse : no root tag <marlin>...</marlin> found in ")
45  + _fileName ) ;
46  }
47 
48 
49  std::set<std::string> collectionNames ;
50 
51  TiXmlNode* section = 0 ;
52  while( (section = root->IterateChildren("processor", section ) ) != 0 ){
53 
54 
55  std::string procName( getAttribute( section, "name") ) ;
56 
57  std::string type=getAttribute( section, "type") ;
58 
59 
60  Processor* processor = ProcessorMgr::instance()->getProcessor( type ) ;
61 
62  std::cout << " processor : " << procName << " ( type: " << type << " ) " ;
63 
64  if( processor != 0 ) {
65 
66  std::cout << std::endl ;
67 
68  // loop over parameters ------
69  TiXmlNode* par = 0 ;
70  while( ( par = section->IterateChildren( "parameter", par ) ) != 0 ){
71 
72 
73  // std::cout << " parameter found : " << par->ToElement()->Attribute("name") << std::endl ;
74 
75 
76  std::string name( getAttribute( par, "name" ) ) ;
77 
78 
79  if( processor->isInputCollectionName( name ) ){
80 
81  std::string inType("") ;
82 
83  try{
84 
85  inType = getAttribute( par , "lcioInType" ) ;
86 
87 // std::cout << " ------ found lcioInType : " << inType
88 // << " name : " << name
89 // << std::endl ;
90 
91 
92  }
93 
94  catch( ParseException ) { }
95 
96 
97  if( inType != processor->getLCIOInType( name ) ) {
98 
99  std::cout << " -- fixing wrong lcioInType : " << inType
100  << " to " << processor->getLCIOInType( name )
101  << std::endl ;
102 
103 
104  par->ToElement()->SetAttribute( "lcioInType" , processor->getLCIOInType( name ) );
105  }
106 
107  collectionNames.insert( name ) ;
108 
109  }
110 
111  if( processor->isOutputCollectionName( name ) ){
112 
113  std::string outType("") ;
114 
115  try{
116 
117 
118  outType = getAttribute( par , "lcioOutType" ) ;
119 // std::cout << " ------ found lcioOutType : " << outType
120 // << " name : " << name
121 // << std::endl ;
122 
123  }
124  catch( ParseException ) { }
125 
126 
127  if( outType != processor->getLCIOOutType( name ) ) {
128 
129  std::cout << " -- fixing wrong lcioOutType : " << outType
130  << " to " << processor->getLCIOOutType( name )
131  << std::endl ;
132 
133 
134  par->ToElement()->SetAttribute( "lcioOutType" , processor->getLCIOOutType( name ) );
135  }
136 
137  collectionNames.insert( name ) ;
138  }
139  }
140  // now add all new in and out collections:
141 
142  typedef ProcParamMap::const_iterator PMI ;
143 
144  for( PMI i = processor->procMap().begin() ; i != processor->procMap().end() ; i ++ ) {
145 
146  ProcessorParameter* p = i->second ;
147 
148  if ( ( processor->isInputCollectionName( p->name() ) ||
149  processor->isOutputCollectionName( p->name() ) ) &&
150  collectionNames.find( p->name() ) == collectionNames.end() ) {
151 
152 
153  TiXmlElement newParam( "parameter" ) ;
154 
155  newParam.SetAttribute( "name" , p->name() ) ;
156 
157  newParam.SetAttribute( "type" , p->type() ) ;
158 
159  if( processor->isInputCollectionName( p->name() ) )
160  newParam.SetAttribute( "lcioInType" , processor->getLCIOInType( p->name() ) ) ;
161  else
162  newParam.SetAttribute( "lcioOutType" , processor->getLCIOOutType( p->name() ) ) ;
163 
164  newParam.SetAttribute( "value" , p->defaultValue() ) ;
165 
166 
167  TiXmlNode* position = section->InsertEndChild( newParam ) ;
168 
169  TiXmlComment comment ;
170  comment.SetValue( p->description() ) ;
171 
172  section->InsertBeforeChild( position , comment ) ;
173 
174  std::cout << " --- adding new collection parameter '" << p->name()
175  << " to processor " << std::endl ;
176 
177 
178 // std::cout << " <!--" << p->description() << "-->" << std::endl ;
179 // if( p->isOptional() ) {
180 // std::cout << " <!--parameter name=\"" << p->name() << "\" "
181 // << "type=\"" << p->type() ;
182 // if ( processor->isInputCollectionName( p->name() ) )
183 // std::cout << "\" lcioInType=\"" << processor->getLCIOInType( p->name() ) ;
184 // if ( processor->isOutputCollectionName( p->name() ) )
185 // std::cout << "\" lcioOutType=\"" << processor->getLCIOOutType( p->name() ) ;
186 // std::cout << "\">"
187 // << p->defaultValue()
188 // << " </parameter-->"
189 // << std::endl ;
190 // } else {
191 // std::cout << " <parameter name=\"" << p->name() << "\" "
192 // << "type=\"" << p->type() ;
193 // if ( processor->isInputCollectionName( p->name() ) )
194 // std::cout << "\" lcioInType=\"" << processor->getLCIOInType( p->name() ) ;
195 // if ( processor->isOutputCollectionName( p->name() ) )
196 // std::cout << "\" lcioOutType=\"" << processor->getLCIOOutType( p->name() ) ;
197 // std::cout << "\">"
198 // << p->defaultValue()
199 // << " </parameter>"
200 // << std::endl ;
201 // }
202  }
203  }
204 
205 
206  } else {
207 
208  std::cout << " UNKNOWN ! " << std::endl ;
209  }
210 
211  }
212 
213  _doc->SaveFile( outFileName ) ;
214 
215  }
216 
217 
218 
219  const char* XMLFixCollTypes::getAttribute( TiXmlNode* node , const std::string& name ){
220 
221  TiXmlElement* el = node->ToElement() ;
222  if( el == 0 )
223  throw ParseException("XMLFixCollTypes::getAttribute not an XMLElement " ) ;
224 
225  const char* at = el->Attribute( name.c_str() ) ;
226 
227  if( at == 0 ){
228 
229  std::stringstream str ;
230  str << "XMLFixCollTypes::getAttribute missing attribute \"" << name
231  << "\" in element <" << el->Value() << "/> in file " << _fileName ;
232  throw ParseException( str.str() ) ;
233  }
234 
235  return at ;
236 
237  }
238 
239 
240 
242  const std::string& attribute, const std::string& value ) {
243 
244  TiXmlNode* child = 0 ;
245  bool elementFound = false ;
246 
247  while( (child = node->IterateChildren( type , child ) ) != 0 ){
248 
249  if( std::string( *child->ToElement()->Attribute( attribute ) ) == value ) {
250  elementFound = true ;
251  break ;
252  }
253  }
254  if( ! elementFound )
255  child = 0 ;
256 
257  return child ;
258  }
259 
260 } // namespace marlin
261 
262 
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
const TiXmlElement * RootElement() const
Get the root element – the only top level element – of the document.
Definition: tinyxml.h:1429
static ProcessorMgr * instance()
Return the instance of this manager.
Definition: ProcessorMgr.cc:60
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
ParseException used for parse errors, e.g.
Definition: Exceptions.h:16
T endl(T...args)
T end(T...args)
TiXmlNode * findElement(TiXmlNode *node, const std::string &type, const std::string &attribute, const std::string &value)
Helper method - finds child element of node with given type and attribute value.
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the current document value.
Definition: tinyxml.cc:915
void SetValue(const char *_value)
Changes the value of the node.
Definition: tinyxml.h:508
int ErrorCol() const
The column where the error occured. See ErrorRow()
Definition: tinyxml.h:1455
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
Add a new node related to this.
Definition: tinyxml.cc:202
TiXmlNode * InsertBeforeChild(TiXmlNode *beforeThis, const TiXmlNode &addThis)
Add a new node related to this.
Definition: tinyxml.cc:217
Processor * getProcessor(const std::string &type)
Return the processor that has been registered with the given type.
virtual const std::string & name()
STL class.
bool isInputCollectionName(const std::string &parameterName)
True if the given parameter defines an LCIO input collection, i.e.
Definition: Processor.cc:259
int ErrorRow() const
Returns the location (if known) of the error.
Definition: tinyxml.h:1454
vector< level > position
const char * ErrorDesc() const
Contains a textual (english) description of the error if one occurs.
Definition: tinyxml.h:1440
void SetAttribute(const char *name, const char *_value)
Sets an attribute of name to a given value.
Definition: tinyxml.cc:708
bool SaveFile() const
Save a file using the current document value. Returns true if successful.
Definition: tinyxml.cc:924
Always the top level node.
Definition: tinyxml.h:1366
T str(T...args)
bool isOutputCollectionName(const std::string &parameterName)
True if the given parameter defines an LCIO output collection.
Definition: Processor.cc:267
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:698
virtual const std::string & description()
void parse(const std::string &filename)
Parse the input file and write the fixed outputfile to filename.
T insert(T...args)
Class that holds a steering variable for a marlin processor - automatically created by Processor::reg...
T find(T...args)
virtual const std::string defaultValue()=0
T begin(T...args)
const ProcParamMap & procMap()
Helper function for fixing old steering files.
Definition: Processor.h:444
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:425
T c_str(T...args)
const char * getAttribute(TiXmlNode *node, const std::string &name)
Return named attribute - throws ParseException if attribute doesn&#39;t exist.
const TiXmlNode * IterateChildren(const TiXmlNode *previous) const
An alternate way to walk the children of a node.
Definition: tinyxml.cc:355
Base class for Marlin processors.
Definition: Processor.h:64
const char * Value() const
The meaning of &#39;value&#39; changes for the specific type of TiXmlNode.
Definition: tinyxml.h:489
An XML comment.
Definition: tinyxml.h:1135
const char * Attribute(const char *name) const
Given an attribute name, Attribute() returns the value for the attribute of that name, or null if none exists.
Definition: tinyxml.cc:546
The element is a container class.
Definition: tinyxml.h:943