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