Jpp  19.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JProject2D.cc File Reference

Auxiliary program to project 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 project 2D histograms.

Author
mdejong

Definition in file JProject2D.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 30 of file JProject2D.cc.

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