Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #include <vector>
10 
11 #include "TString.h"
12 #include "TRegexp.h"
13 #include "TPRegexp.h"
14 #include "TFormula.h"
15 #include "TFile.h"
16 #include "TStreamerInfo.h"
17 #include "TList.h"
18 #include "TIterator.h"
19 #include "TF1.h"
20 #include "TH1.h"
21 #include "TH2.h"
22 #include "TGraph.h"
23 #include "TGraphErrors.h"
24 #include "TNtuple.h"
25 
26 #include "JLang/JType.hh"
27 #include "JLang/JLangToolkit.hh"
28 #include "JROOT/JRootFile.hh"
29 #include "JROOT/JRootDictionary.hh"
30 #include "JROOT/JRootPrinter.hh"
31 
32 #include "JTools/JRange.hh"
33 
34 
35 /**
36  * \author mdejong, mlincett
37  */
38 
39 namespace JROOT {}
40 namespace JPP { using namespace JROOT; }
41 
42 namespace JROOT {
43 
44  using JLANG::JType;
45 
46 
47  /**
48  * Get ROOT name of given data type.
49  *
50  * \return name of object to be read
51  */
52  template<class T>
53  inline const char* getName()
54  {
55  return getName(JType<T>());
56  }
57 
58 
59  /**
60  * Get ROOT name of given data type.
61  *
62  * \param type data type
63  * \return name of object to be read
64  */
65  template<class T>
66  inline const char* getName(const JType<T>& type)
67  {
68  return T::Class_Name();
69  }
70 
71 
72  /**
73  * Reset TH1 object
74  *
75  * \param object ROOT TH1 object
76  * \param reset reset contents if true
77  * \return true if successful; else false
78  */
79  inline bool resetObject(TH1* object, const bool reset = false)
80  {
81  if (object != NULL) {
82 
83  object->SetDirectory(0);
84 
85  if (reset) {
86  object->Reset();
87  }
88 
89  return true;
90  }
91 
92  return false;
93  }
94 
95 
96  /**
97  * Reset TGraph object
98  *
99  * \param object ROOT TGraph object
100  * \param reset reset contents if true
101  * \return true if successful; else false
102  */
103  inline bool resetObject(TGraph* object, const bool reset = false)
104  {
105  if (object != NULL) {
106 
107  if (reset) {
108  for (int i = 0; i != object->GetN(); ++i) {
109  object->SetPoint(i, 0.0, 0.0);
110  }
111  }
112 
113  return true;
114  }
115 
116  return false;
117  }
118 
119 
120  /**
121  * Reset TGraphErrors object
122  *
123  * \param object ROOT TGraphErrors object
124  * \param reset reset contents if true
125  * \return true if successful; else false
126  */
127  inline bool resetObject(TGraphErrors* object, const bool reset = false)
128  {
129  if (object != NULL) {
130 
131  if (reset) {
132  for (int i = 0; i != object->GetN(); ++i) {
133  object->SetPoint (i, 0.0, 0.0);
134  object->SetPointError(i, 0.0, 0.0);
135  }
136  }
137 
138  return true;
139  }
140 
141  return false;
142  }
143 
144 
145  /**
146  * Reset TNtuple object
147  *
148  * \param object ROOT TH1 object
149  * \param reset reset contents if true
150  * \return true if successful; else false
151  */
152  inline bool resetObject(TNtuple* object, const bool reset = false)
153  {
154  if (object != NULL) {
155 
156  object->SetDirectory(0);
157 
158  if (reset) {
159  object->Reset();
160  }
161 
162  return true;
163  }
164 
165  return false;
166  }
167 
168 
169  /**
170  * Write object to ROOT file.
171  *
172  * \param file ROOT file
173  * \param object ROOT object
174  * \return ROOT file
175  */
176  TFile& operator<<(TFile& file, const TObject& object)
177  {
178  file.WriteTObject(&object);
179 
180  return file;
181  }
182 
183 
184  /**
185  * Get ROOT streamer information of class with given name.
186  * Note that the class name should include the name space, if any.
187  *
188  * \param file pointer to ROOT file
189  * \param name class name
190  * \return pointer to TStreamerInfo (NULL in case of error)
191  */
192  inline const TStreamerInfo* getStreamerInfo(TFile* file, const char* name)
193  {
194  if (file != NULL && file->IsOpen()) {
195 
196  const TString buffer(name);
197 
198  TIter iter(file->GetStreamerInfoList());
199 
200  for (const TStreamerInfo* pStreamerInfo; (pStreamerInfo = (TStreamerInfo*) iter.Next()) != NULL; ) {
201  if (buffer == TString(pStreamerInfo->GetName())) {
202  return pStreamerInfo;
203  }
204  }
205  }
206 
207  return NULL;
208  }
209 
210 
211  /**
212  * Get ROOT streamer information of class with given name.
213  * Note that the class name should include the name space, if any.
214  *
215  * \param file_name file name
216  * \param name class name
217  * \return pointer to TStreamerInfo (NULL in case of error)
218  */
219  inline const TStreamerInfo* getStreamerInfo(const char* file_name, const char* name)
220  {
221  JRootInputFile file(file_name);
222 
223  return getStreamerInfo(file.getFile(), name);
224  }
225 
226 
227  /**
228  * Get ROOT streamer version of class with given name.
229  * Note that the class name should include the name space, if any.
230  *
231  * \param file pointer to ROOT file
232  * \param name class name
233  * \return streamer version; (-1 in case of error)
234  */
235  inline int getStreamerVersion(TFile* file, const char* name)
236  {
237  const TStreamerInfo* pStreamerInfo = getStreamerInfo(file, name);
238 
239  if (pStreamerInfo != NULL)
240  return pStreamerInfo->GetClassVersion();
241  else
242  return -1;
243  }
244 
245 
246  /**
247  * Get ROOT streamer version of class with given name.
248  * Note that the class name should include the name space, if any.
249  *
250  * \param file_name file name
251  * \param name class name
252  * \return streamer version; (-1 in case of error)
253  */
254  inline int getStreamerVersion(const char* file_name, const char* name)
255  {
256  JRootInputFile file(file_name);
257 
258  return getStreamerVersion(file.getFile(), name);
259  }
260 
261 
262  /**
263  * Match a regular expression with given string and return the specified matched parts.
264  *
265  * If no matches are found corresponding to the specified index, the original string is returned.
266  *
267  * \param regexp regular expression
268  * \param string input string
269  * \param index index of matched parts (starting at 1)
270  * \return matched part of string
271  */
272  inline TString parse(const TPRegexp& regexp, const TString& string, const int index = 1)
273  {
274  TPRegexp buffer = regexp;
275  TObjArray* array = buffer.MatchS(string);
276 
277  if (index - 1 < array->GetLast())
278  return ((TObjString*) array->At(index))->GetName();
279  else
280  return string;
281  }
282 
283 
284  /**
285  * Auxiliary data structure for a parameter index and its value.
286  */
288  /**
289  * Default constructor.
290  */
292  index(0),
293  value(0.0)
294  {}
295 
296 
297  /**
298  * Constructor.
299  *
300  * \param index parameter index
301  * \param value parameter value
302  */
303  JFitParameter_t(const Int_t index,
304  const Double_t value) :
305  index(index),
306  value(value)
307  {}
308 
309 
310  /**
311  * Type conversion operator
312  *
313  *
314  * \return index
315  */
316  inline operator Int_t() const
317  {
318  return index;
319  }
320 
321 
322  Int_t index;
323  Double_t value;
324  };
325 
326 
327  /**
328  * Set fit parameter.
329  *
330  * \param f1 fit function
331  * \param parameter parameter index and value
332  */
333  inline bool setParameter(TF1& f1, const JFitParameter_t& parameter)
334  {
335  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
336 
337  f1.SetParameter(parameter.index, parameter.value);
338 
339  return true;
340 
341  } else {
342 
343  return false;
344  }
345  }
346 
347 
348  /**
349  * Fix fit parameter.
350  *
351  * \param f1 fit function
352  * \param parameter parameter index and value
353  */
354  inline bool fixParameter(TF1& f1, const JFitParameter_t& parameter)
355  {
356  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
357 
358  f1.FixParameter(parameter.index, parameter.value);
359 
360  return true;
361 
362  } else {
363 
364  return false;
365  }
366  }
367 
368 
369  /**
370  * Release fit parameter.
371  *
372  * \param f1 fit function
373  * \param index parameter index
374  */
375  inline bool releaseParameter(TF1& f1, const Int_t index)
376  {
377  if (index >= 0 && index < f1.GetNpar()) {
378 
379  f1.ReleaseParameter(index);
380 
381  return true;
382 
383  } else {
384 
385  return false;
386  }
387  }
388 
389 
390  /**
391  * Set fit parameter limits.
392  *
393  * \param f1 fit function
394  * \param index parameter index
395  * \param xmin lower limit
396  * \param xmax upper limit
397  */
398  inline bool setParLimits(TF1& f1, const Int_t index, Double_t xmin, Double_t xmax)
399  {
400  using namespace std;
401 
402  if (index >= 0 && index < f1.GetNpar()) {
403 
404  if (xmin == 0.0) { xmin = -numeric_limits<Double_t>::min(); }
405  if (xmax == 0.0) { xmax = +numeric_limits<Double_t>::min(); }
406 
407  f1.SetParLimits(index, xmin, xmax);
408 
409  return true;
410 
411  } else {
412 
413  return false;
414  }
415  }
416 
417 
418  /**
419  * Check if fit parameter is fixed.
420  *
421  * \param f1 fit function
422  * \param index parameter index
423  */
424  inline bool isParameterFixed(TF1& f1, const Int_t index)
425  {
426  if (index >= 0 && index < f1.GetNpar()) {
427 
428  Double_t xmin;
429  Double_t xmax;
430 
431  f1.GetParLimits(index, xmin, xmax);
432 
433  return (xmin != 0.0 && xmax != 0.0 && xmin >= xmax);
434 
435  } else {
436 
437  return false;
438  }
439  }
440 
441 
442  /**
443  * Get result of given textual formula.\n
444  * The formula may contain names of data members of the given object.
445  * Example:
446  * <pre>
447  * getResult("a/b", object, ..);
448  * </pre>
449  * In this, the corresponding data type should have (at least) data members <tt>a</tt> and <tt>b</tt>.
450  *
451  * \param text text
452  * \param object object
453  * \param dictionary dictionary
454  * \return value
455  */
456  template<class T>
457  inline Double_t getResult(const TString& text,
458  const T& object,
459  const JRootDictionary_t& dictionary = JRootDictionary::getInstance())
460  {
461  using namespace std;
462  using namespace JPP;
463 
464  string buffer = (const char*) text;
465 
466  for (string::size_type pos = 0; pos != buffer.size(); ) {
467 
468  if (isalpha(buffer[pos]) || buffer[pos] == '_') {
469 
470  string::size_type len = 1;
471 
472  while (pos + len != buffer.size() && (isalnum(buffer[pos+len]) || buffer[pos+len] == '_' || buffer[pos+len] == '.')) {
473  ++len;
474  }
475 
476  ostringstream os;
477 
478  os.precision(10);
479 
480  JRootPrinter::print(os, object, buffer.substr(pos,len), dictionary);
481 
482  buffer.replace(pos, len, os.str());
483 
484  pos += os.str().size();
485 
486  } else {
487 
488  pos += 1;
489  }
490  }
491 
492  return TFormula("/tmp", buffer.c_str()).Eval(0.0);
493  }
494 
495  /**
496  * Converts a 1D histogram to a TGraph with:
497  * - bin centers as x values;
498  * - bin contents as y values.
499  * Underflow and overflow bins are ignored.
500  * \param in input histogram
501  */
502  inline TGraph* histogramToGraph(const TH1& in) {
503 
504  const int N = in.GetNbinsX();
505 
506  std::vector<double> x(N), y(N);
507 
508  for (int i = 0; i < N; i++) {
509  x[i] = in.GetBinCenter (i + 1);
510  y[i] = in.GetBinContent(i + 1);
511  }
512 
513  TGraph* out = new TGraph(N, &x[0], &y[0]);
514 
515  return out;
516 
517  }
518 
519 
520  /**
521  * Helper method for ROOT Projection(X|Y) based on a JRange object and a coordinate.
522  * \param in 2D histogram
523  * \param R range
524  * \param coordinate projection coordinate
525  */
526  inline TH1* projectHistogram(const TH2& in, const JTOOLS::JRange<int> R, const char coordinate = 'x') {
527 
528  int inf, sup;
529 
530  switch(coordinate) {
531 
532  case 'x':
533  case 'X':
534  inf = in.GetYaxis()->FindBin(R.getLowerLimit());
535  sup = in.GetYaxis()->FindBin(R.getUpperLimit());
536  return in.ProjectionX("_px", inf, sup);
537 
538  case 'y':
539  case 'Y':
540  inf = in.GetXaxis()->FindBin(R.getLowerLimit());
541  sup = in.GetXaxis()->FindBin(R.getUpperLimit());
542  return in.ProjectionY("_py", inf, sup);
543 
544  default:
545  return NULL;
546 
547  }
548 
549  }
550 
551 }
552 
553 
554 /**
555  * Read regular expression from input stream
556  *
557  * \param in input stream
558  * \param object regular expression
559  * \return output stream
560  */
561 std::istream& operator>>(std::istream& in, TRegexp& object)
562 {
563  std::string buffer;
564 
565  if (in >> buffer) {
566  object = TRegexp(buffer.c_str());
567  }
568 
569  return in;
570 }
571 
572 
573 /**
574  * Write regular expression to output stream
575  *
576  * \param out output stream
577  * \param object regular expression
578  * \return output stream
579  */
580 std::ostream& operator<<(std::ostream& out, const TRegexp& object)
581 {
582  return out;
583 }
584 
585 
586 #endif
587 
JFitParameter_t(const Int_t index, const Double_t value)
Constructor.
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:215
int getStreamerVersion(TFile *file, const char *name)
Get ROOT streamer version of class with given name.
const TStreamerInfo * getStreamerInfo(TFile *file, const char *name)
Get ROOT streamer information of class with given name.
char text[TEXT_SIZE]
Definition: elog.cc:72
bool releaseParameter(TF1 &f1, const Int_t index)
Release fit parameter.
Print objects in ASCII format using ROOT dictionary.
JFitParameter_t()
Default constructor.
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
Definition: JRoot.hh:19
Auxiliary class for a type holder.
Definition: JType.hh:19
bool isParameterFixed(TF1 &f1, const Int_t index)
Check if fit parameter is fixed.
boost::property_tree::ptree parse(std::string str)
Definition: configure.hh:24
Auxiliary data structure for a parameter index and its value.
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
TGraph * histogramToGraph(const TH1 &in)
Converts a 1D histogram to a TGraph with:
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
bool setParameter(TF1 &f1, const JFitParameter_t &parameter)
Set fit parameter.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:226
static void print(JRootWriter &writer, const JRootWritableClass &cls)
Write class contents to output.
Definition: JRootPrinter.hh:61
ROOT input file.
Definition: JRootFile.hh:101
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:40
bool setParLimits(TF1 &f1, const Int_t index, Double_t xmin, Double_t xmax)
Set fit parameter limits.
Type definition of ROOT based dictionary for ASCII O.
then echo n User name
Definition: JCookie.sh:45
void reset(T &value)
Reset value.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1278
Auxiliary class to define a range between two values.
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
bool fixParameter(TF1 &f1, const JFitParameter_t &parameter)
Fix fit parameter.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:53
bool resetObject(JManager< JKey_t, JValue_t > *object, const bool reset=false)
Reset JManager object.
Definition: JManager.hh:365
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
TH1 * projectHistogram(const TH2 &in, const JTOOLS::JRange< int > R, const char coordinate= 'x')
Helper method for ROOT Projection(X|Y) based on a JRange object and a coordinate. ...