All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4InfoReducer_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: G4InfoReducer
3 // Plugin Type: producer (Unknown Unknown)
4 // File: G4InfoReducer_module.cc
5 //
6 // Generated at Thu Jul 14 11:04:27 2022 by Laura Domine using cetskelgen
7 // from version .
8 ////////////////////////////////////////////////////////////////////////
9 
10 #include "art/Framework/Core/EDProducer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Principal/Run.h"
15 #include "art/Framework/Principal/SubRun.h"
16 #include "canvas/Utilities/InputTag.h"
17 #include "fhiclcpp/ParameterSet.h"
18 #include "messagefacility/MessageLogger/MessageLogger.h"
19 
24 #include "larcore/CoreUtils/ServiceUtil.h" // for lar::providerFrom
25 #include "lardata/DetectorInfoServices/DetectorPropertiesServiceStandard.h" // for DetectorClocksService
26 
27 #include <memory>
28 #include <set>
29 
30 class G4InfoReducer;
31 
32 
33 class G4InfoReducer : public art::EDProducer {
34 public:
35  explicit G4InfoReducer(fhicl::ParameterSet const& p);
36  // The compiler-generated destructor is fine for non-base
37  // classes without bare pointers or other resource use.
38 
39  // Plugins should not be copied or assigned.
40  G4InfoReducer(G4InfoReducer const&) = delete;
41  G4InfoReducer(G4InfoReducer&&) = delete;
42  G4InfoReducer& operator=(G4InfoReducer const&) = delete;
44 
45  // Required functions.
46  void produce(art::Event& e) override;
47 
48 private:
49 
50  // Declare member data here.
51  art::InputTag fSedLabel; ///< module making the SimEnergyDeposit
52  double fMinX, fMinY, fMinZ; ///< bottom left coordinate of union of all TPC active volumes
53  double fVoxelSizeX, fVoxelSizeY, fVoxelSizeZ; ///< size of a voxel (cm)
54  //services
56 };
57 
58 
59 G4InfoReducer::G4InfoReducer(fhicl::ParameterSet const& p)
60  : EDProducer{p} // ,
61  , fGeometry(*lar::providerFrom<geo::Geometry>())
62 {
63  fSedLabel = p.get<art::InputTag>("SimEnergyDepositLabel", "largeant:TPCActive");
64 
65  // Prepare for voxelization
66  fVoxelSizeX = p.get<double>("VoxelSizeX", 0.3);
67  fVoxelSizeY = p.get<double>("VoxelSizeY", 0.3);
68  fVoxelSizeZ = p.get<double>("VoxelSizeZ", 0.3);
69  if (fVoxelSizeX <= 0. || fVoxelSizeY <= 0. || fVoxelSizeZ <= 0.) {
70  std::cerr << "Voxel size must be strictly greater than zero." << std::endl;
71  throw std::exception();
72  }
73 
74  double min_x = std::numeric_limits<double>::max();
75  double min_y = std::numeric_limits<double>::max();
76  double min_z = std::numeric_limits<double>::max();
77  for(geo::TPCGeo const& tpc: fGeometry.IterateTPCs()) {
78  auto const& tpcabox = tpc.ActiveBoundingBox();
79  min_x = std::min(min_x, tpcabox.MinX());
80  min_y = std::min(min_y, tpcabox.MinY());
81  min_z = std::min(min_z, tpcabox.MinZ());
82  }
83  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
84  // Take into account TPC readout window size
85  // Ionization electrons travel at ~1.6mm/us at 500 V/cm electric field
86  fMinX = min_x + clockData.TriggerOffsetTPC() * 1.6 / 10;
87  fMinY = min_y;
88  fMinZ = min_z;
89  //std::cout << "G4InfoReducer " << fMinX << " " << fMinY << " " << fMinZ << std::endl;
90  //std::cout << clockData.TriggerOffsetTPC() << std::endl;
91 
92  // Call appropriate produces<>() functions here.
93  produces<std::vector<sim::SimEnergyDepositLite>>();
94 
95  // Call appropriate consumes<>() for any products to be retrieved by this module.
96  consumes<std::vector<sim::SimEnergyDeposit>>(fSedLabel);
97 }
98 
99 void G4InfoReducer::produce(art::Event& e)
100 {
101  // Implementation of required member function here.
102 
103  // Collect SimEnergyDeposit
104  auto handle = e.getValidHandle<std::vector<sim::SimEnergyDeposit>>(fSedLabel);
105  if (!handle.isValid()) {
106  // throw some error
107  std::cerr << "SimEnergyDeposit not found" << std::endl;
108  throw std::exception();
109  }
110 
111  struct comp {
112  bool operator() (sim::SimEnergyDepositLite lhs, sim::SimEnergyDepositLite rhs) const {
113  if (lhs.X() < rhs.X()) return true;
114  if (lhs.X() > rhs.X()) return false;
115  if (lhs.Y() < rhs.Y()) return true;
116  if (lhs.Y() > rhs.Y()) return false;
117  if (lhs.Z() < rhs.Z()) return true;
118  if (lhs.Z() > rhs.Z()) return false;
119  return lhs.TrackID() < rhs.TrackID();
120  }
121  };
122  std::set<sim::SimEnergyDepositLite, comp> sedlite_v2;
123 
124  auto const& sed_v = *handle;
125 
126  /*double total_edep = 0.;
127  for (size_t idx = 0; idx < sed_v.size(); ++idx) {
128  total_edep += sed_v[idx].E();
129  }
130  std::cout << "total edep = " << total_edep << " count = " << sed_v.size() << std::endl;*/
131 
132  for (size_t idx = 0; idx < sed_v.size(); ++idx) {
133  auto const& sed = sed_v[idx];
134  // Voxelize coordinates to closest coordinate using fVoxelSize
135  double x = sed.X() - std::fmod(sed.X() - fMinX, fVoxelSizeX);
136  double y = sed.Y() - std::fmod(sed.Y() - fMinY, fVoxelSizeY);
137  double z = sed.Z() - std::fmod(sed.Z() - fMinZ, fVoxelSizeZ);
138  // Copy info to SimEnergyDepositLite
139  sim::SimEnergyDepositLite sed_lite(sed.E(), geo::Point_t(x, y, z), sed.T(), sed.OrigTrackID());
140  // Attempt to insert, if already exist we sum up energy deposit
141  auto it = sedlite_v2.find(sed_lite);
142  if (it!=sedlite_v2.end()) {
143  double new_energy = sed_lite.E() + it->E();
144  double new_time = std::min(sed_lite.T(), it->T());
145  sedlite_v2.erase(it);
146  sim::SimEnergyDepositLite new_sed_lite(new_energy, geo::Point_t(x, y, z), new_time, sed.OrigTrackID());
147  sedlite_v2.insert(new_sed_lite);
148  }
149  else {
150  sedlite_v2.insert(std::move(sed_lite));
151  }
152  }
153 
154  /*double new_total_edep = 0.;
155  int counts = 0;
156 
157  for (auto it : sedlite_v2) {
158  new_total_edep += it.E();
159  counts += 1;
160  }
161  std::cout << "new total edep = " << new_total_edep << " with counts " << counts << " " << sedlite_v2.size() << std::endl;
162  */
163 
164  // Create vector for SEDLite
165  std::unique_ptr<std::vector<sim::SimEnergyDepositLite>> sedlite_v(new std::vector<sim::SimEnergyDepositLite>(sedlite_v2.begin(), sedlite_v2.end()));
166 
167  // Store SEDLite in event
168  e.put(std::move(sedlite_v));
169 
170 }
171 
172 DEFINE_ART_MODULE(G4InfoReducer)
process_name opflash particleana ie ie ie z
Utilities related to art service access.
process_name opflash particleana ie x
BEGIN_PROLOG could also be cerr
art::InputTag fSedLabel
module making the SimEnergyDeposit
geo::Length_t Y() const
void produce(art::Event &e) override
pdgs p
Definition: selectors.fcl:22
Geometry information for a single TPC.
Definition: TPCGeo.h:38
geo::Length_t Z() const
const geo::GeometryCore & fGeometry
double fMinZ
bottom left coordinate of union of all TPC active volumes
G4InfoReducer & operator=(G4InfoReducer const &)=delete
geo::Length_t X() const
Access the description of detector geometry.
double fVoxelSizeZ
size of a voxel (cm)
process_name opflash particleana ie ie y
contains information for a single step in the detector simulation (pared down in size to the essentia...
Description of geometry of one entire detector.
Energy deposition in the active material (lite version).
contains information for a single step in the detector simulation
do i e
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:184
art framework interface to geometry description
G4InfoReducer(fhicl::ParameterSet const &p)