LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
AuxDetSD.cc
Go to the documentation of this file.
1 //
2 // __ __ __ __ __
3 // ____ ______/ /_____ _/ // / / /_/ /__
4 // / __ `/ ___/ __/ __ `/ // /_/ __/ //_/
5 // / /_/ / / / /_/ /_/ /__ __/ /_/ ,<
6 // \__,_/_/ \__/\__, / /_/ \__/_/|_|
7 // /____/
8 //
9 // artg4tk: art based Geant 4 Toolkit
10 //
11 //=============================================================================
12 // AuxDetSD.cc: Class representing a sensitive aux detector
13 // Author: Hans Wenzel (Fermilab)
14 //=============================================================================
16 #include "Geant4/G4Event.hh"
17 #include "Geant4/G4EventManager.hh"
18 #include "Geant4/G4HCofThisEvent.hh"
19 #include "Geant4/G4SDManager.hh"
20 #include "Geant4/G4Step.hh"
21 #include "Geant4/G4SystemOfUnits.hh"
22 #include "Geant4/G4ThreeVector.hh"
23 #include "Geant4/G4UnitsTable.hh"
24 #include "Geant4/G4VSolid.hh"
25 #include "Geant4/G4VVisManager.hh"
26 #include "Geant4/G4ios.hh"
27 #include <algorithm>
28 #include <unordered_set>
29 //#define _verbose_ 1
30 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
31 namespace larg4 {
32 
33  AuxDetSD::AuxDetSD(G4String name) : G4VSensitiveDetector(name)
34  {
35  hitCollection.clear();
36  }
37 
38  //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
39 
41  //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43  {
44  hitCollection.clear();
45  temphitCollection.clear();
46  }
47  //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
48  G4bool AuxDetSD::ProcessHits(G4Step* step, G4TouchableHistory*)
49  {
50  G4double edep = step->GetTotalEnergyDeposit() / CLHEP::MeV;
51  if (edep == 0.) return false;
52  G4Track* track = step->GetTrack();
53  const unsigned int trackID = track->GetTrackID();
54  unsigned int ID = step->GetPreStepPoint()->GetPhysicalVolume()->GetCopyNo();
55  TempHit tmpHit = TempHit(ID,
56  trackID,
57  track->GetParentID(),
58  step->IsFirstStepInVolume(),
59  step->IsLastStepInVolume(),
60  edep,
61  step->GetPreStepPoint()->GetPosition().getX() / CLHEP::cm,
62  step->GetPreStepPoint()->GetPosition().getY() / CLHEP::cm,
63  step->GetPreStepPoint()->GetPosition().getZ() / CLHEP::cm,
64  step->GetPreStepPoint()->GetGlobalTime() / CLHEP::ns,
65  step->GetPostStepPoint()->GetPosition().getX() / CLHEP::cm,
66  step->GetPostStepPoint()->GetPosition().getY() / CLHEP::cm,
67  step->GetPostStepPoint()->GetPosition().getZ() / CLHEP::cm,
68  step->GetPostStepPoint()->GetGlobalTime() / CLHEP::ns,
69  step->GetPostStepPoint()->GetMomentum().getX() / CLHEP::GeV,
70  step->GetPostStepPoint()->GetMomentum().getY() / CLHEP::GeV,
71  step->GetPostStepPoint()->GetMomentum().getZ() / CLHEP::GeV);
72  temphitCollection.push_back(tmpHit);
73 
74  /*
75  sim::AuxDetHit newHit = sim::AuxDetHit(ID,
76  trackID,
77  edep,
78  step->GetPreStepPoint()->GetPosition().getX()/CLHEP::cm,
79  step->GetPreStepPoint()->GetPosition().getY()/CLHEP::cm,
80  step->GetPreStepPoint()->GetPosition().getZ()/CLHEP::cm,
81  step->GetPreStepPoint()->GetGlobalTime()/CLHEP::ns,
82  step->GetPostStepPoint()->GetPosition().getX()/CLHEP::cm,
83  step->GetPostStepPoint()->GetPosition().getY()/CLHEP::cm,
84  step->GetPostStepPoint()->GetPosition().getZ()/CLHEP::cm,
85  step->GetPostStepPoint()->GetGlobalTime()/CLHEP::ns,
86  step->GetPostStepPoint()->GetMomentum().getX()/CLHEP::GeV,
87  step->GetPostStepPoint()->GetMomentum().getY()/CLHEP::GeV,
88  step->GetPostStepPoint()->GetMomentum().getZ()/CLHEP::GeV
89  );
90  hitCollection.push_back(newHit);
91  */
92  return true;
93  }
94 
96  {
97  if (temphitCollection.size() == 0) return; // No hits so nothing to do
98 #if defined _verbose_
99  std::cout << " EndOfEvent number of temp hits: " << temphitCollection.size() << std::endl;
100  std::cout << " EndOfEvent number of aux hits: " << hitCollection.size() << std::endl;
101 #endif
102  std::sort(temphitCollection.begin(), temphitCollection.end());
103  int geoId = -1;
104  int trackId = -1;
105  unsigned int counter = 0;
106  std::unordered_set<unsigned int> setofIDs;
107 
108  for (auto it = temphitCollection.begin(); it != temphitCollection.end(); it++) {
109 #if defined _verbose_
110  std::cout << "geoID: " << it->GetID() << " track ID: " << it->GetTrackID()
111  << " Edep: " << it->GetEnergyDeposited() << " Parent Id: " << it->GetParentID()
112  << " exit Time: " << it->GetExitT() << " is first: " << it->IsIsfirstinVolume()
113  << " is last: " << it->IsIslastinVolume();
114 #endif
115  if (it->GetID() == geoId && trackId == it->GetTrackID()) // trackid and detector didn't change
116  {
117 #if defined _verbose_
118  std::cout << " A" << std::endl;
119 #endif
120  if (it->GetExitT()) // change exit vector and add to total charge
121  {
122  hitCollection[counter - 1].SetEnergyDeposited(
123  hitCollection[counter - 1].GetEnergyDeposited() + it->GetEnergyDeposited());
124  hitCollection[counter - 1].SetExitX(it->GetExitX());
125  hitCollection[counter - 1].SetExitY(it->GetExitY());
126  hitCollection[counter - 1].SetExitZ(it->GetExitZ());
127  hitCollection[counter - 1].SetExitT(it->GetExitT());
128  }
129  else // just add to total charge
130  {
131  hitCollection[counter - 1].SetEnergyDeposited(
132  hitCollection[counter - 1].GetEnergyDeposited() + it->GetEnergyDeposited());
133  }
134  }
135  else if (setofIDs.find(it->GetParentID()) != setofIDs.end()) {
136  setofIDs.insert(it->GetTrackID());
137 #if defined _verbose_
138  std::cout << " A" << std::endl;
139 #endif
140  hitCollection[counter - 1].SetEnergyDeposited(
141  hitCollection[counter - 1].GetEnergyDeposited() + it->GetEnergyDeposited());
142  }
143  else if (it->GetID() != geoId) // new Detector
144  {
145  geoId = it->GetID();
146  trackId = it->GetTrackID();
147  setofIDs.clear();
148  setofIDs.insert(it->GetTrackID());
149 #if defined _verbose_
150  std::cout << " N" << std::endl;
151 #endif
152  counter++;
153  hitCollection.push_back(sim::AuxDetHit(it->GetID(),
154  it->GetTrackID(),
155  it->GetEnergyDeposited(),
156  it->GetEntryX(),
157  it->GetEntryY(),
158  it->GetEntryZ(),
159  it->GetEntryT(),
160  it->GetExitX(),
161  it->GetExitY(),
162  it->GetExitZ(),
163  it->GetExitT(),
164  it->GetExitMomentumX(),
165  it->GetExitMomentumY(),
166  it->GetExitMomentumZ()));
167  }
168  else {
169  trackId = it->GetTrackID();
170 #if defined _verbose_
171  std::cout << " N" << std::endl;
172 #endif
173  counter++;
174  setofIDs.clear();
175  setofIDs.insert(it->GetTrackID());
176  hitCollection.push_back(sim::AuxDetHit(it->GetID(),
177  it->GetTrackID(),
178  it->GetEnergyDeposited(),
179  it->GetEntryX(),
180  it->GetEntryY(),
181  it->GetEntryZ(),
182  it->GetEntryT(),
183  it->GetExitX(),
184  it->GetExitY(),
185  it->GetExitZ(),
186  it->GetExitT(),
187  it->GetExitMomentumX(),
188  it->GetExitMomentumY(),
189  it->GetExitMomentumZ()));
190  }
191  }
192 #if defined _verbose_
193  std::cout << "Number of AuxDetHits: " << counter << std::endl;
194 #endif
195  } // EndOfEvent
196 } // namespace sim
G4bool ProcessHits(G4Step *, G4TouchableHistory *)
Definition: AuxDetSD.cc:48
sim::AuxDetHitCollection hitCollection
Definition: AuxDetSD.h:37
Geant4 interface.
AuxDetSD(G4String name)
Definition: AuxDetSD.cc:33
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:295
virtual ~AuxDetSD()
Definition: AuxDetSD.cc:40
TempHitCollection temphitCollection
Definition: AuxDetSD.h:36
Double_t edep
Definition: macro.C:13
void EndOfEvent(G4HCofThisEvent *)
Definition: AuxDetSD.cc:95
Float_t track
Definition: plot.C:35
void Initialize(G4HCofThisEvent *)
Definition: AuxDetSD.cc:42