Jpp  18.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JConvertToPDF1D.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TKey.h"
10 #include "TH1.h"
11 #include "TProfile.h"
12 #include "TString.h"
13 #include "TRegexp.h"
14 
15 #include "JGizmo/JRootObjectID.hh"
16 #include "JGizmo/JGizmoToolkit.hh"
17 
18 #include "Jeep/JParser.hh"
19 #include "Jeep/JMessage.hh"
20 
21 
22 /**
23  * \file
24  * Auxiliary program to convert 1D histograms to PDFs.
25  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
26  *
27  * Possible operations (option <tt>-O <operation></tt>:
28  * - N\n
29  * make PDF;
30  * - W\n
31  * divide bin contents by width of bin;
32  * - E\n
33  * apply operation also to bin errors;
34  * \author mdejong
35  */
36 int main(int argc, char **argv)
37 {
38  using namespace std;
39  using namespace JPP;
40 
41  vector<JRootObjectID> inputFile;
42  string outputFile;
43  string option;
44  int debug;
45 
46  try {
47 
48  JParser<> zap("Auxiliary program to convert 1D histograms to PDFs.");
49 
50  zap['f'] = make_field(inputFile, "<input file>:<object name>");
51  zap['O'] = make_field(option);
52  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "pdf.root";
53  zap['d'] = make_field(debug) = 1;
54 
55  zap(argc, argv);
56  }
57  catch(const exception &error) {
58  FATAL(error.what() << endl);
59  }
60 
61 
62  vector<TH1*> listOfObjects;
63 
64  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
65 
66  DEBUG("Input: " << *input << endl);
67 
68  TDirectory* dir = getDirectory(*input);
69 
70  if (dir == NULL) {
71  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
72  continue;
73  }
74 
75  const TRegexp regexp(input->getObjectName());
76 
77  TIter iter(dir->GetListOfKeys());
78 
79  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
80 
81  const TString tag(key->GetName());
82 
83  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
84 
85  // option match
86 
87  if (tag.Contains(regexp) && isTObject(key)) {
88 
89  TObject* object = key->ReadObj();
90 
91  TH1* h1 = dynamic_cast<TH1*>(object);
92 
93  try { h1 = dynamic_cast<TProfile&>(*object).ProjectionX(); } catch(exception&) {}
94 
95  if (h1 != NULL) {
96  listOfObjects.push_back(h1);
97  } else {
98  ERROR("Incompatible object " << object->GetName() << endl);
99  }
100  }
101  }
102  }
103 
104  TFile out(outputFile.c_str(), "recreate");
105 
106  for (vector<TH1*>::iterator h1 = listOfObjects.begin(); h1 != listOfObjects.end(); ++h1) {
107 
108  convertToPDF(**h1, option);
109 
110  (*h1)->Write();
111  }
112 
113  out.Write();
114  out.Close();
115 }
Utility class to parse command line options.
Definition: JParser.hh:1514
int main(int argc, char *argv[])
Definition: Main.cc:15
Definition: JRoot.hh:19
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
void convertToPDF(TH1 &h1, const std::string &option="NW", const double factor=1.0)
Convert 1D histogram to PDF.
#define ERROR(A)
Definition: JMessage.hh:66
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62