CFEL - ASG Software Suite  2.5.0
CASS
convenience_functions.h
Go to the documentation of this file.
1 // Copyright (C) 2010,2013 Lutz Foucar
2 
3 /**
4  * @file convenience_functions.h file contains declaration of classes and
5  * functions that help other processors to do
6  * their job.
7  *
8  * @author Lutz Foucar
9  */
10 
11 #ifndef __CONVENIENCE_FUNCTIONS_H__
12 #define __CONVENIENCE_FUNCTIONS_H__
13 
14 #include <QtCore/QFileInfo>
15 #include <QtCore/QDir>
16 #include <QtCore/QString>
17 
18 #include "cass.h"
19 #include "result.hpp"
20 #include "processor.h"
21 
22 namespace cass
23 {
24 class CASSSettings;
25 
26 namespace ACQIRIS
27 {
28 class DetectorBackend;
29 
30 /** load detector from file
31  *
32  * after loading check whether it is a delayline detector, if not throw
33  * invalid_argument exception.
34  *
35  * @return key containing detector name
36  * @param s CASSSettings object to read the info from
37  * @param ppNbr the Postprocessor number of the processor calling this
38  * function
39  * @param key the key of the processor calling this function
40  *
41  * @author Lutz Foucar
42  */
43 std::string loadDelayDet(CASSSettings &s,
44  int ppNbr,
45  const std::string& key);
46 
47 /** load particle for a specific detector
48  *
49  * after loading check whether it is a delayline detector, if not throw
50  * invalid_argument exception.
51  *
52  * @return key containing detector name
53  * @param s CASSSettings object to read the info from
54  * @param detector the name of the detector that contains the layer
55  * @param ppNbr the Postprocessor number of the processor calling this
56  * function
57  * @param key the key of the processor calling this function
58  *
59  * @author Lutz Foucar
60  */
61 std::string loadParticle(CASSSettings &s,
62  const std::string &detector,
63  int ppNbr,
64  const std::string& key);
65 }//end namespace acqiris
66 
67 /** Binary function for thresholding
68  *
69  * Returns the value if it is below threshold. Otherwise, returns 0.
70  *
71  * @author Thomas White
72  */
73 class threshold : public std::binary_function<float, float, float>
74 {
75 public:
76  /** operator */
77  float operator() (float value, float thresh)const
78  {
79  return (value > thresh) ? value : 0.0;
80  }
81 };
82 
83 /** binary function for weighted subtraction.
84  *
85  * @author Lutz Foucar
86  */
87 class weighted_minus : std::binary_function<float, float, float>
88 {
89 public:
90  /** constructor.
91  *
92  * @param first_weight the weight value of the first histogram
93  * @param second_weight the weight value of the second histogram
94  */
95  weighted_minus(float first_weight, float second_weight)
96  :_first_weight(first_weight),_second_weight(second_weight)
97  {}
98  /** operator */
99  float operator() (const float first, const float second)
100  { return first * _first_weight - second * _second_weight;}
101 protected:
103 };
104 
105 
106 /** binary function for averaging.
107  *
108  * this operator performs a moving sum
109  *
110  * @author Nicola Coppola
111  */
112 class TimeAverage : std::binary_function<float,float,float>
113 {
114 public:
115  /** constructor.
116  *
117  * initializes the nEvents value
118  *
119  * @param nEvents The number of Events used up to now
120  */
121  explicit TimeAverage(float nEvents)
122  :_nEvents(nEvents)
123  {}
124 
125  /** the operator calculates the average over the last _nEvents */
126  float operator()(float currentValue, float Average_Nm1)
127  {
128  if(_nEvents!=0)
129  return ( Average_Nm1 * (_nEvents-1) + currentValue ) /_nEvents;
130  else
131  return currentValue;
132  }
133 
134 protected:
135  /** nEvents for the average calculation */
136  float _nEvents;
137 };
138 
139 
140 /** function to set the 1d histogram properties from the ini file.
141  *
142  * @param[in] name the name of the processor too look up in cass.ini
143  *
144  * @author Lutz Foucar
145  */
147 
148 
149 /** function to set the 2d histogram properties from the ini file.
150  *
151  * @param[in] name the name of the processor too look up in cass.ini
152  *
153  * @author Lutz Foucar
154  */
156 
157 /** an alphabetical counter extension
158  *
159  * changes dirs by appending a subdir with an alphabetically increasing counter
160  *
161  * @author Lutz Foucar
162  */
164 {
165 public:
166  /** initialize the directory
167  *
168  * add an alphabtically subdir to the dir of the filename
169  *
170  * @return the filename containing the new subdir
171  * @param fname the filenname whos dir should be modified
172  */
173  static std::string intializeDir(const std::string &fname)
174  {
175  QFileInfo fInfo(QString::fromStdString(fname));
176  QString path(fInfo.path());
177  QString filename(fInfo.fileName());
178  path += "/aa/";
179  QDir dir(path);
180  if (!dir.exists())
181  dir.mkpath(".");
182  const std::string newfilename(path.toStdString() + filename.toStdString());
183  return newfilename;
184  }
185 
186  /** initialize the filename
187  *
188  * add an alphabtically ending to the filename
189  *
190  * @return the filename containing the new counter ending
191  * @param fname the filenname with appended counter ending
192  */
193  static std::string intializeFile(const std::string &fname)
194  {
195  QFileInfo fInfo(QString::fromStdString(fname));
196  if (fInfo.suffix().isEmpty())
197  return (fname + "_aa.h5");
198  QString filename(fInfo.baseName() + "__aa");
199  QString newfilename(fInfo.path() + "/" + filename + "." + fInfo.suffix());
200  return newfilename.toStdString();
201  }
202 
203  /** remove the alpha counter subdir from filename
204  *
205  * @return filename without the alphacounter subdir
206  * @param fname The filename who's subdir should be removed
207  */
208  static std::string removeAlphaSubdir(const std::string &fname)
209  {
210  QFileInfo fInfo(QString::fromStdString(fname));
211  QString path(fInfo.path());
212  QString filename(fInfo.fileName());
213  QStringList dirs = path.split("/");
214  dirs.removeLast();
215  QString newPath(dirs.join("/"));
216  newPath.append("/");
217  const std::string newfilename(newPath.toStdString() + filename.toStdString());
218  return newfilename;
219 
220  }
221 
222  /** increase the alpha counter
223  *
224  * @return filename with alphabetically increased subdir
225  * @param fname the Filename whos subdir should be increased
226  */
227  static std::string increaseDirCounter(const std::string &fname)
228  {
229  QFileInfo fInfo(QString::fromStdString(fname));
230  QString path(fInfo.path());
231  QString filename(fInfo.fileName());
232  QStringList dirs = path.split("/");
233  QString subdir = dirs.last();
234  QByteArray alphaCounter = subdir.toLatin1();
235  if (alphaCounter[1] == 'z')
236  {
237  const char first(alphaCounter[0]);
238  alphaCounter[0] = first + 1;
239  alphaCounter[1] = 'a';
240  }
241  else
242  {
243  const char second(alphaCounter[1]);
244  alphaCounter[1] = second + 1;
245  }
246  QString newSubdir(QString::fromLatin1(alphaCounter));
247  dirs.removeLast();
248  dirs.append(newSubdir);
249  QString newPath(dirs.join("/"));
250  newPath.append("/");
251  QDir dir(newPath);
252  if (!dir.exists())
253  dir.mkpath(".");
254  const std::string newfilename(newPath.toStdString() + filename.toStdString());
255  return newfilename;
256  }
257 
258  /** increase the alpha counter in the file name
259  *
260  * @return filename with alphabetically increased subdir
261  * @param fname the Filename whos subdir should be increased
262  */
263  static std::string increaseFileCounter(const std::string &fname)
264  {
265  QFileInfo fInfo(QString::fromStdString(fname));
266  QString filename(fInfo.baseName());
267  QStringList filenameparts(filename.split("__"));
268  QByteArray counter(filenameparts.last().toLatin1());
269  if (counter[1] == 'z')
270  {
271  const char first(counter[0]);
272  counter[0] = first + 1;
273  counter[1] = 'a';
274  }
275  else
276  {
277  const char second(counter[1]);
278  counter[1] = second + 1;
279  }
280  filenameparts.last() = QString::fromLatin1(counter);
281  QString newfilename(fInfo.path() + "/" + filenameparts.join("__") + "." + fInfo.suffix());
282  return newfilename.toStdString();
283  }
284 };
285 
286 
287 
288 /** Helper function to delete duplicates from a std::list
289  *
290  * This keeps the earliest entry in the list and removes all later ones
291  *
292  * @param l List to remove duplicates from.
293  */
294 template<typename T>
295 inline void unique(std::list<T>& l)
296 {
297  // shorten list by removing consecutive duplicates
298  l.unique();
299  // now remove remaining (harder) duplicates
300  for(typename std::list<T>::iterator i1 = l.begin();
301  i1 != l.end();
302  ++i1) {
303  typename std::list<T>::iterator i2(i1);
304  ++i2;
305  while(l.end() != (i2 = find(i2, l.end(), *i1)))
306  l.erase(i2);
307  }
308 }
309 
310 /** fuzzy compare two floating point variables
311  *
312  * @tparam the type that one want to compare
313  * @return true when the two values are equal within the precision, false otherwise
314  * @param first the first value for the equal comparison
315  * @param second the second value for the equal comparison
316  */
317 template <typename T>
318 bool fuzzycompare(const T& first, const T& second)
319 {
320  return (std::abs(first-second) < std::numeric_limits<T>::epsilon());
321 }
322 
323 /** fuzzy compare a floating point number to 0
324  *
325  * @tparam the type that one want to compare
326  * @param val the value for the comparison
327  */
328 template <typename T>
329 bool fuzzyIsNull(const T& val)
330 {
331  return (val < sqrt(std::numeric_limits<T>::epsilon()));
332 }
333 }//end namespace cass
334 
335 #endif
bool fuzzycompare(const T &first, const T &second)
fuzzy compare two floating point variables
float operator()(const float first, const float second)
operator
std::tr1::shared_ptr< self_type > shared_pointer
a shared pointer of this class
Definition: result.hpp:323
Processor::result_t::shared_pointer set2DHist(const Processor::name_t &name)
function to set the 2d histogram properties from the ini file.
static std::string increaseDirCounter(const std::string &fname)
increase the alpha counter
float operator()(float currentValue, float Average_Nm1)
the operator calculates the average over the last _nEvents
result classes
std::string loadParticle(CASSSettings &s, const std::string &detector, int ppNbr, const std::string &key)
load particle for a specific detector
float operator()(float value, float thresh) const
operator
append(const QString &str)
fromStdString(const std::string &str)
Binary function for thresholding.
float _nEvents
nEvents for the average calculation
static std::string intializeFile(const std::string &fname)
initialize the filename
bool fuzzyIsNull(const T &val)
fuzzy compare a floating point number to 0
file contains global definitions for project cass
an alphabetical counter extension
weighted_minus(float first_weight, float second_weight)
constructor.
file contains processors baseclass declaration
binary function for averaging.
std::string loadDelayDet(CASSSettings &s, int ppNbr, const std::string &key)
load detector from file
void unique(std::list< T > &l)
Helper function to delete duplicates from a std::list.
static std::string removeAlphaSubdir(const std::string &fname)
remove the alpha counter subdir from filename
fromLatin1(const char *str, int size=-1)
TimeAverage(float nEvents)
constructor.
exists(const QString &name)
Processor::result_t::shared_pointer set1DHist(const Processor::name_t &name)
function to set the 1d histogram properties from the ini file.
static std::string increaseFileCounter(const std::string &fname)
increase the alpha counter in the file name
Electron detector
Definition: hdf5-input.ini:62
std::string name_t
define the name type
Definition: processor.h:46
check if there is some light in the chamber based upon the GMD value
static std::string intializeDir(const std::string &fname)
initialize the directory
mkpath(const QString &dirPath)
binary function for weighted subtraction.