QwAnalysis
QwHistogramHelper.h
Go to the documentation of this file.
1 /// This class exists to encapsulate several helper functions to aid in
2 /// describing histograms.
3 /// There will be a global copy defined within the analysis framework.
4 
5 #ifndef __QWHISTOGRAMHELPER__
6 #define __QWHISTOGRAMHELPER__
7 
8 #include <string>
9 #include <vector>
10 #include <TString.h>
11 #include <TRegexp.h>
12 #include <TH1.h>
13 #include <TH2.h>
14 #include <TProfile.h>
15 #include <TProfile2D.h>
16 
17 #include "QwParameterFile.h"
18 #include "QwOptions.h"
19 ///
20 /// \ingroup QwAnalysis
22  public:
23  QwHistogramHelper(): fDEBUG(kFALSE) { fHistParams.clear(); };
24  virtual ~QwHistogramHelper() { };
25 
26  /// \brief Define the configuration options
27  static void DefineOptions(QwOptions &options);
28  /// \brief Process the configuration options
29  void ProcessOptions(QwOptions &options);
30 
31  void LoadHistParamsFromFile(const std::string& filename);
32  void LoadTreeParamsFromFile(const std::string& filename);
33 
34  // Print the histogram parameters
35  void PrintHistParams() const;
36 
37  TH1F* Construct1DHist(const TString& inputfile, const TString& name_title) {
38  std::string tmpfile = inputfile.Data();
39  return Construct1DHist(tmpfile, name_title);
40  };
41  TH2F* Construct2DHist(const TString& inputfile, const TString& name_title) {
42  std::string tmpfile = inputfile.Data();
43  return Construct2DHist(tmpfile, name_title);
44  };
45 
46  TH1F* Construct1DHist(const TString& name_title);
47  TH2F* Construct2DHist(const TString& name_title);
48  TProfile* Construct1DProf(const TString& name_title);
49  TProfile2D* Construct2DProf(const TString& name_title);
50 
51  TH1F* Construct1DHist(const std::string& inputfile, const TString& name_title);
52  TH2F* Construct2DHist(const std::string& inputfile, const TString& name_title);
53  TProfile* Construct1DProf(const std::string& inputfile, const TString& name_title);
54  TProfile2D* Construct2DProf(const std::string& inputfile, const TString& name_title);
55 
56  Bool_t MatchDeviceParamsFromList(const std::string& devicename);
57  Bool_t MatchVQWKElementFromList(const std::string& subsystemname,
58  const std::string& moduletype,
59  const std::string& devicename);
60 
61  protected:
62 
63  /// Histogram parameter class
64  class HistParams {
65  public:
66  TRegexp expression;
67  TString name_title;
68  TString type;
69  Int_t nbins;
70  Int_t x_nbins;
71  Float_t x_min;
72  Float_t x_max;
73  Int_t y_nbins;
74  Float_t y_min;
75  Float_t y_max;
76  Int_t z_nbins;
77  Float_t z_min;
78  Float_t z_max;
79  TString xtitle;
80  TString ytitle;
81  Float_t min;
82  Float_t max;
83 
84  /// Constructor
86 
87  public:
88  /// Relational less-than operator overload
89  bool operator< (const HistParams& that) const {
90  // Compare only lowercase strings
91  TString thisname(this->name_title); thisname.ToLower();
92  TString thatname(that.name_title); thatname.ToLower();
93  if (thisname.MaybeRegexp() && thatname.MaybeRegexp()) {
94  // Both wildcarded: latest occurrence of 'wildest card'
95  if (thisname.Contains("*") != thatname.Contains("*"))
96  return thatname.Contains("*");
97  else if (thisname.First("*") != thatname.First("*"))
98  return (thisname.First("*") > thatname.First("*"));
99  else if (thisname.Contains("+") != thatname.Contains("+"))
100  return thatname.Contains("+");
101  else if (thisname.First("+") != thatname.First("+"))
102  return (thisname.First("+") > thatname.First("+"));
103  else if (thisname.CountChar('?') != thatname.CountChar('?'))
104  return (thisname.CountChar('?') < thatname.CountChar('?'));
105  else if (thisname.CountChar('.') != thatname.CountChar('.'))
106  return (thisname.CountChar('.') < thatname.CountChar('.'));
107  else return (thisname < thatname);
108  } else if (thisname.MaybeRegexp() || thatname.MaybeRegexp())
109  // One wildcarded: explicit case has precedence
110  return thatname.MaybeRegexp();
111  else
112  // No wildcards: alphabetic ordering on the lower case names
113  return (thisname < thatname);
114  };
115 
116  /// Output stream operator overload
117  friend std::ostream& operator<< (std::ostream& stream, const HistParams& h) {
118  stream << h.type << " " << h.name_title
119  << " x (" << h.xtitle << "): " << h.x_min << " -- " << h.x_max << " (" << h.x_nbins << " bins), "
120  << " y (" << h.ytitle << "): " << h.y_min << " -- " << h.y_max << " (" << h.y_nbins << " bins)";
121  return stream;
122  }
123  };
124 
125  protected:
126  TH1F* Construct1DHist(const HistParams &params);
127  TH2F* Construct2DHist(const HistParams &params);
128 
129  TProfile* Construct1DProf(const HistParams &params);
130  TProfile2D* Construct2DProf(const HistParams &params);
131 
132  const HistParams GetHistParamsFromLine(QwParameterFile &mapstr);
133 
134  // Look up the histogram parameters from a file according to histname.
135  const HistParams GetHistParamsFromFile(const std::string& filename,
136  const TString& histname);
137  const HistParams GetHistParamsFromList(const TString& histname);
138 
139  Bool_t DoesMatch(const TString& s, const TRegexp& wildcard);
140 
141  protected:
142  static const Double_t fInvalidNumber;
143  static const TString fInvalidName;
144  Bool_t fDEBUG;
145  Bool_t fTrimDisable;
148 
149  std::string fInputFile;
150  std::vector<HistParams> fHistParams;
151  std::vector< std::pair< TString,TRegexp > > fTreeParams;
152 
153  std::vector<TString> fSubsystemList;//stores the list of subsystems
154  std::vector<std::vector<TString> > fModuleList;//will store list modules in each subsystem (ex. for BCM, BPM etc in Beam line sub system)
155  std::vector<std::vector<std::vector<TString> > > fVQWKTrimmedList; //will store list of VQWK elements for each subsystem for each module
156 };
157 
158 // Declare a global copy of the histogram helper.
159 // It is instantiated in the source file.
161 
162 #endif
163 
164 
static const Double_t fInvalidNumber
Histogram parameter class.
An options class.
Definition: QwOptions.h:133
static void DefineOptions(QwOptions &options)
Define the configuration options.
const HistParams GetHistParamsFromList(const TString &histname)
const HistParams GetHistParamsFromLine(QwParameterFile &mapstr)
TH2F * Construct2DHist(const TString &inputfile, const TString &name_title)
void LoadTreeParamsFromFile(const std::string &filename)
void PrintHistParams() const
std::vector< std::pair< TString, TRegexp > > fTreeParams
friend std::ostream & operator<<(std::ostream &stream, const HistParams &h)
Output stream operator overload.
TProfile2D * Construct2DProf(const TString &name_title)
void ProcessOptions(QwOptions &options)
Process the configuration options.
std::vector< TString > fSubsystemList
Bool_t MatchDeviceParamsFromList(const std::string &devicename)
Bool_t MatchVQWKElementFromList(const std::string &subsystemname, const std::string &moduletype, const std::string &devicename)
static const TString fInvalidName
std::vector< std::vector< std::vector< TString > > > fVQWKTrimmedList
std::vector< HistParams > fHistParams
QwHistogramHelper gQwHists
Globally defined instance of the QwHistogramHelper class.
bool operator<(const HistParams &that) const
Relational less-than operator overload.
std::vector< std::vector< TString > > fModuleList
const HistParams GetHistParamsFromFile(const std::string &filename, const TString &histname)
Bool_t DoesMatch(const TString &s, const TRegexp &wildcard)
An options class which parses command line, config file and environment.
virtual ~QwHistogramHelper()
TH1F * Construct1DHist(const TString &inputfile, const TString &name_title)
void LoadHistParamsFromFile(const std::string &filename)
TProfile * Construct1DProf(const TString &name_title)