Algorithm.h
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \class genie::Algorithm
5 
6 \brief Algorithm abstract base class.
7 
8 \author Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
9  University of Liverpool & STFC Rutherford Appleton Lab
10 
11  Marco Roda <mroda \at liverpool.ac.uk>
12  University of Liverpool
13 
14 \created May 02, 2004
15 
16 \cpright Copyright (c) 2003-2019, The GENIE Collaboration
17  For the full text of the license visit http://copyright.genie-mc.org
18  or see $GENIE/LICENSE
19 */
20 //____________________________________________________________________________
21 
22 #ifndef _ALGORITHM_H_
23 #define _ALGORITHM_H_
24 
25 #include <string>
26 #include <sstream>
27 #include <iostream>
28 #include <cassert>
29 #include <map>
30 
38 
39 using std::string;
40 using std::ostream;
41 using std::map;
42 
43 namespace genie {
44 
45 class Algorithm;
46 
47 ostream & operator << (ostream & stream, const Algorithm & alg);
48 
49 typedef map <string, Algorithm *> AlgMap;
50 typedef map <string, Algorithm *>::iterator AlgMapIter;
51 typedef map <string, Algorithm *>::const_iterator AlgMapConstIter;
52 typedef pair<string, Algorithm *> AlgMapPair;
53 
54 class Algorithm {
55 
56 public:
57  virtual ~Algorithm();
58 
59  //! Configure the algorithm with an external registry
60  //! The registry is merged with the top level registry if it is owned,
61  //! Otherwise a copy of it is added with the highest priority
62  virtual void Configure (const Registry & config);
63 
64  //! Configure the algorithm from the AlgoConfigPool
65  //! based on param_set string given in input
66  //! An algorithm contains a vector of registries coming from different
67  //! xml configuration files, which are loaded according a very precise prioriy
68  //! This methods will load a number registries in order of priority:
69  //! 1) "Tunable" parameter set from CommonParametes. This is loaded with the
70  //! highest prioriry and it is designed to be used for tuning procedure
71  //! Usage not expected from the user.
72  //! 2) For every string defined in "CommonParame" the corresponding parameter set will be loaded
73  //! from CommonParameter.xml
74  //! 3) parameter set specified by the config string and defined in the xml file of the algorithm
75  //! 4) if config is not "Default" also the Default parameter set from the same xml file will be loaded
76  //! Effectively this avoids the repetion of a parameter when it is not changed in the requested configuration
77  virtual void Configure (string config);
78 
79  //! Lookup configuration from the config pool
80  //! Similar logic from void Configure(string)
81  virtual void FindConfig (void);
82 
83  //! Get configuration registry
84  //! Evaluate the summary of the configuration and returns it
85  //! The summary of a configuration is a merge of all the registries
86  //! known to the algorithm (see Configure() methods) but every parameter is appearing only
87  //! once and in case of repetitions, only the parameter from the registry with the highest prioriry
88  //! is considered.
89  virtual const Registry & GetConfig(void) const ;
90 
91  //! Returns the pointer of the summary registry, see previous method
92  //! Gives access to the summary so it could be changed.
93  //! The usage of this method is deprecated as it is mantained only for back compatibility.
94  //! If you need to add or chage a parter (or more), use the AddTopRegistry() instead
95  Registry * GetOwnedConfig(void);
96 
97  //! Get algorithm ID
98  virtual const AlgId & Id(void) const { return fID; }
99 
100  //! Get algorithm status
101  virtual AlgStatus_t GetStatus(void) const { return fStatus; }
102 
103  //! Allow reconfigration after initializaton?
104  //! Algorithms may opt-out, if reconfiguration is not necessary,
105  //! to improve event reweighting speed.
106  virtual bool AllowReconfig(void) const { return fAllowReconfig; }
107 
108  //! Compare with input algorithm
109  virtual AlgCmp_t Compare(const Algorithm * alg) const;
110 
111  //! Set algorithm ID
112  virtual void SetId(const AlgId & id);
113  virtual void SetId(string name, string config);
114 
115  //! Access the sub-algorithm pointed to by the input key, either from the
116  //! local pool or from AlgFactory's pool
117  const Algorithm * SubAlg(const RgKey & registry_key) const;
118 
119  //! Clone the configuration registry looked up from the configuration pool
120  //! and take its ownership
121  void AdoptConfig (void);
122 
123  //! Take ownership of the algorithms subtructure (sub-algorithms,...)
124  //! by copying them from the AlgFactory pool to the local pool
125  //! Also bring all the configuration variables to the top level config Registry.
126  //! This can be used to group together a series of algorithms & their
127  //! configurations and extract (a clone of) this group from the shared
128  //! pools. Having a series of algorithms/configurations behaving as a
129  //! monolithic block, with a single point of configuration (the top level)
130  //! is to be used when bits & pieces of GENIE are used in isolation for
131  //! data fitting or reweighting
132  void AdoptSubstructure (void);
133 
134  //! Print algorithm info
135  virtual void Print(ostream & stream) const;
136  friend ostream & operator << (ostream & stream, const Algorithm & alg);
137 
138 protected:
139  Algorithm();
140  Algorithm(string name);
141  Algorithm(string name, string config);
142 
143  void Initialize (void);
144  void DeleteConfig (void);
145  void DeleteSubstructure (void);
146 
147  //! Split an incoming configuration Registry into a block valid for this algorithm
148  //! Ownership of the returned registry belongs to the algo
149  Registry * ExtractLocalConfig( const Registry & in ) const ;
150  //! Split an incoming configuration Registry into a block valid for the sub-algo identified by alg_key
151  Registry * ExtractLowerConfig( const Registry & in, const string & alg_key ) const ;
152 
153  bool fAllowReconfig; ///<
154  // bool fOwnsConfig; ///< true if it owns its config. registry
155  bool fOwnsSubstruc; ///< true if it owns its substructure (sub-algs,...)
156  AlgId fID; ///< algorithm name and configuration set
157 
158 
159  /// ideally these members should go private
160  /// Registry will be access only through the GetParam method
161  vector<Registry*> fConfVect ; ///< configurations registries from various sources
162  ///< the order of the vector is the precedence in case of repeated parameters
163  ///< position 0 -> Highest precedence
164  vector<bool> fOwnerships ; ///< ownership for every registry in fConfVect
165 
166  AlgStatus_t fStatus; ///< algorithm execution status
167  AlgMap * fOwnedSubAlgMp; ///< local pool for owned sub-algs (taken out of the factory pool)
168 
169  //! Ideal access to a parameter value from the vector of registries
170  //! Returns true if the value is found and the parameters is set
171  template<class T>
172  bool GetParam( const RgKey & name, T & p, bool is_top_call = true ) const ;
173 
174  //! Ideal access to a parameter value from the vector of registries,
175  //! With default value. Returns true if the value is set from the
176  //! registries, false if the value is the default
177  template<class T>
178  bool GetParamDef( const RgKey & name, T & p, const T & def ) const ;
179 
180  //! Handle to load vectors of parameters
181  //! It looks for different registry item with name comm_name0, comm_name1, etc...
182  template<class T>
183  bool GetParamVect( const std::string & comm_name, std::vector<T> & v,
184  unsigned int max, bool is_top_call = true ) const ;
185 
186  int AddTopRegistry( Registry * rp, bool owns = true ); ///< add registry with top priority, also update ownership
187  int AddLowRegistry( Registry * rp, bool owns = true ); ///< add registry with lowest priority, also update ownership
188  int MergeTopRegistry( const Registry & r ) ; ///< Merge with top level registry if first reg of the vector is owned
189  ///< Otherwise an owned copy is added as a top registry
190  int AddTopRegisties( const vector<Registry*> & rs, bool owns = false ) ; ///< Add registries with top priority, also udated Ownerships
191 
192 private:
193 
194  Registry * fConfig; ///< Summary configuration derived from fConvVect, not necessarily allocated
195 
196 };
197 
198 } // genie namespace
199 
200 #ifndef __CINT__ // don't even try for ROOT 5
201 #include "Framework/Algorithm/Algorithm.icc"
202 #endif
203 
204 #endif // _ALGORITHM_H_
const XML_Char * name
Definition: expat.h:151
Registry * GetOwnedConfig(void)
Definition: Algorithm.cxx:287
virtual ~Algorithm()
Definition: Algorithm.cxx:64
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
friend ostream & operator<<(ostream &stream, const Algorithm &alg)
Definition: Algorithm.cxx:39
void DeleteSubstructure(void)
Definition: Algorithm.cxx:496
Registry * ExtractLowerConfig(const Registry &in, const string &alg_key) const
Split an incoming configuration Registry into a block valid for the sub-algo identified by alg_key...
Definition: Algorithm.cxx:549
AlgStatus_t fStatus
algorithm execution status
Definition: Algorithm.h:166
const char * p
Definition: xmltok.h:285
AlgMap * fOwnedSubAlgMp
local pool for owned sub-algs (taken out of the factory pool)
Definition: Algorithm.h:167
bool fOwnsSubstruc
true if it owns its substructure (sub-algs,...)
Definition: Algorithm.h:155
int MergeTopRegistry(const Registry &r)
Definition: Algorithm.cxx:618
Algorithm abstract base class.
Definition: Algorithm.h:54
Definition: config.py:1
AlgId fID
algorithm name and configuration set
Definition: Algorithm.h:156
int AddTopRegisties(const vector< Registry * > &rs, bool owns=false)
Add registries with top priority, also udated Ownerships.
Definition: Algorithm.cxx:653
map< string, Algorithm * > AlgMap
Definition: Algorithm.h:49
bool GetParamVect(const std::string &comm_name, std::vector< T > &v, unsigned int max, bool is_top_call=true) const
virtual const Registry & GetConfig(void) const
Definition: Algorithm.cxx:254
virtual AlgStatus_t GetStatus(void) const
Get algorithm status.
Definition: Algorithm.h:101
enum genie::EAlgCmp AlgCmp_t
enum genie::EAlgStatus AlgStatus_t
virtual void FindConfig(void)
Definition: Algorithm.cxx:135
virtual void Configure(const Registry &config)
Definition: Algorithm.cxx:70
void AdoptSubstructure(void)
Definition: Algorithm.cxx:408
void Initialize(void)
Definition: Algorithm.cxx:343
pair< string, Algorithm * > AlgMapPair
Definition: Algorithm.h:52
int AddTopRegistry(Registry *rp, bool owns=true)
add registry with top priority, also update ownership
Definition: Algorithm.cxx:585
void AdoptConfig(void)
Definition: Algorithm.cxx:394
Algorithm ID (algorithm name + configuration set name)
Definition: AlgId.h:35
void DeleteConfig(void)
Definition: Algorithm.cxx:471
map< string, Algorithm * >::const_iterator AlgMapConstIter
Definition: Algorithm.h:51
virtual const AlgId & Id(void) const
Get algorithm ID.
Definition: Algorithm.h:98
ifstream in
Definition: comparison.C:7
vector< Registry * > fConfVect
Definition: Algorithm.h:161
virtual bool AllowReconfig(void) const
Definition: Algorithm.h:106
string RgKey
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
A registry. Provides the container for algorithm configuration parameters.
Definition: Registry.h:66
vector< bool > fOwnerships
ownership for every registry in fConfVect
Definition: Algorithm.h:164
virtual AlgCmp_t Compare(const Algorithm *alg) const
Compare with input algorithm.
Definition: Algorithm.cxx:294
virtual void Print(ostream &stream) const
Print algorithm info.
Definition: Algorithm.cxx:323
TRandom3 r(0)
Registry * fConfig
Summary configuration derived from fConvVect, not necessarily allocated.
Definition: Algorithm.h:194
virtual void SetId(const AlgId &id)
Set algorithm ID.
Definition: Algorithm.cxx:313
int AddLowRegistry(Registry *rp, bool owns=true)
add registry with lowest priority, also update ownership
Definition: Algorithm.cxx:601
double T
Definition: Xdiff_gwt.C:5
Registry * ExtractLocalConfig(const Registry &in) const
Definition: Algorithm.cxx:518
bool GetParamDef(const RgKey &name, T &p, const T &def) const
bool GetParam(const RgKey &name, T &p, bool is_top_call=true) const
map< string, Algorithm * >::iterator AlgMapIter
Definition: Algorithm.h:50
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
const Algorithm * SubAlg(const RgKey &registry_key) const
Definition: Algorithm.cxx:353
enum BeamMode string