Jpp  18.1.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPrintAxisLabel.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <sstream>
5 #include <cmath>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TObject.h"
10 #include "TH1.h"
11 #include "TAxis.h"
12 #include "TKey.h"
13 #include "TString.h"
14 #include "TRegexp.h"
15 
16 #include "JLang/JException.hh"
17 
18 #include "JGizmo/JRootObjectID.hh"
19 #include "JGizmo/JGizmoToolkit.hh"
20 
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 #include "Jeep/JPrint.hh"
24 
25 
26 /**
27  * \file
28  *
29  * Auxiliary program to print axis label of ROOT objects.
30  *
31  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
32  * \author mdejong, bjung
33  */
34 int main(int argc, char **argv)
35 {
36  using namespace std;
37  using namespace JPP;
38 
39  vector<JRootObjectID> inputFile;
40  char axis;
41  int debug;
42 
43  try {
44 
45  JParser<> zap("Auxiliary program to print the axis label of a ROOT histogram.");
46 
47  zap['f'] = make_field(inputFile, "<input file>:<object name>");
48  zap['A'] = make_field(axis, "axis") = 'x', 'X', 'y', 'Y', 'z', 'Z';
49  zap['d'] = make_field(debug) = 0;
50 
51  zap(argc, argv);
52  }
53  catch(const exception &error) {
54  FATAL(error.what() << endl);
55  }
56 
57  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
58 
59  DEBUG("Input: " << *input << endl);
60 
61  TDirectory* dir = getDirectory(*input);
62 
63  if (dir == NULL) {
64  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
65  continue;
66  }
67 
68  const TRegexp regexp(input->getObjectName());
69 
70  TIter iter(dir->GetListOfKeys());
71 
72  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
73 
74  const TString tag(key->GetName());
75 
76  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
77 
78  // option match
79 
80  if (tag.Contains(regexp) && isTObject(key)) {
81 
82  TH1* h = (TH1*) key->ReadObj();
83 
84  switch (axis) {
85 
86  case 'X':
87  case 'x': {
88  cout << h->GetXaxis()->GetTitle() << endl;
89  break;
90  }
91 
92  case 'Y':
93  case 'y': {
94  cout << h->GetYaxis()->GetTitle() << endl;
95  break;
96  }
97 
98  case 'Z':
99  case 'z': {
100 
101  TAxis* Zaxis = h->GetZaxis();
102 
103  if (Zaxis != NULL) {
104  cout << h->GetZaxis()->GetTitle() << endl;
105  } else {
106  ERROR("Histogram " << h->GetName() << " does not have a Z-axis.");
107  }
108 
109  break;
110  }
111  }
112  }
113  }
114  }
115 }
Utility class to parse command line options.
Definition: JParser.hh:1514
Exceptions.
int main(int argc, char *argv[])
Definition: Main.cc:15
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#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