ATLAS Offline Software
Macros | Functions
MetaObject.cxx File Reference
#include <SampleHandler/MetaObject.h>
#include <cstdlib>
#include <memory>
#include <sstream>
#include <TList.h>
#include <TNamed.h>
#include <TBuffer.h>
#include <RootCoreUtils/Assert.h>
#include <RootCoreUtils/ThrowMsg.h>
#include <SampleHandler/MetaData.h>

Go to the source code of this file.

Macros

#define TYPENAME(X)
 
#define DEFAULT_CONVERT(FROM, TO)
 

Functions

 ClassImp (SH::MetaObject) namespace SH
 

Macro Definition Documentation

◆ DEFAULT_CONVERT

#define DEFAULT_CONVERT (   FROM,
  TO 
)
Value:
template<> struct Convert<FROM,TO> { \
static void convert (const FROM& from, TO& to, const std::string& /*field*/) { \
to = from; } };

◆ TYPENAME

#define TYPENAME (   X)
Value:
template<> struct TypeName<X> { \
static std::string name () {return #X;}; };

Function Documentation

◆ ClassImp()

ClassImp ( SH::MetaObject  )

trim leading/trailing spaces from the given string

Guarantee
strong
Failures
out of memory II

Definition at line 35 of file MetaObject.cxx.

38 {
39  namespace
40  {
41  template<class T> struct TypeName
42  {
43  static std::string name () {return typeid (T).name();}
44  };
45 #define TYPENAME(X) \
46  template<> struct TypeName<X> { \
47  static std::string name () {return #X;}; };
48  TYPENAME (std::string)
49  TYPENAME (double)
50  TYPENAME (int)
51  TYPENAME (bool)
52 #undef TYPENAME
53 
54 
55  template<class From,class To> struct Convert
56  {
57  static void convert (const std::string& /*from*/, To& /*to*/, const std::string& field)
58  {
59  if (field.empty())
60  return;
61  RCU_THROW_MSG ("no conversion defined from type " + TypeName<From>::name + " to type " + TypeName<To>::name + " for field " + field);
62  }
63  };
64 
65  // the conversion string => type is a bit problematic, as it is
66  // expected to produce an exception if the conversion cannot be
67  // performed. this requires several lines of code. in order to avoid
68  // duplication, this has been made into an additional preprocessor
69  // macro. while a template function would have been nicer, this
70  // would not allow to add the target typename to the error message,
71  // which is why this approach has been chosen.
72  template<class To> struct Convert<std::string,To>
73  {
74  static void convert (const std::string& from, To& to, const std::string& field)
75  {
76  std::stringstream ss (from);
77  To retval;
78  ss >> retval;
79  if (!(ss.fail() || ss.rdbuf()->in_avail() > 0))
80  {
81  std::swap (retval, to);
82  return;
83  }
84  if (field.empty())
85  return;
86  RCU_THROW_MSG ("unable to convert string '" + from + "' to type " + TypeName<To>::name() + " for field " + field);
87  }
88  };
89 
90  template<class Type> struct Convert<Type,Type>
91  {
92  static void convert (const Type& from, Type& to, const std::string& /*field*/)
93  {
94  to = from;
95  }
96  };
97 
98  template<class From> struct Convert<From,std::string>
99  {
100  static void convert (const From& from, std::string& to, const std::string& /*field*/)
101  {
102  std::ostringstream ss;
103  ss << from;
104  to = ss.str();
105  }
106  };
107 
108  template<> struct Convert<bool,std::string>
109  {
110  static void convert (const bool& from, std::string& to, const std::string& /*field*/)
111  {
112  if (from)
113  to = "true";
114  else
115  to = "false";
116  }
117  };
118 
119  template<> struct Convert<std::string,bool>
120  {
121  static void convert (const std::string& from, bool& to, const std::string& field)
122  {
123  if (from == "true" || from == "True" || from == "TRUE" || from == "1")
124  {
125  to = true;
126  return;
127  }
128  if (from == "false" || from == "False" || from == "FALSE" || from == "0")
129  {
130  to = false;
131  return;
132  }
133  if (field.empty())
134  return;
135  RCU_THROW_MSG ("unable to convert string '" + from + "' to type bool for field " + field);
136  }
137  };
138 
139 #define DEFAULT_CONVERT(FROM,TO) \
140  template<> struct Convert<FROM,TO> { \
141  static void convert (const FROM& from, TO& to, const std::string& /*field*/) { \
142  to = from; } };
143  DEFAULT_CONVERT (std::string, std::string)
144  DEFAULT_CONVERT (bool, double)
145  DEFAULT_CONVERT (int, double)
146  DEFAULT_CONVERT (bool, int)
147  DEFAULT_CONVERT (double, int)
148  DEFAULT_CONVERT (int, bool)
149  DEFAULT_CONVERT (double, bool)
150 #undef DEFAULT_CONVERT
151 
152 
153  template<class From,class To> bool
154  convertSingle (const TObject *from, To& to, const std::string& field)
155  {
156  const MetaData<From> *myfrom =
157  dynamic_cast<const MetaData<From>* >(from);
158  if (myfrom == 0)
159  return false;
160  Convert<From,To>::convert (myfrom->value, to, field);
161  return true;
162  }
163 
164  template<class To> void
165  convert (const TObject *from, To& to, const std::string& field)
166  {
167  if (convertSingle<std::string> (from, to, field)) return;
168  if (convertSingle<double> (from, to, field)) return;
169  if (convertSingle<int> (from, to, field)) return;
170  if (convertSingle<bool> (from, to, field)) return;
171  if (!field.empty())
172  RCU_THROW_MSG ("unkown input type " + std::string (typeid(*from).name()) + " for field " + field);
173  }
174 
180  std::string trim (const std::string& str)
181  {
182  size_t endpos = str.find_last_not_of(" \t");
183  size_t startpos = str.find_first_not_of(" \t");
184  if( std::string::npos == endpos ) return "";
185  if(str[startpos] == '"' || str[startpos] == '\'') startpos++;
186  if(str[endpos] == '"' || str[endpos] == '\'') endpos--;
187  return str.substr( startpos, endpos+1-startpos );
188  }
189 
190 
191  /*
202  public:
203  template<class T> T getT(const std::string& name, T def_val) const;
204 
214  public:
215  template<class T> static T convertT(const TObject* meta, T defval);
216  */
217  }
218 
219 
220 
221  std::string dbg (const MetaObject& /*obj*/, unsigned /*verbosity*/)
222  {
223  return "MetaObject";
224  }
225 
226 
227 
228  void swap (MetaObject& a, MetaObject& b)
229  {
230  a.swap (b);
231  }
232 
233 
234 
235  void MetaObject ::
236  testInvariant () const
237  {
238  RCU_INVARIANT (m_dataList != 0);
239  }
240 
241 
242 
243  MetaObject ::
244  MetaObject ()
245  : m_dataList (new TList)
246  {
247  m_dataList->SetOwner(true);
248  RCU_NEW_INVARIANT (this);
249  }
250 
251 
252 
253  MetaObject ::
254  MetaObject (const MetaObject& that)
255  : TCollection (), m_dataList (new TList)
256  {
257  m_dataList->SetOwner(true);
258  TIter iter (&that);
259  TObject *object = 0;
260  while ((object = iter.Next()))
261  {
262  m_dataList->Add (object->Clone());
263  };
264 
265  RCU_NEW_INVARIANT (this);
266  }
267 
268 
269 
270  MetaObject ::
271  ~MetaObject ()
272  {
273  RCU_DESTROY_INVARIANT (this);
274 
275  delete m_dataList;
276  }
277 
278 
279 
280  MetaObject& MetaObject ::
281  operator = (const MetaObject& that)
282  {
283  // no invariant used
284  MetaObject tmp (that);
285  tmp.swap (*this);
286  return *this;
287  }
288 
289 
290 
291  void MetaObject ::
292  swap (MetaObject& that)
293  {
294  std::swap (m_dataList, that.m_dataList);
295  }
296 
297 
298 
299  void MetaObject ::
300  remove (const std::string& name)
301  {
302  RCU_CHANGE_INVARIANT (this);
303 
304  TObject *object = 0;
305  while ((object = m_dataList->FindObject (name.c_str())))
306  delete m_dataList->Remove (object);
307  }
308 
309 
310 
311  void MetaObject ::
312  addReplace (TNamed *meta_swallow)
313  {
314  std::unique_ptr<TNamed> meta (meta_swallow);
315  RCU_CHANGE_INVARIANT (this);
316  RCU_REQUIRE_SOFT (meta_swallow != 0);
317 
318  remove (meta_swallow->GetName());
319  m_dataList->Add (meta.release());
320  }
321 
322 
323 
324  TObject *MetaObject ::
325  get (const std::string& name)
326  {
327  RCU_READ_INVARIANT (this);
328  return m_dataList->FindObject (name.c_str());
329  }
330 
331 
332 
333  const TObject *MetaObject ::
334  get (const std::string& name) const
335  {
336  RCU_READ_INVARIANT (this);
337  return m_dataList->FindObject (name.c_str());
338  }
339 
340 
341 
342  double MetaObject ::
343  getDouble (const std::string& name, double def_val) const
344  {
345  // no invariant used
346  return castDouble (name, def_val, CAST_NOCAST_DEFAULT);
347  }
348 
349 
350 
351  std::string MetaObject ::
352  getString (const std::string& name, const std::string& def_val) const
353  {
354  // no invariant used
355  return castString (name, def_val, CAST_NOCAST_DEFAULT);
356  }
357 
358 
359 
360  double MetaObject ::
361  castDouble (const std::string& name, double def_val,
362  CastMode mode) const
363  {
364  return castT (name, def_val, mode);
365  }
366 
367 
368 
369  std::string MetaObject ::
370  castString (const std::string& name, const std::string& def_val,
371  CastMode mode) const
372  {
373  return castT(name,def_val, mode);
374  }
375 
376 
377  int MetaObject ::
378  castInteger (const std::string& name, int def_val,
379  CastMode mode) const
380  {
381  return castT(name,def_val, mode);
382  }
383 
384 
385  bool MetaObject ::
386  castBool (const std::string& name, bool def_val,
387  CastMode mode) const
388  {
389  return castT(name,def_val, mode);
390  }
391 
392 
393  void MetaObject ::
394  setDouble (const std::string& name, double value)
395  {
396  setT (name, value);
397  }
398 
399 
400 
401  void MetaObject ::
402  setString (const std::string& name, const std::string& value)
403  {
404  setT(name,value);
405  }
406 
407 
408  void MetaObject ::
409  setInteger (const std::string& name, int value)
410  {
411  setT(name,value);
412  }
413 
414 
415  void MetaObject ::
416  setBool (const std::string& name, bool value)
417  {
418  setT(name,value);
419  }
420 
421 
422  void MetaObject ::
423  fetch (const MetaObject& source)
424  {
425  RCU_CHANGE_INVARIANT (this);
426 
427  TIter iter (&source);
428  TObject *object = 0;
429  while ((object = iter.Next()))
430  {
431  TNamed *const named = dynamic_cast<TNamed*>(object);
432  if (!named)
433  {
434  m_dataList->Add (object->Clone());
435  } else if (strncmp (named->GetName(), "nc_", 3) != 0)
436  {
437  addReplace (dynamic_cast<TNamed*>(named->Clone ()));
438  };
439  };
440  }
441 
442 
443 
444  void MetaObject ::
445  fetchDefaults (const MetaObject& source)
446  {
447  RCU_CHANGE_INVARIANT (this);
448 
449  TIter iter (&source);
450  TObject *object = 0;
451  while ((object = iter.Next()))
452  {
453  if (get (object->GetName ()) == 0)
454  m_dataList->Add (object->Clone());
455  };
456  }
457 
458 
459 
460  void MetaObject ::
461  Add (TObject *meta_swallow)
462  {
463  std::unique_ptr<TObject> meta (meta_swallow);
464  RCU_CHANGE_INVARIANT (this);
465  RCU_REQUIRE_SOFT (meta_swallow != 0);
466  m_dataList->Add (meta.release());
467  }
468 
469 
470 
471  void MetaObject ::
472  Clear (Option_t *option)
473  {
474  RCU_CHANGE_INVARIANT (this);
475  m_dataList->Clear (option);
476  }
477 
478 
479 
480  void MetaObject ::
481  Delete (Option_t *option)
482  {
483  RCU_CHANGE_INVARIANT (this);
484  m_dataList->Delete (option);
485  }
486 
487 
488 
489  TObject **MetaObject ::
490  GetObjectRef (const TObject *const meta) const
491  {
492  RCU_READ_INVARIANT (this);
493  RCU_REQUIRE_SOFT (meta != 0);
494 
495  TObject **const result = m_dataList->GetObjectRef (meta);
496 
497  RCU_PROVIDE (result != 0);
498  RCU_PROVIDE (*result == meta);
499  return result;
500  }
501 
502 
503 
504  TIterator *MetaObject ::
505  MakeIterator (Bool_t dir) const
506  {
507  RCU_READ_INVARIANT (this);
508 
509  std::unique_ptr<TIterator> result (m_dataList->MakeIterator (dir));
510 
511  RCU_PROVIDE2 (result.get() != 0, "result != 0");
512  return result.release();
513  }
514 
515 
516 
517  TObject *MetaObject ::
518  Remove (TObject *meta)
519  {
520  RCU_CHANGE_INVARIANT (this);
521  RCU_REQUIRE_SOFT (meta != 0);
522  return m_dataList->Remove (meta);
523  }
524 
525 
526 
527  void MetaObject ::
528  Streamer (TBuffer& b)
529  {
530  if (b.IsReading())
531  {
532  RCU_CHANGE_INVARIANT (this);
533  TObject::Streamer (b);
534  m_dataList->Clear ();
535  m_dataList->Streamer (b);
536  } else
537  {
538  RCU_READ_INVARIANT (this);
539  TObject::Streamer (b);
540  m_dataList->Streamer (b);
541  };
542  }
543 
544 
545  template<class T> T MetaObject ::
546  castT (const std::string& name, T def_val, CastMode mode) const
547  {
548  RCU_READ_INVARIANT (this);
549  const TObject* meta = get (name);
550  if (meta == 0)
551  return def_val;
552  const MetaData<T> *const retval = dynamic_cast<const MetaData<T>*>(meta);
553  if (retval)
554  return retval->value;
555  switch (mode)
556  {
557  case CAST_ERROR_THROW:
558  {
559  T result = def_val;
560  convert (meta, result, name);
561  return result;
562  }
563  case CAST_ERROR_DEFAULT:
564  {
565  T result = def_val;
566  convert (meta, result, "");
567  return result;
568  }
569  case CAST_NOCAST_THROW:
570  RCU_THROW_MSG ("invalid input value for " + name);
571  return def_val;
572  case CAST_NOCAST_DEFAULT:
573  return def_val;
574  }
575  RCU_ASSERT0 ("invalid cast value");
576  return def_val; //compiler dummy
577  }
578 
579 
580  void MetaObject ::
581  fetchFromString(const std::string& source){
582  size_t pos=1;
583  size_t oldpos = 0;
584  while(pos<source.size() && pos > 0){
585  size_t split = source.find("=",oldpos);
586  pos = source.find(",",split);
587  if(split < pos){
588  std::string key = trim(source.substr(oldpos,split-oldpos));
589  std::string value = trim(source.substr(split+1,pos-split-1));
590  this->setString(key,value);
591  } else {
592  RCU_THROW_MSG ("unable to parse string '"+source+"'");
593  }
594  oldpos = pos+1;
595  }
596  }
597 
598  std::string MetaObject ::
599  dumpToString(){
600  std::stringstream ss;
601  TIterator* itr = this->MakeIterator();
602  TObject* obj = NULL;
603  while( (obj = itr->Next()) ){
604  Meta* m = dynamic_cast<Meta*>(obj);
605  if(!m) continue;
606  if(!ss.str().empty()) ss << ", ";
607  ss << m->GetName() << "=";
608  MetaData<std::string>* s = dynamic_cast<MetaData<std::string>* >(m);
609  if(s){
610  ss << "\"" << s->value << "\"";
611  } else {
612  std::string mystring;
613  convert<std::string> (m, mystring, m->GetName());
614  ss << mystring;
615  }
616  }
617  return ss.str();
618  }
619 
620  Int_t MetaObject ::
621  GetEntries() const {
622  return this->m_dataList->GetEntries();
623  }
624 
625 }
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
get_generator_info.result
result
Definition: get_generator_info.py:21
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
trim
std::string trim(const std::string &str, const std::string &whitespace=" \t")
Definition: BTaggingTruthTaggingTool.cxx:1149
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
athena.value
value
Definition: athena.py:122
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
SH::Meta::dbg
std::string dbg(const Meta &obj, unsigned verbosity=0)
the debugging info of this object
Definition: Meta.cxx:28
Monitored::X
@ X
Definition: HistogramFillerUtils.h:24
LArCellBinning_test.retval
def retval
Definition: LArCellBinning_test.py:112
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
RCU_PROVIDE2
#define RCU_PROVIDE2(x, y)
Definition: Assert.h:217
DEFAULT_CONVERT
#define DEFAULT_CONVERT(FROM, TO)
RCU_PROVIDE
#define RCU_PROVIDE(x)
Definition: Assert.h:215
RCU_REQUIRE_SOFT
#define RCU_REQUIRE_SOFT(x)
Definition: Assert.h:153
TYPENAME
#define TYPENAME(X)
Preparation.mode
mode
Definition: Preparation.py:95
SH::MetaObject::swap
void swap(MetaObject &a, MetaObject &b)
standard swap
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
xAODType
Definition: ObjectType.h:13
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
RCU_INVARIANT
#define RCU_INVARIANT(x)
Definition: Assert.h:201
beamspotman.dir
string dir
Definition: beamspotman.py:623
python.grid.Add
def Add(name)
Definition: grid.py:41
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
TMVAToMVAUtils::convert
std::unique_ptr< MVAUtils::BDT > convert(TMVA::MethodBDT *bdt, bool isRegression=true, bool useYesNoLeaf=false)
Definition: TMVAToMVAUtils.h:114
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:32
a
TList * a
Definition: liststreamerinfos.cxx:10
RCU_DESTROY_INVARIANT
#define RCU_DESTROY_INVARIANT(x)
Definition: Assert.h:235
generate::GetEntries
double GetEntries(TH1D *h, int ilow, int ihi)
Definition: rmsFrac.cxx:20
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
pickleTool.object
object
Definition: pickleTool.py:30
RCU_ASSERT0
#define RCU_ASSERT0(y)
Definition: Assert.h:226
str
Definition: BTagTrackIpAccessor.cxx:11
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
python.PyAthena.obj
obj
Definition: PyAthena.py:135
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233