GENIEGenerator
Loading...
Searching...
No Matches
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-2025, 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
39using std::string;
40using std::ostream;
41using std::map;
42
43namespace genie {
44
45class Algorithm;
46
47ostream & operator << (ostream & stream, const Algorithm & alg);
48
49typedef map <string, Algorithm *> AlgMap;
50typedef map <string, Algorithm *>::iterator AlgMapIter;
51typedef map <string, Algorithm *>::const_iterator AlgMapConstIter;
52typedef pair<string, Algorithm *> AlgMapPair;
53
54class Algorithm {
55
56public:
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
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
147protected:
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
215private:
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_
string RgKey
Algorithm ID (algorithm name + configuration set name)
Definition AlgId.h:34
Algorithm abstract base class.
Definition Algorithm.h:54
Registry * GetOwnedConfig(void)
static string BuildParamMatKey(const std::string &comm_name, unsigned int i, unsigned int j)
int GetParamMat(const std::string &comm_name, TMatrixT< T > &mat, bool is_top_call=true) const
Handle to load matrix of parameters.
int GetParamMatKeys(const std::string &comm_name, std::vector< RgKey > &k, bool is_top_call=true) const
void AdoptSubstructure(void)
void Initialize(void)
AlgStatus_t fStatus
algorithm execution status
Definition Algorithm.h:175
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.
int GetParamMatSym(const std::string &comm_name, TMatrixTSym< T > &mat, bool is_top_call=true) const
static string BuildParamMatColSizeKey(const std::string &comm_name)
bool fOwnsSubstruc
true if it owns its substructure (sub-algs,...)
Definition Algorithm.h:164
Registry * ExtractLocalConfig(const Registry &in) const
int AddLowRegistry(Registry *rp, bool owns=true)
add registry with lowest priority, also update ownership
virtual AlgCmp_t Compare(const Algorithm *alg) const
Compare with input algorithm.
virtual const Registry & GetConfig(void) const
static string BuildParamVectSizeKey(const std::string &comm_name)
static string BuildParamMatRowSizeKey(const std::string &comm_name)
Registry * fConfig
Summary configuration derived from fConvVect, not necessarily allocated.
Definition Algorithm.h:217
bool GetParam(const RgKey &name, T &p, bool is_top_call=true) const
vector< bool > fOwnerships
ownership for every registry in fConfVect
Definition Algorithm.h:173
virtual void FindConfig(void)
int GetParamVectKeys(const std::string &comm_name, std::vector< RgKey > &k, bool is_top_call=true) const
int GetParamVect(const std::string &comm_name, std::vector< T > &v, bool is_top_call=true) const
Handle to load vectors of parameters.
void AdoptConfig(void)
virtual void SetId(const AlgId &id)
Set algorithm ID.
void DeleteSubstructure(void)
int MergeTopRegistry(const Registry &r)
vector< Registry * > fConfVect
Definition Algorithm.h:170
AlgId fID
algorithm name and configuration set
Definition Algorithm.h:165
virtual void Configure(const Registry &config)
Definition Algorithm.cxx:62
virtual bool AllowReconfig(void) const
Definition Algorithm.h:106
bool GetParamDef(const RgKey &name, T &p, const T &def) const
void DeleteConfig(void)
virtual AlgStatus_t GetStatus(void) const
Get algorithm status.
Definition Algorithm.h:101
const Algorithm * SubAlg(const RgKey &registry_key) const
virtual void Print(ostream &stream) const
Print algorithm info.
virtual ~Algorithm()
Definition Algorithm.cxx:56
int AddTopRegisties(const vector< Registry * > &rs, bool owns=false)
Add registries with top priority, also udated Ownerships.
AlgMap * fOwnedSubAlgMp
local pool for owned sub-algs (taken out of the factory pool)
Definition Algorithm.h:176
friend ostream & operator<<(ostream &stream, const Algorithm &alg)
Definition Algorithm.cxx:31
int AddTopRegistry(Registry *rp, bool owns=true)
add registry with top priority, also update ownership
virtual const AlgId & Id(void) const
Get algorithm ID.
Definition Algorithm.h:98
static string BuildParamVectKey(const std::string &comm_name, unsigned int i)
A registry. Provides the container for algorithm configuration parameters.
Definition Registry.h:65
THE MAIN GENIE PROJECT NAMESPACE
Definition AlgCmp.h:25
map< string, Algorithm * >::iterator AlgMapIter
Definition Algorithm.h:50
enum genie::EAlgStatus AlgStatus_t
pair< string, Algorithm * > AlgMapPair
Definition Algorithm.h:52
map< string, Algorithm * >::const_iterator AlgMapConstIter
Definition Algorithm.h:51
enum genie::EAlgCmp AlgCmp_t
map< string, Algorithm * > AlgMap
Definition Algorithm.h:49
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)