Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JMultiHead.hh
Go to the documentation of this file.
1 #ifndef __JAANET_JMULTIHEAD__
2 #define __JAANET_JMULTIHEAD__
3 
4 #include <map>
5 #include <vector>
6 #include <string>
7 
11 
12 #include "JLang/JUUID.hh"
13 #include "JLang/JException.hh"
14 
15 #include "JAAnet/JHead.hh"
19 
20 
21 /**
22  * \author bjjung
23  */
24 
25 namespace JAANET {}
26 namespace JPP { using namespace JAANET; }
27 
28 namespace JAANET {
29 
30  using JLANG::JUUID;
31 
32 
33  /**
34  * Auxiliary data structure to store multiple headers\n
35  * and bookkeep event-weight normalisations.
36  */
37  struct JMultiHead :
38  public MultiHead
39  {
41 
42 
43  /**
44  * Default constructor.
45  */
47  MultiHead()
48  {}
49 
50 
51  /**
52  * Merge the given header into this object.
53  *
54  * \param header header
55  */
56  void merge(const JHead& header)
57  {
58  insert(header);
59  merge();
60  }
61 
62 
63  /**
64  * Insert the given header.
65  *
66  * \param header header
67  * \return true if insertion was successful; else false
68  */
69  bool insert(const JHead& header)
70  {
71  using namespace std;
72 
73  Head head;
74  JAANET::copy(header, head);
75 
76  uuid_t uuid;
77  uuid_clear(uuid);
78 
79  Head::iterator i = head.find(Head::tags::UUID);
80 
81  if (i != head.end()) {
82  uuid_parse(i->second.c_str(), uuid);
83  }
84 
85  if (uuid_is_null(uuid) != 0) {
86  uuid_generate_random(uuid); // Ensure valid UUID
87  }
88 
89  char buffer[JUUID::BUFFER_SIZE + 1];
90  uuid_unparse_lower(uuid, buffer);
91 
92  head.insert(make_pair(Head::tags::UUID, buffer));
93 
94  return MultiHead::insert(head);
95  }
96 
97 
98  /**
99  * Check if given header UUID correspond to a file which has been merged.
100  *
101  * \param UUID header UUID
102  * \return true if given header UUID correspond to file which has been merged
103  */
104  bool isMerged(const JUUID& UUID) const
105  {
106  using namespace JPP;
107 
108  map_type::const_iterator i = normalisationMap.find(UUID);
109 
110  if (i != normalisationMap.end()) {
111  return i->first != i->second.UUID;
112  } else {
113  THROW(JValueOutOfRange, "JMultiHead::isMerged(): Given header UUID " << UUID << " not found.");
114  }
115  }
116 
117 
118  /**
119  * Get the header UUID corresponding to the given event.
120  *
121  * \param event event
122  * \return header UUID
123  */
124  JUUID getHeaderUUID(const Evt& event) const
125  {
126  using namespace JPP;
127 
128  const JUUID UUID = event.header_uuid;
129 
130  map_type::const_iterator i = normalisationMap.find(UUID);
131 
132  if (i != normalisationMap.cend()) {
133  return i->second.UUID;
134  } else {
135  THROW(JValueOutOfRange, "JMultiHead::getHeaderUUID(): Header UUID " << UUID << " for given event not found.");
136  }
137  }
138 
139 
140  /**
141  * Get normalisation for given header UUID.
142  *
143  * \param UUID header UUID
144  * \return event-weight normalisation factor
145  */
146  double getNormalisation(const JUUID& UUID) const
147  {
148  using namespace JPP;
149 
150  map_type::const_iterator i = normalisationMap.find(UUID);
151 
152  if (i != normalisationMap.end()) {
153  return i->second.normalisation;
154  } else {
155  THROW(JValueOutOfRange, "JMultiHead::getNormalisation(): Given header UUID " << UUID << " not found.");
156  }
157  }
158 
159 
160  /**
161  * Get normalisation for given event.
162  *
163  * \param event event
164  * \return event-weight normalisation factor
165  */
166  double getNormalisation(const Evt& event) const
167  {
168  return getNormalisation(event.header_uuid);
169  }
170 
171 
172  /**
173  * Merge all matching headers.
174  */
175  void merge()
176  {
177  using namespace std;
178  using namespace JPP;
179 
180  const MultiHead copy = static_cast<const MultiHead&>(*this);
181  this->clear();
182 
183  for (const_iterator i = copy.cbegin(); i != copy.cend(); ++i) {
184 
185  JHead header1 = *i;
186  header1.createUUID(); // Ensure header UUID is set
187 
188  const JUUID UUID = header1.UUID; // Initial header UUID
189 
190  // Merge matching headers following this header in the container
191 
192  vector<JUUID> mergedHeaders; // Track all headers involved in mergers
193 
194  for (const_iterator j = next(i); j != copy.end(); ++j) {
195 
196  const JHead header2 = *j;
197 
198  if (header1.match(header2)) {
199 
200  header1.add(header2);
201  mergedHeaders.push_back(header2.UUID);
202  }
203  }
204 
205  // Retrieve event-weight normalisation for merged header
206 
207  const JEvtWeight& weighter = getEventWeighter(header1);
208 
209  JEvtWeightHelper helper(weighter);
210  helper.add(header1);
211 
212  const double norm = helper->getNormalisation();
213 
214  // Set event-weight normalisations for headers involved in mergers
215 
216  for (vector<JUUID>::const_iterator j = mergedHeaders.cbegin(); j != mergedHeaders.cend(); ++j) {
217  normalisationMap[*j] = JEvtWeightNormalisation(header1.UUID, norm);
218  }
219 
220  // Set event-weight normalisation for original header
221 
222  normalisationMap[UUID] = JEvtWeightNormalisation(header1.UUID, norm);
223 
224  insert(header1); // Add merged header to buffer
225  }
226  }
227 
228 
229  map_type normalisationMap; //!< Map between header UUIDs and event-weight normalisations
230  };
231 }
232 
233 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Monte Carlo run header.
Definition: JHead.hh:1236
JHead & add(const JHead &header)
Addition of headers.
Definition: JHead.hh:1532
void createUUID()
Create UUID if not already set.
Definition: JHead.hh:1301
JUUID UUID
Definition: JHead.hh:1584
bool match(const JHead &header) const
Test match of headers.
Definition: JHead.hh:1474
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:180
Extensions to Evt data format.
JEvtWeighter getEventWeighter
Function object for mapping header to event weighter.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
int j
Definition: JPolint.hh:792
Definition: JSTDTypes.hh:14
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:21
uuid_t header_uuid
UUID of header containing the event-weight information.
Definition: Evt.hh:35
static constexpr const char *const UUID
Definition: Head.hh:67
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:65
Helper class for event weighing.
double getNormalisation() const
Get event-weight normalisation.
void add(const JHead &header)
Add header.
Auxiliary data structure for storing pairs of header UUIDs and event-weight normalisations.
Abstract base class for event weighing.
Definition: JEvtWeight.hh:31
Auxiliary data structure to store multiple headers and bookkeep event-weight normalisations.
Definition: JMultiHead.hh:39
JMultiHead()
Default constructor.
Definition: JMultiHead.hh:46
map_type normalisationMap
Map between header UUIDs and event-weight normalisations.
Definition: JMultiHead.hh:229
double getNormalisation(const Evt &event) const
Get normalisation for given event.
Definition: JMultiHead.hh:166
bool insert(const JHead &header)
Insert the given header.
Definition: JMultiHead.hh:69
void merge()
Merge all matching headers.
Definition: JMultiHead.hh:175
void merge(const JHead &header)
Merge the given header into this object.
Definition: JMultiHead.hh:56
double getNormalisation(const JUUID &UUID) const
Get normalisation for given header UUID.
Definition: JMultiHead.hh:146
std::map< JUUID, JAANET::JEvtWeightNormalisation > map_type
Definition: JMultiHead.hh:40
bool isMerged(const JUUID &UUID) const
Check if given header UUID correspond to a file which has been merged.
Definition: JMultiHead.hh:104
JUUID getHeaderUUID(const Evt &event) const
Get the header UUID corresponding to the given event.
Definition: JMultiHead.hh:124
Simple wrapper for UUID.
Definition: JUUID.hh:24
static const int BUFFER_SIZE
number of characters for I/O of uuid_t without trailing '\0', see e.g. man uuid_parse
Definition: JUUID.hh:26
bool insert(const Head &header)
Insert the given header.
Definition: MultiHead.hh:111