Bayeux  3.4.1
Core Foundation library for SuperNEMO
simulated_data.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date: 2010-03-15
4  * Last modified: 2018-08-30
5  *
6  * License: GPL3
7  *
8  * Description:
9  *
10  * Simulation data based on the 'genbb::primary_event'
11  * class from the 'genbb_help' package.
12  *
13  */
14 
15 #ifndef MCTOOLS_SIMULATED_DATA_H
16 #define MCTOOLS_SIMULATED_DATA_H 1
17 
18 // Standard Library:
19 #include <string>
20 #include <map>
21 #include <vector>
22 
23 // Third party:
24 // - Boost :
25 #include <boost/serialization/access.hpp>
26 #include <boost/cstdint.hpp>
27 // - Bayeux/datatools :
29 #include <datatools/i_clear.h>
30 #include <datatools/i_tree_dump.h>
31 #include <datatools/handle.h>
32 #include <datatools/bit_mask.h>
33 #include <datatools/properties.h>
34 // - Bayeux/genbb_help :
36 
37 // This project:
38 #include <mctools/base_step_hit.h>
39 
40 namespace mctools {
41 
46  , public datatools::i_clear
47  {
48  public:
49 
55  };
56 
63  };
64 
67 
69  typedef std::vector<hit_handle_type> hit_handle_collection_type;
70 
72  typedef std::vector<base_step_hit> hit_collection_type;
73 
78  typedef std::map<std::string, hit_handle_collection_type> step_hits_dict_type;
79 
84  typedef std::map<std::string, hit_collection_type> plain_step_hits_dict_type;
85 
87  typedef ::genbb::primary_event primary_event_type;
88 
90  void reset_collection_type();
91 
93  void set_collection_type(const int collection_type_);
94 
96  bool use_plain_hit_collection() const;
97 
99  bool use_handle_hit_collection() const;
100 
102  bool has_data() const;
103 
105  bool has_vertex() const;
106 
108  const geomtools::vector_3d & get_vertex() const;
109 
112 
114  void set_vertex(const geomtools::vector_3d &);
115 
117  bool has_time() const;
118 
120  double get_time() const;
121 
123  void set_time(const double);
124 
126  const primary_event_type & get_primary_event() const;
127 
130 
133 
135  const datatools::properties & get_properties() const;
136 
139 
140  // Set the collection of auxiliary properties
142 
145 
147  const step_hits_dict_type & get_step_hits_dict() const;
148 
151 
154 
156  void get_step_hits_categories(std::vector<std::string> & categories_,
157  unsigned int mode_ = HIT_CATEGORY_TYPE_ALL,
158  const std::string & prefix_ = "") const;
159 
161  simulated_data & add_step_hits(const std::string & category_, size_t capacity_ = 0);
162 
164  simulated_data & remove_step_hits(const std::string & category_);
165 
167  base_step_hit & add_step_hit(const std::string & category_);
168 
170  bool has_step_hits(const std::string & category_) const;
171 
173  size_t get_number_of_step_hits(const std::string & category_) const;
174 
176  const base_step_hit & get_step_hit(const std::string & category_, int hit_index_) const;
177 
179  base_step_hit & grab_step_hit(const std::string & category_, int hit_index_);
180 
182  template <class Hit>
183  Hit & add_hit(const std::string & category_)
184  {
185  Hit * h = nullptr;
187  step_hits_dict_type::iterator found
188  = _step_hits_dict_.find(category_);
189  DT_THROW_IF(found == _step_hits_dict_.end(),
190  std::logic_error,
191  "No collection of hits with category '" << category_ << "' !");
192  datatools::handle<mctools::base_step_hit> hh(dynamic_cast<mctools::base_step_hit*>(new Hit));
193  found->second.push_back(hh);
194  h = dynamic_cast<Hit*>(&found->second.back().grab());
195  } else {
196  DT_THROW(std::logic_error, "Unsupported method for plain hit collection!");
197  }
198  return *h;
199  }
200 
201  bool has_hit(const std::string & category_, const int index_) const;
202 
203  template <class Hit>
204  bool is_hit(const std::string & category_, const int index_) const
205  {
206  DT_THROW_IF(!use_handle_hit_collection(), std::logic_error, "Unsupported method for plain hit collection!");
207  step_hits_dict_type::const_iterator found = _step_hits_dict_.find(category_);
208  DT_THROW_IF(found == _step_hits_dict_.end(),
209  std::logic_error,
210  "No collection of handles of hits with category '" << category_ << "' !");
211  DT_THROW_IF(index_ < 0 || index_ >=(int)found->second.size(),
212  std::logic_error,
213  "Invalid hit index in category '" << category_ << "' !");
214  DT_THROW_IF(! found->second[index_].has_data(),
215  std::logic_error,
216  "Null handle at index " << index_ << " in category '" << category_ << "' !");
217  // Extract the hit:
218  const base_step_hit * bsh = &found->second[index_].get();
219  // Attempt to cast it to the proper type:
220  if (dynamic_cast<const Hit *>(bsh) == nullptr) {
221  return false;
222  }
223  return true;
224  }
225 
226  template <class Hit>
227  const Hit & get_hit(const std::string & category_, const int index_) const
228  {
229  DT_THROW_IF(!use_handle_hit_collection(), std::logic_error, "Unsupported method for plain hit collection!");
230  step_hits_dict_type::const_iterator found = _step_hits_dict_.find(category_);
231  DT_THROW_IF(found == _step_hits_dict_.end(),
232  std::logic_error,
233  "No collection of hits with category '" << category_ << "' !");
234  DT_THROW_IF(index_ < 0 || index_ >=(int)found->second.size(),
235  std::logic_error,
236  "Invalid hit index in category '" << category_ << "' !");
237  DT_THROW_IF(! found->second[index_].has_data(),
238  std::logic_error,
239  "Null handle at index " << index_ << " in category '" << category_ << "' !");
240  // Extract the hit:
241  const base_step_hit * bsh = &found->second[index_].get();
242  // Attempt to cast it to the proper type:
243  const Hit * h = dynamic_cast<const Hit*>(bsh);
244  if (h == nullptr) {
245  DT_THROW(std::logic_error, "Invalid type for hit #" << index_ << " in category '" << category_ << "'!");
246  }
247  return *h;
248  }
249 
251  hit_handle_collection_type & grab_step_hits(const std::string & category_);
252 
254  const hit_handle_collection_type & get_step_hits(const std::string & category_) const;
255 
257  hit_collection_type & grab_plain_step_hits(const std::string & category_);
258 
260  const hit_collection_type & get_plain_step_hits(const std::string & category_) const;
261 
263  simulated_data & reset(bool reset_collection_type_);
264 
266  void reset();
267 
269  simulated_data();
270 
272  explicit simulated_data(int collection_type_);
273 
275  virtual ~simulated_data();
276 
278  virtual void clear();
279 
281  virtual void tree_dump(std::ostream & out_ = std::clog,
282  const std::string & title_ = "",
283  const std::string & indent_ = "",
284  bool inherit_ = false) const;
285 
297  void print_tree(std::ostream & out_ = std::clog,
298  const boost::property_tree::ptree & options_ = datatools::i_tree_dumpable::empty_options()) const override;
299 
300 
301  protected:
302 
304  void _set_defaults();
305 
306  private:
307 
308  // Attributes :
309  geomtools::vector_3d _vertex_;
310  double _time_;
311  primary_event_type _primary_event_;
312  datatools::properties _properties_;
313  int8_t _collection_type_;
314  step_hits_dict_type _step_hits_dict_;
315  plain_step_hits_dict_type _plain_step_hits_dict_;
316 
317  // datatools/Boost/brio serialization:
319 
320 #if MCTOOLS_WITH_REFLECTION == 1
321  DR_CLASS_RTTI()
323 #endif
324 
325  };
326 
327 } // end of namespace mctools
328 
329 #include <boost/serialization/export.hpp>
330 BOOST_CLASS_EXPORT_KEY2(mctools::simulated_data, "mctools::simulated_data")
331 
332 #if MCTOOLS_WITH_REFLECTION == 1
333 // Activate reflection layer for the mctools::simulated_data class :
335 #endif // MCTOOLS_WITH_REFLECTION
336 
337 #include <boost/serialization/version.hpp>
338 BOOST_CLASS_VERSION(mctools::simulated_data, 3)
339 
340 #endif // MCTOOLS_SIMULATED_DATA_H
341 
342 // Local Variables: --
343 // mode: c++ --
344 // c-file-style: "gnu" --
345 // tab-width: 2 --
346 // End: --
static const boost::property_tree::ptree & empty_options()
#define DR_CLASS_INIT(Introspectable)
Inform Camp that class Introspectable exists and trigger the automatic registration of dedicated refl...
Definition: reflection_interface.h:149
The base class for all Monte-Carlo (MC) hit objects.
Definition: base_step_hit.h:47
datatools::handle< base_step_hit > hit_handle_type
Alias for the MC base step hit handle type.
Definition: simulated_data.h:66
void reset()
Reset data.
void reset_collection_type()
Reset the memory layout for hit storage.
simulated_data & add_step_hits(const std::string &category_, size_t capacity_=0)
Add a new collection of MC hits with some given category and a default capacity for memory allocation...
const plain_step_hits_dict_type & get_plain_step_hits_dict() const
Get a reference to the non mutable collection of plain MC hits.
#define DT_THROW(ExceptionType, Message)
Definition: exception.h:121
Base abstract class of all serializable (and possibly introspectable) classes.
Definition: i_serializable.h:51
void set_time(const double)
Set the reference time.
Definition: base_step_hit.h:32
base_step_hit & grab_step_hit(const std::string &category_, int hit_index_)
Get a reference to the mutable MC hit within a given category and index.
double get_time() const
Get the reference time.
const hit_collection_type & get_plain_step_hits(const std::string &category_) const
The container of collections of MC hits.
Definition: simulated_data.h:43
bool has_vertex() const
Check if a vertex is defined.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
static const uint32_t bit01
Definition: bit_mask.h:28
const step_hits_dict_type & get_step_hits_dict() const
Get a reference to the non mutable collection of MC hits handles.
Invalid type of hit collection.
Definition: simulated_data.h:52
const base_step_hit & get_step_hit(const std::string &category_, int hit_index_) const
Get a reference to the non mutable MC hit within a given category and index.
const datatools::properties & get_properties() const
Get a reference to the non mutable collection of auxiliary properties.
geomtools::vector_3d & grab_vertex()
Get a reference to the mutable vertex.
void set_collection_type(const int collection_type_)
Set the memory layout for hit storage (collection of plain MC hits/collections of MC hits handles)
void set_primary_event(const primary_event_type &)
Set the primary event.
bool has_time() const
Check if a time is defined.
#define DATATOOLS_SERIALIZATION_DECLARATION()
Definition: i_serializable.h:266
std::map< std::string, hit_handle_collection_type > step_hits_dict_type
Definition: simulated_data.h:78
virtual ~simulated_data()
Destructor.
collection_type
Type of the memory layout of the collection of MC hits.
Definition: simulated_data.h:51
hit_category_type
Categories of MC hits.
Definition: simulated_data.h:58
std::vector< base_step_hit > hit_collection_type
Alias for the collection of MC base step hits.
Definition: simulated_data.h:72
void _set_defaults()
Set default values to attributes.
Templatized handle class that wraps a Boost shared pointer and behaves like a reference.
Definition: handle.h:114
virtual void clear()
Reset the internal data.
hit_collection_type & grab_plain_step_hits(const std::string &category_)
std::vector< hit_handle_type > hit_handle_collection_type
Alias for the collection of MC base step hit handles.
Definition: simulated_data.h:69
simulated_data()
Default constructor.
static const uint32_t bit02
Definition: bit_mask.h:29
bool has_hit(const std::string &category_, const int index_) const
std::map< std::string, hit_collection_type > plain_step_hits_dict_type
Definition: simulated_data.h:84
size_t get_number_of_step_hits(const std::string &category_) const
Get the number of MC hits within a given category.
A pure abstract class (interface) for inherited clearable classes.
Definition: i_clear.h:9
Definition: simulated_data.h:62
void get_step_hits_categories(std::vector< std::string > &categories_, unsigned int mode_=HIT_CATEGORY_TYPE_ALL, const std::string &prefix_="") const
Get a list of categories associated to existing collections of MC hits.
bool use_handle_hit_collection() const
Check if the memory layout for hit storage uses collection of MC hits handles.
const Hit & get_hit(const std::string &category_, const int index_) const
Definition: simulated_data.h:227
step_hits_dict_type & grab_step_hits_dict()
Get a reference to the mutable collection of MC hits handles.
hit_handle_collection_type & grab_step_hits(const std::string &category_)
Get a reference to the mutable collection of MC hits handles with a given category.
primary_event_type & grab_primary_event()
Get a reference to the mutable primary event.
const geomtools::vector_3d & get_vertex() const
Get a reference to the non mutable vertex.
const hit_handle_collection_type & get_step_hits(const std::string &category_) const
Get a reference to the non mutable collection of MC hits handles with a given category.
::genbb::primary_event primary_event_type
Alias for the primary generated event type.
Definition: simulated_data.h:87
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
CLHEP::Hep3Vector vector_3d
Alias for the CLHEP 3D-vector class.
Definition: clhep.h:63
bool has_step_hits(const std::string &category_) const
Check is some MC hits exists in some given category.
void set_vertex(const geomtools::vector_3d &)
Set the vertex.
datatools::properties & grab_properties()
Get a reference to the mutable collection of auxiliary properties.
const primary_event_type & get_primary_event() const
Get a reference to the non mutable primary event.
A primary event from a Monte-Carlo generator.
Definition: primary_event.h:60
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
bool is_hit(const std::string &category_, const int index_) const
Definition: simulated_data.h:204
base_step_hit & add_step_hit(const std::string &category_)
Add/append a new MC hit in a collection of MC hits with some given category.
plain_step_hits_dict_type & grab_plain_step_hits_dict()
Get a reference to the mutable collection of plain MC hits.
bool has_data() const
Check if some collections of MC hits exist.
Collection of hit handles.
Definition: simulated_data.h:54
static const uint32_t bit00
Definition: bit_mask.h:27
void print_tree(std::ostream &out_=std::clog, const boost::property_tree::ptree &options_=datatools::i_tree_dumpable::empty_options()) const override
#define DR_CLASS_RTTI()
Declare Camp RTTI within class declaration.
Definition: reflection_interface.h:46
bool use_plain_hit_collection() const
Check if the memory layout for hit storage uses collection of plain MC hits.
Hit & add_hit(const std::string &category_)
Add/append a new MC hit in a collection of MC hits with some given category.
Definition: simulated_data.h:183
simulated_data & remove_step_hits(const std::string &category_)
Remove a collection of MC hits with some given category.
void set_properties(const datatools::properties &)
A dictionary of arbitrary properties.
Definition: properties.h:125