Jpp - 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 "TGraph2D.h"
18 #include "TMultiGraph.h"
19 #include "TString.h"
20 #include "TRegexp.h"
21 #include "TFormula.h"
22 #include "TF1.h"
23 #include "TF2.h"
24 #include "TIterator.h"
25 #include "TMethod.h"
26 #include "TMethodCall.h"
27 #include "TAxis.h"
28 #include "TMath.h"
29 
30 #include "JLang/JException.hh"
31 #include "JGizmo/JRootObjectID.hh"
32 
34 
35 
36 /**
37  * \author mdejong
38  */
39 
40 namespace JGIZMO {}
41 namespace JPP { using namespace JGIZMO; }
42 
43 /**
44  * Auxiliary applications for use of ROOT and more.
45  */
46 namespace JGIZMO {
47 
48  using JLANG::JParseError;
51 
52 
53  /**
54  * Auxiliary data structure for JOpera1D.cc and JOpera2D.cc applications.
55  */
56  struct JOpera {
57  //
58  // Histogram name.
59  //
60  static const char* const SAME_AS_OPERATION() { return "%"; } //!< Set name of output histogram to name of operation
61  static const char* const SAME_AS_INPUT() { return "="; } //!< Set name of output histogram to name of input histogram
62 
63  //
64  // Histogram operations.
65  //
66  static const char* const Add() { return "Add"; } //!< ROOT TH1::Add
67  static const char* const add() { return "add"; } //!< Add contents with lookup bin in second histogram
68  static const char* const Subtract() { return "Subtract"; } //!< ROOT TH1::Subtract
69  static const char* const subtract() { return "subtract"; } //!< Subtract contents with lookup bin in second histogram
70  static const char* const Multiply() { return "Multiply"; } //!< ROOT TH1::Multiply
71  static const char* const multiply() { return "multiply"; } //!< Multiply contents with lookup bin in second histogram
72  static const char* const Divide() { return "Divide"; } //!< ROOT TH1::Divide
73  static const char* const divide() { return "divide"; } //!< Divide contents with lookup bin in second histogram
74  static const char* const efficiency() { return "efficiency"; } //!< Divide contents and multiply errors with inefficiency
75  static const char* const stdev() { return "stdev"; } //!< Set contents to standard deviation
76  static const char* const sqrt() { return "sqrt"; } //!< Set contents to signed difference between squares
77  static const char* const Replace() { return "Replace"; } //!< Set contents to associated function
78  };
79 
80 
81  /**
82  * Time stamp of earliest UTC time.
83  */
84  static const char* const TIMESTAMP = "#splitline{}{#splitline{%d:%m:%y}{ %H:%M}}%F1970-01-01 00:00:00";
85 
86 
87  /**
88  * Get TFile pointer corresponding to give file name.
89  *
90  * The TFile pointer of an already opened file is recovered, else a new file is opened.\n
91  * Note that the closure of the opened files should be done by the caller of this method.
92  *
93  * \param file_name file name
94  * \param option TFile::Open option
95  * \return pointer to TFile
96  */
97  inline TFile* getFile(const std::string& file_name, const std::string& option = "exist")
98  {
99  using namespace std;
100 
101  gErrorIgnoreLevel = kError;
102 
103  static map<string, TFile*> zmap;
104 
105  map<string, TFile*>::iterator i = zmap.find(file_name);
106 
107  if (i == zmap.end() || i->second == NULL || !i->second->IsOpen()) {
108 
109  TFile* file = TFile::Open(file_name.c_str(), option.c_str());
110 
111  zmap[file_name] = file;
112 
113  return file;
114 
115  } else {
116 
117  return i->second;
118  }
119  }
120 
121 
122  /**
123  * Get TDirectory pointer.
124  *
125  * The TFile pointer of an already opened file is recovered, else a new file is opened.
126  *
127  * \param id identifier
128  * \return pointer to TDirectory
129  */
130  inline TDirectory* getDirectory(const JRootObjectID& id)
131  {
132  TFile* in = getFile(id.getFilename().c_str(), "exist");
133 
134  if (in == NULL || !in->IsOpen()) {
135  return NULL;
136  }
137 
138  if (id.getDirectory() != "")
139  return in->GetDirectory(id.getDirectory());
140  else
141  return in;
142  }
143 
144 
145  /**
146  * Get first TObject with given identifier.
147  *
148  * \param id identifier
149  * \return pointer to TObject (or NULL)
150  */
151  inline TObject* getObject(const JRootObjectID& id)
152  {
153  TDirectory* dir = getDirectory(id);
154 
155  if (dir != NULL) {
156 
157  const TRegexp regexp(id.getObjectName());
158 
159  TIter iter(dir->GetListOfKeys());
160 
161  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
162 
163  const TString tag(key->GetName());
164 
165  // option match
166 
167  if (tag.Index(regexp) != -1) {
168  return key->ReadObj();
169  }
170  }
171  }
172 
173  return NULL;
174  }
175 
176 
177  /**
178  * Get drawing option of TH1.
179  *
180  * \param object pointer to TObject
181  * \return true if TH1 looks like a line; else false
182  */
183  inline bool isTAttLine(const TObject* object)
184  {
185  const TH1* h1 = dynamic_cast<const TH1*>(object);
186 
187  if (h1 != NULL) {
188 
189  if (h1->GetSumw2N()) {
190 
191  for (Int_t i = 1; i <= h1->GetNbinsX(); ++i) {
192 
193  if (h1->GetBinError(i) != 0.0) {
194  return false;
195  }
196  }
197  }
198 
199  return true;
200 
201  } else {
202 
203  return false;
204  }
205  }
206 
207 
208  /**
209  * Get result of given textual formula.
210  *
211  * The formula may contain names of member methods of the object pointed to.\n
212  * These methods could have arguments and the return type should be compatible with <tt>Double_t</tt>.\n
213  * Example:
214  * <pre>
215  * getResult("1.0/GetEntries", TH1*);
216  * </pre>
217  *
218  * \param text text
219  * \param object pointer to object
220  * \return value
221  */
222  inline Double_t getResult(const TString& text, TObject* object = NULL)
223  {
224  TString buffer(text);
225 
226  if (object != NULL) {
227 
228  TClass* p = TClass::GetClass(object->ClassName());
229 
230  if (p != NULL) {
231 
232  for ( ; ; ) {
233 
234  TIterator* iter = p->GetListOfAllPublicMethods()->MakeIterator();
235  TMethod* method = NULL;
236 
237  for (TMethod* p; (p = (TMethod*) iter->Next()) != NULL; ) {
238  if (buffer.Index(p->GetName()) != -1) {
239  if (method == NULL || strlen(p->GetName()) > strlen(method->GetName())) {
240  method = p;
241  }
242  }
243  }
244 
245  if (method == NULL) {
246  break;
247  }
248 
249  for (Ssiz_t index; (index = buffer.Index(method->GetName())) != -1; ) {
250 
251  const TRegexp fp(" *([^)]*)"); // function call
252 
253  Ssiz_t len;
254  Ssiz_t pos = buffer.Index(fp, &len, index);
255 
256  Double_t value;
257 
258  if (pos == -1 || pos != index + (Ssiz_t) strlen(method->GetName())) {
259 
260  TMethodCall(p, method->GetName(), NULL).Execute(object, value);
261 
262  len = strlen(method->GetName());
263 
264  } else {
265 
266  TMethodCall(p, method->GetName(), NULL).Execute(object, TString(buffer(pos + 1, len - 2)), value);
267 
268  len += strlen(method->GetName());
269  }
270 
271  buffer.Replace(index, len, TString::Format("%15.5e", value));
272  }
273  }
274  }
275  }
276 
277  return TFormula("/tmp", buffer.Data()).Eval(0.0);
278  }
279 
280 
281  /**
282  * Get result of given textual formula.
283  *
284  * The formula may contain names of member methods of the object pointed to.\n
285  * These methods could have arguments and the return type should be compatible with <tt>Double_t</tt>.\n
286  * Example:
287  * <pre>
288  * getResult("1.0/GetEntries", TH1*);
289  * </pre>
290  *
291  * \param text text
292  * \param object pointer to object
293  * \return value
294  */
295  inline Double_t getResult(const std::string& text, TObject* object = NULL)
296  {
297  return getResult(TString(text.c_str()), object);
298  }
299 
300 
301  /**
302  * Get parameter number from text string.
303  *
304  * The number corresponds to the value <tt>[0-9]*</tt> in the expression <tt>"p[0-9]* = .."</tt>.
305  *
306  * \param text text
307  * \return parameter number
308  */
309  inline int getParameter(const std::string& text)
310  {
311  const char* regexp("p[0-9]* *=");
312 
313  TString buffer(text.c_str());
314 
315  buffer = buffer(TRegexp(regexp));
316  buffer = buffer(1, buffer.Length() - 2);
317 
318  if (!buffer.IsDigit()) {
319  THROW(JParseError, "Text is not a number " << text << ' ' << regexp);
320  }
321 
322  return buffer.Atoi();
323  }
324 
325 
326  /**
327  * Get parameter value from text string.
328  *
329  * The formula may contain names of member methods of the object pointed to.\n
330  * These methods could have arguments and the return type should be compatible with <tt>Double_t</tt>.\n
331  * Example:
332  * <pre>
333  * getValue("p[..] = 2*GetMaximum", TH1*);
334  * </pre>
335  *
336  * \param text text
337  * \param object pointer to object
338  * \return value
339  */
340  inline Double_t getValue(const std::string& text, TObject* object = NULL)
341  {
342  const char* regexp("=.*");
343 
344  TString buffer(text.c_str());
345 
346  buffer = buffer(TRegexp(regexp));
347  buffer = buffer(1, buffer.Length() - 1);
348 
349  return getResult(std::string(buffer), object);
350  }
351 
352 
353  /**
354  * Get parameter value from text string.
355  *
356  * The formula may contain names of member methods of the object pointed to.\n
357  * These methods could have arguments and the return type should be compatible with <tt>Double_t</tt>.\n
358  * Example:
359  * <pre>
360  * getValue("p[..] = 1.0 2.0 3.0, 1);
361  * </pre>
362  * will return <tt>2.0</tt>.
363  *
364  * \param text text
365  * \param index index
366  * \return value
367  */
368  inline Double_t getValue(const std::string& text, const int index)
369  {
370  using namespace std;
371 
372  const char* regexp("=.*");
373 
374  TString buffer(text.c_str());
375 
376  buffer = buffer(TRegexp(regexp));
377  buffer = buffer(1, buffer.Length() - 1);
378 
379 
380  istringstream is((std::string(buffer)));
381 
382  Double_t value;
383 
384  for (int i = index; is >> value && i > 0; --i) {}
385 
386  if (is)
387  return value;
388  else
389  THROW(JParseError, "Text des not contain a number at given position " << buffer << ' ' << index);
390  }
391 
392 
393  /**
394  * Make histogram axis logarithmic (e.g.\ after filling with log10()).
395  *
396  * \param axis axis
397  */
398  inline void setLogarithmic(TAxis* axis)
399  {
400  if (axis != NULL) {
401 
402  const Int_t first = axis->GetFirst();
403  const Int_t last = axis->GetLast();
404 
405  const Double_t xmin = axis->GetBinLowEdge(first);
406  const Double_t xmax = axis->GetBinLowEdge(last) + axis->GetBinWidth(last);
407 
408  const Int_t N = axis->GetNbins();
409  Double_t buffer[N+1];
410 
411  buffer[0] = TMath::Power(10.0, axis->GetBinLowEdge(1));
412 
413  for (Int_t i = 1; i <= N; ++i) {
414  buffer[i] = TMath::Power(10.0, axis->GetBinLowEdge(i) + axis->GetBinWidth(i));
415  }
416 
417  axis->Set(N, buffer);
418 
419  if (axis->TestBit(TAxis::kAxisRange)) {
420  axis->SetRangeUser(TMath::Power(10.0, xmin), TMath::Power(10.0, xmax));
421  }
422  }
423  }
424 
425 
426  /**
427  * Make given parameter in formula logarithmic (e.g.\ after filling with log10()).
428  *
429  * \param formula formula
430  * \param parameter parameter
431  * \return formula
432  */
433  inline TString getLogarithmic(const TString& formula, const char parameter)
434  {
435  const TRegexp regexp[] = {
436  TString("^") + TString(parameter) + TString("[^a-zA-Z_]"), // parameter at start of line
437  TString("[^a-zA-Z_]") + TString(parameter) + TString("[^a-zA-Z_]"), // parameter in middle of line
438  TString("[^a-zA-Z_]") + TString(parameter) + TString("$") // parameter at end of line
439  };
440 
441  const TString replacement = TString("log10(") + TString(parameter) + TString(")");
442 
443  TString buffer(formula);
444 
445  if (buffer.Length() == 1 && buffer[0] == parameter) {
446 
447  buffer = replacement;
448 
449  } else {
450 
451  for (Ssiz_t pos = 0, i; pos < buffer.Length(); pos += replacement.Length()) {
452  if ((i = buffer.Index(regexp[0], pos)) != -1) { buffer.Replace((pos = i + 0), 1, replacement); }
453  else if ((i = buffer.Index(regexp[1], pos)) != -1) { buffer.Replace((pos = i + 1), 1, replacement); }
454  else if ((i = buffer.Index(regexp[2], pos)) != -1) { buffer.Replace((pos = i + 1), 1, replacement); }
455  else { break; }
456  }
457  }
458  return buffer;
459  }
460 
461 
462  /**
463  * Copy function parameters.
464  *
465  * \param from function
466  * \param to function
467  */
468  inline void copy(const TF1& from, TF1& to)
469  {
470  static_cast<TAttLine&> (to) = static_cast<const TAttLine&> (from);
471  static_cast<TAttFill&> (to) = static_cast<const TAttFill&> (from);
472  static_cast<TAttMarker&>(to) = static_cast<const TAttMarker&>(from);
473 
474  to.SetParameters(from.GetParameters());
475 
476  to.SetNpx(from.GetNpx());
477  }
478 
479 
480  /**
481  * Copy function parameters.
482  *
483  * \param from function
484  * \param to function
485  */
486  inline void copy(const TF2& from, TF2& to)
487  {
488  copy(static_cast<const TF1&>(from), static_cast<TF1&>(to));
489 
490  to.SetNpy(from.GetNpy());
491  }
492 
493 
494  /**
495  * Make parameter <tt>x</tt> of function logarithmic (e.g.\ after filling with log10()).
496  *
497  * \param f1 function
498  */
499  inline void setLogarithmicX(TF1* f1)
500  {
501  if (f1 != NULL) {
502 
503  TF1 fn(f1->GetName(), getLogarithmic(f1->GetExpFormula(), 'x'));
504 
505  copy(*f1, fn);
506 
507  fn.SetRange(f1->GetXmin(),
508  f1->GetXmax());
509 
510  *f1 = fn;
511 
512  f1->SetRange(TMath::Power(10.0,f1->GetXmin()),
513  TMath::Power(10.0,f1->GetXmax()));
514  }
515  }
516 
517 
518  /**
519  * Make parameter <tt>x</tt> of function logarithmic (e.g.\ after filling with log10()).
520  *
521  * \param f2 function
522  */
523  inline void setLogarithmicX(TF2* f2)
524  {
525  if (f2 != NULL) {
526 
527  TF2 fn(f2->GetName(), getLogarithmic(f2->GetExpFormula(), 'x'));
528 
529  copy(*f2, fn);
530 
531  fn.SetRange(f2->GetXmin(),
532  f2->GetYmin(),
533  f2->GetXmax(),
534  f2->GetYmax());
535 
536  *f2 = fn;
537 
538  f2->SetRange(TMath::Power(10.0,f2->GetXmin()),
539  f2->GetYmin(),
540  TMath::Power(10.0,f2->GetXmax()),
541  f2->GetYmax());
542  }
543  }
544 
545 
546  /**
547  * Make parameter <tt>y</tt> of function logarithmic (e.g.\ after filling with log10()).
548  *
549  * \param f2 function
550  */
551  inline void setLogarithmicY(TF2* f2)
552  {
553  if (f2 != NULL) {
554 
555  TF2 fn(f2->GetName(), getLogarithmic(f2->GetExpFormula(), 'y'));
556 
557  copy(*f2, fn);
558 
559  fn.SetRange(f2->GetXmin(),
560  f2->GetYmin(),
561  f2->GetXmax(),
562  f2->GetYmax());
563 
564  *f2 = fn;
565 
566  f2->SetRange(f2->GetXmin(),
567  TMath::Power(10.0,f2->GetYmin()),
568  f2->GetXmax(),
569  TMath::Power(10.0,f2->GetYmax()));
570  }
571  }
572 
573 
574  /**
575  * Make X-axis and associated functions of given histogram logarithmic (e.g.\ after filling with log10()).
576  *
577  * \param h1 histogram
578  */
579  inline void setLogarithmicX(TH1* h1)
580  {
581  if (h1 != NULL) {
582 
583  for (TIter iter(h1->GetListOfFunctions()); TF1* f1 = dynamic_cast<TF1*>(iter.Next()); ) {
584  setLogarithmicX(f1);
585  }
586 
587  setLogarithmic(h1->GetXaxis());
588  }
589  }
590 
591 
592  /**
593  * Make X-axis and associated functions of given histogram logarithmic (e.g.\ after filling with log10()).
594  *
595  * \param h2 histogram
596  */
597  inline void setLogarithmicX(TH2* h2)
598  {
599  using namespace std;
600 
601  if (h2 != NULL) {
602 
603  for (TIter iter(h2->GetListOfFunctions()); TF2* f2 = dynamic_cast<TF2*>(iter.Next()); ) {
604  setLogarithmicX(f2);
605  }
606 
607  setLogarithmic(h2->GetXaxis());
608  }
609  }
610 
611 
612  /**
613  * Make Y-axis and associated functions of given histogram logarithmic (e.g.\ after filling with log10()).
614  *
615  * \param h2 histogram
616  */
617  inline void setLogarithmicY(TH2* h2)
618  {
619  if (h2 != NULL) {
620 
621  for (TIter iter(h2->GetListOfFunctions()); TF2* f2 = dynamic_cast<TF2*>(iter.Next()); ) {
622  setLogarithmicY(f2);
623  }
624 
625  setLogarithmic(h2->GetYaxis());
626  }
627  }
628 
629 
630  /**
631  * Make parameter <tt>x</tt> of graph logarithmic (e.g.\ after filling with log10()).
632  *
633  * \param g1 graph
634  */
635  inline void setLogarithmicX(TGraph* g1)
636  {
637  if (g1 != NULL) {
638 
639  for (TIter iter(g1->GetListOfFunctions()); TF1* f1 = dynamic_cast<TF1*>(iter.Next()); ) {
640  setLogarithmicX(f1);
641  }
642 
643  for (Int_t i = 0; i != g1->GetN(); ++i) {
644  g1->GetX()[i] = pow(10.0, g1->GetX()[i]);
645  }
646  }
647  }
648 
649 
650  /**
651  * Make parameter <tt>x</tt> of graph logarithmic (e.g.\ after filling with log10()).
652  *
653  * \param g2 graph
654  */
655  inline void setLogarithmicX(TGraph2D* g2)
656  {
657  if (g2 != NULL) {
658 
659  for (TIter iter(g2->GetListOfFunctions()); TF2* f2 = dynamic_cast<TF2*>(iter.Next()); ) {
660  setLogarithmicX(f2);
661  }
662 
663  for (Int_t i = 0; i != g2->GetN(); ++i) {
664  g2->GetX()[i] = pow(10.0, g2->GetX()[i]);
665  }
666  }
667  }
668 
669 
670  /**
671  * Make parameter <tt>y</tt> of graph logarithmic (e.g.\ after filling with log10()).
672  *
673  * \param g2 graph
674  */
675  inline void setLogarithmicY(TGraph2D* g2)
676  {
677  if (g2 != NULL) {
678 
679  for (TIter iter(g2->GetListOfFunctions()); TF2* f2 = dynamic_cast<TF2*>(iter.Next()); ) {
680  setLogarithmicY(f2);
681  }
682 
683  for (Int_t i = 0; i != g2->GetN(); ++i) {
684  g2->GetY()[i] = pow(10.0, g2->GetY()[i]);
685  }
686  }
687  }
688 
689 
690  /**
691  * Make parameter <tt>x</tt> of multi graph logarithmic (e.g.\ after filling with log10()).
692  *
693  * \param m1 multi graph
694  */
695  inline void setLogarithmicX(TMultiGraph* m1)
696  {
697  if (m1 != NULL) {
698 
699  for (TIter i1(m1->GetListOfGraphs()); TGraph* g1 = dynamic_cast<TGraph*>(i1()); ) {
700  setLogarithmicX(g1);
701  }
702  }
703  }
704 
705 
706  /**
707  * Convert 1D histogram to PDF.
708  *
709  * Possible options are:
710  * - N normalise to histogram contents;
711  * - W divide by bin width;
712  * - E convert also bin errors.
713  *
714  * \param h1 histogram
715  * \param option option
716  * \param factor scaling factor
717  */
718  inline void convertToPDF(TH1& h1, const std::string& option = "NW", const double factor = 1.0)
719  {
720  using namespace std;
721 
722  const bool normalise = (option.find('N') != string::npos || option.find('n') != string::npos);
723  const bool bin_width = (option.find('W') != string::npos || option.find('w') != string::npos);
724  const bool use_error = (option.find('E') != string::npos || option.find('e') != string::npos);
725 
726  Double_t W = 1.0;
727 
728  if (normalise) {
729 
730  W = 0.0;
731 
732  for (Int_t i = 1; i <= h1.GetXaxis()->GetNbins(); ++i) {
733  W += h1.GetBinContent(i);
734  }
735  }
736 
737  if (W != 0.0) {
738 
739  for (Int_t i = 1; i <= h1.GetXaxis()->GetNbins(); ++i) {
740 
741  const Double_t w = W * (bin_width ? h1.GetXaxis()->GetBinWidth(i) : 1.0);
742 
743  h1.SetBinContent(i, h1.GetBinContent(i) * factor / w);
744 
745  if (use_error) {
746  h1.SetBinError(i, h1.GetBinError(i) * factor / w);
747  }
748  }
749  }
750  }
751 
752 
753  /**
754  * Convert 2D histogram to PDF.
755  *
756  * Possible options are:
757  * - N normalise to histogram contents;
758  * - X convert x-axis to PDF;
759  * - Y convert y-axis to PDF;
760  * - W divide by bin width;
761  * - E convert also bin errors.
762  *
763  * \param h2 histogram
764  * \param option option
765  * \param factor scaling factor
766  */
767  inline void convertToPDF(TH2& h2, const std::string& option = "NXYW", const double factor = 1.0)
768  {
769  using namespace std;
770 
771  const bool normalise = (option.find('N') != string::npos || option.find('n') != string::npos);
772  const bool X = (option.find('X') != string::npos || option.find('x') != string::npos);
773  const bool Y = (option.find('Y') != string::npos || option.find('y') != string::npos);
774  const bool bin_width = (option.find('W') != string::npos || option.find('w') != string::npos);
775  const bool use_error = (option.find('E') != string::npos || option.find('e') != string::npos);
776 
777  Double_t W = 1.0;
778 
779  if (X && Y) {
780 
781  if (normalise) {
782 
783  W = 0.0;
784 
785  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
786  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
787  W += h2.GetBinContent(ix,iy);
788  }
789  }
790  }
791 
792  if (W != 0.0) {
793 
794  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
795  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
796 
797  const Double_t w = W * (bin_width ? h2.GetXaxis()->GetBinWidth(ix) * h2.GetYaxis()->GetBinWidth(iy) : 1.0);
798 
799  h2.SetBinContent(ix, iy, h2.GetBinContent(ix,iy) * factor / w);
800 
801  if (use_error) {
802  h2.SetBinError(ix, iy, h2.GetBinError(ix,iy) * factor / w);
803  }
804  }
805  }
806  }
807 
808  } else if (X) {
809 
810  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
811 
812  if (normalise) {
813 
814  W = 0.0;
815 
816  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
817  W += h2.GetBinContent(ix,iy);
818  }
819  }
820 
821  if (W != 0.0) {
822 
823  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
824 
825  const Double_t w = W * (bin_width ? h2.GetXaxis()->GetBinWidth(ix) : 1.0);
826 
827  h2.SetBinContent(ix, iy, h2.GetBinContent(ix,iy) * factor / w);
828 
829  if (use_error) {
830  h2.SetBinError(ix, iy, h2.GetBinError(ix,iy) * factor / w);
831  }
832  }
833  }
834  }
835 
836  } else if (Y) {
837 
838  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
839 
840  if (normalise) {
841 
842  W = 0.0;
843 
844  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
845  W += h2.GetBinContent(ix,iy);
846  }
847  }
848 
849  if (W != 0.0) {
850 
851  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
852 
853  const Double_t w = W * (bin_width ? h2.GetYaxis()->GetBinWidth(iy) : 1.0);
854 
855  h2.SetBinContent(ix, iy, h2.GetBinContent(ix,iy) / w);
856 
857  if (use_error) {
858  h2.SetBinError(ix, iy, h2.GetBinError(ix,iy) / w);
859  }
860  }
861  }
862  }
863  }
864  }
865 
866 
867  /**
868  * Set limits of TGraph.
869  *
870  * \param g1 graph
871  */
872  inline void setLimits(TGraph& g1)
873  {
874  using namespace std;
875 
876  Double_t ymin = numeric_limits<Double_t>::max();
877  Double_t ymax = numeric_limits<Double_t>::lowest();
878 
879  for (Int_t i = 0; i != g1.GetN(); ++i) {
880 
881  const Double_t y = g1.GetY()[i];
882 
883  if (y > ymax) { ymax = y; }
884  if (y < ymin) { ymin = y; }
885  }
886 
887  g1.SetMinimum(ymin);
888  g1.SetMaximum(ymax);
889  }
890 
891 
892  /**
893  * Set limits of TGraph2D.
894  *
895  * \param g2 graph
896  */
897  inline void setLimits(TGraph2D& g2)
898  {
899  using namespace std;
900 
901  Double_t zmin = numeric_limits<Double_t>::max();
902  Double_t zmax = numeric_limits<Double_t>::lowest();
903 
904  for (Int_t i = 0; i != g2.GetN(); ++i) {
905 
906  const Double_t z = g2.GetZ()[i];
907 
908  if (z > zmax) { zmax = z; }
909  if (z < zmin) { zmin = z; }
910  }
911 
912  g2.SetMinimum(zmin);
913  g2.SetMaximum(zmax);
914  }
915 
916 
917  /**
918  * Set axis range.
919  *
920  * \param xmin lower limit (I/O)
921  * \param xmax upper limit (I/O)
922  * \param logx logarithmic
923  */
924  inline void setRange(double& xmin,
925  double& xmax,
926  const bool logx)
927  {
928  if (logx) {
929  xmin = log(xmin);
930  xmax = log(xmax);
931  }
932 
933  double dx = (xmax - xmin) * 0.1;
934 
935  if (xmin > dx || xmin < 0.0)
936  xmin -= dx;
937  else
938  xmin = 0.0;
939 
940  xmax += dx;
941 
942  if (logx) {
943  xmin = exp(xmin);
944  xmax = exp(xmax);
945  }
946  }
947 
948 
949  /**
950  * Set axis with PMT address labels.
951  *
952  * This methods sets the labels of the given axis to the sorted values of the PMT ring and position.\n
953  * It should normally be called before filling of the corresponding histogram.\n
954  * The filling should then be made with the textual representation of the PMT ring and position (i.e.\ JDETECTOR::JPMTPhysicalAddress::toString).
955  *
956  * Alternatively, the filling can be made with the index of the PMT in the address map of the corresponding module
957  * (i.e.\ JDETECTOR::JModuleAddressMap::getIndex).\n
958  * In that case, the method can be called before or after filling of the histogram.
959  *
960  * \param axis axis
961  * \param memo module address map
962  */
963  inline void setAxisLabels(TAxis *axis, const JModuleAddressMap& memo)
964  {
965  using namespace JPP;
966 
967  if (axis->GetNbins() == (int) memo.size()) {
968 
969  for (int i = 0; i != axis->GetNbins(); ++i) {
970 
971  const JPMTPhysicalAddress& address = memo[i];
972 
973  axis->SetBinLabel(i + 1, address.toString().c_str());
974  }
975 
976  } else {
977 
978  THROW(JValueOutOfRange, "Number of bins " << axis->GetNbins() << " != " << memo.size());
979  }
980  }
981 
982 
983  /**
984  * Set axis labels with PMT addresses.
985  *
986  * This methods sets the labels of the given axis to the sorted values of the PMT ring and position.\n
987  * It should be called after filling of the corresponding histogram.\n
988  * The filling should have been made with the PMT number (e.g.\ KM3NETDAQ::JDAQHit::getPMT).
989  *
990  * \param h1 histogram
991  * \param axis axis <tt>(x, X, y, Y, z, Z)</tt>
992  * \param memo module address map
993  */
994  inline void setAxisLabels(TH1& h1, const std::string axis, const JModuleAddressMap& memo)
995  {
996  using namespace JPP;
997 
998  TAxis* pax = NULL;
999 
1000  if (axis == "x" || axis == "X")
1001  pax = h1.GetXaxis();
1002  else if (axis == "y" || axis == "Y")
1003  pax = h1.GetYaxis();
1004  else if (axis == "z" || axis == "Z")
1005  pax = h1.GetZaxis();
1006  else
1007  THROW(JValueOutOfRange, "Invalid axis " << axis);
1008 
1009  if (pax->GetNbins() == (int) memo.size()) {
1010 
1011  for (int i = 0; i != pax->GetNbins(); ++i) {
1012 
1013  const JPMTPhysicalAddress& address = memo.getAddressTranslator(i);
1014 
1015  pax->SetBinLabel(i + 1, address.toString().c_str());
1016  }
1017 
1018  h1.LabelsOption("a", axis.c_str()); // sort labels
1019 
1020  } else {
1021 
1022  THROW(JValueOutOfRange, "Number of bins " << pax->GetNbins() << " != " << memo.size());
1023  }
1024  }
1025 }
1026 
1027 #endif
static const char *const sqrt()
Set contents to signed difference between squares.
data_type w[N+1][M+1]
Definition: JPolint.hh:741
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.
static const char *const SAME_AS_INPUT()
Set name of output histogram to name of input histogram.
char text[TEXT_SIZE]
Definition: elog.cc:72
void setLogarithmicX(TF1 *f1)
Make parameter x of function logarithmic (e.g. after filling with log10()).
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
Definition: JRoot.hh:19
static const char *const Replace()
Set contents to associated function.
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:69
Auxiliary class to handle file name, ROOT directory and object name.
then usage $script< string identifier >< detectorfile > input file(toashort file)+" "\nNote that the input files and toashort files should be one-to-one related." fi if (( $
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 set_variable FORMULA *[0] exp(-0.5 *(x-[1])*(x-[1])/([2]*[2]))*exp(-0.5 *(y-[1])*(y-[1])/([2]*[2]))" JF2 -o $WORKDIR/f2.root -F "$FORMULA" -@ "p0
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 filling with 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.
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
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 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
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
void setLogarithmicY(TF2 *f2)
Make parameter y of function logarithmic (e.g. after filling with log10()).
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 filling with 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:38
static const char *const TIMESTAMP
Time stamp of earliest UTC time.
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