Jpp  16.0.3
the software that should make you happy
 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 "TMultiGraph.h"
28 #include "TNtuple.h"
29 
30 #include "JLang/JType.hh"
31 #include "JLang/JLangToolkit.hh"
32 
33 #include "Jeep/JPrint.hh"
34 
35 #include "JROOT/JRootFile.hh"
36 #include "JROOT/JRootDictionary.hh"
37 #include "JROOT/JRootPrinter.hh"
38 
39 /**
40  * \author mdejong, mlincett
41  */
42 
43 namespace JROOT {}
44 namespace JPP { using namespace JROOT; }
45 
46 namespace JROOT {
47 
48  using JLANG::JType;
49 
50 
51  /**
52  * Get ROOT name of given data type.
53  *
54  * \return name of object to be read
55  */
56  template<class T>
57  inline const char* getName()
58  {
59  return getName(JType<T>());
60  }
61 
62 
63  /**
64  * Get ROOT name of given data type.
65  *
66  * \param type data type
67  * \return name of object to be read
68  */
69  template<class T>
70  inline const char* getName(const JType<T>& type)
71  {
72  return T::Class_Name();
73  }
74 
75 
76  /**
77  * Detach TH1 object and optionally reset contents.
78  *
79  * \param object ROOT TH1 object
80  * \param reset reset contents if true
81  * \return true if successful; else false
82  */
83  inline bool resetObject(TH1* object, const bool reset = false)
84  {
85  if (object != NULL) {
86 
87  object->SetDirectory(0);
88 
89  if (reset) {
90  object->Reset();
91  }
92 
93  return true;
94  }
95 
96  return false;
97  }
98 
99 
100  /**
101  * Detach TGraph object and optionally reset contents.
102  *
103  * \param object ROOT TGraph object
104  * \param reset reset contents if true
105  * \return true if successful; else false
106  */
107  inline bool resetObject(TGraph* object, const bool reset = false)
108  {
109  if (object != NULL) {
110 
111  if (reset) {
112  for (int i = 0; i != object->GetN(); ++i) {
113  object->SetPoint(i, 0.0, 0.0);
114  }
115  }
116 
117  return true;
118  }
119 
120  return false;
121  }
122 
123 
124  /**
125  * Detach TGraphErrors object and optionally reset contents.
126  *
127  * \param object ROOT TGraphErrors object
128  * \param reset reset contents if true
129  * \return true if successful; else false
130  */
131  inline bool resetObject(TGraphErrors* object, const bool reset = false)
132  {
133  if (object != NULL) {
134 
135  if (reset) {
136 
137  for (int i = 0; i != object->GetN(); ++i) {
138  object->SetPoint (i, 0.0, 0.0);
139  object->SetPointError(i, 0.0, 0.0);
140  }
141  }
142 
143  return true;
144  }
145 
146  return false;
147  }
148 
149 
150  /**
151  * Detach TGraph2D object and optionally reset contents.
152  *
153  * \param object ROOT TGraph2D object
154  * \param reset reset contents if true
155  * \return true if successful; else false
156  */
157  inline bool resetObject(TGraph2D* object, const bool reset = false)
158  {
159  if (object != NULL) {
160 
161  if (reset) {
162  for (int i = 0; i != object->GetN(); ++i) {
163  object->SetPoint(i, 0.0, 0.0, 0.0);
164  }
165  }
166 
167  return true;
168  }
169 
170  return false;
171  }
172 
173 
174  /**
175  * Detach TGraph2DErrors object and optionally reset contents.
176  *
177  * \param object ROOT TGraph2DErrors object
178  * \param reset reset contents if true
179  * \return true if successful; else false
180  */
181  inline bool resetObject(TGraph2DErrors* object, const bool reset = false)
182  {
183  if (object != NULL) {
184 
185  if (reset) {
186  for (int i = 0; i != object->GetN(); ++i) {
187  object->SetPoint (i, 0.0, 0.0, 0.0);
188  object->SetPointError(i, 0.0, 0.0, 0.0);
189  }
190  }
191 
192  return true;
193  }
194 
195  return false;
196  }
197 
198 
199  /**
200  * Detach TMultiGraph object and optionally reset contents.
201  *
202  * \param object ROOT TMultiGraph object
203  * \param reset reset contents if true
204  * \return true if successful; else false
205  */
206  inline bool resetObject(TMultiGraph* object, const bool reset = false)
207  {
208  if (object != NULL) {
209 
210  bool status = true;
211 
212  if (reset) {
213 
214  TIter next(object->GetListOfGraphs());
215 
216  while (TGraph* graph = (TGraph*)next()) {
217 
218  if (!(resetObject(dynamic_cast<TGraph2DErrors*>(graph), reset) ||
219  resetObject(dynamic_cast<TGraph2D*> (graph), reset)) &&
220  !(resetObject(dynamic_cast<TGraphErrors*> (graph), reset) ||
221  resetObject (graph, reset)) ) {
222 
223  status = false;
224  }
225  }
226  }
227 
228  return status;
229  }
230 
231  return false;
232  }
233 
234 
235  /**
236  * Detach TTree object and optionally reset contents.
237  *
238  * \param object ROOT TTree object
239  * \param reset reset contents if true
240  * \return true if successful; else false
241  */
242  inline bool resetObject(TTree* object, const bool reset = false)
243  {
244  if (object != NULL) {
245 
246  object->SetDirectory(0);
247 
248  if (reset) {
249  object->Reset();
250  }
251 
252  return true;
253  }
254 
255  return false;
256  }
257 
258 
259  /**
260  * Add point to TGraph.
261  *
262  * \param g1 pointer to valid ROOT TGraph object
263  * \param x x value
264  * \param y y value
265  */
266  inline void AddPoint(TGraph* g1,
267  const Double_t x,
268  const Double_t y)
269  {
270  const Int_t n = g1->GetN();
271 
272  g1->Set(n + 1);
273  g1->SetPoint(n, x, y);
274  }
275 
276 
277  /**
278  * Add point to TGraph2D.
279  *
280  * \param g1 pointer to valid ROOT TGraph object
281  * \param x x value
282  * \param y y value
283  * \param z z value
284  */
285  inline void AddPoint(TGraph2D* g1,
286  const Double_t x,
287  const Double_t y,
288  const Double_t z)
289  {
290  const Int_t n = g1->GetN();
291 
292  g1->Set(n + 1);
293  g1->SetPoint(n, x, y, z);
294  }
295 
296 
297  /**
298  * Add point to TGraphErrors.
299  *
300  * \param g1 pointer to valid ROOT TGraph object
301  * \param x x value
302  * \param y y value
303  * \param ex x error
304  * \param ey y error
305  */
306  inline void AddPoint(TGraphErrors* g1,
307  const Double_t x,
308  const Double_t y,
309  const Double_t ex,
310  const Double_t ey)
311  {
312  const Int_t n = g1->GetN();
313 
314  g1->Set(n + 1);
315  g1->SetPoint(n, x, y);
316  g1->SetPointError(n, ex, ey);
317  }
318 
319 
320  /**
321  * Add point to TGraph2DErrors.
322  *
323  * \param g1 pointer to valid ROOT TGraph object
324  * \param x x value
325  * \param y y value
326  * \param z z value
327  * \param ex x error
328  * \param ey y error
329  * \param ez z error
330  */
331  inline void AddPoint(TGraph2DErrors* g1,
332  const Double_t x,
333  const Double_t y,
334  const Double_t z,
335  const Double_t ex,
336  const Double_t ey,
337  const Double_t ez)
338  {
339  const Int_t n = g1->GetN();
340 
341  g1->Set(n + 1);
342  g1->SetPoint(n, x, y, z);
343  g1->SetPointError(n, ex, ey, ez);
344  }
345 
346 
347  /**
348  * Write object to ROOT file.
349  *
350  * \param file ROOT file
351  * \param object ROOT object
352  * \return ROOT file
353  */
354  inline TFile& operator<<(TFile& file, const TObject& object)
355  {
356  file.WriteTObject(&object);
357 
358  return file;
359  }
360 
361 
362  /**
363  * Get ROOT streamer information of class with given name.
364  * Note that the class name should include the name space, if any.
365  *
366  * \param file pointer to ROOT file
367  * \param name class name
368  * \return pointer to TStreamerInfo (NULL in case of error)
369  */
370  inline const TStreamerInfo* getStreamerInfo(TFile* file, const char* name)
371  {
372  if (file != NULL && file->IsOpen()) {
373 
374  const TString buffer(name);
375 
376  TIter iter(file->GetStreamerInfoList());
377 
378  for (const TStreamerInfo* pStreamerInfo; (pStreamerInfo = (TStreamerInfo*) iter.Next()) != NULL; ) {
379  if (buffer == TString(pStreamerInfo->GetName())) {
380  return pStreamerInfo;
381  }
382  }
383  }
384 
385  return NULL;
386  }
387 
388 
389  /**
390  * Get ROOT streamer information of class with given name.
391  * Note that the class name should include the name space, if any.
392  *
393  * \param file_name file name
394  * \param name class name
395  * \return pointer to TStreamerInfo (NULL in case of error)
396  */
397  inline const TStreamerInfo* getStreamerInfo(const char* file_name, const char* name)
398  {
399  JRootInputFile file(file_name);
400 
401  return getStreamerInfo(file.getFile(), name);
402  }
403 
404 
405  /**
406  * Get ROOT streamer version of class with given name.
407  * Note that the class name should include the name space, if any.
408  *
409  * \param file pointer to ROOT file
410  * \param name class name
411  * \return streamer version; (-1 in case of error)
412  */
413  inline int getStreamerVersion(TFile* file, const char* name)
414  {
415  const TStreamerInfo* pStreamerInfo = getStreamerInfo(file, name);
416 
417  if (pStreamerInfo != NULL)
418  return pStreamerInfo->GetClassVersion();
419  else
420  return -1;
421  }
422 
423 
424  /**
425  * Get ROOT streamer version of class with given name.
426  * Note that the class name should include the name space, if any.
427  *
428  * \param file_name file name
429  * \param name class name
430  * \return streamer version; (-1 in case of error)
431  */
432  inline int getStreamerVersion(const char* file_name, const char* name)
433  {
434  JRootInputFile file(file_name);
435 
436  return getStreamerVersion(file.getFile(), name);
437  }
438 
439 
440  /**
441  * Match a regular expression with given string and return the specified matched parts.
442  *
443  * If no matches are found corresponding to the specified index, the original string is returned.
444  *
445  * \param regexp regular expression
446  * \param string input string
447  * \param index index of matched parts (starting at 1)
448  * \return matched part of string
449  */
450  inline TString parse(const TPRegexp& regexp, const TString& string, const int index = 1)
451  {
452  TPRegexp buffer = regexp;
453  TObjArray* array = buffer.MatchS(string);
454 
455  if (index - 1 < array->GetLast())
456  return ((TObjString*) array->At(index))->GetName();
457  else
458  return string;
459  }
460 
461 
462  /**
463  * Auxiliary data structure for a parameter index and its value.
464  */
466  /**
467  * Default constructor.
468  */
470  index(0),
471  value(0.0)
472  {}
473 
474 
475  /**
476  * Constructor.
477  *
478  * \param index parameter index
479  * \param value parameter value
480  */
481  JFitParameter_t(const Int_t index,
482  const Double_t value) :
483  index(index),
484  value(value)
485  {}
486 
487 
488  /**
489  * Type conversion operator
490  *
491  *
492  * \return index
493  */
494  inline operator Int_t() const
495  {
496  return index;
497  }
498 
499 
500  Int_t index;
501  Double_t value;
502  };
503 
504 
505  /**
506  * Set fit parameter.
507  *
508  * \param f1 fit function
509  * \param parameter parameter index and value
510  */
511  inline bool setParameter(TF1& f1, const JFitParameter_t& parameter)
512  {
513  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
514 
515  f1.SetParameter(parameter.index, parameter.value);
516 
517  return true;
518 
519  } else {
520 
521  return false;
522  }
523  }
524 
525 
526  /**
527  * Fix fit parameter.
528  *
529  * \param f1 fit function
530  * \param parameter parameter index and value
531  */
532  inline bool fixParameter(TF1& f1, const JFitParameter_t& parameter)
533  {
534  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
535 
536  f1.FixParameter(parameter.index, parameter.value);
537 
538  return true;
539 
540  } else {
541 
542  return false;
543  }
544  }
545 
546 
547  /**
548  * Release fit parameter.
549  *
550  * \param f1 fit function
551  * \param index parameter index
552  */
553  inline bool releaseParameter(TF1& f1, const Int_t index)
554  {
555  if (index >= 0 && index < f1.GetNpar()) {
556 
557  f1.ReleaseParameter(index);
558 
559  return true;
560 
561  } else {
562 
563  return false;
564  }
565  }
566 
567 
568  /**
569  * Set fit parameter limits.
570  *
571  * \param f1 fit function
572  * \param index parameter index
573  * \param xmin lower limit
574  * \param xmax upper limit
575  */
576  inline bool setParLimits(TF1& f1, const Int_t index, Double_t xmin, Double_t xmax)
577  {
578  using namespace std;
579 
580  if (index >= 0 && index < f1.GetNpar()) {
581 
582  if (xmin == 0.0) { xmin = -numeric_limits<Double_t>::min(); }
583  if (xmax == 0.0) { xmax = +numeric_limits<Double_t>::min(); }
584 
585  f1.SetParLimits(index, xmin, xmax);
586 
587  return true;
588 
589  } else {
590 
591  return false;
592  }
593  }
594 
595 
596  /**
597  * Check if fit parameter is fixed.
598  *
599  * \param f1 fit function
600  * \param index parameter index
601  */
602  inline bool isParameterFixed(const TF1& f1, const Int_t index)
603  {
604  if (index >= 0 && index < f1.GetNpar()) {
605 
606  Double_t xmin;
607  Double_t xmax;
608 
609  f1.GetParLimits(index, xmin, xmax);
610 
611  return (xmin != 0.0 && xmax != 0.0 && xmin >= xmax);
612 
613  } else {
614 
615  return false;
616  }
617  }
618 
619 
620  /**
621  * Get result of given textual formula.
622  *
623  * The formula may contain names of data members of the given object.
624  * For example:
625  * <pre>
626  * getResult("a / b.value", object, ..);
627  * </pre>
628  * In this, the corresponding data type should have (at least) data members <tt>a</tt> and <tt>b</tt>
629  * and <tt>b</tt> should have (at least) data member <tt>value</tt>.
630  *
631  * \param text text
632  * \param object object
633  * \param dictionary dictionary
634  * \return value
635  */
636  template<class T>
637  inline Double_t getResult(const TString& text,
638  const T& object,
639  const JRootDictionary_t& dictionary = JRootDictionary::getInstance())
640  {
641  using namespace std;
642  using namespace JPP;
643 
644  const TRegexp DOUBLE("[0-9.][eE][+-0-9]");
645 
646  string buffer = (const char*) text;
647 
648  for (string::size_type pos = 0; pos != buffer.size(); ) {
649 
650  if (isalpha(buffer[pos]) || buffer[pos] == '_') {
651 
652  string::size_type len = 1;
653 
654  while (pos + len != buffer.size() && (isalnum(buffer[pos+len]) || buffer[pos+len] == '_' || buffer[pos+len] == '.')) {
655  ++len;
656  }
657 
658  if (len != 1 || pos == 0 || TString(buffer.substr(pos-1).c_str()).Index(DOUBLE) != 0) {
659 
660  ostringstream os;
661 
662  os.setf(ios::fixed);
663  os.precision(10);
664 
665  JRootPrinter::print(os, object, buffer.substr(pos,len), dictionary);
666 
667  buffer.replace(pos, len, os.str());
668 
669  pos += os.str().size();
670 
671  continue;
672  }
673  }
674 
675  pos += 1;
676  }
677 
678  return TFormula("/tmp", buffer.c_str()).Eval(0.0);
679  }
680 
681 
682  /**
683  * Helper method to convert a 1D histogram to a graph.
684  *
685  * The graph consists of:
686  * - bin centers as x values;
687  * - bin contents as y values.
688  *
689  * Note that underflow and overflow bins are ignored.
690  *
691  * \param h1 1D histogram
692  * \return pointer to newly create graph
693  */
694  inline TGraph* histogramToGraph(const TH1& h1)
695  {
696  const int N = h1.GetNbinsX();
697 
698  std::vector<double> x(N), y(N);
699 
700  for (int i = 0; i < N; i++) {
701  x[i] = h1.GetBinCenter (i + 1);
702  y[i] = h1.GetBinContent(i + 1);
703  }
704 
705  return new TGraph(N, &x[0], &y[0]);
706  }
707 
708 
709  /**
710  * Helper method for ROOT histogram projections.
711  *
712  * \param h2 2D histogram
713  * \param xmin lower limit
714  * \param xmax upper limit
715  * \param projection projection (x|X|y|Y)
716  * \return pointer to newly created 1D histogram
717  */
718  inline TH1* projectHistogram(const TH2& h2, const Double_t xmin, const Double_t xmax, const char projection)
719  {
720  switch (projection) {
721 
722  case 'x':
723  case 'X':
724  return h2.ProjectionX("_px", h2.GetYaxis()->FindBin(xmin), h2.GetYaxis()->FindBin(xmax));
725 
726  case 'y':
727  case 'Y':
728  return h2.ProjectionY("_py", h2.GetXaxis()->FindBin(xmin), h2.GetXaxis()->FindBin(xmax));
729 
730  default:
731  return NULL;
732  }
733  }
734 }
735 
736 
737 /**
738  * Read regular expression from input stream
739  *
740  * \param in input stream
741  * \param object regular expression
742  * \return output stream
743  */
744 inline std::istream& operator>>(std::istream& in, TRegexp& object)
745 {
746  std::string buffer;
747 
748  if (in >> buffer) {
749  object = TRegexp(buffer.c_str());
750  }
751 
752  return in;
753 }
754 
755 
756 /**
757  * Write regular expression to output stream
758  *
759  * \param out output stream
760  * \param object regular expression
761  * \return output stream
762  */
763 inline std::ostream& operator<<(std::ostream& out, const TRegexp& object)
764 {
765  return out;
766 }
767 
768 #endif
769 
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.
then set_variable PMT_FILE set_variable DAQ_FILE set_variable OUTPUT_FILE set_variable DETECTOR else fatal Wrong number of arguments fi set_variable RUNBYRUN file
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.
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
Definition: JRoot.hh:19
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
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
const int n
Definition: JPolint.hh:676
Auxiliary data structure for a parameter index and its value.
TFile * getFile() const
Get file.
Definition: JRootFile.hh:77
I/O formatting auxiliaries.
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
static void print(JRootWriter &writer, const JRootWritableClass &cls)
Write class contents to output.
Definition: JRootPrinter.hh:62
ROOT input file.
Definition: JRootFile.hh:113
bool setParLimits(TF1 &f1, const Int_t index, Double_t xmin, Double_t xmax)
Set fit parameter limits.
TGraph * histogramToGraph(const TH1 &h1)
Helper method to convert a 1D histogram to a graph.
Type definition of ROOT based dictionary for ASCII I/O.
void reset(T &value)
Reset value.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1693
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:57
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 CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
TH1 * projectHistogram(const TH2 &h2, const Double_t xmin, const Double_t xmax, const char projection)
Helper method for ROOT histogram projections.
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25