All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MergeSimSources.cxx
Go to the documentation of this file.
1 /*!
2  * Title: MergeSimSources Utility Class
3  * Author: Wes Ketchum (wketchum@lanl.gov)
4  *
5  * Description:
6  * Class that merges different simulation sources together to created a combined sim list.
7  * Typically just merges vectors/maps/etc together. But, if anything as a G4 trackID, applies
8  * a user-defined offset to those IDs.
9  *
10 */
11 
12 #include <algorithm>
13 #include <stdexcept>
14 #include <sstream>
15 
16 #include "MergeSimSources.h"
17 
19  : fG4TrackIDOffsets(offsets)
20 {
21  Reset();
22 }
23 
25 {
26  fG4TrackIDRanges.clear();
27  fG4TrackIDRanges.resize(fG4TrackIDOffsets.size(),
28  std::make_pair(std::numeric_limits<int>::max(),
29  std::numeric_limits<int>::min()));
30  fMCParticleListMap.clear();
31  fMCParticleListMap.resize(fG4TrackIDOffsets.size(),
32  std::vector<size_t>());
33 }
34 
35 void sim::MergeSimSourcesUtility::MergeMCParticles( std::vector<simb::MCParticle>& merged_vector,
36  const std::vector<simb::MCParticle>& input_vector,
37  size_t source_index)
38 {
39 
40  if(source_index >= fG4TrackIDOffsets.size())
41  std::runtime_error("ERROR in MergeSimSourcesUtility: Source index out of range!");
42 
43  fMCParticleListMap[source_index].resize(input_vector.size());
44  merged_vector.reserve(merged_vector.size() + input_vector.size());
45 
46  std::pair<int,int> range_trackID(std::numeric_limits<int>::max(),
47  std::numeric_limits<int>::min());
48 
49  for(size_t i_p=0; i_p<input_vector.size(); i_p++){
50  merged_vector.emplace_back(input_vector[i_p],fG4TrackIDOffsets[source_index]);
51 
52  fMCParticleListMap[source_index][i_p] = merged_vector.size() - 1;
53 
54  if( std::abs(merged_vector.back().TrackId()) < range_trackID.first)
55  range_trackID.first = std::abs(merged_vector.back().TrackId());
56  if( std::abs(merged_vector.back().TrackId()) > range_trackID.second)
57  range_trackID.second = std::abs(merged_vector.back().TrackId());
58 
59  }
60 
61  UpdateG4TrackIDRange(range_trackID,source_index);
62 }
63 
64 void sim::MergeSimSourcesUtility::MergeSimChannels(std::vector<sim::SimChannel>& merged_vector,
65  const std::vector<sim::SimChannel>& input_vector,
66  size_t source_index)
67 {
68  if(source_index >= fG4TrackIDOffsets.size())
69  std::runtime_error("ERROR in MergeSimSourcesUtility: Source index out of range!");
70 
71  merged_vector.reserve( merged_vector.size() + input_vector.size() );
72 
73  std::pair<int,int> range_trackID(std::numeric_limits<int>::max(),
74  std::numeric_limits<int>::min());
75 
76  for(auto const& simchannel : input_vector){
77  std::vector<sim::SimChannel>::iterator it = std::find(merged_vector.begin(),merged_vector.end(),simchannel);
78 
79  if(it==merged_vector.end()){
80  merged_vector.emplace_back(simchannel.Channel());
81  it = merged_vector.end() - 1;
82  }
83 
84  std::pair<int,int> thisrange = it->MergeSimChannel(simchannel,fG4TrackIDOffsets[source_index]);
85  if( std::abs(thisrange.first) < std::abs(range_trackID.first))
86  range_trackID.first = std::abs(thisrange.first);
87  if( std::abs(thisrange.second) > std::abs(range_trackID.second))
88  range_trackID.second = std::abs(thisrange.second);
89  }
90 
91  UpdateG4TrackIDRange(range_trackID,source_index);
92 }
93 
94 void sim::MergeSimSourcesUtility::MergeAuxDetSimChannels(std::vector<sim::AuxDetSimChannel>& merged_vector,
95  const std::vector<sim::AuxDetSimChannel>& input_vector,
96  size_t source_index)
97 {
98  if(source_index >= fG4TrackIDOffsets.size())
99  std::runtime_error("ERROR in MergeSimSourcesUtility: Source index out of range!");
100 
101  merged_vector.reserve( merged_vector.size() + input_vector.size() );
102 
103  std::pair<int,int> range_trackID(std::numeric_limits<int>::max(),
104  std::numeric_limits<int>::min());
105 
106  for(auto const& simchannel : input_vector){
107  std::vector<sim::AuxDetSimChannel>::iterator it = std::find(merged_vector.begin(),merged_vector.end(),simchannel);
108 
109  if(it==merged_vector.end()){
110  merged_vector.emplace_back(simchannel.AuxDetID(), simchannel.AuxDetSensitiveID());
111  it = merged_vector.end() - 1;
112  }
113 
114  // re-make the AuxDetSimChannel with both pairs of AuxDetIDEs
115  int offset = fG4TrackIDOffsets[source_index];
116  std::vector<sim::AuxDetIDE> all_ides = it->AuxDetIDEs();
117  for (const sim::AuxDetIDE &ide: simchannel.AuxDetIDEs()) {
118  all_ides.emplace_back(ide, offset);
119 
120  auto tid = std::abs(ide.trackID)+offset;
121 
122  if( tid < range_trackID.first )
123  range_trackID.first = tid;
124  if( tid > range_trackID.second )
125  range_trackID.second = tid;
126  }
127 
128 
129  *it = sim::AuxDetSimChannel(simchannel.AuxDetID(), std::move(all_ides), simchannel.AuxDetSensitiveID());
130  }
131 
132  UpdateG4TrackIDRange(range_trackID,source_index);
133 }
134 
135 void sim::MergeSimSourcesUtility::MergeSimPhotons( std::vector<sim::SimPhotons>& merged_vector,
136  const std::vector<sim::SimPhotons>& input_vector)
137 {
138 
139  merged_vector.reserve( merged_vector.size() + input_vector.size() );
140 
141  for(auto const& simphotons : input_vector){
142  std::vector<sim::SimPhotons>::iterator it = std::find(merged_vector.begin(),merged_vector.end(),simphotons);
143 
144  if(it==merged_vector.end()){
145  merged_vector.emplace_back(simphotons.OpChannel());
146  it = merged_vector.end() - 1;
147  }
148 
149  *it += simphotons;
150  }
151 }
152 
153 void sim::MergeSimSourcesUtility::MergeSimPhotonsLite( std::vector<sim::SimPhotonsLite>& merged_vector,
154  const std::vector<sim::SimPhotonsLite>& input_vector)
155 {
156 
157  merged_vector.reserve( merged_vector.size() + input_vector.size() );
158 
159  for(auto const& simphotons : input_vector){
160  std::vector<sim::SimPhotonsLite>::iterator it = std::find(merged_vector.begin(),merged_vector.end(),simphotons);
161 
162  if(it==merged_vector.end()){
163  merged_vector.emplace_back(simphotons.OpChannel);
164  it = merged_vector.end() - 1;
165  }
166 
167  *it += simphotons;
168  }
169 }
170 
171 
173  std::vector<sim::SimEnergyDeposit>& dest,
174  const std::vector<sim::SimEnergyDeposit>& src,
175  std::size_t source_index
176 ) const {
177 
178  int const offset = fG4TrackIDOffsets.at(source_index);
179  auto const offsetEDepID = [offset](sim::SimEnergyDeposit const& edep)
181 
182  dest.reserve(dest.size() + src.size());
183  std::transform(begin(src), end(src), back_inserter(dest), offsetEDepID);
184 
185 }
186 
187 
189  std::vector<sim::AuxDetHit>& dest,
190  const std::vector<sim::AuxDetHit>& src,
191  std::size_t source_index
192 ) const {
193 
194  int const offset = fG4TrackIDOffsets.at(source_index);
195  auto const offsetAuxDetHitID = [offset](sim::AuxDetHit const& adh)
197 
198  dest.reserve(dest.size() + src.size());
199  std::transform(begin(src), end(src), back_inserter(dest), offsetAuxDetHitID);
200 
201 }
202 
203 
204 void sim::MergeSimSourcesUtility::UpdateG4TrackIDRange(std::pair<int,int> newrange, size_t source_index)
205 {
206  if(source_index >= fG4TrackIDOffsets.size())
207  std::runtime_error("ERROR in MergeSimSourcesUtility: Source index out of range!");
208 
209  if( newrange.first >= fG4TrackIDRanges[source_index].first &&
210  newrange.second <= fG4TrackIDRanges[source_index].second)
211  return;
212 
213  for(size_t i=0; i<fG4TrackIDRanges.size(); i++){
214  if(i==source_index) continue;
215 
216  if( (newrange.first >= fG4TrackIDRanges[i].first && newrange.first <= fG4TrackIDRanges[i].second) ||
217  (newrange.second >= fG4TrackIDRanges[i].first && newrange.second <= fG4TrackIDRanges[i].second) )
218  {
219  std::stringstream ss;
220  ss << "ERROR in MergeSimSourcesUtility: Source trackIDs overlap!"
221  << "\n\t" << i << "\t" << fG4TrackIDRanges[i].first << " " << fG4TrackIDRanges[i].second
222  << "\n\t" << "n\t" << newrange.first << " " << newrange.second;
223  throw std::runtime_error(ss.str());
224  }
225  }
226 
227  if(newrange.first < fG4TrackIDRanges[source_index].first)
228  fG4TrackIDRanges[source_index].first = newrange.first;
229  if(newrange.second > fG4TrackIDRanges[source_index].second)
230  fG4TrackIDRanges[source_index].second = newrange.second;
231 
232 
233 }
234 
235 
237  (sim::SimEnergyDeposit const& edep, int offset)
238 {
239 
240  auto tid = (edep.TrackID()>=0) ? (edep.TrackID() + offset) : (edep.TrackID() - offset);
241 
242  return sim::SimEnergyDeposit{
243  edep.NumPhotons(), // np
244  edep.NumElectrons(), // ne
245  edep.ScintYieldRatio(), // sy
246  edep.Energy(), // e
247  edep.Start(), // start
248  edep.End(), // end
249  edep.T0(), // t0
250  edep.T1(), // t1
251  tid, // id
252  edep.PdgCode() // pdg
253  };
254 } // sim::MergeSimSourcesUtility::offsetTrackID()
255 
256 
258  (sim::AuxDetHit const& adh, int offset)
259 {
260 
261  auto tid = (adh.GetTrackID()>=0) ? (adh.GetTrackID() + offset) : (adh.GetTrackID() - offset);
262 
263  return sim::AuxDetHit{
264  adh.GetID(), // copy number
265  tid, // g4 track id
266  adh.GetEnergyDeposited(),
267  adh.GetEntryX(),
268  adh.GetEntryY(),
269  adh.GetEntryZ(),
270  adh.GetEntryT(),
271  adh.GetExitX(),
272  adh.GetExitY(),
273  adh.GetExitZ(),
274  adh.GetExitT(),
275  adh.GetExitMomentumX(),
276  adh.GetExitMomentumY(),
277  adh.GetExitMomentumZ(),
278  };
279 } // sim::MergeSimSourcesUtility::offsetAuxDetHitTrackID()
280 
281 
unsigned int GetTrackID() const
Definition: AuxDetHit.h:169
BEGIN_PROLOG TPC Trig offset(g4 rise time) ProjectToHeight
Definition: CORSIKAGen.fcl:7
void MergeSimPhotonsLite(std::vector< sim::SimPhotonsLite > &, const std::vector< sim::SimPhotonsLite > &)
static constexpr Sample_t transform(Sample_t sample)
void MergeSimChannels(std::vector< sim::SimChannel > &, const std::vector< sim::SimChannel > &, size_t)
void UpdateG4TrackIDRange(std::pair< int, int >, size_t)
float GetExitX() const
Definition: AuxDetHit.h:121
MergeSimSourcesUtility(std::vector< int > const &)
void MergeSimPhotons(std::vector< sim::SimPhotons > &, const std::vector< sim::SimPhotons > &)
static sim::SimEnergyDeposit offsetSimEnergyDepositTrackID(sim::SimEnergyDeposit const &, int)
void MergeAuxDetSimChannels(std::vector< sim::AuxDetSimChannel > &, const std::vector< sim::AuxDetSimChannel > &, size_t)
float GetExitMomentumY() const
Definition: AuxDetHit.h:81
void MergeAuxDetHits(std::vector< sim::AuxDetHit > &, const std::vector< sim::AuxDetHit > &, size_t) const
Collection of particles crossing one auxiliary detector cell.
T abs(T value)
geo::Point_t Start() const
geo::Point_t End() const
float GetEntryX() const
Definition: AuxDetHit.h:153
float GetExitT() const
Definition: AuxDetHit.h:97
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
float GetEntryT() const
Definition: AuxDetHit.h:129
float GetEnergyDeposited() const
Definition: AuxDetHit.h:161
unsigned int GetID() const
Definition: AuxDetHit.h:177
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
float GetExitY() const
Definition: AuxDetHit.h:113
void MergeMCParticles(std::vector< simb::MCParticle > &, const std::vector< simb::MCParticle > &, size_t)
float GetExitMomentumX() const
Definition: AuxDetHit.h:89
float GetEntryZ() const
Definition: AuxDetHit.h:137
float GetExitZ() const
Definition: AuxDetHit.h:105
MC truth information to make RawDigits and do back tracking.
static sim::AuxDetHit offsetAuxDetHitTrackID(sim::AuxDetHit const &, int)
Energy deposition in the active material.
void MergeSimEnergyDeposits(std::vector< sim::SimEnergyDeposit > &, const std::vector< sim::SimEnergyDeposit > &, size_t) const
double Energy() const
float GetExitMomentumZ() const
Definition: AuxDetHit.h:73
double ScintYieldRatio() const
float GetEntryY() const
Definition: AuxDetHit.h:145