Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JProject2D.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 #include <cmath>
7 
8 #include "TROOT.h"
9 #include "TFile.h"
10 #include "TKey.h"
11 #include "TH2.h"
12 #include "TProfile2D.h"
13 #include "TString.h"
14 #include "TRegexp.h"
15 
16 #include "JTools/JRange.hh"
17 
18 #include "JGizmo/JRootObjectID.hh"
19 #include "JGizmo/JGizmoToolkit.hh"
20 
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 /**
26  * \file
27  * Auxiliary program to project 2D histograms.
28  * \author mdejong
29  */
30 int main(int argc, char **argv)
31 {
32  using namespace std;
33  using namespace JPP;
34 
35  typedef JRange<double> JRange_t;
36 
37  vector<JRootObjectID> inputFile;
38  string outputFile;
41  char project;
42  bool overflow;
43  string format;
44  int debug;
45 
46  try {
47 
48  JParser<> zap("Auxiliary program to project 2D histograms.");
49 
50  zap['f'] = make_field(inputFile, "<input file>:<object name>");
51  zap['P'] = make_field(project, "projection") = ' ', 'x', 'X', 'y', 'Y';
52  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "project.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['O'] = make_field(overflow);
56  zap['F'] = make_field(format, "format, e.g. \"%s %i\" or \"%s %f %f\"") = "";
57  zap['d'] = make_field(debug) = 1;
58 
59  zap(argc, argv);
60  }
61  catch(const exception &error) {
62  FATAL(error.what() << endl);
63  }
64 
65  const bool px = (project == 'x' || project == 'X' || !Y.empty()); // projection on x-axis
66  const bool py = (project == 'y' || project == 'Y' || !X.empty()); // projection on y-axis
67 
68  if (px == py) {
69  FATAL("Invalid operation: "
70  << (px ? "" : "no") << " X projection " << " and "
71  << (py ? "" : "no") << " X projection " << endl);
72  }
73 
74  if (format == "") {
75 
76  format = "%s_";
77 
78  if (px) { format += "px"; }
79  if (py) { format += "py"; }
80 
81  if (X.empty() && Y.empty())
82  format += "[%i]";
83  else
84  format += "[%f,%f]";
85  }
86 
87  vector<TObject*> listOfObjects;
88 
89  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
90 
91  DEBUG("Input: " << *input << endl);
92 
93  TDirectory* dir = getDirectory(*input);
94 
95  if (dir == NULL) {
96  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
97  continue;
98  }
99 
100  const TRegexp regexp(input->getObjectName());
101 
102  TIter iter(dir->GetListOfKeys());
103 
104  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
105 
106  const TString tag(key->GetName());
107 
108  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
109 
110  // option match
111 
112  if (tag.Contains(regexp)) {
113 
114  TH2* h2 = dynamic_cast<TH2*>(key->ReadObj());
115 
116  if (h2 != NULL) {
117 
118  if (px) {
119 
120  if (Y.empty()) {
121 
122  for (Int_t i = (overflow ? 0 : 1); i <= h2->GetYaxis()->GetNbins() + (overflow ? 1 : 0); ++i) {
123  listOfObjects.push_back(h2->ProjectionX(TString::Format(format.c_str(), h2->GetName(), i),
124  i,
125  i));
126  }
127 
128  } else {
129 
130  for (Int_t i = 0; i != (Int_t) Y.size(); ++i) {
131  listOfObjects.push_back(h2->ProjectionX(TString::Format(format.c_str(), h2->GetName(), Y[i].getLowerLimit(), Y[i].getUpperLimit()),
132  h2->GetYaxis()->FindBin(Y[i].getLowerLimit()),
133  h2->GetYaxis()->FindBin(Y[i].getUpperLimit()) - 1));
134  }
135  }
136 
137  } else if (py) {
138 
139  if (X.empty()) {
140 
141  for (Int_t i = (overflow ? 0 : 1); i <= h2->GetXaxis()->GetNbins() + (overflow ? 1 : 0); ++i) {
142  listOfObjects.push_back(h2->ProjectionY(TString::Format(format.c_str(), h2->GetName(), i), i, i));
143  }
144 
145  } else {
146 
147  for (Int_t i = 0; i != (Int_t) X.size(); ++i) {
148  listOfObjects.push_back(h2->ProjectionY(TString::Format(format.c_str(), h2->GetName(), X[i].getLowerLimit(), X[i].getUpperLimit()),
149  h2->GetXaxis()->FindBin(X[i].getLowerLimit()),
150  h2->GetXaxis()->FindBin(X[i].getUpperLimit()) - 1));
151  }
152  }
153  }
154  }
155  }
156  }
157  }
158 
159  if (!listOfObjects.empty()) {
160 
161  TFile out(outputFile.c_str(), "recreate");
162 
163  for (vector<TObject*>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
164  (*i)->Write();
165  }
166 
167  out.Write();
168  out.Close();
169  }
170 }
Utility class to parse command line options.
Definition: JParser.hh:1493
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:63
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
#define ERROR(A)
Definition: JMessage.hh:66
int debug
debug level
Definition: JSirene.cc:61
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
JRange< Double_t > JRange_t
Definition: JFitToT.hh:34
Auxiliary class to define a range between two values.
Utility class to parse command line options.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int main(int argc, char *argv[])
Definition: Main.cpp:15