Jpp
JRunsetups.hh
Go to the documentation of this file.
1 #ifndef __JDB_JRUNSETUPS__
2 #define __JDB_JRUNSETUPS__
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <algorithm>
8 
9 #include "JDB/JRuns.hh"
10 #include "Jeep/JeepToolkit.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 namespace JDATABASE {}
17 namespace JPP { using namespace JDATABASE; }
18 
19 namespace JDATABASE {
20 
21  /**
22  * Auxiliary class for run setup evaluation.
23  *
24  * The run number is mapped to a unique floating point value according the setup name.
25  */
26  struct JRunsetups :
27  public std::map<int, float>
28  {
29 
30  static const char SEPARATOR = '.'; //!< separator between tokens in setup name
31  static const int MAX_NUMBER_OF_SETUPS = 1000; //!< maximal number of setups for a main category
32 
33 
34  /**
35  * Main run setup category.
36  */
37  enum JRuntype_t {
38  CALIB = 100, //!< Calibration
39  NANOCALIB = 200, //!< Nano-beacon calibration
40  TEST = 300, //!< Test
41  WRONG_TEST = 400, //!< Wrong test
42  PHYS = 1000, //!< Physics
43  OTHER = -1 //!< Other
44  };
45 
46 
47  /**
48  * Get run setup type.
49  *
50  * \param setup run setup name
51  * \return type
52  */
53  static inline int getType(const std::string& setup)
54  {
55  using namespace std;
56  using namespace JPP;
57 
58  static std::map<std::string, int> buffer;
59 
60  if (buffer.empty()) {
61 
62 #define MAKE_ENTRY(A) std::make_pair(getClassname(#A), A)
63 
64  buffer.insert(MAKE_ENTRY(CALIB));
65  buffer.insert(MAKE_ENTRY(NANOCALIB));
66  buffer.insert(MAKE_ENTRY(TEST));
67  buffer.insert(MAKE_ENTRY(WRONG_TEST));
68  buffer.insert(MAKE_ENTRY(PHYS));
69  buffer.insert(MAKE_ENTRY(OTHER));
70 
71 #undef MAKE_ENTRY
72  }
73 
74  string key = setup;
75 
76  string::size_type pos = key.find(SEPARATOR);
77 
78  if (pos != string::npos) {
79  key = key.substr(0,pos);
80  }
81 
82  std::map<std::string, int>::const_iterator i = buffer.find(key);
83 
84  if (i != buffer.end())
85  return i->second;
86  else
87  return OTHER;
88  }
89 
90 
91  /**
92  * Put run parameters.
93  *
94  * \param run run number
95  * \param setup run setup name
96  */
97  void put(const int run,
98  const std::string setup)
99  {
100  using namespace std;
101  using namespace JPP;
102 
103  const int type = getType(setup);
104  vector<string>& buffer = data[type];
105 
106  vector<string>::iterator p = std::find(buffer.begin(), buffer.end(), setup);
107 
108  if (p == buffer.end()) {
109 
110  buffer.push_back(setup);
111 
112  p = buffer.rbegin().base();
113  }
114 
115  const int index = distance(buffer.begin(),p) + 1;
116 
117  if (index >= MAX_NUMBER_OF_SETUPS) {
118  cerr << "Index of " << setup << ' ' << index << " >= " << MAX_NUMBER_OF_SETUPS << endl;
119  }
120 
121  if (type > 0)
122  (*this)[run] = type + (float) index / (float) MAX_NUMBER_OF_SETUPS;
123  else
124  (*this)[run] = type - (float) index / (float) MAX_NUMBER_OF_SETUPS;
125  }
126 
127 
128  /**
129  * Put run parameters.
130  *
131  * \param parameters run parameters
132  */
133  void put(const JRuns& parameters)
134  {
135  put(parameters.RUN, parameters.RUNSETUPNAME);
136  }
137 
138 
139  /**
140  * Check if run setup is vailable.
141  *
142  * \param run run number
143  * \return true if available; else false
144  */
145  bool has(const int run) const
146  {
147  return this->find(run) != this->end();
148  }
149 
150 
151  /**
152  * Get run setup value.
153  *
154  * \param run run number
155  * \return value
156  */
157  float get(const int run) const
158  {
159  const_iterator p = this->find(run);
160 
161  if (p != this->end())
162  return p->second;
163  else
164  return 0.0;
165  }
166 
167  protected:
168  mutable std::map<int, std::vector<std::string> > data; // run type -> run setup names
169  };
170 }
171 
172 #endif
173 
JDATABASE::JRuns::RUNSETUPNAME
std::string RUNSETUPNAME
Definition: JRuns.hh:27
JDATABASE::JRunsetups::TEST
Test.
Definition: JRunsetups.hh:40
JDATABASE::JRunsetups::JRuntype_t
JRuntype_t
Main run setup category.
Definition: JRunsetups.hh:37
JDATABASE::JRunsetups::OTHER
Other.
Definition: JRunsetups.hh:43
JDATABASE::JRunsetups::NANOCALIB
Nano-beacon calibration.
Definition: JRunsetups.hh:39
JDATABASE::JRunsetups::MAX_NUMBER_OF_SETUPS
static const int MAX_NUMBER_OF_SETUPS
maximal number of setups for a main category
Definition: JRunsetups.hh:31
JDATABASE::JRunsetups::put
void put(const int run, const std::string setup)
Put run parameters.
Definition: JRunsetups.hh:97
std::vector
Definition: JSTDTypes.hh:12
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
JDATABASE::JRunsetups::getType
static int getType(const std::string &setup)
Get run setup type.
Definition: JRunsetups.hh:53
JDATABASE
Auxiliary classes and methods for database I/O.
Definition: JAHRS.hh:12
JDATABASE::JRunsetups::CALIB
Calibration.
Definition: JRunsetups.hh:38
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
MAKE_ENTRY
#define MAKE_ENTRY(A)
JDATABASE::JRunsetups
Auxiliary class for run setup evaluation.
Definition: JRunsetups.hh:26
JDATABASE::JRunsetups::get
float get(const int run) const
Get run setup value.
Definition: JRunsetups.hh:157
JDATABASE::JRunsetups::data
std::map< int, std::vector< std::string > > data
Definition: JRunsetups.hh:168
JDATABASE::JRunsetups::PHYS
Physics.
Definition: JRunsetups.hh:42
std::map
Definition: JSTDTypes.hh:16
JDATABASE::JRunsetups::SEPARATOR
static const char SEPARATOR
separator between tokens in setup name
Definition: JRunsetups.hh:30
JDATABASE::JRunsetups::WRONG_TEST
Wrong test.
Definition: JRunsetups.hh:41
JDATABASE::JRunsetups::has
bool has(const int run) const
Check if run setup is vailable.
Definition: JRunsetups.hh:145
std
Definition: jaanetDictionary.h:36
JRuns.hh
JeepToolkit.hh
JDATABASE::JRunsetups::put
void put(const JRuns &parameters)
Put run parameters.
Definition: JRunsetups.hh:133
JDATABASE::JRuns
Definition: JRuns.hh:17
JDATABASE::JRuns::RUN
int RUN
Definition: JRuns.hh:23