Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRootToolkit.hh
Go to the documentation of this file.
1 #ifndef __JROOTTOOLKIT__
2 #define __JROOTTOOLKIT__
3 
4 #include <limits>
5 
6 #include "TFile.h"
7 #include "TStreamerInfo.h"
8 #include "TList.h"
9 #include "TIterator.h"
10 #include "TString.h"
11 #include "TF1.h"
12 
13 #include "JROOT/JRootFile.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JROOT {}
21 namespace JPP { using namespace JROOT; }
22 
23 namespace JROOT {
24 
25 
26  /**
27  * Write object to ROOT file.
28  *
29  * \param file ROOT file
30  * \param object ROOT object
31  * \return ROOT file
32  */
33  TFile& operator<<(TFile& file, const TObject& object)
34  {
35  file.WriteTObject(&object);
36 
37  return file;
38  }
39 
40 
41  /**
42  * Get ROOT streamer information of class with given name.
43  * Note that the class name should include the name space, if any.
44  *
45  * \param file pointer to ROOT file
46  * \param name class name
47  * \return pointer to TStreamerInfo (NULL in case of error)
48  */
49  inline const TStreamerInfo* getStreamerInfo(TFile* file, const char* name)
50  {
51  if (file != NULL && file->IsOpen()) {
52 
53  const TString buffer(name);
54 
55  TIter iter(file->GetStreamerInfoList());
56 
57  for (const TStreamerInfo* pStreamerInfo; (pStreamerInfo = (TStreamerInfo*) iter.Next()) != NULL; ) {
58  if (buffer == TString(pStreamerInfo->GetName())) {
59  return pStreamerInfo;
60  }
61  }
62  }
63 
64  return NULL;
65  }
66 
67 
68  /**
69  * Get ROOT streamer information of class with given name.
70  * Note that the class name should include the name space, if any.
71  *
72  * \param file_name file name
73  * \param name class name
74  * \return pointer to TStreamerInfo (NULL in case of error)
75  */
76  inline const TStreamerInfo* getStreamerInfo(const char* file_name, const char* name)
77  {
78  JRootInputFile file(file_name);
79 
80  return getStreamerInfo(file.getFile(), name);
81  }
82 
83 
84  /**
85  * Get ROOT streamer version of class with given name.
86  * Note that the class name should include the name space, if any.
87  *
88  * \param file pointer to ROOT file
89  * \param name class name
90  * \return streamer version; (-1 in case of error)
91  */
92  inline int getStreamerVersion(TFile* file, const char* name)
93  {
94  const TStreamerInfo* pStreamerInfo = getStreamerInfo(file, name);
95 
96  if (pStreamerInfo != NULL)
97  return pStreamerInfo->GetClassVersion();
98  else
99  return -1;
100  }
101 
102 
103  /**
104  * Get ROOT streamer version of class with given name.
105  * Note that the class name should include the name space, if any.
106  *
107  * \param file_name file name
108  * \param name class name
109  * \return streamer version; (-1 in case of error)
110  */
111  inline int getStreamerVersion(const char* file_name, const char* name)
112  {
113  JRootInputFile file(file_name);
114 
115  return getStreamerVersion(file.getFile(), name);
116  }
117 
118 
119  /**
120  * Auxiliary data structure for a parameter index and its value.
121  */
123  /**
124  * Default constructor.
125  */
127  index(0),
128  value(0.0)
129  {}
130 
131 
132  /**
133  * Constructor.
134  *
135  * \param index parameter index
136  * \param value parameter value
137  */
138  JFitParameter_t(const Int_t index,
139  const Double_t value) :
140  index(index),
141  value(value)
142  {}
143 
144 
145  /**
146  * Type conversion operator
147  *
148  *
149  * \return index
150  */
151  inline operator Int_t() const
152  {
153  return index;
154  }
155 
156 
157  Int_t index;
158  Double_t value;
159  };
160 
161 
162  /**
163  * Set fit parameter.
164  *
165  * \param f1 fit function
166  * \param parameter parameter index and value
167  */
168  inline bool setParameter(TF1& f1, const JFitParameter_t& parameter)
169  {
170  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
171 
172  f1.SetParameter(parameter.index, parameter.value);
173 
174  return true;
175 
176  } else {
177 
178  return false;
179  }
180  }
181 
182 
183  /**
184  * Fix fit parameter.
185  *
186  * \param f1 fit function
187  * \param parameter parameter index and value
188  */
189  inline bool fixParameter(TF1& f1, const JFitParameter_t& parameter)
190  {
191  if (parameter.index >= 0 && parameter.index < f1.GetNpar()) {
192 
193  f1.FixParameter(parameter.index, parameter.value);
194 
195  return true;
196 
197  } else {
198 
199  return false;
200  }
201  }
202 
203 
204  /**
205  * Release fit parameter.
206  *
207  * \param f1 fit function
208  * \param index parameter index
209  */
210  inline bool releaseParameter(TF1& f1, const Int_t index)
211  {
212  if (index >= 0 && index < f1.GetNpar()) {
213 
214  f1.ReleaseParameter(index);
215 
216  return true;
217 
218  } else {
219 
220  return false;
221  }
222  }
223 
224 
225  /**
226  * Set fit parameter limits.
227  *
228  * \param f1 fit function
229  * \param index parameter index
230  * \param xmin lower limit
231  * \param xmax upper limit
232  */
233  inline bool setParLimits(TF1& f1, const Int_t index, Double_t xmin, Double_t xmax)
234  {
235  using namespace std;
236 
237  if (index >= 0 && index < f1.GetNpar()) {
238 
239  if (xmin == 0.0) { xmin = -numeric_limits<Double_t>::min(); }
240  if (xmax == 0.0) { xmax = +numeric_limits<Double_t>::min(); }
241 
242  f1.SetParLimits(index, xmin, xmax);
243 
244  return true;
245 
246  } else {
247 
248  return false;
249  }
250  }
251 
252 
253  /**
254  * Check if fit parameter is fixed.
255  *
256  * \param f1 fit function
257  * \param index parameter index
258  */
259  inline bool isParameterFixed(TF1& f1, const Int_t index)
260  {
261  if (index >= 0 && index < f1.GetNpar()) {
262 
263  Double_t xmin;
264  Double_t xmax;
265 
266  f1.GetParLimits(index, xmin, xmax);
267 
268  return (xmin != 0.0 && xmax != 0.0 && xmin >= xmax);
269 
270  } else {
271 
272  return false;
273  }
274  }
275 }
276 
277 #endif
278 
JFitParameter_t(const Int_t index, const Double_t value)
Constructor.
int getStreamerVersion(TFile *file, const char *name)
Get ROOT streamer version of class with given name.
Definition: JRootToolkit.hh:92
const TStreamerInfo * getStreamerInfo(TFile *file, const char *name)
Get ROOT streamer information of class with given name.
Definition: JRootToolkit.hh:49
bool releaseParameter(TF1 &f1, const Int_t index)
Release fit parameter.
JFitParameter_t()
Default constructor.
Definition: JRoot.hh:19
bool isParameterFixed(TF1 &f1, const Int_t index)
Check if fit parameter is fixed.
Auxiliary data structure for a parameter index and its value.
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
bool setParameter(TF1 &f1, const JFitParameter_t &parameter)
Set fit parameter.
ROOT input file.
Definition: JRootFile.hh:101
bool setParLimits(TF1 &f1, const Int_t index, Double_t xmin, Double_t xmax)
Set fit parameter limits.
bool fixParameter(TF1 &f1, const JFitParameter_t &parameter)
Fix fit parameter.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)