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