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 "TObjString.h"
13 #include "TRegexp.h"
14 #include "TPRegexp.h"
15 #include "TFormula.h"
16 #include "TFile.h"
17 #include "TStreamerInfo.h"
18 #include "TList.h"
19 #include "TIterator.h"
20 #include "TF1.h"
21 #include "TH1.h"
22 #include "TH2.h"
23 #include "TGraph.h"
24 #include "TGraphErrors.h"
25 #include "TGraph2D.h"
26 #include "TGraph2DErrors.h"
27 #include "TNtuple.h"
28 
29 #include "JLang/JType.hh"
30 #include "JLang/JLangToolkit.hh"
31 #include "JROOT/JRootFile.hh"
32 #include "JROOT/JRootDictionary.hh"
33 #include "JROOT/JRootPrinter.hh"
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  * Detach TH1 object and optionally reset contents.
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  * Detach TGraph object and optionally reset contents.
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  * Detach TGraphErrors object and optionally reset contents.
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  * Detach TGraph object and optionally reset contents.
147  *
148  * \param object ROOT TGraph2D object
149  * \param reset reset contents if true
150  * \return true if successful; else false
151  */
152  inline bool resetObject(TGraph2D* object, const bool reset = false)
153  {
154  if (object != NULL) {
155 
156  if (reset) {
157  for (int i = 0; i != object->GetN(); ++i) {
158  object->SetPoint(i, 0.0, 0.0, 0.0);
159  }
160  }
161 
162  return true;
163  }
164 
165  return false;
166  }
167 
168 
169  /**
170  * Detach TGraphErrors object and optionally reset contents.
171  *
172  * \param object ROOT TGraph2DErrors object
173  * \param reset reset contents if true
174  * \return true if successful; else false
175  */
176  inline bool resetObject(TGraph2DErrors* object, const bool reset = false)
177  {
178  if (object != NULL) {
179 
180  if (reset) {
181  for (int i = 0; i != object->GetN(); ++i) {
182  object->SetPoint (i, 0.0, 0.0, 0.0);
183  object->SetPointError(i, 0.0, 0.0, 0.0);
184  }
185  }
186 
187  return true;
188  }
189 
190  return false;
191  }
192 
193 
194  /**
195  * Detach TNtuple object and optionally reset contents.
196  *
197  * \param object ROOT TNtuple object
198  * \param reset reset contents if true
199  * \return true if successful; else false
200  */
201  inline bool resetObject(TNtuple* object, const bool reset = false)
202  {
203  if (object != NULL) {
204 
205  object->SetDirectory(0);
206 
207  if (reset) {
208  object->Reset();
209  }
210 
211  return true;
212  }
213 
214  return false;
215  }
216 
217 
218  /**
219  * Add point to TGraph.
220  *
221  * \param g1 pointer to valid ROOT TGraph object
222  * \param x x value
223  * \param y y value
224  */
225  inline void AddPoint(TGraph* g1,
226  const Double_t x,
227  const Double_t y)
228  {
229  const Int_t n = g1->GetN();
230 
231  g1->Set(n + 1);
232  g1->SetPoint(n, x, y);
233  }
234 
235 
236  /**
237  * Add point to TGraph2D.
238  *
239  * \param g1 pointer to valid ROOT TGraph object
240  * \param x x value
241  * \param y y value
242  * \param z z value
243  */
244  inline void AddPoint(TGraph2D* g1,
245  const Double_t x,
246  const Double_t y,
247  const Double_t z)
248  {
249  const Int_t n = g1->GetN();
250 
251  g1->Set(n + 1);
252  g1->SetPoint(n, x, y, z);
253  }
254 
255 
256  /**
257  * Add point to TGraphErrors.
258  *
259  * \param g1 pointer to valid ROOT TGraph object
260  * \param x x value
261  * \param y y value
262  * \param ex x error
263  * \param ey y error
264  */
265  inline void AddPoint(TGraphErrors* g1,
266  const Double_t x,
267  const Double_t y,
268  const Double_t ex,
269  const Double_t ey)
270  {
271  const Int_t n = g1->GetN();
272 
273  g1->Set(n + 1);
274  g1->SetPoint(n, x, y);
275  g1->SetPointError(n, ex, ey);
276  }
277 
278 
279  /**
280  * Add point to TGraph2DErrors.
281  *
282  * \param g1 pointer to valid ROOT TGraph object
283  * \param x x value
284  * \param y y value
285  * \param z z value
286  * \param ex x error
287  * \param ey y error
288  * \param ez z error
289  */
290  inline void AddPoint(TGraph2DErrors* g1,
291  const Double_t x,
292  const Double_t y,
293  const Double_t z,
294  const Double_t ex,
295  const Double_t ey,
296  const Double_t ez)
297  {
298  const Int_t n = g1->GetN();
299 
300  g1->Set(n + 1);
301  g1->SetPoint(n, x, y, z);
302  g1->SetPointError(n, ex, ey, ez);
303  }
304 
305 
306  /**
307  * Write object to ROOT file.
308  *
309  * \param file ROOT file
310  * \param object ROOT object
311  * \return ROOT file
312  */
313  TFile& operator<<(TFile& file, const TObject& object)
314  {
315  file.WriteTObject(&object);
316 
317  return file;
318  }
319 
320 
321  /**
322  * Get ROOT streamer information of class with given name.
323  * Note that the class name should include the name space, if any.
324  *
325  * \param file pointer to ROOT file
326  * \param name class name
327  * \return pointer to TStreamerInfo (NULL in case of error)
328  */
329  inline const TStreamerInfo* getStreamerInfo(TFile* file, const char* name)
330  {
331  if (file != NULL && file->IsOpen()) {
332 
333  const TString buffer(name);
334 
335  TIter iter(file->GetStreamerInfoList());
336 
337  for (const TStreamerInfo* pStreamerInfo; (pStreamerInfo = (TStreamerInfo*) iter.Next()) != NULL; ) {
338  if (buffer == TString(pStreamerInfo->GetName())) {
339  return pStreamerInfo;
340  }
341  }
342  }
343 
344  return NULL;
345  }
346 
347 
348  /**
349  * Get ROOT streamer information of class with given name.
350  * Note that the class name should include the name space, if any.
351  *
352  * \param file_name file name
353  * \param name class name
354  * \return pointer to TStreamerInfo (NULL in case of error)
355  */
356  inline const TStreamerInfo* getStreamerInfo(const char* file_name, const char* name)
357  {
358  JRootInputFile file(file_name);
359 
360  return getStreamerInfo(file.getFile(), name);
361  }
362 
363 
364  /**
365  * Get ROOT streamer version of class with given name.
366  * Note that the class name should include the name space, if any.
367  *
368  * \param file pointer to ROOT file
369  * \param name class name
370  * \return streamer version; (-1 in case of error)
371  */
372  inline int getStreamerVersion(TFile* file, const char* name)
373  {
374  const TStreamerInfo* pStreamerInfo = getStreamerInfo(file, name);
375 
376  if (pStreamerInfo != NULL)
377  return pStreamerInfo->GetClassVersion();
378  else
379  return -1;
380  }
381 
382 
383  /**
384  * Get ROOT streamer version of class with given name.
385  * Note that the class name should include the name space, if any.
386  *
387  * \param file_name file name
388  * \param name class name
389  * \return streamer version; (-1 in case of error)
390  */
391  inline int getStreamerVersion(const char* file_name, const char* name)
392  {
393  JRootInputFile file(file_name);
394 
395  return getStreamerVersion(file.getFile(), name);
396  }
397 
398 
399  /**
400  * Match a regular expression with given string and return the specified matched parts.
401  *
402  * If no matches are found corresponding to the specified index, the original string is returned.
403  *
404  * \param regexp regular expression
405  * \param string input string
406  * \param index index of matched parts (starting at 1)
407  * \return matched part of string
408  */
409  inline TString parse(const TPRegexp& regexp, const TString& string, const int index = 1)
410  {
411  TPRegexp buffer = regexp;
412  TObjArray* array = buffer.MatchS(string);
413 
414  if (index - 1 < array->GetLast())
415  return ((TObjString*) array->At(index))->GetName();
416  else
417  return string;
418  }
419 
420 
421  /**
422  * Auxiliary data structure for a parameter index and its value.
423  */
425  /**
426  * Default constructor.
427  */
429  index(0),
430  value(0.0)
431  {}
432 
433 
434  /**
435  * Constructor.
436  *
437  * \param index parameter index
438  * \param value parameter value
439  */
440  JFitParameter_t(const Int_t index,
441  const Double_t value) :
442  index(index),
443  value(value)
444  {}
445 
446 
447  /**
448  * Type conversion operator
449  *
450  *
451  * \return index
452  */
453  inline operator Int_t() const
454  {
455  return index;
456  }
457 
458 
459  Int_t index;
460  Double_t value;
461  };
462 
463 
464  /**
465  * Set fit parameter.
466  *
467  * \param f1 fit function
468  * \param parameter parameter index and value
469  */
470  inline bool setParameter(TF1& f1, const JFitParameter_t& parameter)
471  {
472  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
473 
474  f1.SetParameter(parameter.index, parameter.value);
475 
476  return true;
477 
478  } else {
479 
480  return false;
481  }
482  }
483 
484 
485  /**
486  * Fix fit parameter.
487  *
488  * \param f1 fit function
489  * \param parameter parameter index and value
490  */
491  inline bool fixParameter(TF1& f1, const JFitParameter_t& parameter)
492  {
493  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
494 
495  f1.FixParameter(parameter.index, parameter.value);
496 
497  return true;
498 
499  } else {
500 
501  return false;
502  }
503  }
504 
505 
506  /**
507  * Release fit parameter.
508  *
509  * \param f1 fit function
510  * \param index parameter index
511  */
512  inline bool releaseParameter(TF1& f1, const Int_t index)
513  {
514  if (index >= 0 && index < f1.GetNpar()) {
515 
516  f1.ReleaseParameter(index);
517 
518  return true;
519 
520  } else {
521 
522  return false;
523  }
524  }
525 
526 
527  /**
528  * Set fit parameter limits.
529  *
530  * \param f1 fit function
531  * \param index parameter index
532  * \param xmin lower limit
533  * \param xmax upper limit
534  */
535  inline bool setParLimits(TF1& f1, const Int_t index, Double_t xmin, Double_t xmax)
536  {
537  using namespace std;
538 
539  if (index >= 0 && index < f1.GetNpar()) {
540 
541  if (xmin == 0.0) { xmin = -numeric_limits<Double_t>::min(); }
542  if (xmax == 0.0) { xmax = +numeric_limits<Double_t>::min(); }
543 
544  f1.SetParLimits(index, xmin, xmax);
545 
546  return true;
547 
548  } else {
549 
550  return false;
551  }
552  }
553 
554 
555  /**
556  * Check if fit parameter is fixed.
557  *
558  * \param f1 fit function
559  * \param index parameter index
560  */
561  inline bool isParameterFixed(const TF1& f1, const Int_t index)
562  {
563  if (index >= 0 && index < f1.GetNpar()) {
564 
565  Double_t xmin;
566  Double_t xmax;
567 
568  f1.GetParLimits(index, xmin, xmax);
569 
570  return (xmin != 0.0 && xmax != 0.0 && xmin >= xmax);
571 
572  } else {
573 
574  return false;
575  }
576  }
577 
578 
579  /**
580  * Get result of given textual formula.\n
581  * The formula may contain names of data members of the given object.
582  * Example:
583  * <pre>
584  * getResult("a/b", object, ..);
585  * </pre>
586  * In this, the corresponding data type should have (at least) data members <tt>a</tt> and <tt>b</tt>.
587  *
588  * \param text text
589  * \param object object
590  * \param dictionary dictionary
591  * \return value
592  */
593  template<class T>
594  inline Double_t getResult(const TString& text,
595  const T& object,
596  const JRootDictionary_t& dictionary = JRootDictionary::getInstance())
597  {
598  using namespace std;
599  using namespace JPP;
600 
601  string buffer = (const char*) text;
602 
603  for (string::size_type pos = 0; pos != buffer.size(); ) {
604 
605  if (isalpha(buffer[pos]) || buffer[pos] == '_') {
606 
607  string::size_type len = 1;
608 
609  while (pos + len != buffer.size() && (isalnum(buffer[pos+len]) || buffer[pos+len] == '_' || buffer[pos+len] == '.')) {
610  ++len;
611  }
612 
613  ostringstream os;
614 
615  os.setf(ios::fixed);
616  os.precision(10);
617 
618  JRootPrinter::print(os, object, buffer.substr(pos,len), dictionary);
619 
620  buffer.replace(pos, len, os.str());
621 
622  pos += os.str().size();
623 
624  } else {
625 
626  pos += 1;
627  }
628  }
629 
630  return TFormula("/tmp", buffer.c_str()).Eval(0.0);
631  }
632 
633  /**
634  * Converts a 1D histogram to a TGraph with:
635  * - bin centers as x values;
636  * - bin contents as y values.
637  * Underflow and overflow bins are ignored.
638  * \param in input histogram
639  */
640  inline TGraph* histogramToGraph(const TH1& in) {
641 
642  const int N = in.GetNbinsX();
643 
644  std::vector<double> x(N), y(N);
645 
646  for (int i = 0; i < N; i++) {
647  x[i] = in.GetBinCenter (i + 1);
648  y[i] = in.GetBinContent(i + 1);
649  }
650 
651  TGraph* out = new TGraph(N, &x[0], &y[0]);
652 
653  return out;
654 
655  }
656 
657 
658  /**
659  * Helper method for ROOT Projection(X|Y) based on a JRange object and a coordinate.
660  * \param in 2D histogram
661  * \param inf lower limit
662  * \param sup upper limit
663  * \param coordinate projection coordinate
664  */
665  inline TH1* projectHistogram(const TH2& in, Double_t inf, Double_t sup, const char coordinate = 'x') {
666 
667  int inf_bin, sup_bin;
668 
669  switch(coordinate) {
670 
671  case 'x':
672  case 'X':
673  inf_bin = in.GetYaxis()->FindBin(inf);
674  sup_bin = in.GetYaxis()->FindBin(sup);
675  return in.ProjectionX("_px", inf_bin, sup_bin);
676 
677  case 'y':
678  case 'Y':
679  inf_bin = in.GetXaxis()->FindBin(inf);
680  sup_bin = in.GetXaxis()->FindBin(sup);
681  return in.ProjectionY("_py", inf_bin, sup_bin);
682 
683  default:
684  return NULL;
685 
686  }
687 
688  }
689 
690 }
691 
692 
693 /**
694  * Read regular expression from input stream
695  *
696  * \param in input stream
697  * \param object regular expression
698  * \return output stream
699  */
700 std::istream& operator>>(std::istream& in, TRegexp& object)
701 {
702  std::string buffer;
703 
704  if (in >> buffer) {
705  object = TRegexp(buffer.c_str());
706  }
707 
708  return in;
709 }
710 
711 
712 /**
713  * Write regular expression to output stream
714  *
715  * \param out output stream
716  * \param object regular expression
717  * \return output stream
718  */
719 std::ostream& operator<<(std::ostream& out, const TRegexp& object)
720 {
721  return out;
722 }
723 
724 
725 #endif
726 
JFitParameter_t(const Int_t index, const Double_t value)
Constructor.
void AddPoint(TGraph *g1, const Double_t x, const Double_t y)
Add point to TGraph.
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.
Definition: JRoot.hh:19
Auxiliary class for a type holder.
Definition: JType.hh:19
bool resetObject(JManager< JKey_t, JValue_t > *object, const bool reset=false)
Reset JManager object.
Definition: JManager.hh:366
bool isParameterFixed(const 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.
TH1 * projectHistogram(const TH2 &in, Double_t inf, Double_t sup, const char coordinate= 'x')
Helper method for ROOT Projection(X|Y) based on a JRange object and a coordinate. ...
do set_variable OUTPUT_DIRECTORY $WORKDIR T
static void print(JRootWriter &writer, const JRootWritableClass &cls)
Write class contents to output.
Definition: JRootPrinter.hh:61
ROOT input file.
Definition: JRootFile.hh:101
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 I/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:1549
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
alias put_queue eval echo n
Definition: qlib.csh:19
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
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25