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