LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
ActiveVolumeVertexSampler.cxx
Go to the documentation of this file.
1 
11 
12 //------------------------------------------------------------------------------
15  rndm::NuRandomService& rand_service, const geo::Geometry& geom,
16  const std::string& generator_name)
17  : fVertexType(vertex_type_t::kSampled), fGeneratorName(generator_name),
18  fTPCDist(nullptr)
19 {
20  // Configure the algorithm using the FHiCL parameters
21  this->reconfigure(conf, geom);
22 
23  // Register the TPC sampling engine with the seed service. If you need the
24  // seed later, get it from the seed service using the value of the variable
25  // generator_name as the instance name.
26  rndm::NuRandomService::seed_t tpc_seed = rand_service.registerEngine(
27  [this](rndm::NuRandomService::EngineId const& /* unused */,
28  rndm::NuRandomService::seed_t lar_seed) -> void
29  {
30  auto seed = static_cast<uint_fast64_t>(lar_seed);
31  // Use the obtained seed to prepare the random number engine. This is
32  // an attempt to do a decent job, but optimally accomplishing this can
33  // be tricky (see, for example,
34  // http://www.pcg-random.org/posts/cpp-seeding-surprises.html)
35  std::seed_seq seed_sequence{seed};
36  fTPCEngine.seed(seed_sequence);
37  },
38  fGeneratorName, conf.get_PSet(), { "seed" }
39  );
40 
41  // TODO: resolve the other workaround mentioned in the MARLEYHelper
42  // class, then fix this as well
43  uint_fast64_t tpc_cast_seed = static_cast<uint_fast64_t>(tpc_seed);
44  std::seed_seq tpc_seed_sequence{tpc_cast_seed};
45  fTPCEngine.seed(tpc_seed_sequence);
46 }
47 
48 //------------------------------------------------------------------------------
50  const geo::Geometry& geom)
51 {
52  // sample a new position if needed
54 
55  // Sample a TPC index using the active masses as weights
56  size_t tpc_index = fTPCDist->operator()(fTPCEngine);
57 
58  // Get the dimensions of the chosen TPC's active volume
59  const auto& tpc = geom.TPC(tpc_index);
60  double minX = tpc.MinX();
61  double maxX = tpc.MaxX();
62  double minY = tpc.MinY();
63  double maxY = tpc.MaxY();
64  double minZ = tpc.MinZ();
65  double maxZ = tpc.MaxZ();
66  std::uniform_real_distribution<double>::param_type x_range(minX, maxX);
67  std::uniform_real_distribution<double>::param_type y_range(minY, maxY);
68  std::uniform_real_distribution<double>::param_type z_range(minZ, maxZ);
69 
70  // Sample a location uniformly over this volume
71  static std::uniform_real_distribution<double> uniform_dist;
72  double x = uniform_dist(fTPCEngine, x_range);
73  double y = uniform_dist(fTPCEngine, y_range);
74  double z = uniform_dist(fTPCEngine, z_range);
75  LOG_INFO("ActiveVolumeVertexSampler " + fGeneratorName)
76  << "Sampled primary vertex in TPC #" << tpc_index << ", x = " << x
77  << ", y = " << y << ", z = " << z;
78 
79  // Update the vertex position 4-vector
80  fVertexPosition.SetXYZT(x, y, z, 0.); // TODO: add time sampling
81  }
82 
83  // if we're using a fixed vertex position, we don't need to do any sampling
84 
85  return fVertexPosition;
86 }
87 
88 //------------------------------------------------------------------------------
91  const geo::Geometry& geom)
92 {
93  auto type = conf().type_();
94  if (type == "sampled") {
95 
97 
98  // Get the active masses of each of the TPCs in the current geometry. Use
99  // them as weights for sampling a TPC to use for the primary vertex.
100  std::vector<double> tpc_masses;
101  size_t num_tpcs = geom.NTPC();
102  for (size_t iTPC = 0; iTPC < num_tpcs; ++iTPC) {
103  // For each TPC, use its active mass (returned in kg) as its sampling
104  // weight
105  tpc_masses.push_back(geom.TPC(iTPC).ActiveMass());
106  }
107 
108  // Load the discrete distribution used to sample TPCs with the up-to-date
109  // set of weights
110  fTPCDist.reset(new std::discrete_distribution<size_t>(tpc_masses.begin(),
111  tpc_masses.end()));
112  }
113  else if (type == "fixed") {
114 
116 
117  auto vertex_pos = conf().position_();
118  double Vx = vertex_pos.at(0);
119  double Vy = vertex_pos.at(0);
120  double Vz = vertex_pos.at(0);
121 
122  fVertexPosition.SetXYZT(Vx, Vy, Vz, 0.); // TODO: add time setting
123  }
124  else throw cet::exception("ActiveVolumeVertexSampler " + fGeneratorName)
125  << "Invalid vertex type '" << type << "' requested. Allowed values are"
126  << " 'sampled' and 'fixed'";
127 }
Float_t x
Definition: compare.C:6
#define LOG_INFO(category)
double minY
Definition: plot_hist.C:9
Float_t y
Definition: compare.C:6
Double_t z
Definition: plot.C:279
double MinX() const
Returns the world x coordinate of the start of the box.
Definition: BoxBoundedGeo.h:90
ParameterSet const & get_PSet() const
Definition: Table.h:69
double maxY
Definition: plot_hist.C:10
TLorentzVector sample_vertex_pos(const geo::Geometry &geom)
double ActiveMass() const
Half width (associated with x coordinate) of active TPC volume [cm].
Definition: TPCGeo.h:110
art::RandomNumberGenerator::seed_t seed_t
ActiveVolumeVertexSampler(const fhicl::Table< Config > &conf, rndm::NuRandomService &rand_service, const geo::Geometry &geom, const std::string &generator_name)
Algorithm that samples vertex locations uniformly within the active volume of a detector. It is fully experiment-agnostic and multi-TPC aware.
long seed
Definition: chem4.cc:68
Identifier for a engine, made of module name and optional instance name.
Definition: EngineId.h:22
The geometry of one entire detector, as served by art.
Definition: Geometry.h:110
unsigned int NTPC(unsigned int cstat=0) const
Returns the total number of TPCs in the specified cryostat.
void reconfigure(const fhicl::Table< Config > &conf, const geo::Geometry &geom)
std::unique_ptr< std::discrete_distribution< size_t > > fTPCDist
TPCGeo const & TPC(unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified TPC.
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string instance="")
Registers an existing engine with NuRandomService.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33