1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | #include "GlueXSensitiveDetectorCCAL.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 | int GlueXSensitiveDetectorCCAL::MAX_HITS = 100; |
26 | |
27 | |
28 | |
29 | |
30 | double GlueXSensitiveDetectorCCAL::WIDTH_OF_BLOCK = 2.0*cm; |
31 | double GlueXSensitiveDetectorCCAL::LENGTH_OF_BLOCK = 20.0*cm; |
32 | |
33 | |
34 | double GlueXSensitiveDetectorCCAL::ATTENUATION_LENGTH = 200.*cm; |
35 | double GlueXSensitiveDetectorCCAL::C_EFFECTIVE = 13.*cm/ns; |
36 | |
37 | |
38 | double GlueXSensitiveDetectorCCAL::TWO_HIT_TIME_RESOL = 75.*ns; |
39 | |
40 | |
41 | double GlueXSensitiveDetectorCCAL::THRESH_MEV = 5.; |
42 | |
43 | int GlueXSensitiveDetectorCCAL::instanceCount = 0; |
44 | G4Mutex GlueXSensitiveDetectorCCAL::fMutex = G4MUTEX_INITIALIZER{ { 0, 0, 0, 0, 0, 0, 0, { 0, 0 } } }; |
45 | |
46 | GlueXSensitiveDetectorCCAL::GlueXSensitiveDetectorCCAL(const G4String& name) |
47 | : G4VSensitiveDetector(name), |
48 | fBlocksMap(0), fPointsMap(0) |
49 | { |
50 | collectionName.insert("CCALBlockHitsCollection"); |
51 | collectionName.insert("CCALPointsCollection"); |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | G4AutoLock barrier(&fMutex); |
60 | if (instanceCount++ == 0) { |
61 | int runno = HddmOutput::getRunNo(); |
62 | extern jana::JApplication *japp; |
63 | if (japp == 0) { |
64 | G4cerr(*G4cerr_p) << "Error in GlueXSensitiveDetector constructor - " |
65 | << "jana global DApplication object not set, " |
66 | << "cannot continue." << G4endlstd::endl; |
67 | exit(-1); |
68 | } |
69 | jana::JCalibration *jcalib = japp->GetJCalibration(runno); |
| Value stored to 'jcalib' during its initialization is never read |
70 | if (japp == 0) { |
71 | jcalib = 0; |
72 | G4cout(*G4cout_p) << "CCAL: ALL parameters loaded from ccdb" << G4endlstd::endl; |
73 | } |
74 | } |
75 | } |
76 | |
77 | GlueXSensitiveDetectorCCAL::GlueXSensitiveDetectorCCAL( |
78 | const GlueXSensitiveDetectorCCAL &src) |
79 | : G4VSensitiveDetector(src), |
80 | fBlocksMap(src.fBlocksMap), fPointsMap(src.fPointsMap) |
81 | { |
82 | G4AutoLock barrier(&fMutex); |
83 | ++instanceCount; |
84 | } |
85 | |
86 | GlueXSensitiveDetectorCCAL &GlueXSensitiveDetectorCCAL::operator=(const |
87 | GlueXSensitiveDetectorCCAL &src) |
88 | { |
89 | G4AutoLock barrier(&fMutex); |
90 | *(G4VSensitiveDetector*)this = src; |
91 | fBlocksMap = src.fBlocksMap; |
92 | fPointsMap = src.fPointsMap; |
93 | return *this; |
94 | } |
95 | |
96 | GlueXSensitiveDetectorCCAL::~GlueXSensitiveDetectorCCAL() |
97 | { |
98 | G4AutoLock barrier(&fMutex); |
99 | --instanceCount; |
100 | } |
101 | |
102 | void GlueXSensitiveDetectorCCAL::Initialize(G4HCofThisEvent* hce) |
103 | { |
104 | fBlocksMap = new |
105 | GlueXHitsMapCCALblock(SensitiveDetectorName, collectionName[0]); |
106 | fPointsMap = new |
107 | GlueXHitsMapCCALpoint(SensitiveDetectorName, collectionName[1]); |
108 | G4SDManager *sdm = G4SDManager::GetSDMpointer(); |
109 | hce->AddHitsCollection(sdm->GetCollectionID(collectionName[0]), fBlocksMap); |
110 | hce->AddHitsCollection(sdm->GetCollectionID(collectionName[1]), fPointsMap); |
111 | } |
112 | |
113 | G4bool GlueXSensitiveDetectorCCAL::ProcessHits(G4Step* step, |
114 | G4TouchableHistory* ROhist) |
115 | { |
116 | double dEsum = step->GetTotalEnergyDeposit(); |
117 | if (dEsum == 0) |
118 | return false; |
119 | |
120 | const G4ThreeVector &pin = step->GetPreStepPoint()->GetMomentum(); |
121 | const G4ThreeVector &xin = step->GetPreStepPoint()->GetPosition(); |
122 | const G4ThreeVector &xout = step->GetPostStepPoint()->GetPosition(); |
123 | double Ein = step->GetPreStepPoint()->GetTotalEnergy(); |
124 | double tin = step->GetPreStepPoint()->GetGlobalTime(); |
125 | double tout = step->GetPostStepPoint()->GetGlobalTime(); |
126 | G4ThreeVector x = (xin + xout) / 2; |
127 | double t = (tin + tout) / 2; |
128 | |
129 | const G4VTouchable* touch = step->GetPreStepPoint()->GetTouchable(); |
130 | const G4AffineTransform &local_from_global = touch->GetHistory() |
131 | ->GetTopTransform(); |
132 | G4ThreeVector xlocal = local_from_global.TransformPoint(x); |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | if (tout > 1.0*s) |
141 | t = tin; |
142 | |
143 | |
144 | |
145 | |
146 | G4Track *track = step->GetTrack(); |
147 | G4int trackID = track->GetTrackID(); |
148 | GlueXUserTrackInformation *trackinfo = (GlueXUserTrackInformation*) |
149 | track->GetUserInformation(); |
150 | int itrack = trackinfo->GetGlueXTrackID(); |
151 | if (trackinfo->GetGlueXHistory() == 0 && |
152 | xin.dot(pin) > 0 && Ein/MeV > THRESH_MEV) |
153 | { |
154 | int pdgtype = track->GetDynamicParticle()->GetPDGcode(); |
155 | int g3type = GlueXPrimaryGeneratorAction::ConvertPdgToGeant3(pdgtype); |
156 | GlueXHitCCALpoint newPoint; |
157 | newPoint.ptype_G3 = g3type; |
158 | newPoint.track_ = trackID; |
159 | newPoint.trackID_ = itrack; |
160 | newPoint.primary_ = (track->GetParentID() == 0); |
161 | newPoint.t_ns = t/ns; |
162 | newPoint.x_cm = xin[0]/cm; |
163 | newPoint.y_cm = xin[1]/cm; |
164 | newPoint.z_cm = xin[2]/cm; |
165 | newPoint.px_GeV = pin[0]/GeV; |
166 | newPoint.py_GeV = pin[1]/GeV; |
167 | newPoint.pz_GeV = pin[2]/GeV; |
168 | newPoint.E_GeV = Ein/GeV; |
169 | G4int key = fPointsMap->entries(); |
170 | fPointsMap->add(key, newPoint); |
171 | trackinfo->SetGlueXHistory(4); |
172 | } |
173 | |
174 | |
175 | |
176 | if (dEsum > 0) { |
177 | int column = GetIdent("column", touch); |
178 | int row = GetIdent("row", touch); |
179 | int key = GlueXHitCCALblock::GetKey(column, row); |
180 | GlueXHitCCALblock *block = (*fBlocksMap)[key]; |
181 | if (block == 0) { |
182 | GlueXHitCCALblock newblock(column, row); |
183 | fBlocksMap->add(key, newblock); |
184 | block = (*fBlocksMap)[key]; |
185 | } |
186 | double dist = 0.5 * LENGTH_OF_BLOCK - xlocal[2]; |
187 | double dEcorr = dEsum * exp(-dist / ATTENUATION_LENGTH); |
188 | double tcorr = t + dist / C_EFFECTIVE; |
189 | |
190 | |
191 | |
192 | int merge_hit = 0; |
193 | std::vector<GlueXHitCCALblock::hitinfo_t>::iterator hiter; |
194 | for (hiter = block->hits.begin(); hiter != block->hits.end(); ++hiter) { |
195 | if (fabs(hiter->t_ns*ns - tcorr) < TWO_HIT_TIME_RESOL) { |
196 | merge_hit = 1; |
197 | break; |
198 | } |
199 | else if (hiter->t_ns*ns > t) { |
200 | break; |
201 | } |
202 | } |
203 | if (merge_hit) { |
204 | |
205 | hiter->E_GeV += dEcorr/GeV; |
206 | if (hiter->t_ns*ns > tcorr) { |
207 | hiter->t_ns = tcorr/ns; |
208 | } |
209 | } |
210 | else if ((int)block->hits.size() < MAX_HITS) { |
211 | |
212 | hiter = block->hits.insert(hiter, GlueXHitCCALblock::hitinfo_t()); |
213 | hiter->E_GeV = dEcorr/GeV; |
214 | hiter->t_ns = tcorr/ns; |
215 | } |
216 | else { |
217 | G4cerr(*G4cerr_p) << "GlueXSensitiveDetectorCCAL::ProcessHits error: " |
218 | << "max hit count " << MAX_HITS << " exceeded, truncating!" |
219 | << G4endlstd::endl; |
220 | } |
221 | } |
222 | return true; |
223 | } |
224 | |
225 | void GlueXSensitiveDetectorCCAL::EndOfEvent(G4HCofThisEvent*) |
226 | { |
227 | std::map<int,GlueXHitCCALblock*> *blocks = fBlocksMap->GetMap(); |
228 | std::map<int,GlueXHitCCALpoint*> *points = fPointsMap->GetMap(); |
229 | if (blocks->size() == 0 && points->size() == 0) |
230 | return; |
231 | std::map<int,GlueXHitCCALblock*>::iterator biter; |
232 | std::map<int,GlueXHitCCALpoint*>::iterator piter; |
233 | |
234 | if (verboseLevel > 1) { |
235 | G4cout(*G4cout_p) << G4endlstd::endl |
236 | << "--------> Hits Collection: in this event there are " |
237 | << blocks->size() << " blocks with hits in the CCAL: " |
238 | << G4endlstd::endl; |
239 | for (biter = blocks->begin(); biter != blocks->end(); ++biter) |
240 | biter->second->Print(); |
241 | |
242 | G4cout(*G4cout_p) << G4endlstd::endl |
243 | << "--------> Hits Collection: in this event there are " |
244 | << points->size() << " truth showers in the CCAL: " |
245 | << G4endlstd::endl; |
246 | for (piter = points->begin(); piter != points->end(); ++piter) |
247 | piter->second->Print(); |
248 | } |
249 | |
250 | |
251 | |
252 | G4EventManager* mgr = G4EventManager::GetEventManager(); |
253 | G4VUserEventInformation* info = mgr->GetUserInformation(); |
254 | hddm_s::HDDM *record = ((GlueXUserEventInformation*)info)->getOutputRecord(); |
255 | if (record == 0) { |
256 | G4cerr(*G4cerr_p) << "GlueXSensitiveDetectorCCAL::EndOfEvent error - " |
257 | << "hits seen but no output hddm record to save them into, " |
258 | << "cannot continue!" << G4endlstd::endl; |
259 | exit(1); |
260 | } |
261 | |
262 | if (record->getPhysicsEvents().size() == 0) |
263 | record->addPhysicsEvents(); |
264 | if (record->getHitViews().size() == 0) |
265 | record->getPhysicsEvent().addHitViews(); |
266 | hddm_s::HitView &hitview = record->getPhysicsEvent().getHitView(); |
267 | if (hitview.getComptonEMcals().size() == 0) |
268 | hitview.addComptonEMcals(); |
269 | hddm_s::ComptonEMcal &comptonEMcal = hitview.getComptonEMcal(); |
270 | |
271 | |
272 | for (biter = blocks->begin(); biter != blocks->end(); ++biter) { |
273 | std::vector<GlueXHitCCALblock::hitinfo_t> &hits = biter->second->hits; |
274 | |
275 | for (unsigned int ih=0; ih < hits.size(); ++ih) { |
276 | if (hits[ih].E_GeV <= THRESH_MEV/1e3) { |
277 | hits.erase(hits.begin() + ih); |
278 | --ih; |
279 | } |
280 | } |
281 | |
282 | if (hits.size() > 0) { |
283 | hddm_s::CcalBlockList block = comptonEMcal.addCcalBlocks(1); |
284 | block(0).setColumn(biter->second->column_); |
285 | block(0).setRow(biter->second->row_); |
286 | for (int ih=0; ih < (int)hits.size(); ++ih) { |
287 | hddm_s::CcalTruthHitList thit = block(0).addCcalTruthHits(1); |
288 | thit(0).setE(hits[ih].E_GeV); |
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::CcalTruthShowerList point = comptonEMcal.addCcalTruthShowers(1); |
297 | point(0).setE(piter->second->E_GeV); |
298 | point(0).setPrimary(piter->second->primary_); |
299 | point(0).setPtype(piter->second->ptype_G3); |
300 | point(0).setPx(piter->second->px_GeV); |
301 | point(0).setPy(piter->second->py_GeV); |
302 | point(0).setPz(piter->second->pz_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 GlueXSensitiveDetectorCCAL::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 | } |