Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JHistogramToolkit.hh
Go to the documentation of this file.
1 #ifndef __JHISTOGRAM_TOOLKIT__
2 #define __JHISTOGRAM_TOOLKIT__
3 
4 #include <string>
5 
6 #include "TROOT.h"
7 #include "TFile.h"
8 #include "TH1D.h"
9 
10 #include "JFit/JFitParameters.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 using namespace JFIT;
18 
19 static const int JQUALITY = -1; //!< fit quality identifier
20 
21 
22 /**
23  * Map key.
24  */
25 struct JKey {
26  /**
27  * Constructor.
28  *
29  * \param name parameter name
30  * \param index parameter value (= index in user data)
31  */
32  JKey(const char* name,
33  const int index)
34  {
35  this->name = name;
36  this->index = index;
37  }
38 
39 
40  /**
41  * Less than operator.
42  *
43  * \param first first key
44  * \param second second key
45  * \return true if index of first key less than that of second; else false
46  */
47  friend inline bool operator<(const JKey& first, const JKey& second)
48  {
49  return first.index < second.index;
50  }
51 
52  std::string name;
53  int index;
54 };
55 
56 
57 /**
58  * Make key.
59  *
60  * \param PARAMETER parameter
61  * \return key
62  */
63 #define make_key(PARAMETER) JKey(#PARAMETER, PARAMETER)
64 
65 
66 /**
67  * Map value.
68  */
69 struct JValue {
70  /**
71  * Default constructor.
72  */
73  JValue() :
74  hA(NULL),
75  hB(NULL),
76  hC(NULL),
77  hD(NULL),
78  logx(false)
79  {}
80 
81 
82  /**
83  * Constructor.
84  *
85  * \param hA pointer to histogram
86  * \param hB pointer to histogram
87  * \param hC pointer to histogram
88  * \param hD pointer to histogram
89  * \param logx log10(x) filling mode
90  */
91  JValue(TH1D* hA,
92  TH1D* hB,
93  TH1D* hC,
94  TH1D* hD,
95  bool logx = false)
96  {
97  this->hA = hA;
98  this->hB = hB;
99  this->hC = hC;
100  this->hD = hD;
101  this->logx = logx;
102  }
103 
104 
105  /**
106  * Fill histograms.
107  *
108  * \param xA first abscissa value
109  * \param xB second abscissa value
110  * \param option option
111  */
112  void Fill(const Double_t xA, const Double_t xB, const bool option)
113  {
114  hA->Fill(logx ? log10(xA) : xA);
115  hB->Fill(logx ? log10(xB) : xB);
116  if (option)
117  hC->Fill(logx ? log10(xB/xA) : xB - xA);
118  else
119  hD->Fill(logx ? log10(xB/xA) : xB - xA);
120  }
121 
122 
123  /**
124  * Scale histograms.
125  */
126  void Scale()
127  {
128  if (hA->GetEntries() != 0) { hA->Scale(1.0/hA->GetEntries()); }
129  if (hB->GetEntries() != 0) { hB->Scale(1.0/hB->GetEntries()); }
130  if (hC->GetEntries() != 0) { hC->Scale(1.0/hC->GetEntries()); }
131  if (hD->GetEntries() != 0) { hD->Scale(1.0/hD->GetEntries()); }
132  }
133 
134 
135  /**
136  * Write histograms to file.
137  *
138  * \param out ROOT file
139  */
140  void Write(TFile& out)
141  {
142  out.WriteTObject(hA);
143  out.WriteTObject(hB);
144  out.WriteTObject(hC);
145  out.WriteTObject(hD);
146  }
147 
148 protected:
149  TH1D* hA;
150  TH1D* hB;
151  TH1D* hC;
152  TH1D* hD;
153  bool logx;
154 };
155 
156 
157 /**
158  * Auxiliary class to manage set of histograms.
159  */
160 struct JManager :
161  public std::map<JKey, JValue>
162 {
163  /**
164  * Insert set of histograms.
165  *
166  * \param key key
167  * \param nx number of bins
168  * \param xmin minimal abscissa value
169  * \param xmax maximal abscissa value
170  * \param logx log10(x)
171  */
172  void insert(const JKey& key,
173  const Int_t nx,
174  const Double_t xmin,
175  const Double_t xmax,
176  const double logx = false)
177  {
178  TH1D* hA = new TH1D(("[A " + key.name + "]").c_str(), NULL, nx, xmin, xmax);
179  TH1D* hB = new TH1D(("[B " + key.name + "]").c_str(), NULL, nx, xmin, xmax);
180  TH1D* hC = new TH1D(("[C " + key.name + "]").c_str(), NULL, nx, -xmax, xmax);
181  TH1D* hD = new TH1D(("[D " + key.name + "]").c_str(), NULL, nx, -xmax, xmax);
182 
183  std::map<JKey, JValue>::insert(make_pair(key, JValue(hA,hB,hC,hD,logx)));
184  }
185 
186 
187  /**
188  * Fill histograms.
189  *
190  * \param fA first fit result
191  * \param fB second fit result
192  * \param option option
193  */
194  void Fill(const JFit& fA, const JFit& fB, const bool option)
195  {
196  for (iterator i = begin(); i != end(); ++i) {
197 
198  const int index = i->first.index;
199 
200  if (index == JQUALITY) {
201  i->second.Fill(fA.getQ(), fB.getQ(), option);
202  } else if (fA.hasW(index) && fB.hasW(index)) {
203  i->second.Fill(fA.getW(index), fB.getW(index), option);
204  }
205  };
206  }
207 
208 
209  /**
210  * Scale histograms.
211  */
212  void Scale()
213  {
214  for (iterator i = this->begin(); i != this->end(); ++i) {
215  i->second.Scale();
216  }
217  }
218 
219 
220  /**
221  * Write histograms to file.
222  *
223  * \param out ROOT file
224  */
225  void Write(TFile& out)
226  {
227  for (iterator i = this->begin(); i != this->end(); ++i) {
228  i->second.Write(out);
229  }
230  }
231 
232 
233  /**
234  * Write histograms to file.
235  *
236  * \param file_name file name
237  */
238  void Write(const char* file_name)
239  {
240  TFile out(file_name, "RECREATE");
241 
242  this->Write(out);
243 
244  out.Write();
245  out.Close();
246  }
247 };
248 
249 #endif
void insert(const JKey &key, const Int_t nx, const Double_t xmin, const Double_t xmax, const double logx=false)
Insert set of histograms.
JKey(const char *name, const int index)
Constructor.
Map key.
Auxiliary class to manage set of histograms.
void Scale()
Scale histograms.
JValue()
Default constructor.
void Fill(const JFit &fA, const JFit &fB, const bool option)
Fill histograms.
Data structure for track fit results.
Definition: JEvt.hh:32
Definition of fit parameters from various applications.
std::string name
bool hasW(const int i) const
Check availability of value.
Definition: JEvt.hh:200
static const int JQUALITY
fit quality identifier
JValue(TH1D *hA, TH1D *hB, TH1D *hC, TH1D *hD, bool logx=false)
Constructor.
friend bool operator<(const JKey &first, const JKey &second)
Less than operator.
void Fill(const Double_t xA, const Double_t xB, const bool option)
Fill histograms.
double getQ() const
Get quality.
Definition: JEvt.hh:151
Map value.
void Write(TFile &out)
Write histograms to file.
double getW(const int i) const
Get value.
Definition: JEvt.hh:212
void Write(TFile &out)
Write histograms to file.
void Scale()
Scale histograms.
void Write(const char *file_name)
Write histograms to file.