Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JEnergyCorrection.hh
Go to the documentation of this file.
1#ifndef __JRECONSTRUCTION__JENERGYCORRECTION__
2#define __JRECONSTRUCTION__JENERGYCORRECTION__
3
4#include <istream>
5#include <ostream>
6#include <sstream>
7#include <fstream>
8#include <iostream>
9#include <cmath>
10#include <cctype>
11#include <memory>
12
13#include "TFile.h"
14#include "TString.h"
15#include "TFormula.h"
16
17#include "JLang/JException.hh"
18#include "Jeep/JeepToolkit.hh"
19
20
21/**
22 * \author mdejong
23 */
24
25namespace JRECONSTRUCTION {}
26namespace JPP { using namespace JRECONSTRUCTION; }
27
28namespace JRECONSTRUCTION {
29
32
33
34 /**
35 * Auxiliary class for correction of energy determined by JEnergy.cc.
36 * Note that the correction is applied to <tt>10log(E)</tt> with <tt>E</tt> in GeV.
37 */
39 public std::shared_ptr<const TFormula>
40 {
41 public:
42 /**
43 * Default constructor.
44 * No correction is applied to the energy.
45 */
48
49
50 /**
51 * Constructor.
52 *
53 * \param formula formula
54 */
55 JEnergyCorrection(const std::string& formula)
56 {
57 setFormula(formula);
58 }
59
60
61 /**
62 * Constructor.
63 *
64 * \param fcn pointer to ROOT formula
65 */
66 JEnergyCorrection(const TFormula* fcn) :
67 std::shared_ptr<const TFormula>(fcn)
68 {}
69
70
71 /**
72 * Check validity of pointer.
73 *
74 * \return true if pointer not null; else false
75 */
76 bool is_valid() const
77 {
78 return (bool) std::shared_ptr<const TFormula>(*this);
79 }
80
81
82 /**
83 * Get formula.
84 *
85 * \return formula
86 */
87 TString getFormula() const
88 {
89 if (is_valid())
90 return get()->GetExpFormula();
91 else
92 return TString();
93 }
94
95
96 /**
97 * Set formula.
98 *
99 * The input can be:
100 * - TFormula compatible expression;
101 * - name of ROOT file (extension <tt>.root</tt>);
102 * - name of ASCII file (extension <tt>.txt</tt>);
103 *
104 * \param formula formula
105 */
106 void setFormula(const std::string& formula)
107 {
108 if (formula != "") {
109
110 TString buffer(formula.c_str());
111
112 if (buffer.EndsWith(".root") ||
113 buffer.EndsWith(".txt")) {
114
115 load(buffer);
116
117 } else {
118
119 reset(new TFormula(JEnergyCorrection::getName(), buffer));
120 }
121 }
122 }
123
124
125 /**
126 * Get corrected energy.
127 *
128 * \param E energy [GeV]
129 * \return corrected energy [GeV]
130 */
131 double operator()(const double E) const
132 {
133 if (is_valid()) {
134
135 const double y = get()->Eval(log10(E));
136
137 return pow(10.0, y);
138 }
139
140 return E;
141 }
142
143
144 /**
145 * Get name of energy correction formula.
146 *
147 * \return name of formula
148 */
149 static const char* getName()
150 {
151 return "energy_correction";
152 }
153
154
155 /**
156 * Load formula from file.
157 *
158 * Supported file formats:
159 * - ROOT file (extension <tt>.root</tt>) containing TFormula object with name JEnergyCorrection::getName();
160 * - ASCII file (extension <tt>.txt</tt>) containing TFormula compatible expression;
161 *
162 * Note that the method JEEP::getFullFilename() is used to search for a possible location of
163 * a file with the given name in the LD_LIBRARY_PATH environment variable.
164 *
165 * \param file_name file name
166 */
167 void load(const char* file_name)
168 {
169 using namespace std;
170 using namespace JLANG;
171 using namespace JEEP;
172
173 TString buffer(getFullFilename(LD_LIBRARY_PATH, file_name).c_str());
174
175 if (buffer.EndsWith(".root")) {
176
177 TFile in(buffer, "READ");
178
179 if (in.IsOpen()) {
180
181 TFormula* f1;
182
183 in.GetObject(JEnergyCorrection::getName(), f1);
184
185 reset(f1);
186
187 in.Close();
188
189 } else {
190
191 THROW(JFileOpenException, "Error opening file " << buffer);
192 }
193
194 } else if (buffer.EndsWith(".txt")) {
195
196 ifstream in(buffer);
197
198 if (in) {
199
200 buffer.ReadFile(in);
201
202 // remove control characters
203
204 for (Ssiz_t i = 0; i != buffer.Length(); ++i) {
205 if (iscntrl(buffer[i])) {
206 buffer[i] = ' ';
207 }
208 }
209
210 in.close();
211
212 reset(new TFormula(JEnergyCorrection::getName(), buffer));
213
214 } else {
215
216 THROW(JFileOpenException, "Error opening file " << buffer);
217 }
218
219 } else {
220
221 THROW(JProtocolException, "Protocol not defined.");
222 }
223 }
224
225
226 /**
227 * Store formula to file.
228 *
229 * Supported file formats:
230 * - ROOT file (extension <tt>.root</tt>) containing TFormula object with name JEnergyCorrection::getName;
231 * - ASCII file (extension <tt>.txt</tt>) containing TFormula compatible expression;
232 *
233 * \param file_name file name
234 */
235 void store(const char* file_name)
236 {
237 using namespace std;
238 using namespace JLANG;
239 using namespace JEEP;
240
241 if (is_valid()) {
242
243 const TString buffer(file_name);
244
245 if (buffer.EndsWith(".root")) {
246
247 TFile out(file_name, "RECREATE");
248
249 out.WriteObject(get(), JEnergyCorrection::getName());
250
251 out.Write();
252 out.Close();
253
254 } else if (buffer.EndsWith(".txt")) {
255
256 ofstream out(file_name);
257
258 out << *this;
259
260 out.close();
261
262 } else {
263
264 THROW(JProtocolException, "Protocol not defined.");
265 }
266 }
267 }
268
269
270 /**
271 * Read energy correction from input.
272 *
273 * In case a file name is specified, the method load() is used
274 * to read the energy correction from the corresponding file.
275 *
276 * \param in input stream
277 * \param object energy correction
278 * \return input stream
279 */
280 friend inline std::istream& operator>>(std::istream& in, JEnergyCorrection& object)
281 {
282 std::string buffer;
283
284 getline(in, buffer);
285
286 object.setFormula(buffer);
287
288 return in;
289 }
290
291
292 /**
293 * Write energy correction to output.
294 *
295 * \param out output stream
296 * \param object energy correction
297 * \return output stream
298 */
299 friend inline std::ostream& operator<<(std::ostream& out, const JEnergyCorrection& object)
300 {
301 return out << object.getFormula();
302 }
303 };
304}
305
306#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Auxiliary methods for handling file names, type names and environment.
Exception for opening of file.
Protocol exception.
Auxiliary class for correction of energy determined by JEnergy.cc.
friend std::ostream & operator<<(std::ostream &out, const JEnergyCorrection &object)
Write energy correction to output.
void load(const char *file_name)
Load formula from file.
JEnergyCorrection(const TFormula *fcn)
Constructor.
friend std::istream & operator>>(std::istream &in, JEnergyCorrection &object)
Read energy correction from input.
JEnergyCorrection(const std::string &formula)
Constructor.
void store(const char *file_name)
Store formula to file.
TString getFormula() const
Get formula.
double operator()(const double E) const
Get corrected energy.
bool is_valid() const
Check validity of pointer.
static const char * getName()
Get name of energy correction formula.
void setFormula(const std::string &formula)
Set formula.
General puprpose classes and methods.
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Model fits to data.