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