1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | #include "GlueXSensitiveDetectorCERE.hh" |
8 | #include "GlueXDetectorConstruction.hh" |
9 | #include "GlueXPrimaryGeneratorAction.hh" |
10 | #include "GlueXUserEventInformation.hh" |
11 | #include "GlueXUserTrackInformation.hh" |
12 | #include "HddmOutput.hh" |
13 | |
14 | #include "G4VPhysicalVolume.hh" |
15 | #include "G4PVPlacement.hh" |
16 | #include "G4EventManager.hh" |
17 | #include "G4HCofThisEvent.hh" |
18 | #include "G4Step.hh" |
19 | #include "G4SDManager.hh" |
20 | #include "G4ios.hh" |
21 | |
22 | #include <JANA/JApplication.h> |
23 | |
24 | |
25 | #define OPTICAL_PHOTON50 50 |
26 | |
27 | |
28 | int GlueXSensitiveDetectorCERE::MAX_HITS = 100; |
29 | |
30 | |
31 | double GlueXSensitiveDetectorCERE::TWO_HIT_TIME_RESOL = 50*ns; |
32 | |
33 | |
34 | double GlueXSensitiveDetectorCERE::THRESH_PE = 2.; |
35 | |
36 | int GlueXSensitiveDetectorCERE::instanceCount = 0; |
37 | G4Mutex GlueXSensitiveDetectorCERE::fMutex = G4MUTEX_INITIALIZER{ { 0, 0, 0, 0, 0, 0, 0, { 0, 0 } } }; |
38 | |
39 | GlueXSensitiveDetectorCERE::GlueXSensitiveDetectorCERE(const G4String& name) |
40 | : G4VSensitiveDetector(name), |
41 | fTubeHitsMap(0), fPointsMap(0) |
42 | { |
43 | collectionName.insert("CERETubeHitsCollection"); |
44 | collectionName.insert("CEREPointsCollection"); |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | G4AutoLock tuberier(&fMutex); |
53 | if (instanceCount++ == 0) { |
54 | int runno = HddmOutput::getRunNo(); |
55 | extern jana::JApplication *japp; |
56 | if (japp == 0) { |
57 | G4cerr(*G4cerr_p) << "Error in GlueXSensitiveDetector constructor - " |
58 | << "jana global DApplication object not set, " |
59 | << "cannot continue." << G4endlstd::endl; |
60 | exit(-1); |
61 | } |
62 | jana::JCalibration *jcalib = japp->GetJCalibration(runno); |
63 | if (japp == 0) { |
64 | jcalib = 0; |
65 | G4cout(*G4cout_p) << "CERE: ALL parameters loaded from ccdb" << G4endlstd::endl; |
66 | } |
67 | } |
68 | } |
69 | |
70 | GlueXSensitiveDetectorCERE::GlueXSensitiveDetectorCERE( |
71 | const GlueXSensitiveDetectorCERE &src) |
72 | : G4VSensitiveDetector(src), |
73 | fTubeHitsMap(src.fTubeHitsMap), fPointsMap(src.fPointsMap) |
74 | { |
75 | G4AutoLock tuberier(&fMutex); |
76 | ++instanceCount; |
77 | } |
78 | |
79 | GlueXSensitiveDetectorCERE &GlueXSensitiveDetectorCERE::operator=(const |
80 | GlueXSensitiveDetectorCERE &src) |
81 | { |
82 | G4AutoLock tuberier(&fMutex); |
83 | *(G4VSensitiveDetector*)this = src; |
84 | fTubeHitsMap = src.fTubeHitsMap; |
85 | fPointsMap = src.fPointsMap; |
86 | return *this; |
87 | } |
88 | |
89 | GlueXSensitiveDetectorCERE::~GlueXSensitiveDetectorCERE() |
90 | { |
91 | G4AutoLock tuberier(&fMutex); |
92 | --instanceCount; |
93 | } |
94 | |
95 | void GlueXSensitiveDetectorCERE::Initialize(G4HCofThisEvent* hce) |
96 | { |
97 | fTubeHitsMap = new |
98 | GlueXHitsMapCEREtube(SensitiveDetectorName, collectionName[0]); |
99 | fPointsMap = new |
100 | GlueXHitsMapCEREpoint(SensitiveDetectorName, collectionName[1]); |
101 | G4SDManager *sdm = G4SDManager::GetSDMpointer(); |
102 | hce->AddHitsCollection(sdm->GetCollectionID(collectionName[0]), fTubeHitsMap); |
103 | hce->AddHitsCollection(sdm->GetCollectionID(collectionName[1]), fPointsMap); |
104 | } |
105 | |
106 | G4bool GlueXSensitiveDetectorCERE::ProcessHits(G4Step* step, |
107 | G4TouchableHistory* ROhist) |
108 | { |
109 | double dEsum = step->GetTotalEnergyDeposit(); |
110 | if (dEsum == 0) |
111 | return false; |
112 | |
113 | const G4ThreeVector &pin = step->GetPreStepPoint()->GetMomentum(); |
114 | const G4ThreeVector &xin = step->GetPreStepPoint()->GetPosition(); |
115 | const G4ThreeVector &xout = step->GetPostStepPoint()->GetPosition(); |
116 | double Ein = step->GetPreStepPoint()->GetTotalEnergy(); |
117 | double tin = step->GetPreStepPoint()->GetGlobalTime(); |
118 | double tout = step->GetPostStepPoint()->GetGlobalTime(); |
119 | G4ThreeVector x = (xin + xout) / 2; |
120 | G4ThreeVector dx = xout - xin; |
121 | double t = (tin + tout) / 2; |
122 | |
123 | const G4VTouchable* touch = step->GetPreStepPoint()->GetTouchable(); |
124 | const G4AffineTransform &local_from_global = touch->GetHistory() |
125 | ->GetTopTransform(); |
126 | G4ThreeVector xlocal = local_from_global.TransformPoint(x); |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | if (tout > 1.0*s) |
135 | t = tin; |
136 | |
137 | |
138 | |
139 | |
140 | G4Track *track = step->GetTrack(); |
141 | G4int trackID = track->GetTrackID(); |
142 | int pdgtype = track->GetDynamicParticle()->GetPDGcode(); |
143 | int g3type = GlueXPrimaryGeneratorAction::ConvertPdgToGeant3(pdgtype); |
144 | GlueXUserTrackInformation *trackinfo = (GlueXUserTrackInformation*) |
145 | track->GetUserInformation(); |
146 | int itrack = trackinfo->GetGlueXTrackID(); |
147 | if (touch->GetVolume()->GetName() == "CERW") { |
148 | if (trackinfo->GetGlueXHistory() == 0 && itrack > 0 && xin.dot(pin) > 0) { |
149 | G4int key = fPointsMap->entries(); |
150 | GlueXHitCEREpoint* lastPoint = (*fPointsMap)[key - 1]; |
151 | if (lastPoint == 0 || lastPoint->track_ != trackID || |
152 | fabs(lastPoint->t_ns - t/ns) > 0.1 || |
153 | fabs(lastPoint->x_cm - x[0]/cm) > 2. || |
154 | fabs(lastPoint->y_cm - x[1]/cm) > 2. || |
155 | fabs(lastPoint->z_cm - x[2]/cm) > 2.) |
156 | { |
157 | GlueXHitCEREpoint newPoint; |
158 | newPoint.ptype_G3 = g3type; |
159 | newPoint.track_ = trackID; |
160 | newPoint.trackID_ = itrack; |
161 | newPoint.primary_ = (track->GetParentID() == 0); |
162 | newPoint.t_ns = t/ns; |
163 | newPoint.x_cm = x[0]/cm; |
164 | newPoint.y_cm = x[1]/cm; |
165 | newPoint.z_cm = x[2]/cm; |
166 | newPoint.px_GeV = pin[0]/GeV; |
167 | newPoint.py_GeV = pin[1]/GeV; |
168 | newPoint.pz_GeV = pin[2]/GeV; |
169 | newPoint.E_GeV = Ein/GeV; |
170 | fPointsMap->add(key, newPoint); |
171 | } |
172 | } |
173 | |
174 | |
175 | |
176 | return true; |
177 | } |
178 | |
179 | |
180 | |
181 | if (dEsum > 0) { |
182 | int sector = GetIdent("sector", touch); |
183 | int key = GlueXHitCEREtube::GetKey(sector); |
184 | GlueXHitCEREtube *counter = (*fTubeHitsMap)[key]; |
185 | if (counter == 0) { |
186 | GlueXHitCEREtube newcounter(sector); |
187 | fTubeHitsMap->add(key, newcounter); |
188 | counter = (*fTubeHitsMap)[key]; |
189 | } |
190 | |
191 | |
192 | |
193 | int merge_hit = 0; |
194 | std::vector<GlueXHitCEREtube::hitinfo_t>::iterator hiter; |
195 | for (hiter = counter->hits.begin(); hiter != counter->hits.end(); ++hiter) { |
196 | if (fabs(hiter->t_ns*ns - t) < TWO_HIT_TIME_RESOL) { |
197 | merge_hit = 1; |
| Value stored to 'merge_hit' is never read |
198 | break; |
199 | } |
200 | else if (hiter->t_ns*ns > t) { |
201 | break; |
202 | } |
203 | if (merge_hit) { |
204 | |
205 | hiter->pe_ += 1; |
206 | if (hiter->t_ns*ns > t) { |
207 | hiter->t_ns = t/ns; |
208 | } |
209 | } |
210 | else if ((int)counter->hits.size() < MAX_HITS) { |
211 | |
212 | hiter = counter->hits.insert(hiter, GlueXHitCEREtube::hitinfo_t()); |
213 | hiter->pe_ = 1; |
214 | hiter->t_ns = t/ns; |
215 | } |
216 | else { |
217 | G4cerr(*G4cerr_p) << "GlueXSensitiveDetectorCERE::ProcessHits error: " |
218 | << "max hit count " << MAX_HITS |
219 | << " exceeded, truncating!" |
220 | << G4endlstd::endl; |
221 | } |
222 | } |
223 | } |
224 | return true; |
225 | } |
226 | |
227 | void GlueXSensitiveDetectorCERE::EndOfEvent(G4HCofThisEvent*) |
228 | { |
229 | std::map<int,GlueXHitCEREtube*> *tubes = fTubeHitsMap->GetMap(); |
230 | std::map<int,GlueXHitCEREpoint*> *points = fPointsMap->GetMap(); |
231 | if (tubes->size() == 0 && points->size() == 0) |
232 | return; |
233 | std::map<int,GlueXHitCEREtube*>::iterator siter; |
234 | std::map<int,GlueXHitCEREpoint*>::iterator piter; |
235 | |
236 | if (verboseLevel > 1) { |
237 | G4cout(*G4cout_p) << G4endlstd::endl |
238 | << "--------> Hits Collection: in this event there are " |
239 | << tubes->size() << " tubes with hits in the Cerenkov: " |
240 | << G4endlstd::endl; |
241 | for (siter = tubes->begin(); siter != tubes->end(); ++siter) |
242 | siter->second->Print(); |
243 | |
244 | G4cout(*G4cout_p) << G4endlstd::endl |
245 | << "--------> Hits Collection: in this event there are " |
246 | << points->size() << " truth points in the Cerenkov: " |
247 | << G4endlstd::endl; |
248 | for (piter = points->begin(); piter != points->end(); ++piter) |
249 | piter->second->Print(); |
250 | } |
251 | |
252 | |
253 | |
254 | G4EventManager* mgr = G4EventManager::GetEventManager(); |
255 | G4VUserEventInformation* info = mgr->GetUserInformation(); |
256 | hddm_s::HDDM *record = ((GlueXUserEventInformation*)info)->getOutputRecord(); |
257 | if (record == 0) { |
258 | G4cerr(*G4cerr_p) << "GlueXSensitiveDetectorCERE::EndOfEvent error - " |
259 | << "hits seen but no output hddm record to save them into, " |
260 | << "cannot continue!" << G4endlstd::endl; |
261 | exit(1); |
262 | } |
263 | |
264 | if (record->getPhysicsEvents().size() == 0) |
265 | record->addPhysicsEvents(); |
266 | if (record->getHitViews().size() == 0) |
267 | record->getPhysicsEvent().addHitViews(); |
268 | hddm_s::HitView &hitview = record->getPhysicsEvent().getHitView(); |
269 | if (hitview.getCerenkovs().size() == 0) |
270 | hitview.addCerenkovs(); |
271 | hddm_s::Cerenkov &cerenkov = hitview.getCerenkov(); |
272 | |
273 | |
274 | for (siter = tubes->begin(); siter != tubes->end(); ++siter) { |
275 | std::vector<GlueXHitCEREtube::hitinfo_t> &hits = siter->second->hits; |
276 | |
277 | for (unsigned int ih=0; ih < hits.size(); ++ih) { |
278 | if (hits[ih].pe_ <= THRESH_PE) { |
279 | hits.erase(hits.begin() + ih); |
280 | --ih; |
281 | } |
282 | } |
283 | if (hits.size() > 0) { |
284 | hddm_s::CereSectionList tube = cerenkov.addCereSections(1); |
285 | tube(0).setSector(siter->second->sector_); |
286 | for (int ih=0; ih < (int)hits.size(); ++ih) { |
287 | hddm_s::CereTruthHitList thit = tube(0).addCereTruthHits(1); |
288 | thit(0).setPe(hits[ih].pe_); |
289 | thit(0).setT(hits[ih].t_ns); |
290 | } |
291 | } |
292 | } |
293 | |
294 | |
295 | for (piter = points->begin(); piter != points->end(); ++piter) { |
296 | hddm_s::CereTruthPointList point = cerenkov.addCereTruthPoints(1); |
297 | point(0).setPrimary(piter->second->primary_); |
298 | point(0).setPtype(piter->second->ptype_G3); |
299 | point(0).setPx(piter->second->px_GeV); |
300 | point(0).setPy(piter->second->py_GeV); |
301 | point(0).setPz(piter->second->pz_GeV); |
302 | point(0).setE(piter->second->E_GeV); |
303 | point(0).setX(piter->second->x_cm); |
304 | point(0).setY(piter->second->y_cm); |
305 | point(0).setZ(piter->second->z_cm); |
306 | point(0).setT(piter->second->t_ns); |
307 | point(0).setTrack(piter->second->track_); |
308 | hddm_s::TrackIDList tid = point(0).addTrackIDs(); |
309 | tid(0).setItrack(piter->second->trackID_); |
310 | } |
311 | } |
312 | |
313 | int GlueXSensitiveDetectorCERE::GetIdent(std::string div, |
314 | const G4VTouchable *touch) |
315 | { |
316 | const HddsG4Builder* bldr = GlueXDetectorConstruction::GetBuilder(); |
317 | std::map<std::string, std::vector<int> >::const_iterator iter; |
318 | std::map<std::string, std::vector<int> > *identifiers; |
319 | int max_depth = touch->GetHistoryDepth(); |
320 | for (int depth = 0; depth < max_depth; ++depth) { |
321 | G4VPhysicalVolume *pvol = touch->GetVolume(depth); |
322 | G4LogicalVolume *lvol = pvol->GetLogicalVolume(); |
323 | int volId = fVolumeTable[lvol]; |
324 | if (volId == 0) { |
325 | volId = bldr->getVolumeId(lvol); |
326 | fVolumeTable[lvol] = volId; |
327 | } |
328 | identifiers = &Refsys::fIdentifierTable[volId]; |
329 | if ((iter = identifiers->find(div)) != identifiers->end()) { |
330 | int copyNum = touch->GetCopyNumber(depth); |
331 | copyNum += (dynamic_cast<G4PVPlacement*>(pvol))? -1 : 0; |
332 | return iter->second[copyNum]; |
333 | } |
334 | } |
335 | return -1; |
336 | } |