Jpp
JRootToolkit.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JROOTTOOLKIT__
2 #define __JROOT__JROOTTOOLKIT__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <limits>
8 #include <cctype>
9 
10 #include "TString.h"
11 #include "TRegexp.h"
12 #include "TPRegexp.h"
13 #include "TFormula.h"
14 #include "TFile.h"
15 #include "TStreamerInfo.h"
16 #include "TList.h"
17 #include "TIterator.h"
18 #include "TF1.h"
19 
20 #include "JLang/JType.hh"
21 #include "JLang/JLangToolkit.hh"
22 #include "JROOT/JRootFile.hh"
23 #include "JROOT/JRootDictionary.hh"
24 #include "JROOT/JRootPrinter.hh"
25 
26 
27 /**
28  * \author mdejong
29  */
30 
31 namespace JROOT {}
32 namespace JPP { using namespace JROOT; }
33 
34 namespace JROOT {
35 
36  using JLANG::JType;
37 
38 
39  /**
40  * Get ROOT name of given data type.
41  *
42  * \return name of object to be read
43  */
44  template<class T>
45  inline const char* getName()
46  {
47  return getName(JType<T>());
48  }
49 
50 
51  /**
52  * Get ROOT name of given data type.
53  *
54  * \param type data type
55  * \return name of object to be read
56  */
57  template<class T>
58  inline const char* getName(const JType<T>& type)
59  {
60  return T::Class_Name();
61  }
62 
63 
64  /**
65  * Write object to ROOT file.
66  *
67  * \param file ROOT file
68  * \param object ROOT object
69  * \return ROOT file
70  */
71  TFile& operator<<(TFile& file, const TObject& object)
72  {
73  file.WriteTObject(&object);
74 
75  return file;
76  }
77 
78 
79  /**
80  * Get ROOT streamer information of class with given name.
81  * Note that the class name should include the name space, if any.
82  *
83  * \param file pointer to ROOT file
84  * \param name class name
85  * \return pointer to TStreamerInfo (NULL in case of error)
86  */
87  inline const TStreamerInfo* getStreamerInfo(TFile* file, const char* name)
88  {
89  if (file != NULL && file->IsOpen()) {
90 
91  const TString buffer(name);
92 
93  TIter iter(file->GetStreamerInfoList());
94 
95  for (const TStreamerInfo* pStreamerInfo; (pStreamerInfo = (TStreamerInfo*) iter.Next()) != NULL; ) {
96  if (buffer == TString(pStreamerInfo->GetName())) {
97  return pStreamerInfo;
98  }
99  }
100  }
101 
102  return NULL;
103  }
104 
105 
106  /**
107  * Get ROOT streamer information of class with given name.
108  * Note that the class name should include the name space, if any.
109  *
110  * \param file_name file name
111  * \param name class name
112  * \return pointer to TStreamerInfo (NULL in case of error)
113  */
114  inline const TStreamerInfo* getStreamerInfo(const char* file_name, const char* name)
115  {
116  JRootInputFile file(file_name);
117 
118  return getStreamerInfo(file.getFile(), name);
119  }
120 
121 
122  /**
123  * Get ROOT streamer version of class with given name.
124  * Note that the class name should include the name space, if any.
125  *
126  * \param file pointer to ROOT file
127  * \param name class name
128  * \return streamer version; (-1 in case of error)
129  */
130  inline int getStreamerVersion(TFile* file, const char* name)
131  {
132  const TStreamerInfo* pStreamerInfo = getStreamerInfo(file, name);
133 
134  if (pStreamerInfo != NULL)
135  return pStreamerInfo->GetClassVersion();
136  else
137  return -1;
138  }
139 
140 
141  /**
142  * Get ROOT streamer version of class with given name.
143  * Note that the class name should include the name space, if any.
144  *
145  * \param file_name file name
146  * \param name class name
147  * \return streamer version; (-1 in case of error)
148  */
149  inline int getStreamerVersion(const char* file_name, const char* name)
150  {
151  JRootInputFile file(file_name);
152 
153  return getStreamerVersion(file.getFile(), name);
154  }
155 
156 
157 /**
158  * Match a TString to a TPRegexp, and return the chosen match.
159  * If no matches are found corresponding to the indicated index,
160  * the original TString is returned.
161  *
162  * \param string string to parse
163  * \param regexp regular expression
164  * \param i index of match to return
165  * \return match; (original string in case that matches are not found)
166  */
167  inline TString parse(TPRegexp& regexp, const TString& string, int i=1)
168  {
169  TObjArray* a = regexp.MatchS(string);
170  TString s = string;
171  if (a->GetLast() > i-1){
172  s = ((TObjString*)a->At(i))->GetName();
173  }
174 
175  return s;
176  }
177 
178 
179  /**
180  * Auxiliary data structure for a parameter index and its value.
181  */
183  /**
184  * Default constructor.
185  */
187  index(0),
188  value(0.0)
189  {}
190 
191 
192  /**
193  * Constructor.
194  *
195  * \param index parameter index
196  * \param value parameter value
197  */
198  JFitParameter_t(const Int_t index,
199  const Double_t value) :
200  index(index),
201  value(value)
202  {}
203 
204 
205  /**
206  * Type conversion operator
207  *
208  *
209  * \return index
210  */
211  inline operator Int_t() const
212  {
213  return index;
214  }
215 
216 
217  Int_t index;
218  Double_t value;
219  };
220 
221 
222  /**
223  * Set fit parameter.
224  *
225  * \param f1 fit function
226  * \param parameter parameter index and value
227  */
228  inline bool setParameter(TF1& f1, const JFitParameter_t& parameter)
229  {
230  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
231 
232  f1.SetParameter(parameter.index, parameter.value);
233 
234  return true;
235 
236  } else {
237 
238  return false;
239  }
240  }
241 
242 
243  /**
244  * Fix fit parameter.
245  *
246  * \param f1 fit function
247  * \param parameter parameter index and value
248  */
249  inline bool fixParameter(TF1& f1, const JFitParameter_t& parameter)
250  {
251  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
252 
253  f1.FixParameter(parameter.index, parameter.value);
254 
255  return true;
256 
257  } else {
258 
259  return false;
260  }
261  }
262 
263 
264  /**
265  * Release fit parameter.
266  *
267  * \param f1 fit function
268  * \param index parameter index
269  */
270  inline bool releaseParameter(TF1& f1, const Int_t index)
271  {
272  if (index >= 0 && index < f1.GetNpar()) {
273 
274  f1.ReleaseParameter(index);
275 
276  return true;
277 
278  } else {
279 
280  return false;
281  }
282  }
283 
284 
285  /**
286  * Set fit parameter limits.
287  *
288  * \param f1 fit function
289  * \param index parameter index
290  * \param xmin lower limit
291  * \param xmax upper limit
292  */
293  inline bool setParLimits(TF1& f1, const Int_t index, Double_t xmin, Double_t xmax)
294  {
295  using namespace std;
296 
297  if (index >= 0 && index < f1.GetNpar()) {
298 
299  if (xmin == 0.0) { xmin = -numeric_limits<Double_t>::min(); }
300  if (xmax == 0.0) { xmax = +numeric_limits<Double_t>::min(); }
301 
302  f1.SetParLimits(index, xmin, xmax);
303 
304  return true;
305 
306  } else {
307 
308  return false;
309  }
310  }
311 
312 
313  /**
314  * Check if fit parameter is fixed.
315  *
316  * \param f1 fit function
317  * \param index parameter index
318  */
319  inline bool isParameterFixed(TF1& f1, const Int_t index)
320  {
321  if (index >= 0 && index < f1.GetNpar()) {
322 
323  Double_t xmin;
324  Double_t xmax;
325 
326  f1.GetParLimits(index, xmin, xmax);
327 
328  return (xmin != 0.0 && xmax != 0.0 && xmin >= xmax);
329 
330  } else {
331 
332  return false;
333  }
334  }
335 
336 
337  /**
338  * Get result of given textual formula.\n
339  * The formula may contain names of data members of the given object.
340  * Example:
341  * <pre>
342  * getResult("a/b", object, ..);
343  * </pre>
344  * In this, the corresponding data type should have (at least) data members <tt>a</tt> and <tt>b</tt>.
345  *
346  * \param text text
347  * \param object object
348  * \param dictionary dictionary
349  * \return value
350  */
351  template<class T>
352  inline Double_t getResult(const TString& text,
353  const T& object,
354  const JRootDictionary_t& dictionary = JRootDictionary::getInstance())
355  {
356  using namespace std;
357  using namespace JPP;
358 
359  string buffer = (const char*) text;
360 
361  for (string::size_type pos = 0; pos != buffer.size(); ) {
362 
363  if (isalpha(buffer[pos]) || buffer[pos] == '_') {
364 
365  string::size_type len = 1;
366 
367  while (pos + len != buffer.size() && (isalnum(buffer[pos+len]) || buffer[pos+len] == '_' || buffer[pos+len] == '.')) {
368  ++len;
369  }
370 
371  ostringstream os;
372 
373  os.precision(10);
374 
375  JRootPrinter::print(os, object, buffer.substr(pos,len), dictionary);
376 
377  buffer.replace(pos, len, os.str());
378 
379  pos += os.str().size();
380 
381  } else {
382 
383  pos += 1;
384  }
385  }
386 
387  return TFormula("/tmp", buffer.c_str()).Eval(0.0);
388  }
389 }
390 
391 
392 /**
393  * Read regular expression from input stream
394  *
395  * \param in input stream
396  * \param object regular expression
397  * \return output stream
398  */
399 std::istream& operator>>(std::istream& in, TRegexp& object)
400 {
401  std::string buffer;
402 
403  if (in >> buffer) {
404  object = TRegexp(buffer.c_str());
405  }
406 
407  return in;
408 }
409 
410 
411 /**
412  * Write regular expression to output stream
413  *
414  * \param out output stream
415  * \param object regular expression
416  * \return output stream
417  */
418 std::ostream& operator<<(std::ostream& out, const TRegexp& object)
419 {
420  return out;
421 }
422 
423 
424 #endif
425 
JROOT::JFitParameter_t::JFitParameter_t
JFitParameter_t(const Int_t index, const Double_t value)
Constructor.
Definition: JRootToolkit.hh:198
text
char text[TEXT_SIZE]
Definition: elog.cc:72
TObject
Definition: JRoot.hh:19
JROOT::isParameterFixed
bool isParameterFixed(TF1 &f1, const Int_t index)
Check if fit parameter is fixed.
Definition: JRootToolkit.hh:319
JRootPrinter.hh
JLANG::JType
Auxiliary class for a type holder.
Definition: JType.hh:19
JROOT
Auxiliary classes and methods for ROOT I/O.
Definition: JAbstractStreamer.hh:13
operator>>
std::istream & operator>>(std::istream &in, TRegexp &object)
Read regular expression from input stream.
Definition: JRootToolkit.hh:399
JLANG::JSingleton::getInstance
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
JROOT::getStreamerVersion
int getStreamerVersion(const char *file_name, const char *name)
Get ROOT streamer version of class with given name.
Definition: JRootToolkit.hh:149
JROOT::JFitParameter_t::JFitParameter_t
JFitParameter_t()
Default constructor.
Definition: JRootToolkit.hh:186
JROOT::JRootFile::getFile
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JROOT::JFitParameter_t::index
Int_t index
Definition: JRootToolkit.hh:217
JROOT::JRootInputFile
ROOT input file.
Definition: JRootFile.hh:101
operator<<
std::ostream & operator<<(std::ostream &out, const TRegexp &object)
Write regular expression to output stream.
Definition: JRootToolkit.hh:418
JROOT::JRootDictionary_t
Type definition of ROOT based dictionary for ASCII O.
Definition: JRootDictionary_t.hh:30
JROOT::getStreamerInfo
const TStreamerInfo * getStreamerInfo(const char *file_name, const char *name)
Get ROOT streamer information of class with given name.
Definition: JRootToolkit.hh:114
JROOT::setParLimits
bool setParLimits(TF1 &f1, const Int_t index, Double_t xmin, Double_t xmax)
Set fit parameter limits.
Definition: JRootToolkit.hh:293
JROOT::parse
TString parse(TPRegexp &regexp, const TString &string, int i=1)
Match a TString to a TPRegexp, and return the chosen match.
Definition: JRootToolkit.hh:167
JRootDictionary.hh
JROOT::releaseParameter
bool releaseParameter(TF1 &f1, const Int_t index)
Release fit parameter.
Definition: JRootToolkit.hh:270
JROOT::setParameter
bool setParameter(TF1 &f1, const JFitParameter_t &parameter)
Set fit parameter.
Definition: JRootToolkit.hh:228
JROOT::fixParameter
bool fixParameter(TF1 &f1, const JFitParameter_t &parameter)
Fix fit parameter.
Definition: JRootToolkit.hh:249
std
Definition: jaanetDictionary.h:36
JROOT::getName
const char * getName(const JType< T > &type)
Get ROOT name of given data type.
Definition: JRootToolkit.hh:58
JROOT::JFitParameter_t::value
Double_t value
Definition: JRootToolkit.hh:218
JROOT::JFitParameter_t
Auxiliary data structure for a parameter index and its value.
Definition: JRootToolkit.hh:182
JType.hh
JRootFile.hh
JROOT::getResult
Double_t getResult(const TString &text, const T &object, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Get result of given textual formula.
Definition: JRootToolkit.hh:352
JROOT::JRootPrinter::print
static void print(JRootWriter &writer, const JRootWritableClass &cls)
Write class contents to output.
Definition: JRootPrinter.hh:61
JLangToolkit.hh