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