KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVVarGlob.cpp
Go to the documentation of this file.
1 #include "KVVarGlob.h"
2 #include "KVClassFactory.h"
3 #include "TObjString.h"
4 
7 /*
8 
9 */
11 
12 //_________________________________________________________________
13 
14 
16 void KVVarGlob::init(void)
17 {
18  nameList.Clear();
19  // one-body variable by default
20  fType = kOneBody;
21  fValueType = 'D';
22  fMaxNumBranches = -1;
23  fIsInitialized = kFALSE;
24  fNormalization = 1.0;
25  fOptions.SetName("Options");
26  fParameters.SetName("Parameters");
27 }
28 
29 
30 
44 
45 void KVVarGlob::MakeClass(const Char_t* classname, const Char_t* classdesc, int type)
46 {
47  // Creates skeleton '.h' and '.cpp' files for a new global variable class which
48  // inherits from this class. Give a name for the new class and a short description
49  // which will be used to document the class.
50  //
51  // By default the new class will be of type 1-body. A fill(const KVNucleus*) method
52  // will be generated which the user should complete.
53  //
54  // For a 2-body variable, call MakeClass with type = KVVarGlob::kTwoBody.
55  // A skeleton fill2(const KVNucleus*,const KVNucleus*) method will be generated.
56  //
57  // For a N-body variable, call MakeClass with type = KVVarGlob::kNBody.
58  // A skeleton fillN(KVEvent*) method will be generated.
59 
60  KVClassFactory cf(classname, classdesc, "KVVarGlob", kTRUE);
61 
62  KVString body;
63 
64  // add 'init' method
65  KVVarGlob::AddInitMethod(cf, body);
68 
69  // add 'fill', 'fill2', or 'FillN' method
71 
72  // body of 'fill', 'fill2', or 'FillN' method
74 
75  // add body of method
77 
78  cf.GenerateCode();
79 }
80 
81 
82 
86 
87 void KVVarGlob::FillMethodBody(KVString& body, int type)
88 {
89  // PRIVATE method used by MakeClass.
90  // body of 'fill', 'fill2', or 'FillN' method
91  switch (type) {
92  case kTwoBody:
93  body = "Calculation of contribution to 2-body global variable of pair (n1,n2) of nuclei.\n";
94  body += "NOTE: this method will be called once for each distinct pair of nuclei in the event\n";
95  body += "including pairs of identical nuclei (n1 = n2).\n";
96  body += "If you want to calculate a global variable using only each non-identical pair,\n";
97  body += "then make sure in your implementation that you check n1!=n2\n";
98  break;
99  case kNBody:
100  body = "Calculation of contribution to N-body global variable of particles in event e.\n";
101  break;
102  default:
103  body = "Calculation of contribution to 1-body global variable of nucleus n\n";
104  }
105 }
106 
107 
108 
110 
112 {
113  switch (type) {
114  case kTwoBody:
115  body = " fType = KVVarGlob::kTwoBody; // this is a 2-body variable\n";
116  break;
117  case kNBody:
118  body = " fType = KVVarGlob::kNBody; // this is a N-body variable\n";
119  break;
120  default:
121  body = " fType = KVVarGlob::kOneBody; // this is a 1-body variable\n";
122  }
123  cf.AddMethodBody("init", body);
124 }
125 
126 
127 
129 
131 {
132  body += "\n";
133  body += "You should also (if your variable calculates several different quantities)\n";
134  body += "set up a correspondance between named values and index number\n";
135  body += "using method SetNameIndex(const Char_t*,Int_t)\n";
136  body += "in order for GetValue(const Char_t*) to work correctly.\n";
137  body += "The index numbers should be the same as in your getvalue_int(Int_t) method.\n";
138  body += "\n";
139  cf.AddMethodComment("init", body);
140 }
141 
142 
143 
147 
149 {
150  // PRIVATE method used by MakeClass.
151  // add 'init' method
152  cf.AddMethod("init", "void", "private");
153  body = "Private initialisation method called by all constructors.\n";
154  body += "All member initialisations should be done here.\n";
155  cf.AddMethodComment("init", body);
156 }
157 
158 
159 
163 
165 {
166  // PRIVATE method used by MakeClass.
167  // add 'Fill', 'Fill2', or 'FillN' method
168  switch (type) {
169  case kTwoBody:
170  cf.AddMethod("fill2", "void", "protected");
171  cf.AddMethodArgument("fill2", "const KVNucleus*", "n1");
172  cf.AddMethodArgument("fill2", "const KVNucleus*", "n2");
173  break;
174  case kNBody:
175  cf.AddMethod("FillN", "void");
176  cf.AddMethodArgument("FillN", "KVEvent*", "e");
177  cf.AddHeaderIncludeFile("KVEvent.h");
178  break;
179  default:
180  cf.AddMethod("fill", "void", "protected");
181  cf.AddMethodArgument("fill", "const KVNucleus*", "n");
182  }
183 }
184 
185 
186 
190 
192 {
193  // PRIVATE method used by MakeClass.
194  // add body of fill method
195  switch (type) {
196  case kTwoBody:
197  cf.AddMethodComment("fill2", body);
198  break;
199  case kNBody:
200  cf.AddMethodComment("FillN", body);
201  break;
202  default:
203  cf.AddMethodComment("fill", body);
204  }
205 }
206 
207 
208 
222 
223 void KVVarGlob::SetNameIndex(const Char_t* name, Int_t index)
224 {
225  // For a multi-valued global variable, sets up the correspondance between value name and index.
226  //
227  // These can then be used to retrieve values with GetValue("name") or GetValue(index).
228  //
229  // When automatic branch creation in a TTree is used (see KVGVList::MakeBranches()), the names given
230  // to this method will be used to name the branches as:
231  // - [varName].[value0_name]
232  // - [varName].[value1_name]
233  // - ... etc.
234  //
235  // GetNumberOfValues() returns the number of values associated with the variable,
236  // corresponding to the number of name-index associations are created by calling this method.
237 
238  if (!(nameList.HasParameter(name))) {
239  nameList.SetValue(name, index);
240  }
241  else {
242  Warning("SetNameIndex(const Char_t *name,Int_t index)",
243  "No link between \"%s\" and the index %d: the name already exists.",
244  name, index);
245  }
246 }
247 
248 
249 
253 
255 {
256  // \return the index corresponding to name
257  // \sa SetNameIndex()
258 
259  Int_t index = 0;
260  if (nameList.HasParameter(name)) {
261  index = nameList.GetIntValue(name);
262  }
263  else {
264  Warning("GetNameIndex(const Char_t *name)",
265  "The parameter \"%s\" does not exist fot the Class %s.\n 0 returned.",
266  name, ClassName());
267 
268  }
269  return index;
270 }
271 
272 
273 
275 
277 {
278  printf("GLOBVAR: %s class=%s [", GetName(), ClassName());
279  switch (fType) {
280  case kOneBody:
281  printf("One-body variable]\n");
282  break;
283  case kTwoBody:
284  printf("Two-body variable]\n");
285  break;
286  case kNBody:
287  printf("N-body variable]\n");
288  break;
289  default:
290  printf("Variable type unknown!]\n");
291  }
292  if (GetNumberOfValues() > 1)
293  printf("- Multi-valued variable:\n");
294  else
295  printf("- Single-valued variable:\n");
296  printf("- Kinematics calculated in ");
297  if (fFrame != "") printf("%s", fFrame.Data());
298  else printf("default");
299  printf(" frame\n");
300  if (fOptions.GetNpar()) {
301  printf("- List of options:\n");
302  fOptions.Print();
303  }
304  if (fParameters.GetNpar()) {
305  printf("- List of parameters:\n");
306  fParameters.Print();
307  }
308  if (fSelection.IsSet()) {
309  printf("- Particle selection criteria:\n");
310  fSelection.Print();
311  }
312  printf("- Number of branches in TTree: %d\n", GetNumberOfBranches());
313  if (GetNumberOfValues() > 1) {
314  printf("- Available values:\n");
315  printf(" IND. NAME VALUE\n");
316  for (int i = 0; i < GetNumberOfValues(); ++i) {
318  printf(" %d %s %f\n", np->GetInt(), np->GetName(), GetValue(np->GetName()));
319  }
320  }
321  printf("\n");
322 }
323 
324 
325 
int Int_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
const Bool_t kTRUE
const char Option_t
int type
virtual void Clear(Option_t *opt="")
Clear object properties : name, type/title, number, label.
Definition: KVBase.cpp:383
Factory class for generating skeleton files for new classes.
void AddMethodComment(const Char_t *method_name, const KVString &comment)
Set the comments for method 'method_name' added to the class using AddMethod.
void GenerateCode()
Generate header and implementation file for currently-defined class.
void AddHeaderIncludeFile(const Char_t *filename)
void AddMethodBody(const Char_t *method_name, const KVString &body)
void AddMethodArgument(const Char_t *method_name, const Char_t *argument_type, const Char_t *argument_name="", const Char_t *default_value="")
KVClassMethod * AddMethod(const Char_t *name, const Char_t *return_type, const Char_t *access="public", Bool_t isVirtual=kFALSE, Bool_t isConst=kFALSE)
virtual void Print(Option_t *opt="") const
KVNamedParameter * GetParameter(Int_t idx) const
return the parameter object with index idx
Int_t GetIntValue(const Char_t *name) const
void SetValue(const Char_t *name, value_type value)
Int_t GetNpar() const
return the number of stored parameters
Bool_t HasParameter(const Char_t *name) const
A generic named parameter storing values of different types.
Int_t GetInt() const
void Print(Option_t *opt="") const
Print informations on object.
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
Base class for all global variable implementations.
Definition: KVVarGlob.h:217
static void AddInitMethod(KVClassFactory &cf, KVString &body)
Definition: KVVarGlob.cpp:148
void Print(Option_t *="") const
Definition: KVVarGlob.cpp:276
virtual Int_t GetNumberOfValues() const
Definition: KVVarGlob.h:603
Double_t GetValue(void) const
Definition: KVVarGlob.h:412
KVString fFrame
(optional) name of reference frame used for kinematics
Definition: KVVarGlob.h:232
KVNameValueList fOptions
list of options
Definition: KVVarGlob.h:233
void SetNameIndex(const Char_t *name, Int_t index)
Definition: KVVarGlob.cpp:223
Int_t GetNameIndex(const Char_t *name) const
Definition: KVVarGlob.cpp:254
static void MakeClass(const Char_t *classname, const Char_t *classdesc, int type=kOneBody)
Definition: KVVarGlob.cpp:45
static void AddFillMethodBody(KVClassFactory &cf, KVString &body, int type)
Definition: KVVarGlob.cpp:191
static void ImplementInitMethod(KVClassFactory &cf, KVString &body, int type)
Definition: KVVarGlob.cpp:111
static void AddFillMethod(KVClassFactory &cf, int type)
Definition: KVVarGlob.cpp:164
static void FillMethodBody(KVString &body, int type)
Definition: KVVarGlob.cpp:87
KVNameValueList nameList
correspondence between variable name and index
Definition: KVVarGlob.h:230
Int_t GetNumberOfBranches() const
Definition: KVVarGlob.h:609
KVParticleCondition fSelection
(optional) condition used to select particles
Definition: KVVarGlob.h:235
KVNameValueList fParameters
list of parameters
Definition: KVVarGlob.h:234
static void AddExtraInitMethodComment(KVClassFactory &cf, KVString &body)
Definition: KVVarGlob.cpp:130
Int_t fType
type of variable global; = kOneBody, kTwoBody or kNBody
Definition: KVVarGlob.h:227
virtual const char * GetName() const
virtual const char * ClassName() const
virtual void Warning(const char *method, const char *msgfmt,...) const
const char * Data() const