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