GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <c.andreopoulos \at cern.ch>
9  University of Liverpool
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-2024, 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 #include "TMatrixT.h"
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 
139  static string BuildParamVectKey( const std::string & comm_name, unsigned int i ) ;
140  static string BuildParamVectSizeKey( const std::string & comm_name ) ;
141 
142  static string BuildParamMatKey( const std::string & comm_name, unsigned int i, unsigned int j) ;
143  static string BuildParamMatRowSizeKey( const std::string & comm_name ) ;
144  static string BuildParamMatColSizeKey( const std::string & comm_name ) ;
145 
146 
147 protected:
148  Algorithm();
149  Algorithm(string name);
150  Algorithm(string name, string config);
151 
152  void Initialize (void);
153  void DeleteConfig (void);
154  void DeleteSubstructure (void);
155 
156  //! Split an incoming configuration Registry into a block valid for this algorithm
157  //! Ownership of the returned registry belongs to the algo
158  Registry * ExtractLocalConfig( const Registry & in ) const ;
159  //! Split an incoming configuration Registry into a block valid for the sub-algo identified by alg_key
160  Registry * ExtractLowerConfig( const Registry & in, const string & alg_key ) const ;
161 
162  bool fAllowReconfig; ///<
163  // bool fOwnsConfig; ///< true if it owns its config. registry
164  bool fOwnsSubstruc; ///< true if it owns its substructure (sub-algs,...)
165  AlgId fID; ///< algorithm name and configuration set
166 
167 
168  /// ideally these members should go private
169  /// Registry will be access only through the GetParam method
170  vector<Registry*> fConfVect ; ///< configurations registries from various sources
171  ///< the order of the vector is the precedence in case of repeated parameters
172  ///< position 0 -> Highest precedence
173  vector<bool> fOwnerships ; ///< ownership for every registry in fConfVect
174 
175  AlgStatus_t fStatus; ///< algorithm execution status
176  AlgMap * fOwnedSubAlgMp; ///< local pool for owned sub-algs (taken out of the factory pool)
177 
178  //! Ideal access to a parameter value from the vector of registries
179  //! Returns true if the value is found and the parameters is set
180  template<class T>
181  bool GetParam( const RgKey & name, T & p, bool is_top_call = true ) const ;
182 
183  //! Ideal access to a parameter value from the vector of registries,
184  //! With default value. Returns true if the value is set from the
185  //! registries, false if the value is the default
186  template<class T>
187  bool GetParamDef( const RgKey & name, T & p, const T & def ) const ;
188 
189  //! Handle to load vectors of parameters
190  template<class T>
191  int GetParamVect( const std::string & comm_name, std::vector<T> & v,
192  bool is_top_call = true ) const ;
193 
194  int GetParamVectKeys( const std::string & comm_name, std::vector<RgKey> & k,
195  bool is_top_call = true ) const ;
196 
197  //! Handle to load matrix of parameters
198  template<class T>
199  int GetParamMat( const std::string & comm_name, TMatrixT<T> & mat,
200  bool is_top_call = true ) const ;
201  template<class T>
202  int GetParamMatSym( const std::string & comm_name, TMatrixTSym<T> & mat,
203  bool is_top_call = true ) const ;
204 
205  int GetParamMatKeys( const std::string & comm_name, std::vector<RgKey> & k,
206  bool is_top_call = true ) const ;
207 
208 
209  int AddTopRegistry( Registry * rp, bool owns = true ); ///< add registry with top priority, also update ownership
210  int AddLowRegistry( Registry * rp, bool owns = true ); ///< add registry with lowest priority, also update ownership
211  int MergeTopRegistry( const Registry & r ) ; ///< Merge with top level registry if first reg of the vector is owned
212  ///< Otherwise an owned copy is added as a top registry
213  int AddTopRegisties( const vector<Registry*> & rs, bool owns = false ) ; ///< Add registries with top priority, also udated Ownerships
214 
215 private:
216 
217  Registry * fConfig; ///< Summary configuration derived from fConvVect, not necessarily allocated
218 
219 };
220 
221 } // genie namespace
222 
223 #ifndef __CINT__ // don't even try for ROOT 5
224 #include "Framework/Algorithm/Algorithm.icc"
225 #endif
226 
227 #endif // _ALGORITHM_H_
static string BuildParamMatKey(const std::string &comm_name, unsigned int i, unsigned int j)
Definition: Algorithm.cxx:527
Registry * GetOwnedConfig(void)
Definition: Algorithm.cxx:279
virtual ~Algorithm()
Definition: Algorithm.cxx:56
friend ostream & operator<<(ostream &stream, const Algorithm &alg)
Definition: Algorithm.cxx:31
static string BuildParamMatColSizeKey(const std::string &comm_name)
Definition: Algorithm.cxx:545
void DeleteSubstructure(void)
Definition: Algorithm.cxx:488
int GetParamMatKeys(const std::string &comm_name, std::vector< RgKey > &k, bool is_top_call=true) const
Definition: Algorithm.cxx:585
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:650
AlgStatus_t fStatus
algorithm execution status
Definition: Algorithm.h:175
AlgMap * fOwnedSubAlgMp
local pool for owned sub-algs (taken out of the factory pool)
Definition: Algorithm.h:176
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:164
int MergeTopRegistry(const Registry &r)
Definition: Algorithm.cxx:719
Algorithm abstract base class.
Definition: Algorithm.h:54
AlgId fID
algorithm name and configuration set
Definition: Algorithm.h:165
int AddTopRegisties(const vector< Registry * > &rs, bool owns=false)
Add registries with top priority, also udated Ownerships.
Definition: Algorithm.cxx:754
map< string, Algorithm * > AlgMap
Definition: Algorithm.h:49
int GetParamMat(const std::string &comm_name, TMatrixT< T > &mat, bool is_top_call=true) const
Handle to load matrix of parameters.
virtual const Registry & GetConfig(void) const
Definition: Algorithm.cxx:246
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:127
static string BuildParamMatRowSizeKey(const std::string &comm_name)
Definition: Algorithm.cxx:537
virtual void Configure(const Registry &config)
Definition: Algorithm.cxx:62
void AdoptSubstructure(void)
Definition: Algorithm.cxx:400
void Initialize(void)
Definition: Algorithm.cxx:335
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:686
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:51
virtual const AlgId & Id(void) const
Get algorithm ID.
Definition: Algorithm.h:98
vector< Registry * > fConfVect
Definition: Algorithm.h:170
static string BuildParamVectSizeKey(const std::string &comm_name)
Definition: Algorithm.cxx:520
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:65
vector< bool > fOwnerships
ownership for every registry in fConfVect
Definition: Algorithm.h:173
int GetParamVectKeys(const std::string &comm_name, std::vector< RgKey > &k, bool is_top_call=true) const
Definition: Algorithm.cxx:555
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
int GetParamMatSym(const std::string &comm_name, TMatrixTSym< T > &mat, bool is_top_call=true) const
Registry * fConfig
Summary configuration derived from fConvVect, not necessarily allocated.
Definition: Algorithm.h:217
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:702
Registry * ExtractLocalConfig(const Registry &in) const
Definition: Algorithm.cxx:619
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
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