Jpp  19.1.0-rc.1
the software that should make you happy
Functions
JProject3D.cc File Reference

Auxiliary program to project 3D histograms. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include "TROOT.h"
#include "TFile.h"
#include "TKey.h"
#include "TH3.h"
#include "TProfile3D.h"
#include "TString.h"
#include "TRegexp.h"
#include "JGizmo/JRootObjectID.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "Jeep/JPrint.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to project 3D histograms.

Author
mdejong

Definition in file JProject3D.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 29 of file JProject3D.cc.

30 {
31  using namespace std;
32  using namespace JPP;
33 
34  vector<JRootObjectID> inputFile;
35  string outputFile;
36  char project;
37  int debug;
38 
39  try {
40 
41  JParser<> zap("Auxiliary program to project 3D histograms.");
42 
43  zap['f'] = make_field(inputFile, "<input file>:<object name>");
44  zap['P'] = make_field(project, "projection") = ' ', 'x', 'X', 'y', 'Y', 'z', 'Z';
45  zap['o'] = make_field(outputFile, "ROOT file with histogram(s)") = "project.root";
46  zap['d'] = make_field(debug) = 1;
47 
48  zap(argc, argv);
49  }
50  catch(const exception &error) {
51  FATAL(error.what() << endl);
52  }
53 
54  const bool px = (project == 'x' || project == 'X'); // projection on x-axis
55  const bool py = (project == 'y' || project == 'Y'); // projection on y-axis
56  const bool pz = (project == 'z' || project == 'Z'); // projection on z-axis
57 
58  if ((px ? 1 : 0) +
59  (py ? 1 : 0) +
60  (pz ? 1 : 0) != 1) {
61  FATAL("Invalid operation: "
62  << (px ? "" : "no") << " X projection " << " and "
63  << (py ? "" : "no") << " Y projection " << " and "
64  << (pz ? "" : "no") << " Z projection " << endl);
65  }
66 
67  vector<TObject*> listOfObjects;
68 
69  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
70 
71  DEBUG("Input: " << *input << endl);
72 
73  TDirectory* dir = getDirectory(*input);
74 
75  if (dir == NULL) {
76  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
77  continue;
78  }
79 
80  const TRegexp regexp(input->getObjectName());
81 
82  TIter iter(dir->GetListOfKeys());
83 
84  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
85 
86  const TString tag(key->GetName());
87 
88  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
89 
90  // option match
91 
92  if (tag.Contains(regexp) && isTObject(key)) {
93 
94  TH3* h3 = dynamic_cast<TH3*>(key->ReadObj());
95 
96  if (h3 != NULL) {
97 
98  if (px)
99  listOfObjects.push_back(h3->ProjectionX(MAKE_CSTRING(h3->GetName() << "_px")));
100  else if (py)
101  listOfObjects.push_back(h3->ProjectionY(MAKE_CSTRING(h3->GetName() << "_py")));
102  else if (pz)
103  listOfObjects.push_back(h3->ProjectionZ(MAKE_CSTRING(h3->GetName() << "_pz")));
104  }
105  }
106  }
107  }
108 
109  if (!listOfObjects.empty()) {
110 
111  TFile out(outputFile.c_str(), "recreate");
112 
113  for (vector<TObject*>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
114  (*i)->Write();
115  }
116 
117  out.Write();
118  out.Close();
119  }
120 }
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:2158
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
Utility class to parse command line options.
Definition: JParser.hh:1714
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