Jpp  16.0.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGizmoToolkit.hh
Go to the documentation of this file.
1 #ifndef __JGIZMOTOOLKIT__
2 #define __JGIZMOTOOLKIT__
3 
4 #include <string>
5 #include <sstream>
6 #include <map>
7 #include <cmath>
8 
9 #include "TError.h"
10 #include "TFile.h"
11 #include "TClass.h"
12 #include "TObject.h"
13 #include "TKey.h"
14 #include "TH1.h"
15 #include "TH2.h"
16 #include "TGraph.h"
17 #include "TGraphErrors.h"
18 #include "TGraph2D.h"
19 #include "TGraph2DErrors.h"
20 #include "TMultiGraph.h"
21 #include "TLine.h"
22 #include "TEllipse.h"
23 #include "TString.h"
24 #include "TRegexp.h"
25 #include "TFormula.h"
26 #include "TF1.h"
27 #include "TF2.h"
28 #include "TIterator.h"
29 #include "TMethod.h"
30 #include "TMethodCall.h"
31 #include "TAxis.h"
32 #include "TMath.h"
33 
34 #include "JLang/JException.hh"
35 #include "JGizmo/JRootObjectID.hh"
36 
38 
39 
40 /**
41  * \author mdejong
42  */
43 
44 namespace JGIZMO {}
45 namespace JPP { using namespace JGIZMO; }
46 
47 /**
48  * Auxiliary applications for use of ROOT and more.
49  */
50 namespace JGIZMO {
51 
52  using JLANG::JParseError;
56 
57 
58  /**
59  * Auxiliary data structure for JOpera1D.cc and JOpera2D.cc applications.
60  */
61  struct JOpera {
62  //
63  // Histogram name.
64  //
65  static const char* const SAME_AS_OPERATION() { return "%"; } //!< Set name of output histogram to name of operation
66  static const char* const SAME_AS_INPUT() { return "="; } //!< Set name of output histogram to name of input histogram
67 
68  //
69  // Histogram operations.
70  //
71  static const char* const Add() { return "Add"; } //!< ROOT TH1::Add
72  static const char* const add() { return "add"; } //!< Add contents with lookup bin in second histogram
73  static const char* const Subtract() { return "Subtract"; } //!< ROOT TH1::Subtract
74  static const char* const subtract() { return "subtract"; } //!< Subtract contents with lookup bin in second histogram
75  static const char* const Multiply() { return "Multiply"; } //!< ROOT TH1::Multiply
76  static const char* const multiply() { return "multiply"; } //!< Multiply contents with lookup bin in second histogram
77  static const char* const Divide() { return "Divide"; } //!< ROOT TH1::Divide
78  static const char* const divide() { return "divide"; } //!< Divide contents with lookup bin in second histogram
79  static const char* const efficiency() { return "efficiency"; } //!< Divide contents and multiply errors with inefficiency
80  static const char* const stdev() { return "stdev"; } //!< Set contents to standard deviation
81  static const char* const sqrt() { return "sqrt"; } //!< Set contents to signed difference between squares
82  static const char* const Replace() { return "Replace"; } //!< Set contents to associated function
83  };
84 
85 
86  /**
87  * Time stamp of earliest UTC time.
88  */
89  static const char* const TIMESTAMP = "#splitline{}{#splitline{%d:%m:%y}{ %H:%M}}%F1970-01-01 00:00:00";
90 
91 
92  /**
93  * Get TFile pointer corresponding to give file name.
94  *
95  * The TFile pointer of an already opened file is recovered, else a new file is opened.\n
96  * Note that the closure of the opened files should be done by the caller of this method.
97  *
98  * \param file_name file name
99  * \param option TFile::Open option
100  * \return pointer to TFile
101  */
102  inline TFile* getFile(const std::string& file_name, const std::string& option = "exist")
103  {
104  using namespace std;
105 
106  gErrorIgnoreLevel = kError;
107 
108  static map<string, TFile*> zmap;
109 
110  map<string, TFile*>::iterator i = zmap.find(file_name);
111 
112  if (i == zmap.end() || i->second == NULL || !i->second->IsOpen()) {
113 
114  TFile* file = TFile::Open(file_name.c_str(), option.c_str());
115 
116  zmap[file_name] = file;
117 
118  return file;
119 
120  } else {
121 
122  return i->second;
123  }
124  }
125 
126 
127  /**
128  * Get TDirectory pointer.
129  *
130  * The TFile pointer of an already opened file is recovered, else a new file is opened.
131  *
132  * \param id identifier
133  * \return pointer to TDirectory
134  */
135  inline TDirectory* getDirectory(const JRootObjectID& id)
136  {
137  TFile* in = getFile(id.getFilename().c_str(), "exist");
138 
139  if (in == NULL || !in->IsOpen()) {
140  return NULL;
141  }
142 
143  if (id.getDirectory() != "")
144  return in->GetDirectory(id.getDirectory());
145  else
146  return in;
147  }
148 
149 
150  /**
151  * Get first TObject with given identifier.
152  *
153  * \param id identifier
154  * \return pointer to TObject (or NULL)
155  */
156  inline TObject* getObject(const JRootObjectID& id)
157  {
158  TDirectory* dir = getDirectory(id);
159 
160  if (dir != NULL) {
161 
162  const TRegexp regexp(id.getObjectName());
163 
164  TIter iter(dir->GetListOfKeys());
165 
166  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
167 
168  const TString tag(key->GetName());
169 
170  // option match
171 
172  if (tag.Index(regexp) != -1) {
173  return key->ReadObj();
174  }
175  }
176  }
177 
178  return NULL;
179  }
180 
181 
182  /**
183  * Get drawing option of TH1.
184  *
185  * \param object pointer to TObject
186  * \return true if TH1 looks like a line; else false
187  */
188  inline bool isTAttLine(const TObject* object)
189  {
190  {
191  const TH1* h1 = dynamic_cast<const TH1*>(object);
192 
193  if (h1 != NULL) {
194 
195  if (h1->GetSumw2N()) {
196  for (Int_t i = 1; i <= h1->GetNbinsX(); ++i) {
197  if (h1->GetBinError(i) != 0.0) {
198  return false;
199  }
200  }
201  }
202  }
203  }
204  {
205  const TGraphErrors* g1 = dynamic_cast<const TGraphErrors*>(object);
206 
207  if (g1 != NULL) {
208 
209  for (Int_t i = 0; i != g1->GetN(); ++i) {
210  if (g1->GetEY()[i] != 0.0) {
211  return false;
212  }
213  }
214 
215  return g1->GetN() > 1;
216  }
217  }
218  {
219  const TGraph* g1 = dynamic_cast<const TGraph*>(object);
220 
221  if (g1 != NULL) {
222  return g1->GetN() > 1;
223  }
224  }
225 
226  return true;
227  }
228 
229 
230  /**
231  * Get result of given textual formula.
232  *
233  * The formula may contain names of member methods of the object pointed to.\n
234  * These methods should return a value that is compatible with <tt>Double_t</tt> and could have arguments.\n
235  * For example:
236  * <pre>
237  * getResult("1.0 / GetEntries", TH1*);
238  * </pre>
239  *
240  * \param text text
241  * \param object pointer to object
242  * \return value
243  */
244  inline Double_t getResult(const TString& text, TObject* object = NULL)
245  {
246  TString buffer(text);
247 
248  if (object != NULL) {
249 
250  TClass* p = TClass::GetClass(object->ClassName());
251 
252  if (p != NULL) {
253 
254  for ( ; ; ) {
255 
256  TIterator* iter = p->GetListOfAllPublicMethods()->MakeIterator();
257  TMethod* method = NULL;
258 
259  for (TMethod* p; (p = (TMethod*) iter->Next()) != NULL; ) {
260  if (buffer.Index(p->GetName()) != -1) {
261  if (method == NULL || strlen(p->GetName()) > strlen(method->GetName())) {
262  method = p;
263  }
264  }
265  }
266 
267  if (method == NULL) {
268  break;
269  }
270 
271  for (Ssiz_t index; (index = buffer.Index(method->GetName())) != -1; ) {
272 
273  const TRegexp fp(" *([^)]*)"); // function call
274 
275  Ssiz_t len;
276  Ssiz_t pos = buffer.Index(fp, &len, index);
277 
278  Double_t value;
279 
280  if (pos == -1 || pos != index + (Ssiz_t) strlen(method->GetName())) {
281 
282  TMethodCall(p, method->GetName(), NULL).Execute(object, value);
283 
284  len = strlen(method->GetName());
285 
286  } else {
287 
288  TMethodCall(p, method->GetName(), NULL).Execute(object, TString(buffer(pos + 1, len - 2)), value);
289 
290  len += strlen(method->GetName());
291  }
292 
293  buffer.Replace(index, len, TString::Format("%15.5e", value));
294  }
295  }
296  }
297  }
298 
299  return TFormula("/tmp", buffer.Data()).Eval(0.0);
300  }
301 
302 
303  /**
304  * Get result of given textual formula.
305  *
306  * The formula may contain names of member methods of the object pointed to.\n
307  * These methods should return a value that is compatible with <tt>Double_t</tt> and could have arguments.\n
308  * For example:
309  * <pre>
310  * getResult("1.0 / GetEntries", TH1*);
311  * </pre>
312  *
313  * \param text text
314  * \param object pointer to object
315  * \return value
316  */
317  inline Double_t getResult(const std::string& text, TObject* object = NULL)
318  {
319  return getResult(TString(text.c_str()), object);
320  }
321 
322 
323  /**
324  * Get parameter number from text string.
325  *
326  * The number corresponds to the value <tt>[0-9]*</tt> in the expression <tt>"p[0-9]* = .."</tt>.
327  *
328  * \param text text
329  * \return parameter number
330  */
331  inline int getParameter(const std::string& text)
332  {
333  const char* regexp("p[0-9]* *=");
334 
335  TString buffer(text.c_str());
336 
337  buffer = buffer(TRegexp(regexp));
338  buffer = buffer(1, buffer.Length() - 2);
339 
340  if (!buffer.IsDigit()) {
341  THROW(JParseError, "Text is not a number " << text << ' ' << regexp);
342  }
343 
344  return buffer.Atoi();
345  }
346 
347 
348  /**
349  * Get parameter value from text string.
350  *
351  * The formula may contain names of member methods of the object pointed to.\n
352  * These methods should return a value that is compatible with <tt>Double_t</tt> and could have arguments.\n
353  * For example:
354  * <pre>
355  * getValue("p[..] = 2 * GetMaximum", TH1*);
356  * </pre>
357  *
358  * \param text text
359  * \param object pointer to object
360  * \return value
361  */
362  inline Double_t getValue(const std::string& text, TObject* object = NULL)
363  {
364  const char* regexp("=.*");
365 
366  TString buffer(text.c_str());
367 
368  buffer = buffer(TRegexp(regexp));
369  buffer = buffer(1, buffer.Length() - 1);
370 
371  return getResult(std::string(buffer), object);
372  }
373 
374 
375  /**
376  * Get parameter value from text string.
377  *
378  * The formula may contain names of member methods of the object pointed to.\n
379  * These methods should return a value that is compatible with <tt>Double_t</tt> and could have arguments.\n
380  * For example:
381  * <pre>
382  * getValue("p[..] = 1.0 2.0 3.0", 1);
383  * </pre>
384  * will return <tt>2.0</tt>.
385  *
386  * \param text text
387  * \param index index
388  * \return value
389  */
390  inline Double_t getValue(const std::string& text, const int index)
391  {
392  using namespace std;
393 
394  const char* regexp("=.*");
395 
396  TString buffer(text.c_str());
397 
398  buffer = buffer(TRegexp(regexp));
399  buffer = buffer(1, buffer.Length() - 1);
400 
401 
402  istringstream is((std::string(buffer)));
403 
404  Double_t value;
405 
406  for (int i = index; is >> value && i > 0; --i) {}
407 
408  if (is)
409  return value;
410  else
411  THROW(JParseError, "Text des not contain a number at given position " << buffer << ' ' << index);
412  }
413 
414 
415  /**
416  * Make histogram axis logarithmic (e.g.\ after using <tt>log10()</tt>).
417  *
418  * \param axis axis
419  */
420  inline void setLogarithmic(TAxis* axis)
421  {
422  if (axis != NULL) {
423 
424  const Int_t first = axis->GetFirst();
425  const Int_t last = axis->GetLast();
426 
427  const Double_t xmin = axis->GetBinLowEdge(first);
428  const Double_t xmax = axis->GetBinLowEdge(last) + axis->GetBinWidth(last);
429 
430  const Int_t N = axis->GetNbins();
431  Double_t buffer[N+1];
432 
433  buffer[0] = TMath::Power(10.0, axis->GetBinLowEdge(1));
434 
435  for (Int_t i = 1; i <= N; ++i) {
436  buffer[i] = TMath::Power(10.0, axis->GetBinLowEdge(i) + axis->GetBinWidth(i));
437  }
438 
439  axis->Set(N, buffer);
440 
441  if (axis->TestBit(TAxis::kAxisRange)) {
442  axis->SetRangeUser(TMath::Power(10.0, xmin), TMath::Power(10.0, xmax));
443  }
444  }
445  }
446 
447 
448  /**
449  * Make given parameter in formula logarithmic (e.g.\ after using <tt>log10()</tt>).
450  *
451  * \param formula formula
452  * \param parameter parameter
453  * \return formula
454  */
455  inline TString getLogarithmic(const TString& formula, const char parameter)
456  {
457  const TRegexp regexp[] = {
458  TString("^") + TString(parameter) + TString("[^a-zA-Z_]"), // parameter at start of line
459  TString("[^a-zA-Z_]") + TString(parameter) + TString("[^a-zA-Z_]"), // parameter in middle of line
460  TString("[^a-zA-Z_]") + TString(parameter) + TString("$") // parameter at end of line
461  };
462 
463  const TString replacement = TString("log10(") + TString(parameter) + TString(")");
464 
465  TString buffer(formula);
466 
467  if (buffer.Length() == 1 && buffer[0] == parameter) {
468 
469  buffer = replacement;
470 
471  } else {
472 
473  for (Ssiz_t pos = 0, i; pos < buffer.Length(); pos += replacement.Length()) {
474  if ((i = buffer.Index(regexp[0], pos)) != -1) { buffer.Replace((pos = i + 0), 1, replacement); }
475  else if ((i = buffer.Index(regexp[1], pos)) != -1) { buffer.Replace((pos = i + 1), 1, replacement); }
476  else if ((i = buffer.Index(regexp[2], pos)) != -1) { buffer.Replace((pos = i + 1), 1, replacement); }
477  else { break; }
478  }
479  }
480 
481  return buffer;
482  }
483 
484 
485  /**
486  * Copy function parameters.
487  *
488  * \param from function
489  * \param to function
490  */
491  inline void copy(const TF1& from, TF1& to)
492  {
493  static_cast<TAttLine&> (to) = static_cast<const TAttLine&> (from);
494  static_cast<TAttFill&> (to) = static_cast<const TAttFill&> (from);
495  static_cast<TAttMarker&>(to) = static_cast<const TAttMarker&>(from);
496 
497  to.SetParameters(from.GetParameters());
498 
499  to.SetNpx(from.GetNpx());
500  }
501 
502 
503  /**
504  * Copy function parameters.
505  *
506  * \param from function
507  * \param to function
508  */
509  inline void copy(const TF2& from, TF2& to)
510  {
511  copy(static_cast<const TF1&>(from), static_cast<TF1&>(to));
512 
513  to.SetNpy(from.GetNpy());
514  }
515 
516 
517  /**
518  * Make x-axis of objects in list logarithmic (e.g.\ after using <tt>log10()</tt>).
519  *
520  * \param list list
521  */
522  template<class T>
523  inline void setLogarithmicX(TList* list);
524 
525 
526  /**
527  * Make y-axis of objects in list logarithmic (e.g.\ after using <tt>log10()</tt>).
528  *
529  * \param list list
530  */
531  template<class T>
532  inline void setLogarithmicY(TList* list);
533 
534 
535  /**
536  * Make x-axis of given function logarithmic (e.g.\ after using <tt>log10()</tt>).
537  *
538  * \param f1 function
539  */
540  inline void setLogarithmicX(TF1* f1)
541  {
542  if (f1 != NULL) {
543 
544  TF1 fn(f1->GetName(), getLogarithmic(f1->GetExpFormula(), 'x'));
545 
546  copy(*f1, fn);
547 
548  fn.SetRange(f1->GetXmin(),
549  f1->GetXmax());
550 
551  *f1 = fn;
552 
553  f1->SetRange(TMath::Power(10.0,f1->GetXmin()),
554  TMath::Power(10.0,f1->GetXmax()));
555  }
556  }
557 
558 
559  /**
560  * Make y-axis of given function logarithmic (e.g.\ after using <tt>log10()</tt>).
561  *
562  * \param f1 function
563  */
564  inline void setLogarithmicY(TF1* f1)
565  {
566  if (f1 != NULL) {
567 
568  TString buffer = f1->GetExpFormula();
569 
570  buffer = "pow(10.0, " + buffer + ")";
571 
572  TF1 fn(f1->GetName(), buffer);
573 
574  copy(*f1, fn);
575 
576  *f1 = fn;
577  }
578  }
579 
580 
581  /**
582  * Make x-axis of given function logarithmic (e.g.\ after using <tt>log10()</tt>).
583  *
584  * \param f2 function
585  */
586  inline void setLogarithmicX(TF2* f2)
587  {
588  if (f2 != NULL) {
589 
590  TF2 fn(f2->GetName(), getLogarithmic(f2->GetExpFormula(), 'x'));
591 
592  copy(*f2, fn);
593 
594  fn.SetRange(f2->GetXmin(),
595  f2->GetYmin(),
596  f2->GetXmax(),
597  f2->GetYmax());
598 
599  *f2 = fn;
600 
601  f2->SetRange(TMath::Power(10.0,f2->GetXmin()),
602  f2->GetYmin(),
603  TMath::Power(10.0,f2->GetXmax()),
604  f2->GetYmax());
605  }
606  }
607 
608 
609  /**
610  * Make y-axis of given function logarithmic (e.g.\ after using <tt>log10()</tt>).
611  *
612  * \param f2 function
613  */
614  inline void setLogarithmicY(TF2* f2)
615  {
616  if (f2 != NULL) {
617 
618  TF2 fn(f2->GetName(), getLogarithmic(f2->GetExpFormula(), 'y'));
619 
620  copy(*f2, fn);
621 
622  fn.SetRange(f2->GetXmin(),
623  f2->GetYmin(),
624  f2->GetXmax(),
625  f2->GetYmax());
626 
627  *f2 = fn;
628 
629  f2->SetRange(f2->GetXmin(),
630  TMath::Power(10.0,f2->GetYmin()),
631  f2->GetXmax(),
632  TMath::Power(10.0,f2->GetYmax()));
633  }
634  }
635 
636 
637  /**
638  * Make x-axis and associated functions of given histogram logarithmic (e.g.\ after using <tt>log10()</tt>).
639  *
640  * \param h1 histogram
641  */
642  inline void setLogarithmicX(TH1* h1)
643  {
644  if (h1 != NULL) {
645 
646  setLogarithmicX<TF1>(h1->GetListOfFunctions());
647 
648  setLogarithmic(h1->GetXaxis());
649  }
650  }
651 
652 
653  /**
654  * Make x-axis and associated functions of given histogram logarithmic (e.g.\ after using <tt>log10()</tt>).
655  *
656  * \param h2 histogram
657  */
658  inline void setLogarithmicX(TH2* h2)
659  {
660  using namespace std;
661 
662  if (h2 != NULL) {
663 
664  setLogarithmicX<TF2>(h2->GetListOfFunctions());
665 
666  setLogarithmic(h2->GetXaxis());
667  }
668  }
669 
670 
671  /**
672  * Make y-axis and associated functions of given histogram logarithmic (e.g.\ after using <tt>log10()</tt>).
673  *
674  * \param h2 histogram
675  */
676  inline void setLogarithmicY(TH2* h2)
677  {
678  if (h2 != NULL) {
679 
680  setLogarithmicY<TF2>(h2->GetListOfFunctions());
681 
682  setLogarithmic(h2->GetYaxis());
683  }
684  }
685 
686 
687  /**
688  * Make x-axis and associated functions of given graph logarithmic (e.g.\ after using <tt>log10()</tt>).
689  *
690  * \param g1 graph
691  */
692  inline void setLogarithmicX(TGraph* g1)
693  {
694  if (g1 != NULL) {
695 
696  setLogarithmicX<TF1>(g1->GetListOfFunctions());
697 
698  for (Int_t i = 0; i != g1->GetN(); ++i) {
699  g1->GetX()[i] = pow(10.0, g1->GetX()[i]);
700  }
701  }
702  }
703 
704 
705  /**
706  * Make y-axis and associated functions of given graph logarithmic (e.g.\ after using <tt>log10()</tt>).
707  *
708  * \param g1 graph
709  */
710  inline void setLogarithmicY(TGraph* g1)
711  {
712  if (g1 != NULL) {
713 
714  setLogarithmicY<TF1>(g1->GetListOfFunctions());
715 
716  for (Int_t i = 0; i != g1->GetN(); ++i) {
717  g1->GetY()[i] = pow(10.0, g1->GetY()[i]);
718  }
719  }
720  }
721 
722 
723  /**
724  * Make x-axis and associated functions of given graph logarithmic (e.g.\ after using <tt>log10()</tt>).
725  *
726  * \param g1 graph
727  */
728  inline void setLogarithmicX(TGraphErrors* g1)
729  {
730  if (g1 != NULL) {
731 
732  setLogarithmicX<TF1>(g1->GetListOfFunctions());
733 
734  for (Int_t i = 0; i != g1->GetN(); ++i) {
735  g1->GetEX()[i] = pow(10.0, g1->GetX()[i] + g1->GetEX()[i]) - pow(10.0, g1->GetX()[i]);
736  g1->GetX() [i] = pow(10.0, g1->GetX()[i]);
737  }
738  }
739  }
740 
741 
742  /**
743  * Make y-axis and associated functions of given graph logarithmic (e.g.\ after using <tt>log10()</tt>).
744  *
745  * \param g1 graph
746  */
747  inline void setLogarithmicY(TGraphErrors* g1)
748  {
749  if (g1 != NULL) {
750 
751  setLogarithmicY<TF1>(g1->GetListOfFunctions());
752 
753  for (Int_t i = 0; i != g1->GetN(); ++i) {
754  g1->GetEY()[i] = pow(10.0, g1->GetY()[i] + g1->GetEY()[i]) - pow(10.0, g1->GetY()[i]);
755  g1->GetY() [i] = pow(10.0, g1->GetY()[i]);
756  }
757  }
758  }
759 
760 
761  /**
762  * Make x-axis and associated functions of given graph logarithmic (e.g.\ after using <tt>log10()</tt>).
763  *
764  * \param g2 graph
765  */
766  inline void setLogarithmicX(TGraph2D* g2)
767  {
768  if (g2 != NULL) {
769 
770  setLogarithmicX<TF2>(g2->GetListOfFunctions());
771 
772  for (Int_t i = 0; i != g2->GetN(); ++i) {
773  g2->GetX()[i] = pow(10.0, g2->GetX()[i]);
774  }
775  }
776  }
777 
778 
779  /**
780  * Make y-axis and associated functions of given graph logarithmic (e.g.\ after using <tt>log10()</tt>).
781  *
782  * \param g2 graph
783  */
784  inline void setLogarithmicY(TGraph2D* g2)
785  {
786  if (g2 != NULL) {
787 
788  setLogarithmicY<TF2>(g2->GetListOfFunctions());
789 
790  for (Int_t i = 0; i != g2->GetN(); ++i) {
791  g2->GetY()[i] = pow(10.0, g2->GetY()[i]);
792  }
793  }
794  }
795 
796 
797  /**
798  * Make x-axis and associated functions of given graph logarithmic (e.g.\ after using <tt>log10()</tt>).
799  *
800  * \param g2 graph
801  */
802  inline void setLogarithmicX(TGraph2DErrors* g2)
803  {
804  if (g2 != NULL) {
805 
806  setLogarithmicX<TF2>(g2->GetListOfFunctions());
807 
808  for (Int_t i = 0; i != g2->GetN(); ++i) {
809  g2->GetEX()[i] = pow(10.0, g2->GetX()[i] + g2->GetEX()[i]) - pow(10.0, g2->GetX()[i]);
810  g2->GetX() [i] = pow(10.0, g2->GetX()[i]);
811  }
812  }
813  }
814 
815 
816  /**
817  * Make y-axis and associated functions of given graph logarithmic (e.g.\ after using <tt>log10()</tt>).
818  *
819  * \param g2 graph
820  */
821  inline void setLogarithmicY(TGraph2DErrors* g2)
822  {
823  if (g2 != NULL) {
824 
825  setLogarithmicY<TF2>(g2->GetListOfFunctions());
826 
827  for (Int_t i = 0; i != g2->GetN(); ++i) {
828  g2->GetEY()[i] = pow(10.0, g2->GetY()[i] + g2->GetEY()[i]) - pow(10.0, g2->GetY()[i]);
829  g2->GetY() [i] = pow(10.0, g2->GetY()[i]);
830  }
831  }
832  }
833 
834 
835  /**
836  * Make x-axis of given multi-graph logarithmic (e.g.\ after using <tt>log10()</tt>).
837  *
838  * \param gn multi graph
839  */
840  inline void setLogarithmicX(TMultiGraph* gn)
841  {
842  if (gn != NULL) {
843  setLogarithmicX<TGraph>(gn->GetListOfGraphs());
844  }
845  }
846 
847 
848  /**
849  * Make y-axis of given multi-graph logarithmic (e.g.\ after using <tt>log10()</tt>).
850  *
851  * \param gn multi graph
852  */
853  inline void setLogarithmicY(TMultiGraph* gn)
854  {
855  if (gn != NULL) {
856  setLogarithmicY<TGraph>(gn->GetListOfGraphs());
857  }
858  }
859 
860 
861  /**
862  * Make x-axis of given line logarithmic (e.g.\ after using <tt>log10()</tt>).
863  *
864  * \param line line
865  */
866  inline void setLogarithmicX(TLine* line)
867  {
868  if (line != NULL) {
869  line->SetX1(pow(10.0, line->GetX1()));
870  line->SetX2(pow(10.0, line->GetX2()));
871  }
872  }
873 
874 
875  /**
876  * Make y-axis of given line logarithmic (e.g.\ after using <tt>log10()</tt>).
877  *
878  * \param line line
879  */
880  inline void setLogarithmicY(TLine* line)
881  {
882  if (line != NULL) {
883  line->SetY1(pow(10.0, line->GetY1()));
884  line->SetY2(pow(10.0, line->GetY2()));
885  }
886  }
887 
888 
889  /**
890  * Make x-axis of given ellipse logarithmic (e.g.\ after using <tt>log10()</tt>).
891  *
892  * \param ellipse ellipse
893  */
894  inline void setLogarithmicX(TEllipse* ellipse)
895  {
896  THROW(JFunctionalException, "Operation setLogarithmicX on TEllipse not allowed.");
897  }
898 
899 
900  /**
901  * Make y-axis of given ellipse logarithmic (e.g.\ after using <tt>log10()</tt>).
902  *
903  * \param ellipse ellipse
904  */
905  inline void setLogarithmicY(TEllipse* ellipse)
906  {
907  THROW(JFunctionalException, "Operation setLogarithmicY on TEllipse not allowed.");
908  }
909 
910 
911  /**
912  * Make x-axis of objects in list logarithmic (e.g.\ after using <tt>log10()</tt>).
913  *
914  * \param list list
915  */
916  template<class T>
917  inline void setLogarithmicX(TList* list)
918  {
919  for (TIter i(list); T* p = dynamic_cast<T*>(i.Next()); ) {
920  setLogarithmicX(p);
921  }
922  }
923 
924 
925  /**
926  * Make y-axis of objects in list logarithmic (e.g.\ after using <tt>log10()</tt>).
927  *
928  * \param list list
929  */
930  template<class T>
931  inline void setLogarithmicY(TList* list)
932  {
933  for (TIter i(list); T* p = dynamic_cast<T*>(i.Next()); ) {
934  setLogarithmicY(p);
935  }
936  }
937 
938 
939  /**
940  * Convert 1D histogram to PDF.
941  *
942  * Possible options are:
943  * - N normalise to histogram contents;
944  * - W divide by bin width;
945  * - E convert also bin errors.
946  *
947  * \param h1 histogram
948  * \param option option
949  * \param factor scaling factor
950  */
951  inline void convertToPDF(TH1& h1, const std::string& option = "NW", const double factor = 1.0)
952  {
953  using namespace std;
954 
955  const bool normalise = (option.find('N') != string::npos || option.find('n') != string::npos);
956  const bool bin_width = (option.find('W') != string::npos || option.find('w') != string::npos);
957  const bool use_error = (option.find('E') != string::npos || option.find('e') != string::npos);
958 
959  Double_t W = 1.0;
960 
961  if (normalise) {
962 
963  W = 0.0;
964 
965  for (Int_t i = 1; i <= h1.GetXaxis()->GetNbins(); ++i) {
966  W += h1.GetBinContent(i);
967  }
968  }
969 
970  if (W != 0.0) {
971 
972  for (Int_t i = 1; i <= h1.GetXaxis()->GetNbins(); ++i) {
973 
974  const Double_t w = W * (bin_width ? h1.GetXaxis()->GetBinWidth(i) : 1.0);
975 
976  h1.SetBinContent(i, h1.GetBinContent(i) * factor / w);
977 
978  if (use_error) {
979  h1.SetBinError(i, h1.GetBinError(i) * factor / w);
980  }
981  }
982  }
983  }
984 
985 
986  /**
987  * Convert 2D histogram to PDF.
988  *
989  * Possible options are:
990  * - N normalise to histogram contents;
991  * - X convert x-axis to PDF;
992  * - Y convert y-axis to PDF;
993  * - W divide by bin width;
994  * - E convert also bin errors.
995  *
996  * \param h2 histogram
997  * \param option option
998  * \param factor scaling factor
999  */
1000  inline void convertToPDF(TH2& h2, const std::string& option = "NXYW", const double factor = 1.0)
1001  {
1002  using namespace std;
1003 
1004  const bool normalise = (option.find('N') != string::npos || option.find('n') != string::npos);
1005  const bool X = (option.find('X') != string::npos || option.find('x') != string::npos);
1006  const bool Y = (option.find('Y') != string::npos || option.find('y') != string::npos);
1007  const bool bin_width = (option.find('W') != string::npos || option.find('w') != string::npos);
1008  const bool use_error = (option.find('E') != string::npos || option.find('e') != string::npos);
1009 
1010  Double_t W = 1.0;
1011 
1012  if (X && Y) {
1013 
1014  if (normalise) {
1015 
1016  W = 0.0;
1017 
1018  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
1019  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
1020  W += h2.GetBinContent(ix,iy);
1021  }
1022  }
1023  }
1024 
1025  if (W != 0.0) {
1026 
1027  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
1028  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
1029 
1030  const Double_t w = W * (bin_width ? h2.GetXaxis()->GetBinWidth(ix) * h2.GetYaxis()->GetBinWidth(iy) : 1.0);
1031 
1032  h2.SetBinContent(ix, iy, h2.GetBinContent(ix,iy) * factor / w);
1033 
1034  if (use_error) {
1035  h2.SetBinError(ix, iy, h2.GetBinError(ix,iy) * factor / w);
1036  }
1037  }
1038  }
1039  }
1040 
1041  } else if (X) {
1042 
1043  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
1044 
1045  if (normalise) {
1046 
1047  W = 0.0;
1048 
1049  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
1050  W += h2.GetBinContent(ix,iy);
1051  }
1052  }
1053 
1054  if (W != 0.0) {
1055 
1056  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
1057 
1058  const Double_t w = W * (bin_width ? h2.GetXaxis()->GetBinWidth(ix) : 1.0);
1059 
1060  h2.SetBinContent(ix, iy, h2.GetBinContent(ix,iy) * factor / w);
1061 
1062  if (use_error) {
1063  h2.SetBinError(ix, iy, h2.GetBinError(ix,iy) * factor / w);
1064  }
1065  }
1066  }
1067  }
1068 
1069  } else if (Y) {
1070 
1071  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
1072 
1073  if (normalise) {
1074 
1075  W = 0.0;
1076 
1077  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
1078  W += h2.GetBinContent(ix,iy);
1079  }
1080  }
1081 
1082  if (W != 0.0) {
1083 
1084  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
1085 
1086  const Double_t w = W * (bin_width ? h2.GetYaxis()->GetBinWidth(iy) : 1.0);
1087 
1088  h2.SetBinContent(ix, iy, h2.GetBinContent(ix,iy) / w);
1089 
1090  if (use_error) {
1091  h2.SetBinError(ix, iy, h2.GetBinError(ix,iy) / w);
1092  }
1093  }
1094  }
1095  }
1096  }
1097  }
1098 
1099 
1100  /**
1101  * Set limits of TGraph.
1102  *
1103  * \param g1 graph
1104  */
1105  inline void setLimits(TGraph& g1)
1106  {
1107  using namespace std;
1108 
1109  Double_t ymin = numeric_limits<Double_t>::max();
1110  Double_t ymax = numeric_limits<Double_t>::lowest();
1111 
1112  for (Int_t i = 0; i != g1.GetN(); ++i) {
1113 
1114  const Double_t y = g1.GetY()[i];
1115 
1116  if (y > ymax) { ymax = y; }
1117  if (y < ymin) { ymin = y; }
1118  }
1119 
1120  g1.SetMinimum(ymin);
1121  g1.SetMaximum(ymax);
1122  }
1123 
1124 
1125  /**
1126  * Set limits of TGraph2D.
1127  *
1128  * \param g2 graph
1129  */
1130  inline void setLimits(TGraph2D& g2)
1131  {
1132  using namespace std;
1133 
1134  Double_t zmin = numeric_limits<Double_t>::max();
1135  Double_t zmax = numeric_limits<Double_t>::lowest();
1136 
1137  for (Int_t i = 0; i != g2.GetN(); ++i) {
1138 
1139  const Double_t z = g2.GetZ()[i];
1140 
1141  if (z > zmax) { zmax = z; }
1142  if (z < zmin) { zmin = z; }
1143  }
1144 
1145  g2.SetMinimum(zmin);
1146  g2.SetMaximum(zmax);
1147  }
1148 
1149 
1150  /**
1151  * Set axis range.
1152  *
1153  * \param xmin lower limit (I/O)
1154  * \param xmax upper limit (I/O)
1155  * \param logx logarithmic
1156  */
1157  inline void setRange(double& xmin,
1158  double& xmax,
1159  const bool logx)
1160  {
1161  if (logx) {
1162  xmin = log(xmin);
1163  xmax = log(xmax);
1164  }
1165 
1166  double dx = (xmax - xmin) * 0.1;
1167 
1168  if (xmin > dx || xmin < 0.0)
1169  xmin -= dx;
1170  else
1171  xmin = 0.0;
1172 
1173  xmax += dx;
1174 
1175  if (logx) {
1176  xmin = exp(xmin);
1177  xmax = exp(xmax);
1178  }
1179  }
1180 
1181 
1182  /**
1183  * Set axis with PMT address labels.
1184  *
1185  * This methods sets the labels of the given axis to the sorted values of the PMT ring and position.\n
1186  * It should normally be called before filling of the corresponding histogram.\n
1187  * The filling should then be made with the textual representation of the PMT ring and position (i.e.\ JDETECTOR::JPMTPhysicalAddress::toString).
1188  *
1189  * Alternatively, the filling can be made with the index of the PMT in the address map of the corresponding module
1190  * (i.e.\ JDETECTOR::JModuleAddressMap::getIndex).\n
1191  * In that case, the method can be called before or after filling of the histogram.
1192  *
1193  * \param axis axis
1194  * \param memo module address map
1195  */
1196  inline void setAxisLabels(TAxis *axis, const JModuleAddressMap& memo)
1197  {
1198  using namespace JPP;
1199 
1200  if (axis->GetNbins() == (int) memo.size()) {
1201 
1202  for (int i = 0; i != axis->GetNbins(); ++i) {
1203 
1204  const JPMTPhysicalAddress& address = memo[i];
1205 
1206  axis->SetBinLabel(i + 1, address.toString().c_str());
1207  }
1208 
1209  } else {
1210 
1211  THROW(JValueOutOfRange, "Number of bins " << axis->GetNbins() << " != " << memo.size());
1212  }
1213  }
1214 
1215 
1216  /**
1217  * Set axis labels with PMT addresses.
1218  *
1219  * This methods sets the labels of the given axis to the sorted values of the PMT ring and position.\n
1220  * It should be called after filling of the corresponding histogram.\n
1221  * The filling should have been made with the PMT number (e.g.\ KM3NETDAQ::JDAQHit::getPMT).
1222  *
1223  * \param h1 histogram
1224  * \param axis axis <tt>(x, X, y, Y, z, Z)</tt>
1225  * \param memo module address map
1226  */
1227  inline void setAxisLabels(TH1& h1, const std::string axis, const JModuleAddressMap& memo)
1228  {
1229  using namespace JPP;
1230 
1231  TAxis* pax = NULL;
1232 
1233  if (axis == "x" || axis == "X")
1234  pax = h1.GetXaxis();
1235  else if (axis == "y" || axis == "Y")
1236  pax = h1.GetYaxis();
1237  else if (axis == "z" || axis == "Z")
1238  pax = h1.GetZaxis();
1239  else
1240  THROW(JValueOutOfRange, "Invalid axis " << axis);
1241 
1242  if (pax->GetNbins() == (int) memo.size()) {
1243 
1244  for (int i = 0; i != pax->GetNbins(); ++i) {
1245 
1246  const JPMTPhysicalAddress& address = memo.getAddressTranslator(i);
1247 
1248  pax->SetBinLabel(i + 1, address.toString().c_str());
1249  }
1250 
1251  h1.LabelsOption("a", axis.c_str()); // sort labels
1252 
1253  } else {
1254 
1255  THROW(JValueOutOfRange, "Number of bins " << pax->GetNbins() << " != " << memo.size());
1256  }
1257  }
1258 
1259 
1260  /**
1261  * Check if given key corresponds to a TObject.
1262  *
1263  * \param key ROOT key
1264  * \return true if given key corresponds to a TObject; else false
1265  */
1266  inline bool isTObject(const TKey* key)
1267  {
1268  return (key != NULL && TClass::GetClass(key->GetClassName())->IsTObject());
1269  }
1270 }
1271 
1272 #endif
static const char *const sqrt()
Set contents to signed difference between squares.
data_type w[N+1][M+1]
Definition: JPolint.hh:757
Exceptions.
TObject * getObject(const JRootObjectID &id)
Get first TObject with given identifier.
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
int getParameter(const std::string &text)
Get parameter number from text string.
static const char *const Divide()
ROOT TH1::Divide.
static const char *const Subtract()
ROOT TH1::Subtract.
Exception for a functional operation.
Definition: JException.hh:126
static const char *const SAME_AS_INPUT()
Set name of output histogram to name of input histogram.
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
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
Definition: JRoot.hh:19
static const char *const Replace()
Set contents to associated function.
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
void setLimits(TGraph &g1)
Set limits of TGraph.
static const char *const subtract()
Subtract contents with lookup bin in second histogram.
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
Auxiliary class to handle file name, ROOT directory and object name.
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
static const char *const stdev()
Set contents to standard deviation.
is
Definition: JDAQCHSM.chsm:167
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable STRING $argv[2] set_array QUANTILES set_variable FORMULA *[0] exp(-0.5 *(x-[1])*(x-[1])/([2]*[2]))" set_variable MODULE `getModule -a $DETECTOR -L "$STRING 0"` source JAcoustics.sh -- typeset -A TRIPODS get_tripods $WORKDIR/tripod.txt TRIPODS XMEAN
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
void setLogarithmic(TAxis *axis)
Make histogram axis logarithmic (e.g. after using log10()).
bool isTAttLine(const TObject *object)
Get drawing option of TH1.
static const char *const efficiency()
Divide contents and multiply errors with inefficiency.
static const char *const Add()
ROOT TH1::Add.
Lookup table for PMT addresses in optical module.
static const char *const multiply()
Multiply contents with lookup bin in second histogram.
TFile * getFile(const std::string &file_name, const std::string &option="exist")
Get TFile pointer corresponding to give file name.
static const char *const add()
Add contents with lookup bin in second histogram.
Auxiliary data structure for JOpera1D.cc and JOpera2D.cc applications.
void setLogarithmicX(TList *list)
Make x-axis of objects in list logarithmic (e.g. after using log10()).
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
void convertToPDF(TH1 &h1, const std::string &option="NW", const double factor=1.0)
Convert 1D histogram to PDF.
void setAxisLabels(TAxis *axis, const JModuleAddressMap &memo)
Set axis with PMT address labels.
T pow(const T &x, const double y)
Power .
Definition: JMath.hh:98
then break fi done getCenter read X Y Z let X
static const char *const Multiply()
ROOT TH1::Multiply.
static const char *const SAME_AS_OPERATION()
Set name of output histogram to name of operation.
void setLogarithmicY(TList *list)
Make y-axis of objects in list logarithmic (e.g. after using log10()).
void setRange(double &xmin, double &xmax, const bool logx)
Set axis range.
const JPMTAddressTranslator & getAddressTranslator(const int tdc) const
Get PMT address translator.
static const char *const divide()
Divide contents with lookup bin in second histogram.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
Exception for parsing value.
Definition: JException.hh:180
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
Definition: JeepToolkit.hh:88
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
TString getLogarithmic(const TString &formula, const char parameter)
Make given parameter in formula logarithmic (e.g. after using log10()).
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
static const char *const TIMESTAMP
Time stamp of earliest UTC time.
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25