clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -main-file-name Df250EmulatorAlgorithm_v2.cc -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /w/halld-scifs17exp/home/sdobbs/clang/llvm-project/install/lib/clang/12.0.0 -D HAVE_EVIO -D HAVE_TMVA=1 -I .Linux_CentOS7.7-x86_64-gcc4.8.5/libraries/DAQ -I libraries/DAQ -I . -I libraries -I libraries/include -I /w/halld-scifs17exp/home/sdobbs/clang/halld_recon/Linux_CentOS7.7-x86_64-gcc4.8.5/include -I libraries -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/evio/evio-4.4.6/Linux-x86_64/include -I /w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/root/root-6.08.06/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/backward -internal-isystem /usr/local/include -internal-isystem /w/halld-scifs17exp/home/sdobbs/clang/llvm-project/install/lib/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /home/sdobbs/work/clang/halld_recon/src -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-01-21-110224-160369-1 -x c++ libraries/DAQ/Df250EmulatorAlgorithm_v2.cc
1 | #include <DAQ/Df250EmulatorAlgorithm_v2.h> |
2 | |
3 | |
4 | |
5 | Df250EmulatorAlgorithm_v2::Df250EmulatorAlgorithm_v2(JEventLoop *loop){ |
6 | |
7 | FORCE_DEFAULT = 0; |
8 | |
9 | NSA_DEF = 20; |
10 | NSB_DEF = 5; |
11 | THR_DEF = 120; |
12 | NPED_DEF = 4; |
13 | MAXPED_DEF = 512; |
14 | NSAT_DEF = 2; |
15 | |
16 | |
17 | NSA_DEF = 15; |
18 | NSB_DEF = 1; |
19 | THR_DEF = 108; |
20 | NPED_DEF = 4; |
21 | MAXPED_DEF = 512; |
22 | NSAT_DEF = 2; |
23 | |
24 | |
25 | |
26 | VERBOSE = 0; |
27 | |
28 | if(gPARMS){ |
29 | gPARMS->SetDefaultParameter("EMULATION250:FORCE_DEFAULT", FORCE_DEFAULT,"Set to >0 to force use of default values"); |
30 | gPARMS->SetDefaultParameter("EMULATION250:NSA", NSA_DEF,"Set NSA for firmware emulation, will be overwritten by BORConfig if present"); |
31 | gPARMS->SetDefaultParameter("EMULATION250:NSB", NSB_DEF,"Set NSB for firmware emulation, will be overwritten by BORConfig if present"); |
32 | gPARMS->SetDefaultParameter("EMULATION250:THR", THR_DEF,"Set threshold for firmware emulation, will be overwritten by BORConfig if present"); |
33 | gPARMS->SetDefaultParameter("EMULATION250:NPED", NPED_DEF,"Set NPED for firmware emulation, will be overwritten by BORConfig if present"); |
34 | gPARMS->SetDefaultParameter("EMULATION250:MAXPED", MAXPED_DEF,"Set MAXPED for firmware emulation, will be overwritten by BORConfig if present"); |
35 | gPARMS->SetDefaultParameter("EMULATION250:NSAT", NSAT_DEF,"Set NSAT for firmware emulation, will be overwritten by BORConfig if present"); |
36 | gPARMS->SetDefaultParameter("EMULATION250:VERBOSE", VERBOSE,"Set verbosity for f250 emulation"); |
37 | } |
38 | } |
39 | |
40 | void Df250EmulatorAlgorithm_v2::EmulateFirmware(const Df250WindowRawData* rawData, |
41 | std::vector<Df250PulseData*> &pdat_objs) |
42 | { |
43 | |
44 | if (VERBOSE > 0) { |
| 1 | Assuming field 'VERBOSE' is <= 0 | |
|
| |
45 | jout << " Df250EmulatorAlgorithm_v2::EmulateFirmware ==> Starting emulation <==" << endl; |
46 | jout << "rocid : " << rawData->rocid << " slot: " << rawData->slot << " channel: " << rawData->channel << endl; |
47 | } |
48 | |
49 | |
50 | if (rawData == NULL) { |
| 3 | | Assuming 'rawData' is not equal to NULL | |
|
| |
51 | jerr << " ERROR: Df250EmulatorAlgorithm_v2::EmulateFirmware - raw sample data is missing" << endl; |
52 | jerr << " Contact mstaib@jlab.org" << endl; |
53 | return; |
54 | } |
55 | |
56 | |
57 | uint32_t channel = rawData->channel; |
58 | |
59 | |
60 | |
61 | |
62 | const Df250BORConfig *f250BORConfig = NULL; |
63 | rawData->GetSingle(f250BORConfig); |
| 5 | | Calling 'JObject::GetSingle' | |
|
| 10 | | Returning from 'JObject::GetSingle' | |
|
64 | |
65 | uint32_t NSA; |
66 | int32_t NSB; |
67 | uint32_t NPED, MAXPED; |
68 | uint16_t THR; |
69 | uint16_t NSAT; |
70 | |
71 | if (f250BORConfig == NULL || FORCE_DEFAULT){ |
| 11 | | Assuming field 'FORCE_DEFAULT' is 0 | |
|
| |
72 | static int counter = 0; |
73 | NSA = NSA_DEF; |
74 | NSB = NSB_DEF; |
75 | THR = THR_DEF; |
76 | NPED = NPED_DEF; |
77 | MAXPED = MAXPED_DEF; |
78 | NSAT = NSAT_DEF; |
79 | if (counter < 10){ |
80 | counter++; |
81 | if (counter == 10) jout << " WARNING Df250EmulatorAlgorithm_v2::EmulateFirmware No Df250BORConfig == Using default values == LAST WARNING" << endl; |
82 | else jout << " WARNING Df250EmulatorAlgorithm_v2::EmulateFirmware No Df250BORConfig == Using default values " << endl; |
83 | |
84 | } |
85 | } |
86 | else{ |
87 | NSA = f250BORConfig->NSA; |
88 | NSB = f250BORConfig->NSB; |
89 | THR = f250BORConfig->adc_thres[channel]; |
90 | NPED = f250BORConfig->NPED; |
| 13 | | Value assigned to 'NPED' | |
|
91 | MAXPED = f250BORConfig->MaxPed; |
92 | NSAT = f250BORConfig->NSAT; |
93 | |
94 | } |
95 | |
96 | if (VERBOSE > 0) jout << "Df250EmulatorAlgorithm_v2::EmulateFirmware NSA: " << NSA << " NSB: " << NSB << " THR: " << THR << endl; |
| |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | bool bad_pedestal = false; |
111 | bool bad_timing_pedestal = false; |
112 | bool no_timing_calculation = false; |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | vector<uint16_t> samples = rawData->samples; |
120 | uint16_t NW = samples.size(); |
121 | uint32_t npulses = 0; |
122 | const int max_pulses = 3; |
123 | uint32_t TC[max_pulses] = {}; |
124 | uint32_t TMIN[max_pulses] = {3}; |
125 | |
126 | uint32_t pulse_integral[max_pulses] = {}; |
127 | bool has_overflow_samples[max_pulses] = {false}; |
128 | bool has_underflow_samples[max_pulses] = {false}; |
129 | uint32_t number_samples_above_threshold[max_pulses] = {0}; |
130 | bool NSA_beyond_PTW[max_pulses] = {false}; |
131 | bool vpeak_beyond_NSA[max_pulses] = {false}; |
132 | bool vpeak_not_found[max_pulses] = {false}; |
133 | |
134 | |
135 | if(VERBOSE > 0) { |
| |
136 | for (unsigned int i=0; i < NW; i++) { |
137 | if(VERBOSE > 2) { |
138 | if(samples[i] == 0x1fff) |
139 | jout << "Overflow at sample " << i << endl; |
140 | if(samples[i] == 0x1000) |
141 | jout << "Underflow at sample " << i << endl; |
142 | } |
143 | if (VERBOSE > 5) jout << "Df250EmulatorAlgorithm_v2::EmulateFirmware samples[" << i << "]: " << samples[i] << endl; |
144 | } |
145 | } |
146 | |
147 | |
148 | |
149 | |
150 | unsigned int MAX_SAMPLE = NW-NSAT; |
151 | for (unsigned int i=0; i < MAX_SAMPLE; i++) { |
| 16 | | Assuming 'i' is >= 'MAX_SAMPLE' | |
|
| 17 | | Loop condition is false. Execution continues on line 231 | |
|
152 | if ((samples[i] & 0xfff) > THR) { |
153 | if (VERBOSE > 1) { |
154 | jout << "threshold crossing at " << i << endl; |
155 | } |
156 | |
157 | |
158 | TC[npulses] = i+1; |
159 | |
160 | |
161 | if( NSAT>1 ){ |
162 | int samples_over_threshold = 1; |
163 | |
164 | if(i==0) { |
165 | |
166 | for(unsigned int j=i+1; ((samples[j]&0xfff)>=THR) && (j<MAX_SAMPLE+1); j++) { |
167 | |
168 | if ((samples[j] & 0xfff) > THR) |
169 | samples_over_threshold++; |
170 | |
171 | if( samples_over_threshold == NSAT ) { |
172 | |
173 | |
174 | break; |
175 | } |
176 | |
177 | } |
178 | } else { |
179 | for(unsigned int j=i+1; ((samples[j]&0xfff)>THR) && (j<MAX_SAMPLE+1); j++) { |
180 | samples_over_threshold++; |
181 | |
182 | if( samples_over_threshold == NSAT ) |
183 | break; |
184 | } |
185 | } |
186 | |
187 | |
188 | if( samples_over_threshold != NSAT ) |
189 | continue; |
190 | } |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | unsigned int ibegin; |
198 | if(NSB > 0) |
199 | ibegin = i > uint32_t(NSB) ? (i - NSB) : 0; |
200 | else { |
201 | ibegin = i - NSB; |
202 | if(ibegin > uint32_t(NW)) |
203 | break; |
204 | } |
205 | unsigned int iend = (i + NSA) < uint32_t(NW) ? (i + NSA) : NW; |
206 | |
207 | NSA_beyond_PTW[npulses] = (i + NSA - 1) >= uint32_t(NW); |
208 | for (i = ibegin; i < iend; ++i) { |
209 | pulse_integral[npulses] += (samples[i] & 0xfff); |
210 | |
211 | if(samples[i] == 0x1fff) { |
212 | has_overflow_samples[npulses] = true; |
213 | } |
214 | if(samples[i] == 0x1000) { |
215 | has_underflow_samples[npulses] = true; |
216 | } |
217 | |
218 | if( (i+1>=TC[npulses]) && ((samples[i] & 0xfff) > THR) ) |
219 | number_samples_above_threshold[npulses]++; |
220 | } |
221 | for (; i < NW && (samples[i] & 0xfff) >= THR; ++i) {} |
222 | if (++npulses == max_pulses) |
223 | break; |
224 | TMIN[npulses] = i; |
225 | } |
226 | } |
227 | |
228 | |
229 | |
230 | |
231 | uint32_t VPEAK[max_pulses] = {}; |
232 | uint32_t TPEAK[max_pulses] = {}; |
233 | uint16_t TMID[max_pulses] = {}; |
234 | uint16_t VMID[max_pulses] = {}; |
235 | uint16_t TFINE[max_pulses] = {}; |
236 | uint32_t pulse_time[max_pulses] = {}; |
237 | |
238 | |
239 | uint32_t pedestal = 0; |
240 | uint32_t VMIN = 0; |
241 | for (unsigned int i=0; i < NPED; i++) { |
| 18 | | Assuming 'i' is >= 'NPED' | |
|
| 19 | | Loop condition is false. Execution continues on line 255 | |
|
242 | pedestal += (samples[i] & 0xfff); |
243 | if(i<4) |
244 | VMIN += (samples[i] & 0xfff); |
245 | |
246 | |
247 | if ((samples[i] & 0xfff) > MAXPED) { |
248 | bad_pedestal = true; |
249 | } |
250 | |
251 | if( (samples[i] == 0x1fff) || (samples[i] == 0x1000) ) { |
252 | bad_pedestal = true; |
253 | } |
254 | } |
255 | VMIN /= NPED; |
| |
256 | |
257 | |
258 | |
259 | |
260 | for (unsigned int i=0; i < 4; i++) { |
261 | |
262 | if ( ((samples[i] & 0xfff) > MAXPED) || ((samples[i] & 0xfff) > THR) ) { |
263 | bad_timing_pedestal = true; |
264 | } |
265 | |
266 | if ( (samples[i] == 0x1000) || (samples[i] == 0x1fff) ) { |
267 | bad_timing_pedestal = true; |
268 | } |
269 | |
270 | |
271 | |
272 | |
273 | |
274 | |
275 | |
276 | if( (samples[i] & 0xfff) > THR ) { |
277 | no_timing_calculation = true; |
278 | } |
279 | } |
280 | |
281 | |
282 | for (unsigned int p=0; p < npulses; ++p) { |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 | if(no_timing_calculation) { |
291 | TMID[p] = TC[p]; |
292 | TFINE[p] = 0; |
293 | VPEAK[p] = 0; |
294 | vpeak_not_found[p] = true; |
295 | |
296 | } |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | while ( (!no_timing_calculation) && true) { |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | unsigned int ipeak; |
314 | for (ipeak = TC[p]; (int)ipeak < NW-1; ++ipeak) { |
315 | |
316 | if ((samples[ipeak] & 0xfff) < (samples[ipeak-1] & 0xfff)) { |
317 | VPEAK[p] = (samples[ipeak-1] & 0xfff); |
318 | TPEAK[p] = ipeak-1; |
319 | break; |
320 | } |
321 | } |
322 | |
323 | |
324 | if(ipeak > TC[p]+NSA) |
325 | vpeak_beyond_NSA[p] = true; |
326 | |
327 | if (VERBOSE > 1) { |
328 | jout << " pulse " << p << ": VMIN: " << VMIN |
329 | << " TC: " << TC[p] << " VPEAK: " << VPEAK[p] << endl; |
330 | } |
331 | |
332 | |
333 | if (VPEAK[p] == 0) { |
334 | TMID[p] = TC[p]; |
335 | TFINE[p] = 0; |
336 | VPEAK[p] = 0; |
337 | vpeak_beyond_NSA[p] = true; |
338 | vpeak_not_found[p] = true; |
339 | break; |
340 | } |
341 | |
342 | |
343 | VMID[p] = (VMIN + VPEAK[p]) >> 1; |
344 | |
345 | |
346 | |
347 | |
348 | |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | |
358 | |
359 | for (unsigned int i = TPEAK[p]; i >= 1; --i) { |
360 | if ( ((samples[i-1] & 0xfff) <= VMID[p]) && ((samples[i] & 0xfff) > VMID[p]) ) { |
361 | |
362 | |
363 | TMID[p] = i; |
364 | break; |
365 | } |
366 | } |
367 | |
368 | if (TMID[p] == 0) { |
369 | TFINE[p] = 0; |
370 | } |
371 | else { |
372 | |
373 | int Vnext = (samples[TMID[p]] & 0xfff); |
374 | int Vlast = (samples[TMID[p]-1] & 0xfff); |
375 | if (VERBOSE > 2) { |
376 | jout << " TMIN = " << TMIN[p] << " TMID = " << TMID[p] << " TPEAK = " << TPEAK[p] << endl |
377 | << " VMID = " << VMID[p] << " Vnext = " << Vnext << " Vlast = " << Vlast << endl; |
378 | } |
379 | if (Vnext > Vlast && VMID[p] >= Vlast) |
380 | TFINE[p] = 64 * (VMID[p] - Vlast) / (Vnext - Vlast); |
381 | else |
382 | TFINE[p] = 62; |
383 | if(TFINE[p] == 64) |
384 | TFINE[p] = 0; |
385 | } |
386 | pulse_time[p] = ((TMID[p]-1) << 6) + TFINE[p]; |
387 | break; |
388 | } |
389 | VMIN = (VMIN < 99999)? VMIN : 0; |
390 | |
391 | if (VERBOSE > 1) { |
392 | jout << " pulse " << p << ": VMID: " << VMID[p] << " TMID: " << TMID[p] |
393 | << " TFINE: " << TFINE[p] << " time: " << pulse_time[p] |
394 | << " integral: " << pulse_integral[p] << endl; |
395 | if (VERBOSE > 2) { |
396 | jout << " TMIN = " << TMIN[p] << " TMID = " << TMID[p] << " TPEAK = " << TPEAK[p] << endl; |
397 | |
398 | } |
399 | } |
400 | |
401 | |
402 | Df250PulseData* f250PulseData; |
403 | if( p < pdat_objs.size() ) { |
404 | f250PulseData = pdat_objs[p]; |
405 | |
406 | if(f250PulseData == NULL) { |
407 | jerr << " NULL f250PulseData object!" << endl; |
408 | continue; |
409 | } |
410 | } else { |
411 | |
412 | f250PulseData = new Df250PulseData; |
413 | |
414 | f250PulseData->rocid = rawData->rocid; |
415 | f250PulseData->slot = rawData->slot; |
416 | f250PulseData->channel = rawData->channel; |
417 | f250PulseData->itrigger = rawData->itrigger; |
418 | |
419 | f250PulseData->event_within_block = 1; |
420 | f250PulseData->QF_pedestal = bad_pedestal; |
421 | f250PulseData->pedestal = pedestal; |
422 | |
423 | f250PulseData->integral = pulse_integral[p]; |
424 | f250PulseData->QF_NSA_beyond_PTW = NSA_beyond_PTW[p]; |
425 | f250PulseData->QF_overflow = has_overflow_samples[p]; |
426 | f250PulseData->QF_underflow = has_underflow_samples[p]; |
427 | f250PulseData->nsamples_over_threshold = number_samples_above_threshold[p]; |
428 | |
429 | f250PulseData->course_time = TMID[p]; |
430 | f250PulseData->fine_time = TFINE[p]; |
431 | f250PulseData->QF_vpeak_beyond_NSA = vpeak_beyond_NSA[p]; |
432 | f250PulseData->QF_vpeak_not_found = vpeak_not_found[p]; |
433 | f250PulseData->QF_bad_pedestal = bad_timing_pedestal; |
434 | |
435 | f250PulseData->pulse_number = p; |
436 | f250PulseData->nsamples_integral = NSA + NSB; |
437 | f250PulseData->nsamples_pedestal = NPED; |
438 | f250PulseData->emulated = true; |
439 | |
440 | f250PulseData->AddAssociatedObject(rawData); |
441 | const_cast<Df250WindowRawData*>(rawData)->AddAssociatedObject(f250PulseData); |
442 | pdat_objs.push_back(f250PulseData); |
443 | } |
444 | |
445 | |
446 | f250PulseData->integral_emulated = pulse_integral[p]; |
447 | f250PulseData->pedestal_emulated = pedestal; |
448 | f250PulseData->pulse_peak_emulated = VPEAK[p]; |
449 | f250PulseData->course_time_emulated = TMID[p]; |
450 | f250PulseData->fine_time_emulated = TFINE[p]; |
451 | |
452 | |
453 | uint32_t QF = 0; |
454 | if( bad_pedestal ) QF |= (1<<0); |
455 | if( NSA_beyond_PTW[p] ) QF |= (1<<1); |
456 | if( has_overflow_samples[p] ) QF |= (1<<2); |
457 | if( has_underflow_samples[p] ) QF |= (1<<3); |
458 | if( vpeak_beyond_NSA[p] ) QF |= (1<<4); |
459 | if( vpeak_not_found[p] ) QF |= (1<<5); |
460 | if( bad_timing_pedestal ) QF |= (1<<6); |
461 | f250PulseData->QF_emulated = QF; |
462 | |
463 | if(VERBOSE > 3) { |
464 | cout << boolalpha; |
465 | cout << "bad_pedestal = " << bad_pedestal << endl; |
466 | cout << "NSA_beyond_PTW = " << NSA_beyond_PTW[p] << endl; |
467 | cout << "has_overflow_samples = " << has_overflow_samples[p] << endl; |
468 | cout << "has_underflow_samples = " << has_underflow_samples[p] << endl; |
469 | cout << "vpeak_beyond_NSA = " << vpeak_beyond_NSA[p] << endl; |
470 | cout << "vpeak_not_found = " << vpeak_not_found[p] << endl; |
471 | cout << "bad_timing_pedestal = " << bad_timing_pedestal << endl; |
472 | cout << "total QF = " << QF << endl; |
473 | } |
474 | |
475 | |
476 | if( f250PulseData->emulated ) { |
477 | f250PulseData->integral = f250PulseData->integral_emulated; |
478 | f250PulseData->pedestal = f250PulseData->pedestal_emulated; |
479 | f250PulseData->pulse_peak = f250PulseData->pulse_peak_emulated; |
480 | f250PulseData->course_time = f250PulseData->course_time_emulated; |
481 | f250PulseData->fine_time = f250PulseData->fine_time_emulated; |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | |
488 | |
489 | } |
490 | |
491 | } |
492 | |
493 | if (VERBOSE > 0) jout << " Df250EmulatorAlgorithm_v2::EmulateFirmware ==> Emulation complete <==" << endl; |
494 | return; |
495 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | #ifndef _JObject_ |
9 | #define _JObject_ |
10 | |
11 | #include <cstdio> |
12 | #include <sstream> |
13 | #include <cassert> |
14 | #include <map> |
15 | #include <vector> |
16 | #include <set> |
17 | #include <string> |
18 | #include <cstdint> |
19 | #include <type_traits> |
20 | #include <typeinfo> |
21 | #include <stdint.h> |
22 | using std::pair; |
23 | using std::map; |
24 | using std::set; |
25 | using std::vector; |
26 | using std::string; |
27 | using std::stringstream; |
28 | |
29 | |
30 | #include "cint.h" |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | #define JOBJECT_PUBLIC(T) \ |
51 | virtual const char* className(void) const {return static_className();} \ |
52 | static const char* static_className(void) {return #T;} \ |
53 | virtual JObject* Clone() const {return CloneObject<T>( *this );} |
54 | |
55 | |
56 | |
57 | |
58 | namespace jana{ |
59 | |
60 | class JFactory_base; |
61 | class JEventLoop; |
62 | |
63 | class JObject{ |
64 | |
65 | public: |
66 | JOBJECT_PUBLIC(JObject); |
67 | |
68 | typedef unsigned long long oid_t; |
69 | |
70 | JObject() : id((oid_t)this),append_types(false),factory(NULL) {} |
71 | JObject( oid_t aId ) : id( aId ),append_types(false),factory(NULL) {} |
72 | |
73 | virtual ~JObject(){ |
74 | for(unsigned int i=0; i<auto_delete.size(); i++)delete auto_delete[i]; |
75 | } |
76 | |
77 | |
78 | JObject(const JObject& o) : |
79 | id( (oid_t)this ), append_types(o.append_types), associated( |
80 | o.associated), auto_delete(), messagelog( |
81 | o.messagelog), factory(o.factory) { |
82 | |
83 | for( auto obj : o.auto_delete ) { |
84 | auto_delete.push_back( obj->Clone() ); |
85 | } |
86 | } |
87 | |
88 | |
89 | JObject( const JObject&& o ) : id( (oid_t)this ), append_types(o.append_types), associated( |
90 | std::move(o.associated)), auto_delete(std::move(o.auto_delete)), messagelog( std::move( |
91 | o.messagelog)), factory(o.factory) { |
92 | factory = nullptr; |
93 | } |
94 | |
95 | |
96 | JObject& operator=( const JObject& o) { |
97 | if( this == &o ) return *this; |
98 | append_types = o.append_types; |
99 | associated = o.associated; |
100 | messagelog = o.messagelog; |
101 | factory = o.factory; |
102 | auto_delete.clear(); |
103 | for( auto obj : o.auto_delete ) { |
104 | auto_delete.push_back( obj->Clone() ); |
105 | } |
106 | return *this; |
107 | } |
108 | |
109 | |
110 | |
111 | template<typename TYPE> |
112 | static typename std::enable_if< |
113 | (std::is_abstract <TYPE>::value || !std::is_copy_constructible<TYPE>::value), JObject*>::type CloneObject( |
114 | const TYPE& obj) { |
115 | |
116 | |
117 | return nullptr; |
118 | } |
119 | template<typename TYPE> |
120 | static typename std::enable_if< |
121 | (!std::is_abstract <TYPE>::value |
122 | && std::is_copy_constructible <TYPE>::value ), JObject*>::type CloneObject( |
123 | const TYPE& obj) { |
124 | |
125 | |
126 | return new TYPE(obj); |
127 | } |
128 | |
129 | |
130 | template<typename T> bool IsA(const T *t) const {return dynamic_cast<const T*>(this)!=0L;} |
131 | |
132 | |
133 | template<typename T> bool IsAT(const T *t) const {return dynamic_cast<const T*>(this)!=0L;} |
134 | |
135 | |
136 | inline void AddAssociatedObject(const JObject *obj); |
137 | inline void AddAssociatedObjectAutoDelete(JObject *obj, bool auto_delete=true); |
138 | inline void RemoveAssociatedObject(const JObject *obj); |
139 | inline void ClearAssociatedObjects(void); |
140 | inline bool IsAssociated(const JObject* locObject) const {return (associated.find(locObject) != associated.end());} |
141 | template<typename T> void Get(vector<const T*> &ptrs, string classname="", int max_depth=1000000) const ; |
142 | template<typename T> void GetT(vector<const T*> &ptrs) const ; |
143 | template<typename T> void GetSingle(const T* &ptrs, string classname="") const ; |
144 | template<typename T> void GetSingleT(const T* &ptrs) const ; |
145 | template<typename T> void GetAssociatedAncestors(set<const JObject*> &already_checked, int &max_depth, set<const T*> &objs_found, string classname="") const; |
146 | template<typename T> void GetAssociatedDescendants(JEventLoop *loop, vector<const T*> &associatedTo, int max_depth=1000000); |
147 | void GetAssociatedDescendants(JEventLoop *loop, vector<const JObject*> &associatedTo, int max_depth=1000000); |
148 | |
149 | template<typename T,typename S> void CopyToVector(T itbegin, T itend, vector<const S*> &v) const; |
150 | |
151 | |
152 | virtual void toStrings(vector<pair<string,string> > &items)const; |
153 | template<typename T> void AddString(vector<pair<string,string> > &items, const char *name, const char *format, const T &val) const; |
154 | |
155 | |
156 | void AddLog(string &message) const {messagelog.push_back(message);} |
157 | void AddLog(vector<string> &messages) const {messagelog.insert(messagelog.end(), messages.begin(), messages.end());} |
158 | void GetLog(vector<string> &messagelog) const {messagelog = this->messagelog;} |
159 | |
160 | |
161 | bool GetAppendTypes(void) const {return append_types;} |
162 | void SetAppendTypes(bool append_types){this->append_types=append_types;} |
163 | void SetFactoryPointer(JFactory_base *factory){this->factory=factory;} |
164 | JFactory_base * GetFactoryPointer(void) const {return factory;} |
165 | string GetName(void) const {return string(className());} |
166 | string GetTag(void) const ; |
167 | string GetNameTag(void) const {return GetName() + (GetTag()=="" ? "":":") + GetTag();} |
168 | |
169 | oid_t id; |
170 | |
171 | private: |
172 | |
173 | bool append_types; |
174 | set<const JObject*> associated; |
175 | |
176 | vector<JObject*> auto_delete; |
177 | mutable vector<string> messagelog; |
178 | JFactory_base *factory; |
179 | |
180 | }; |
181 | |
182 | #if !defined(__CINT__) && !defined(__CLING__) |
183 | |
184 | |
185 | |
186 | |
187 | |
188 | void JObject::AddAssociatedObject(const JObject *obj) |
189 | { |
190 | |
191 | |
192 | assert(obj!=NULL); |
193 | |
194 | associated.insert(obj); |
195 | |
196 | } |
197 | |
198 | |
199 | |
200 | |
201 | void JObject::AddAssociatedObjectAutoDelete(JObject *obj, bool auto_delete) |
202 | { |
203 | |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | |
213 | AddAssociatedObject(obj); |
214 | |
215 | if(auto_delete)this->auto_delete.push_back(obj); |
216 | } |
217 | |
218 | |
219 | |
220 | |
221 | void JObject::RemoveAssociatedObject(const JObject *obj) |
222 | { |
223 | |
224 | |
225 | |
226 | |
227 | |
228 | |
229 | auto iter = associated.find(obj); |
230 | |
231 | if(iter!=associated.end()){ |
232 | associated.erase(iter); |
233 | } |
234 | } |
235 | |
236 | |
237 | |
238 | |
239 | void JObject::ClearAssociatedObjects(void) |
240 | { |
241 | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | associated.clear(); |
248 | |
249 | |
250 | for(unsigned int i=0; i<auto_delete.size(); i++)delete auto_delete[i]; |
251 | auto_delete.clear(); |
252 | } |
253 | |
254 | |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | template<typename T,typename S> |
263 | void JObject::CopyToVector(T itbegin, T itend, vector<const S*> &v) const |
264 | { |
265 | for(T it=itbegin; it!=itend; it++) v.push_back(*it); |
266 | } |
267 | |
268 | |
269 | |
270 | |
271 | template<typename T> |
272 | void JObject::Get(vector<const T*> &ptrs, string classname, int max_depth) const |
273 | { |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | |
283 | |
284 | |
285 | |
286 | if(classname=="")classname=T::static_className(); |
287 | |
288 | |
289 | |
290 | |
291 | |
292 | set<const JObject*> already_checked; |
293 | set<const T*> objs_found; |
294 | int my_max_depth = max_depth; |
295 | GetAssociatedAncestors(already_checked, my_max_depth, objs_found, classname); |
296 | |
297 | |
298 | ptrs.clear(); |
299 | CopyToVector(objs_found.begin(), objs_found.end(), ptrs); |
300 | |
301 | |
302 | |
303 | |
304 | } |
305 | |
306 | |
307 | |
308 | |
309 | template<typename T> |
310 | void JObject::GetAssociatedAncestors(set<const JObject*> &already_checked, int &max_depth, set<const T*> &objs_found, string classname) const |
311 | { |
312 | |
313 | |
314 | |
315 | |
316 | |
317 | |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | |
326 | |
327 | |
328 | |
329 | |
330 | if(already_checked.find(this) == already_checked.end()) already_checked.insert(this); |
331 | |
332 | if(classname=="")classname=T::static_className(); |
333 | max_depth--; |
334 | |
335 | |
336 | for( auto obj : associated ){ |
337 | |
338 | |
339 | if( classname == obj->className() ){ |
340 | objs_found.insert( dynamic_cast<const T*>(obj) ); |
341 | } |
342 | |
343 | |
344 | if(max_depth<=0) continue; |
345 | if(already_checked.find(obj) != already_checked.end()) continue; |
346 | already_checked.insert(obj); |
347 | obj->GetAssociatedAncestors(already_checked, max_depth, objs_found, classname); |
348 | } |
349 | |
350 | max_depth++; |
351 | } |
352 | |
353 | |
354 | |
355 | |
356 | template<typename T> |
357 | void GetAssociatedDescendants(JEventLoop *loop, vector<const T*> &associatedTo, int max_depth=1000000) |
358 | { |
359 | |
360 | |
361 | |
362 | |
363 | |
364 | |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | |
377 | vector<const JObject*> ajobjs; |
378 | GetAssociatedDescendants(loop, ajobjs, max_depth); |
379 | for(uint32_t i=0; i<ajobjs.size(); i++){ |
380 | const T *ptr = dynamic_cast<const T*>(ajobjs[i]); |
381 | if(ptr != NULL) associatedTo.push_back(ptr); |
382 | } |
383 | } |
384 | |
385 | |
386 | |
387 | |
388 | template<typename T> |
389 | void JObject::GetT(vector<const T*> &ptrs) const |
390 | { |
391 | |
392 | |
393 | |
394 | |
395 | |
396 | |
397 | |
398 | |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | |
405 | ptrs.clear(); |
406 | |
407 | |
408 | |
409 | for( auto obj : associated ){ |
410 | const T *ptr = dynamic_cast<const T*>(obj); |
411 | if(ptr != NULL)ptrs.push_back(ptr); |
412 | } |
413 | } |
414 | |
415 | |
416 | |
417 | |
418 | template<class T> |
419 | void JObject::GetSingle(const T* &t, string classname) const |
420 | { |
421 | |
422 | |
423 | |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | |
430 | |
431 | |
432 | t = NULL; |
433 | |
434 | if(classname=="")classname=T::static_className(); |
| |
435 | |
436 | |
437 | |
438 | for( auto obj : associated ){ |
439 | if( classname == obj->className() ){ |
| 7 | | Value assigned to field 'NPED' | |
|
| |
440 | t = dynamic_cast<const T*>(obj); |
441 | if(t!=NULL)return; |
| |
442 | } |
443 | } |
444 | } |
445 | |
446 | |
447 | |
448 | |
449 | template<class T> |
450 | void JObject::GetSingleT(const T* &t) const |
451 | { |
452 | |
453 | |
454 | |
455 | |
456 | |
457 | |
458 | |
459 | |
460 | |
461 | |
462 | |
463 | |
464 | |
465 | |
466 | |
467 | t = NULL; |
468 | |
469 | |
470 | |
471 | for( auto obj : associated ){ |
472 | t = dynamic_cast<const T*>(obj); |
473 | if(t!=NULL)return; |
474 | } |
475 | } |
476 | |
477 | |
478 | |
479 | |
480 | inline void JObject::toStrings(vector<pair<string,string> > &items) const |
481 | { |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | |
488 | |
489 | |
490 | |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | AddString(items, "JObject", "0x%08x", (unsigned long)this); |
497 | } |
498 | |
499 | |
500 | |
501 | |
502 | template<typename T> |
503 | void JObject::AddString(vector<pair<string,string> > &items, const char *name, const char *format, const T &val) const |
504 | { |
505 | |
506 | |
507 | |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | |
517 | |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | |
524 | |
525 | |
526 | |
527 | |
528 | |
529 | |
530 | char str[256]; |
531 | sprintf(str, format, val); |
532 | |
533 | stringstream ss; |
534 | ss<<name; |
535 | if(append_types){ |
536 | if(typeid(T)==typeid(int)){ |
537 | ss<<":int:"<<val; |
538 | }else if(typeid(T)==typeid(int32_t)){ |
539 | ss<<":int:"<<val; |
540 | }else if(typeid(T)==typeid(unsigned int)){ |
541 | ss<<":uint:"<<val; |
542 | }else if(typeid(T)==typeid(uint32_t)){ |
543 | ss<<":uint:"<<val; |
544 | }else if(typeid(T)==typeid(long)){ |
545 | ss<<":long:"<<val; |
546 | }else if(typeid(T)==typeid(int64_t)){ |
547 | ss<<":long:"<<val; |
548 | }else if(typeid(T)==typeid(unsigned long)){ |
549 | ss<<":ulong:"<<val; |
550 | }else if(typeid(T)==typeid(uint64_t)){ |
551 | ss<<":ulong:"<<val; |
552 | }else if(typeid(T)==typeid(short)){ |
553 | ss<<":short:"<<val; |
554 | }else if(typeid(T)==typeid(int16_t)){ |
555 | ss<<":short:"<<val; |
556 | }else if(typeid(T)==typeid(unsigned short)){ |
557 | ss<<":ushort:"<<val; |
558 | }else if(typeid(T)==typeid(uint16_t)){ |
559 | ss<<":ushort:"<<val; |
560 | }else if(typeid(T)==typeid(float)){ |
561 | ss<<":float:"<<val; |
562 | }else if(typeid(T)==typeid(double)){ |
563 | ss<<":double:"<<val; |
564 | }else if(typeid(T)==typeid(string)){ |
565 | ss<<":string:"<<val; |
566 | }else if(typeid(T)==typeid(const char*)){ |
567 | ss<<":string:"<<val; |
568 | }else if(typeid(T)==typeid(char*)){ |
569 | ss<<":string:"<<val; |
570 | }else{ |
571 | ss<<":unknown:"<<str; |
572 | } |
573 | } |
574 | |
575 | pair<string, string> item; |
576 | item.first = ss.str(); |
577 | item.second = string(str); |
578 | items.push_back(item); |
579 | } |
580 | |
581 | #endif // __CINT__ __CLING__ |
582 | |
583 | |
584 | } |
585 | |
586 | #endif // _JObject_ |
587 | |