1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include <iostream> |
10 | #include <iomanip> |
11 | using namespace std; |
12 | |
13 | #include <CDC/DCDCDigiHit.h> |
14 | #include "DCDCHit_factory.h" |
15 | #include "DCDCWire.h" |
16 | #include <DAQ/Df125PulseIntegral.h> |
17 | #include <DAQ/Df125Config.h> |
18 | #include <DAQ/Df125CDCPulse.h> |
19 | |
20 | using namespace jana; |
21 | |
22 | static double DIGI_THRESHOLD = -1000000.0; |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | jerror_t DCDCHit_factory::init(void) |
29 | { |
30 | gPARMS->SetDefaultParameter("CDC:DIGI_THRESHOLD",DIGI_THRESHOLD, |
31 | "Do not convert CDC digitized hits into DCDCHit objects" |
32 | " that would have q less than this"); |
33 | |
34 | |
35 | Nrings = 0; |
36 | a_scale = 0.; |
37 | t_scale = 0.; |
38 | t_base = 0.; |
39 | |
40 | |
41 | maxChannels = 3522; |
42 | |
43 | |
44 | a_scale = 4.0E3/1.0E2; |
45 | t_scale = 8.0/10.0; |
46 | t_base = 0.; |
47 | |
48 | return NOERROR; |
49 | } |
50 | |
51 | |
52 | |
53 | |
54 | jerror_t DCDCHit_factory::brun(jana::JEventLoop *eventLoop, int32_t runnumber) |
55 | { |
56 | |
57 | static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, 0, 0, { 0, 0 } } }; |
58 | static set<int> runs_announced; |
59 | pthread_mutex_lock(&print_mutex); |
60 | bool print_messages = false; |
61 | if(runs_announced.find(runnumber) == runs_announced.end()){ |
62 | print_messages = true; |
63 | runs_announced.insert(runnumber); |
64 | } |
65 | pthread_mutex_unlock(&print_mutex); |
66 | |
67 | |
68 | CalcNstraws(eventLoop, runnumber, Nstraws); |
69 | Nrings = Nstraws.size(); |
70 | |
71 | |
72 | vector<double> raw_gains; |
73 | vector<double> raw_pedestals; |
74 | vector<double> raw_time_offsets; |
75 | |
76 | if(print_messages) jout << "In DCDCHit_factory, loading constants..." << std::endl; |
77 | |
78 | |
79 | map<string,double> scale_factors; |
80 | if (eventLoop->GetCalib("/CDC/digi_scales", scale_factors)) |
81 | jout << "Error loading /CDC/digi_scales !" << endl; |
82 | if (scale_factors.find("CDC_ADC_ASCALE") != scale_factors.end()) |
83 | a_scale = scale_factors["CDC_ADC_ASCALE"]; |
84 | else |
85 | jerr << "Unable to get CDC_ADC_ASCALE from /CDC/digi_scales !" << endl; |
86 | |
87 | #ifdef ENABLE_UPSAMPLING |
88 | |
89 | #else |
90 | if (scale_factors.find("CDC_ADC_TSCALE") != scale_factors.end()) |
91 | t_scale = scale_factors["CDC_ADC_TSCALE"]; |
92 | else |
93 | jerr << "Unable to get CDC_ADC_TSCALE from /CDC/digi_scales !" << endl; |
94 | #endif |
95 | |
96 | |
97 | map<string,double> base_time_offset; |
98 | if (eventLoop->GetCalib("/CDC/base_time_offset",base_time_offset)) |
99 | jout << "Error loading /CDC/base_time_offset !" << endl; |
100 | if (base_time_offset.find("CDC_BASE_TIME_OFFSET") != base_time_offset.end()) |
101 | t_base = base_time_offset["CDC_BASE_TIME_OFFSET"]; |
102 | else |
103 | jerr << "Unable to get CDC_BASE_TIME_OFFSET from /CDC/base_time_offset !" << endl; |
104 | |
105 | |
106 | if (eventLoop->GetCalib("/CDC/wire_gains", raw_gains)) |
107 | jout << "Error loading /CDC/wire_gains !" << endl; |
108 | if (eventLoop->GetCalib("/CDC/pedestals", raw_pedestals)) |
109 | jout << "Error loading /CDC/pedestals !" << endl; |
110 | if (eventLoop->GetCalib("/CDC/timing_offsets", raw_time_offsets)) |
111 | jout << "Error loading /CDC/timing_offsets !" << endl; |
112 | |
113 | |
114 | FillCalibTable(gains, raw_gains, Nstraws); |
115 | FillCalibTable(pedestals, raw_pedestals, Nstraws); |
116 | FillCalibTable(time_offsets, raw_time_offsets, Nstraws); |
117 | |
118 | |
119 | char str[256]; |
120 | if (gains.size() != Nrings) { |
121 | sprintf(str, "Bad # of rings for CDC gain from CCDB! CCDB=%zu , should be %d", gains.size(), Nrings); |
122 | std::cerr << str << std::endl; |
123 | throw JException(str); |
124 | } |
125 | if (pedestals.size() != Nrings) { |
126 | sprintf(str, "Bad # of rings for CDC pedestal from CCDB! CCDB=%zu , should be %d", pedestals.size(), Nrings); |
127 | std::cerr << str << std::endl; |
128 | throw JException(str); |
129 | } |
130 | if (time_offsets.size() != Nrings) { |
131 | sprintf(str, "Bad # of rings for CDC time_offset from CCDB!" |
132 | " CCDB=%zu , should be %d", time_offsets.size(), Nrings); |
133 | std::cerr << str << std::endl; |
134 | throw JException(str); |
135 | } |
136 | |
137 | |
138 | for (unsigned int i=0; i < Nrings; i++) { |
139 | if (gains[i].size() != Nstraws[i]) { |
140 | sprintf(str, "Bad # of straws for CDC gain from CCDB!" |
141 | " CCDB=%zu , should be %d for ring %d", |
142 | gains[i].size(), Nstraws[i], i+1); |
143 | std::cerr << str << std::endl; |
144 | throw JException(str); |
145 | } |
146 | if (pedestals[i].size() != Nstraws[i]) { |
147 | sprintf(str, "Bad # of straws for CDC pedestal from CCDB!" |
148 | " CCDB=%zu , should be %d for ring %d", |
149 | pedestals[i].size(), Nstraws[i], i+1); |
150 | std::cerr << str << std::endl; |
151 | throw JException(str); |
152 | } |
153 | if (time_offsets[i].size() != Nstraws[i]) { |
154 | sprintf(str, "Bad # of straws for CDC time_offset from CCDB!" |
155 | " CCDB=%zu , should be %d for ring %d", |
156 | time_offsets[i].size(), Nstraws[i], i+1); |
157 | std::cerr << str << std::endl; |
158 | throw JException(str); |
159 | } |
160 | } |
161 | |
162 | return NOERROR; |
163 | } |
164 | |
165 | |
166 | |
167 | |
168 | jerror_t DCDCHit_factory::evnt(JEventLoop *loop, uint64_t eventnumber) |
169 | { |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | vector<const DCDCDigiHit*> digihits; |
182 | loop->Get(digihits); |
183 | char str[256]; |
184 | for (unsigned int i=0; i < digihits.size(); i++) { |
185 | const DCDCDigiHit *digihit = digihits[i]; |
186 | |
187 | const int &ring = digihit->ring; |
188 | const int &straw = digihit->straw; |
189 | |
190 | |
191 | if ( (ring < 1) || (ring > (int)Nrings)) { |
192 | sprintf(str, "DCDCDigiHit ring out of range!" |
193 | " ring=%d (should be 1-%d)", ring, Nrings); |
194 | throw JException(str); |
195 | } |
196 | if ( (straw < 1) || (straw > (int)Nstraws[ring-1])) { |
197 | sprintf(str, "DCDCDigiHit straw out of range!" |
198 | " straw=%d for ring=%d (should be 1-%d)", |
199 | straw, ring, Nstraws[ring-1]); |
200 | throw JException(str); |
201 | } |
202 | |
203 | |
204 | |
205 | double pedestal = pedestals[ring-1][straw-1]; |
| Value stored to 'pedestal' during its initialization is never read |
206 | |
207 | |
208 | uint32_t raw_ped = digihit->pedestal; |
209 | uint32_t nsamples_integral = digihit->nsamples_integral; |
210 | |
211 | |
212 | uint16_t IBIT = 0; |
213 | uint16_t ABIT = 0; |
214 | uint16_t PBIT = 0; |
215 | |
216 | |
217 | |
218 | const Df125CDCPulse *CDCPulseObj = NULL__null; |
219 | digihit->GetSingle(CDCPulseObj); |
220 | if (CDCPulseObj != NULL__null){ |
221 | const Df125Config *config = NULL__null; |
222 | CDCPulseObj->GetSingle(config); |
223 | |
224 | |
225 | IBIT = config->IBIT == 0xffff ? 4 : config->IBIT; |
226 | ABIT = config->ABIT == 0xffff ? 3 : config->ABIT; |
227 | PBIT = config->PBIT == 0xffff ? 0 : config->PBIT; |
228 | uint16_t NW = config->NW == 0xffff ? 200 : config->NW; |
229 | |
230 | |
231 | |
232 | nsamples_integral = (NW - (digihit->pulse_time / 10)); |
233 | |
234 | } |
235 | else{ |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | |
242 | if (digihit->pulse_time==0.) continue; |
243 | |
244 | |
245 | |
246 | |
247 | const Df125PulsePedestal* PPobj = NULL__null; |
248 | digihit->GetSingle(PPobj); |
249 | if (PPobj != NULL__null){ |
250 | if (PPobj->pedestal == 0 || PPobj->pulse_peak == 0) continue; |
251 | if (PPobj->pulse_number == 1) continue; |
252 | } |
253 | |
254 | const Df125PulseIntegral* PIobj = NULL__null; |
255 | digihit->GetSingle(PIobj); |
256 | if (PPobj == NULL__null || PIobj == NULL__null) continue; |
257 | } |
258 | |
259 | |
260 | uint32_t scaled_ped = raw_ped << PBIT; |
261 | pedestal = double(scaled_ped * nsamples_integral); |
262 | |
263 | |
264 | double integral = double(digihit->pulse_integral << IBIT); |
265 | double t_raw = double(digihit->pulse_time); |
266 | |
267 | double q = a_scale * gains[ring-1][straw-1] * (integral - pedestal); |
268 | double t = t_scale * t_raw - time_offsets[ring-1][straw-1] + t_base; |
269 | |
270 | if (q < DIGI_THRESHOLD) |
271 | continue; |
272 | |
273 | DCDCHit *hit = new DCDCHit; |
274 | hit->ring = ring; |
275 | hit->straw = straw; |
276 | |
277 | |
278 | |
279 | hit->q = q; |
280 | hit->t = t; |
281 | hit->d = 0.0; |
282 | hit->itrack = -1; |
283 | hit->ptype = 0; |
284 | |
285 | hit->AddAssociatedObject(digihit); |
286 | |
287 | _data.push_back(hit); |
288 | } |
289 | |
290 | return NOERROR; |
291 | } |
292 | |
293 | |
294 | |
295 | |
296 | jerror_t DCDCHit_factory::erun(void) |
297 | { |
298 | return NOERROR; |
299 | } |
300 | |
301 | |
302 | |
303 | |
304 | jerror_t DCDCHit_factory::fini(void) |
305 | { |
306 | return NOERROR; |
307 | } |
308 | |
309 | |
310 | |
311 | |
312 | void DCDCHit_factory::CalcNstraws(jana::JEventLoop *eventLoop, int32_t runnumber, vector<unsigned int> &Nstraws) |
313 | { |
314 | DGeometry *dgeom; |
315 | vector<vector<DCDCWire *> >cdcwires; |
316 | |
317 | |
318 | DApplication* dapp=dynamic_cast<DApplication*>(eventLoop->GetJApplication()); |
319 | dgeom = dapp->GetDGeometry(runnumber); |
320 | |
321 | |
322 | dgeom->GetCDCWires(cdcwires); |
323 | |
324 | |
325 | |
326 | maxChannels = 0; |
327 | Nstraws.clear(); |
328 | for (unsigned int i=0; i<cdcwires.size(); i++) { |
329 | Nstraws.push_back( cdcwires[i].size() ); |
330 | maxChannels += cdcwires[i].size(); |
331 | } |
332 | |
333 | |
334 | for (unsigned int i=0; i<cdcwires.size(); i++) { |
335 | for (unsigned int j=0; j<cdcwires[i].size(); j++) { |
336 | delete cdcwires[i][j]; |
337 | } |
338 | } |
339 | cdcwires.clear(); |
340 | } |
341 | |
342 | |
343 | |
344 | |
345 | |
346 | void DCDCHit_factory::FillCalibTable(vector< vector<double> > &table, vector<double> &raw_table, |
347 | vector<unsigned int> &Nstraws) |
348 | { |
349 | int ring = 0; |
350 | int straw = 0; |
351 | |
352 | |
353 | table.clear(); |
354 | table.resize( Nstraws.size() ); |
355 | |
356 | for (unsigned int channel=0; channel<raw_table.size(); channel++,straw++) { |
357 | |
358 | if (channel == maxChannels) break; |
359 | |
360 | |
361 | if (straw == (int)Nstraws[ring]) { |
362 | ring++; |
363 | straw = 0; |
364 | } |
365 | |
366 | table[ring].push_back( raw_table[channel] ); |
367 | } |
368 | |
369 | } |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | const double DCDCHit_factory::GetConstant(const cdc_digi_constants_t &the_table, |
377 | const int in_ring, const int in_straw) const { |
378 | |
379 | char str[256]; |
380 | |
381 | if ( (in_ring <= 0) || (static_cast<unsigned int>(in_ring) > Nrings)) { |
382 | sprintf(str, "Bad ring # requested in DCDCHit_factory::GetConstant()!" |
383 | " requested=%d , should be %ud", in_ring, Nrings); |
384 | std::cerr << str << std::endl; |
385 | throw JException(str); |
386 | } |
387 | if ( (in_straw <= 0) || |
388 | (static_cast<unsigned int>(in_straw) > Nstraws[in_ring])) |
389 | { |
390 | sprintf(str, "Bad straw # requested in DCDCHit_factory::GetConstant()!" |
391 | " requested=%d , should be %ud", in_straw, Nstraws[in_ring]); |
392 | std::cerr << str << std::endl; |
393 | throw JException(str); |
394 | } |
395 | |
396 | return the_table[in_ring-1][in_straw-1]; |
397 | } |
398 | |
399 | const double DCDCHit_factory::GetConstant(const cdc_digi_constants_t &the_table, |
400 | const DCDCDigiHit *in_digihit) const { |
401 | |
402 | char str[256]; |
403 | |
404 | if ( (in_digihit->ring <= 0) || |
405 | (static_cast<unsigned int>(in_digihit->ring) > Nrings)) |
406 | { |
407 | sprintf(str, "Bad ring # requested in DCDCHit_factory::GetConstant()!" |
408 | " requested=%d , should be %ud", in_digihit->ring, Nrings); |
409 | std::cerr << str << std::endl; |
410 | throw JException(str); |
411 | } |
412 | if ( (in_digihit->straw <= 0) || |
413 | (static_cast<unsigned int>(in_digihit->straw) > Nstraws[in_digihit->ring])) |
414 | { |
415 | sprintf(str, "Bad straw # requested in DCDCHit_factory::GetConstant()!" |
416 | " requested=%d , should be %ud", |
417 | in_digihit->straw, Nstraws[in_digihit->ring]); |
418 | std::cerr << str << std::endl; |
419 | throw JException(str); |
420 | } |
421 | |
422 | return the_table[in_digihit->ring-1][in_digihit->straw-1]; |
423 | } |
424 | |
425 | const double DCDCHit_factory::GetConstant(const cdc_digi_constants_t &the_table, |
426 | const DCDCHit *in_hit) const { |
427 | |
428 | char str[256]; |
429 | |
430 | if ( (in_hit->ring <= 0) || (static_cast<unsigned int>(in_hit->ring) > Nrings)) { |
431 | sprintf(str, "Bad ring # requested in DCDCHit_factory::GetConstant()!" |
432 | " requested=%d , should be %ud", in_hit->ring, Nrings); |
433 | std::cerr << str << std::endl; |
434 | throw JException(str); |
435 | } |
436 | if ( (in_hit->straw <= 0) || |
437 | (static_cast<unsigned int>(in_hit->straw) > Nstraws[in_hit->ring])) { |
438 | sprintf(str, "Bad straw # requested in DCDCHit_factory::GetConstant()!" |
439 | " requested=%d , should be %ud", |
440 | in_hit->straw, Nstraws[in_hit->ring]); |
441 | std::cerr << str << std::endl; |
442 | throw JException(str); |
443 | } |
444 | |
445 | return the_table[in_hit->ring-1][in_hit->straw-1]; |
446 | } |
447 | |
448 | |
449 | |
450 | |
451 | |
452 | |
453 | |
454 | |
455 | |
456 | |
457 | |
458 | |
459 | |
460 | |
461 | |
462 | |
463 | |
464 | |
465 | |
466 | |
467 | |
468 | |
469 | |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |