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