Jpp  master_rocky-43-ge265d140c
the software that should make you happy
Functions
JProfile2D.cc File Reference

Auxiliary program to profile 2D histograms. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include "TROOT.h"
#include "TFile.h"
#include "TKey.h"
#include "TH2.h"
#include "TProfile2D.h"
#include "TString.h"
#include "TRegexp.h"
#include "JTools/JRange.hh"
#include "JGizmo/JRootObjectID.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to profile 2D histograms.

Author
mdejong, adomi

Definition in file JProfile2D.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 29 of file JProfile2D.cc.

30 {
31  using namespace std;
32  using namespace JPP;
33 
34  typedef JRange<double> JRange_t;
35 
36  vector<JRootObjectID> inputFile;
37  string outputFile;
40  char profile;
41  bool overflow;
42  string format;
43  string option;
44  int debug;
45 
46  try {
47 
48  JParser<> zap("Auxiliary program to profile 2D histograms.");
49 
50  zap['f'] = make_field(inputFile, "<input file>:<object name>");
51  zap['P'] = make_field(profile, "profiling") = ' ', 'x', 'X', 'y', 'Y';
52  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "profile.root";
53  zap['x'] = make_field(X, "x-abscissa ranges") = JPARSER::initialised();
54  zap['y'] = make_field(Y, "y-abscissa ranges") = JPARSER::initialised();
55  zap['+'] = make_field(overflow);
56  zap['F'] = make_field(format, "format, e.g. \"%s %i\" or \"%s %f %f\"") = "";
57  zap['O'] = make_field(option, "option, see TH2::Profile(X|Y)") = "", "s", "i", "g";
58  zap['d'] = make_field(debug) = 1;
59 
60  zap(argc, argv);
61  }
62  catch(const exception &error) {
63  FATAL(error.what() << endl);
64  }
65 
66  const bool px = (profile == 'x' || profile == 'X' || !Y.empty()); // profiling on x-axis
67  const bool py = (profile == 'y' || profile == 'Y' || !X.empty()); // profiling on y-axis
68 
69  if (px == py) {
70  FATAL("Invalid operation: "
71  << (px ? "" : "no") << " X profiling " << " and "
72  << (py ? "" : "no") << " Y profiling " << endl);
73  }
74 
75  if (format == "") {
76 
77  format = "%s_";
78 
79  if (px) { format += "px"; }
80  if (py) { format += "py"; }
81 
82  if (X.empty() && Y.empty())
83  format += "[%i]";
84  else
85  format += "[%f,%f]";
86  }
87 
88  vector<TObject*> listOfObjects;
89 
90  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
91 
92  DEBUG("Input: " << *input << endl);
93 
94  TDirectory* dir = getDirectory(*input);
95 
96  if (dir == NULL) {
97  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
98  continue;
99  }
100 
101  const TRegexp regexp(input->getObjectName());
102 
103  TIter iter(dir->GetListOfKeys());
104 
105  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
106 
107  const TString tag(key->GetName());
108 
109  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
110 
111  // option match
112 
113  if (tag.Contains(regexp) && isTObject(key)) {
114 
115  TH2* h2 = dynamic_cast<TH2*>(key->ReadObj());
116 
117  if (h2 != NULL) {
118 
119  if (px) {
120 
121  if (Y.empty()) {
122 
123  listOfObjects.push_back(h2->ProfileX(TString::Format(format.c_str(), h2->GetName()),
124  (overflow ? 0 : 1),
125  h2->GetYaxis()->GetNbins() + (overflow ? 1 : 0), option.c_str()));
126 
127  } else {
128 
129  for (Int_t i = 0; i != (Int_t) Y.size(); ++i) {
130  listOfObjects.push_back(h2->ProfileX(TString::Format(format.c_str(), h2->GetName(),
131  Y[i].getLowerLimit(),
132  Y[i].getUpperLimit()),
133  h2->GetYaxis()->FindBin(Y[i].getLowerLimit()),
134  h2->GetYaxis()->FindBin(Y[i].getUpperLimit()) - 1, option.c_str()));
135 
136  }
137  }
138 
139  } else if (py) {
140 
141  if (X.empty()) {
142 
143  listOfObjects.push_back(h2->ProfileY(TString::Format(format.c_str(), h2->GetName()),
144  (overflow ? 0 : 1),
145  h2->GetXaxis()->GetNbins() + (overflow ? 1 : 0), option.c_str()));
146 
147  }
148 
149 
150  } else {
151 
152  for (Int_t i = 0; i != (Int_t) Y.size(); ++i) {
153 
154  listOfObjects.push_back(h2->ProfileY(TString::Format(format.c_str(), h2->GetName(),
155  X[i].getLowerLimit(),
156  X[i].getUpperLimit()),
157  h2->GetXaxis()->FindBin(X[i].getLowerLimit()),
158  h2->GetXaxis()->FindBin(X[i].getUpperLimit()) - 1, option.c_str()));
159 
160  }
161  }
162  }
163  }
164  }
165  }
166 
167  if (!listOfObjects.empty()) {
168 
169  TFile out(outputFile.c_str(), "recreate");
170 
171  for (vector<TObject*>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
172  (*i)->Write();
173  }
174 
175  out.Write();
176  out.Close();
177  }
178 }
string outputFile
#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
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
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
Type definition of range.
Definition: JHead.hh:43
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:68