Jpp  19.1.0
the software that should make you happy
JPrintMaximum2D.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <limits>
5 
6 #include "TROOT.h"
7 #include "TFile.h"
8 #include "TClass.h"
9 #include "TApplication.h"
10 #include "TKey.h"
11 #include "TRegexp.h"
12 #include "TH2.h"
13 #include "TGraph2D.h"
14 #include "TProfile2D.h"
15 
16 #include "JGizmo/JRootObjectID.hh"
17 #include "JGizmo/JGizmoToolkit.hh"
18 
19 #include "Jeep/JPrint.hh"
20 #include "Jeep/JParser.hh"
21 #include "Jeep/JMessage.hh"
22 
23 
24 /**
25  * \file
26  * Auxiliary program to print (x,y) of the maximum of 2D ROOT objects.
27  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
28  * \author mdejong
29  */
30 int main(int argc, char **argv)
31 {
32  using namespace std;
33  using namespace JPP;
34 
35  vector<JRootObjectID> inputFile;
36  int debug;
37 
38  try {
39 
40  JParser<> zap("Auxiliary program to print (x,y) of the maximum of 2D ROOT objects.");
41 
42  zap['f'] = make_field(inputFile, "<input file>:<object name>");
43  zap['d'] = make_field(debug) = 0;
44 
45  zap(argc, argv);
46  }
47  catch(const exception &error) {
48  FATAL(error.what() << endl);
49  }
50 
51 
52  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
53 
54  DEBUG("Input: " << *input << endl);
55 
56  TDirectory* dir = getDirectory(*input);
57 
58  if (dir == NULL) {
59  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
60  continue;
61  }
62 
63  const TRegexp regexp(input->getObjectName());
64 
65  TIter iter(dir->GetListOfKeys());
66 
67  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
68 
69  const TString tag(key->GetName());
70 
71  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
72 
73  // option match
74 
75  if (tag.Contains(regexp) && isTObject(key)) {
76 
77  TObject* object = key->ReadObj();
78 
79  double x = 0.0;
80  double y = 0.0;
81  double zmax = numeric_limits<double>::lowest();
82 
83  try {
84 
85  TH2& h2 = dynamic_cast<TH2&>(*object);
86 
87  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
88  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
89 
90  double z = h2.GetBinContent(ix,iy);
91 
92  if (z > zmax) {
93  zmax = z;
94  x = h2.GetXaxis()->GetBinCenter(ix);
95  y = h2.GetYaxis()->GetBinCenter(iy);
96  }
97  }
98  }
99  }
100  catch(exception&) {}
101 
102  try {
103 
104  TProfile2D& h2 = dynamic_cast<TProfile2D&>(*object);
105 
106  for (Int_t ix = 1; ix <= h2.GetXaxis()->GetNbins(); ++ix) {
107  for (Int_t iy = 1; iy <= h2.GetYaxis()->GetNbins(); ++iy) {
108 
109  double z = h2.GetBinContent(ix,iy);
110 
111  if (z > zmax) {
112  zmax = z;
113  x = h2.GetXaxis()->GetBinCenter(ix);
114  y = h2.GetYaxis()->GetBinCenter(iy);
115  }
116  }
117  }
118  }
119  catch(exception&) {}
120 
121  try {
122 
123  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*object);
124 
125  for (Int_t i = 0; i != g2.GetN(); ++i) {
126  if (g2.GetZ()[i] > zmax) {
127  zmax = g2.GetZ()[i];
128  x = g2.GetX()[i];
129  y = g2.GetY()[i];
130  }
131  }
132  }
133  catch(exception&) {}
134 
135  if (zmax != numeric_limits<double>::lowest()) {
136  cout << setw(32) << left << key->GetName() << right << ' ' << SCIENTIFIC(15,5) << x << ' ' << SCIENTIFIC(15,5) << y << endl;
137  }
138  }
139  }
140  }
141 }
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:66
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
int main(int argc, char **argv)
I/O formatting auxiliaries.
Utility class to parse command line options.
Definition: JParser.hh:1698
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:488
Definition: JRoot.hh:19