1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | #define USE_JANA1 1 |
9 | |
10 | #include <iostream> |
11 | #include <iomanip> |
12 | using namespace std; |
13 | |
14 | #include <TF1.h> |
15 | #include <TFile.h> |
16 | #include <TH2.h> |
17 | #include <TH1.h> |
18 | |
19 | #include <signal.h> |
20 | #include <time.h> |
21 | |
22 | #if USE_JANA1 |
23 | #include <DANA/DApplication.h> |
24 | #include "MyProcessor.h" |
25 | #endif |
26 | |
27 | #include "units.h" |
28 | #include "HDDM/hddm_s.h" |
29 | |
30 | void Smear(s_HDDM_t *hddm_s); |
31 | void ParseCommandLineArguments(int narg, char* argv[]); |
32 | void Usage(void); |
33 | |
34 | #if ! USE_JANA1 |
35 | void ctrlCHandleMCSmear(int x); |
36 | #endif |
37 | |
38 | char *INFILENAME = NULL__null; |
39 | char *OUTFILENAME = NULL__null; |
40 | int QUIT = 0; |
41 | |
42 | bool ADD_NOISE = false; |
43 | bool SMEAR_HITS = true; |
44 | bool SMEAR_BCAL = true; |
45 | bool FDC_ELOSS_OFF = false; |
46 | bool IGNORE_SEEDS = false; |
47 | |
48 | |
49 | double BCAL_DARKRATE_GHZ = 0.; |
50 | double BCAL_SIGMA_SIG_RELATIVE = 0.; |
51 | double BCAL_SIGMA_PED_RELATIVE = 0.; |
52 | double BCAL_SIPM_GAIN_VARIATION = 0.; |
53 | double BCAL_XTALK_FRACT = 0.; |
54 | double BCAL_INTWINDOW_NS = 0.; |
55 | double BCAL_DEVICEPDE = 0.; |
56 | double BCAL_SAMPLING_FRACT = 0.; |
57 | double BCAL_PHOTONSPERSIDEPERMEV_INFIBER = 0.0; |
58 | double BCAL_AVG_DARK_DIGI_VALS_PER_EVENT = 0.0; |
59 | double BCAL_SAMPLINGCOEFA = 0.0; |
60 | double BCAL_SAMPLINGCOEFB = 0.0; |
61 | double BCAL_TIMEDIFFCOEFA = 0.0; |
62 | double BCAL_TIMEDIFFCOEFB = 0.0; |
63 | |
64 | double BCAL_TDC_THRESHOLD = 44.7; |
65 | |
66 | |
67 | bool NO_E_SMEAR = false; |
68 | bool NO_T_SMEAR = false; |
69 | bool NO_DARK_PULSES = false; |
70 | bool NO_SAMPLING_FLUCTUATIONS = false; |
71 | bool NO_SAMPLING_FLOOR_TERM = false; |
72 | bool NO_POISSON_STATISTICS = false; |
73 | bool NO_TIME_JITTER = false; |
74 | bool NO_THRESHOLD_CUT = false; |
75 | bool BCAL_DEBUG_HISTS = false; |
76 | |
77 | |
78 | double FCAL_PHOT_STAT_COEF = 0.0; |
79 | double FCAL_BLOCK_THRESHOLD = 0.0; |
80 | |
81 | double CDC_TDRIFT_SIGMA = 0.0; |
82 | double CDC_TIME_WINDOW = 0.0; |
83 | double CDC_PEDESTAL_SIGMA = 0.0; |
84 | |
85 | double FDC_TDRIFT_SIGMA = 0.0; |
86 | double FDC_CATHODE_SIGMA = 0.0; |
87 | double FDC_PED_NOISE = 0.0; |
88 | double FDC_HIT_DROP_FRACTION = 0.0; |
89 | double FDC_TIME_WINDOW = 0.0; |
90 | |
91 | double START_SIGMA = 0.0; |
92 | double START_PHOTONS_PERMEV = 0.0; |
93 | |
94 | |
95 | double TOF_SIGMA = 100.*k_psec; |
96 | double TOF_PHOTONS_PERMEV = 400.; |
97 | |
98 | #include <JANA/JCalibrationFile.h> |
99 | using namespace jana; |
100 | static JCalibration *jcalib=NULL__null; |
101 | |
102 | |
103 | pthread_mutex_t root_mutex = PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, 0, 0, { 0, 0 } } }; |
104 | TH2F *fdc_drift_time_smear_hist; |
105 | TH2F *fdc_drift_dist_smear_hist; |
106 | TH2F *fdc_drift_time; |
107 | TH2F *cdc_drift_time; |
108 | TH1F *fdc_anode_mult; |
109 | TH2F *cdc_drift_smear; |
110 | |
111 | |
112 | |
113 | |
114 | int main(int narg,char* argv[]) |
115 | { |
116 | #if ! USE_JANA1 |
117 | |
118 | signal(SIGINT2,ctrlCHandleMCSmear); |
119 | #endif |
120 | ParseCommandLineArguments(narg, argv); |
121 | |
122 | |
123 | TFile *hfile = new TFile("smear.root","RECREATE","smearing histograms"); |
124 | fdc_drift_time_smear_hist=new TH2F("fdc_drift_time_smear_hist","Drift time smearing for FDC", |
125 | 300,0.0,0.6,400,-200,200); |
126 | fdc_drift_dist_smear_hist=new TH2F("fdc_drift_dist_smear_hist","Drift distance smearing for FDC", |
127 | 100,0.0,0.6,400,-0.5,0.5); |
128 | fdc_drift_time=new TH2F("fdc_drift_time","FDC drift distance vs. time",100,-20,380,100,0,1.); |
129 | |
130 | fdc_anode_mult = new TH1F("fdc_anode_mult","wire hit multiplicity",20,-0.5,19.5); |
131 | |
132 | |
133 | cdc_drift_time = new TH2F("cdc_drift_time","CDC drift distance vs time",100,-20,800,100,0.,0.8); |
134 | |
135 | cdc_drift_smear = new TH2F("cdc_drift_smear","CDC drift smearing", |
136 | 100,0.0,0.8,100,-0.8,0.8); |
137 | |
138 | |
139 | |
140 | const char *url = getenv("JANA_CALIB_URL"); |
141 | if(!url){ |
142 | _DBG_std::cerr<<"mcsmear.cc"<<":"<<142<<" "<<"JANA_CALIB_URL environment not set."<<endl; |
143 | exit(-1); |
144 | } |
145 | jcalib = new JCalibrationFile(url, 1, ""); |
146 | |
147 | if(!jcalib){ |
148 | _DBG_std::cerr<<"mcsmear.cc"<<":"<<148<<" "<<"ERROR - jcalib not set!"<<endl; |
149 | _DBG_std::cerr<<"mcsmear.cc"<<":"<<149<<" "<<"ERROR - Exiting ..."<<endl; |
150 | return 0; |
151 | } |
152 | |
153 | |
154 | { |
155 | cout<<"get TOF/tof_parms parameters from calibDB"<<endl; |
156 | map<string, double> tofparms; |
157 | jcalib->Get("TOF/tof_parms", tofparms); |
158 | TOF_SIGMA = tofparms["TOF_SIGMA"]; |
159 | TOF_PHOTONS_PERMEV = tofparms["TOF_PHOTONS_PERMEV"]; |
160 | } |
161 | |
162 | |
163 | { |
164 | cout<<"get BCAL/bcal_parms parameters from calibDB"<<endl; |
165 | map<string, double> bcalparms; |
166 | jcalib->Get("BCAL/bcal_parms", bcalparms); |
167 | BCAL_DARKRATE_GHZ = bcalparms["BCAL_DARKRATE_GHZ"]; |
168 | BCAL_SIGMA_SIG_RELATIVE = bcalparms["BCAL_SIGMA_SIG_RELATIVE"]; |
169 | BCAL_SIGMA_PED_RELATIVE = bcalparms["BCAL_SIGMA_PED_RELATIVE"]; |
170 | BCAL_SIPM_GAIN_VARIATION = bcalparms["BCAL_SIPM_GAIN_VARIATION"]; |
171 | BCAL_XTALK_FRACT = bcalparms["BCAL_XTALK_FRACT"]; |
172 | BCAL_INTWINDOW_NS = bcalparms["BCAL_INTWINDOW_NS"]; |
173 | BCAL_DEVICEPDE = bcalparms["BCAL_DEVICEPDE"]; |
174 | BCAL_SAMPLING_FRACT = bcalparms["BCAL_SAMPLING_FRACT"]; |
175 | BCAL_AVG_DARK_DIGI_VALS_PER_EVENT = bcalparms["BCAL_AVG_DARK_DIGI_VALS_PER_EVENT"]; |
176 | BCAL_PHOTONSPERSIDEPERMEV_INFIBER = bcalparms["BCAL_PHOTONSPERSIDEPERMEV_INFIBER"]; |
177 | BCAL_SAMPLINGCOEFA = bcalparms["BCAL_SAMPLINGCOEFA"]; |
178 | BCAL_SAMPLINGCOEFB = bcalparms["BCAL_SAMPLINGCOEFB"]; |
179 | BCAL_TIMEDIFFCOEFA = bcalparms["BCAL_TIMEDIFFCOEFA"]; |
180 | BCAL_TIMEDIFFCOEFB = bcalparms["BCAL_TIMEDIFFCOEFB"]; |
181 | } |
182 | |
183 | { |
184 | cout<<"get FCAL/fcal_parms parameters from calibDB"<<endl; |
185 | map<string, double> fcalparms; |
186 | jcalib->Get("FCAL/fcal_parms", fcalparms); |
187 | if (FCAL_PHOT_STAT_COEF == 0.0) |
188 | FCAL_PHOT_STAT_COEF = fcalparms["FCAL_PHOT_STAT_COEF"]; |
189 | if (FCAL_BLOCK_THRESHOLD == 0.0) |
190 | FCAL_BLOCK_THRESHOLD = fcalparms["FCAL_BLOCK_THRESHOLD"]; |
191 | } |
192 | { |
193 | cout<<"get CDC/cdc_parms parameters from calibDB"<<endl; |
194 | map<string, double> cdcparms; |
195 | jcalib->Get("CDC/cdc_parms", cdcparms); |
196 | if (CDC_TDRIFT_SIGMA == 0.0) |
197 | CDC_TDRIFT_SIGMA = cdcparms["CDC_TDRIFT_SIGMA"]; |
198 | if (CDC_TIME_WINDOW == 0.0) |
199 | CDC_TIME_WINDOW = cdcparms["CDC_TIME_WINDOW"]; |
200 | if (CDC_PEDESTAL_SIGMA == 0.0) |
201 | CDC_PEDESTAL_SIGMA = cdcparms["CDC_PEDESTAL_SIGMA"]; |
202 | } |
203 | |
204 | { |
205 | cout<<"get FDC/fdc_parms parameters from calibDB"<<endl; |
206 | map<string, double> fdcparms; |
207 | jcalib->Get("FDC/fdc_parms", fdcparms); |
208 | |
209 | if (FDC_TDRIFT_SIGMA == 0.0) |
210 | FDC_TDRIFT_SIGMA = fdcparms["FDC_TDRIFT_SIGMA"]; |
211 | if (FDC_CATHODE_SIGMA ==0.0) |
212 | FDC_CATHODE_SIGMA = fdcparms["FDC_CATHODE_SIGMA"]; |
213 | |
214 | FDC_PED_NOISE = fdcparms["FDC_PED_NOISE"]; |
215 | |
216 | if (FDC_TIME_WINDOW == 0.0) |
217 | FDC_TIME_WINDOW = fdcparms["FDC_TIME_WINDOW"]; |
218 | |
219 | if (FDC_HIT_DROP_FRACTION == 0.0) |
220 | FDC_HIT_DROP_FRACTION = fdcparms["FDC_HIT_DROP_FRACTION"]; |
221 | } |
222 | |
223 | { |
224 | cout<<"get START_COUNTER/start_parms parameters from calibDB"<<endl; |
225 | map<string, double> startparms; |
226 | jcalib->Get("START_COUNTER/start_parms", startparms); |
227 | |
228 | START_SIGMA = startparms["START_SIGMA"] ; |
229 | START_PHOTONS_PERMEV = startparms["START_PHOTONS_PERMEV"]; |
230 | |
231 | } |
232 | |
233 | |
234 | #if ! USE_JANA1 |
235 | cout<<" input file: "<<INFILENAME<<endl; |
236 | cout<<" output file: "<<OUTFILENAME<<endl; |
237 | |
238 | |
239 | s_iostream_t *fin = open_s_HDDM(INFILENAME); |
240 | if(!fin){ |
241 | cout<<" Error opening input file \""<<INFILENAME<<"\"!"<<endl; |
242 | exit(-1); |
243 | } |
244 | |
245 | |
246 | s_iostream_t *fout = init_s_HDDM(OUTFILENAME); |
247 | if(!fout){ |
248 | cout<<" Error opening output file \""<<OUTFILENAME<<"\"!"<<endl; |
249 | exit(-1); |
250 | } |
251 | |
252 | |
253 | s_HDDM_t *hddm_s; |
254 | int NEvents = 0; |
255 | time_t last_time = time(NULL__null); |
256 | while((hddm_s = read_s_HDDM(fin))){ |
257 | NEvents++; |
258 | time_t now = time(NULL__null); |
259 | if(now != last_time){ |
260 | cout<<" "<<NEvents<<" events processed \r";cout.flush(); |
261 | last_time = now; |
262 | } |
263 | |
264 | |
265 | Smear(hddm_s); |
266 | |
267 | |
268 | flush_s_HDDM(hddm_s, fout); |
269 | |
270 | if(QUIT)break; |
271 | } |
272 | cout<<endl; |
273 | |
274 | |
275 | close_s_HDDM(fin); |
276 | close_s_HDDM(fout); |
277 | |
278 | cout<<" "<<NEvents<<" events read"<<endl; |
279 | #else |
280 | DApplication dapp(narg, argv); |
281 | |
282 | MyProcessor myproc; |
283 | |
284 | dapp.Run(&myproc); |
285 | |
286 | #endif |
287 | |
288 | hfile->Write(); |
289 | hfile->Close(); |
290 | |
291 | return 0; |
292 | } |
293 | |
294 | |
295 | |
296 | |
297 | void ParseCommandLineArguments(int narg, char* argv[]) |
298 | { |
299 | bool warn_obsolete = false; |
300 | |
301 | for(int i=1; i<narg; i++){ |
| 1 | Assuming 'i' is >= 'narg' | |
|
| 2 | | Loop condition is false. Execution continues on line 341 | |
|
302 | char *ptr = argv[i]; |
303 | |
304 | if(ptr[0] == '-'){ |
305 | switch(ptr[1]){ |
306 | case 'h': Usage(); break; |
307 | case 'o': OUTFILENAME = strdup(&ptr[2]); break; |
308 | case 'n': warn_obsolete=true; break; |
309 | case 'N': ADD_NOISE=true; break; |
310 | case 's': SMEAR_HITS=false; break; |
311 | case 'i': IGNORE_SEEDS=true; break; |
312 | case 'u': CDC_TDRIFT_SIGMA=atof(&ptr[2])*1.0E-9; break; |
313 | case 't': CDC_TIME_WINDOW=atof(&ptr[2])*1.0E-9; break; |
314 | case 'U': FDC_TDRIFT_SIGMA=atof(&ptr[2])*1.0E-9; break; |
315 | case 'C': FDC_CATHODE_SIGMA=atof(&ptr[2])*1.0E-6; break; |
316 | case 'T': FDC_TIME_WINDOW=atof(&ptr[2])*1.0E-9; break; |
317 | case 'e': FDC_ELOSS_OFF = true; break; |
318 | case 'E': CDC_PEDESTAL_SIGMA = atof(&ptr[2])*k_keV; break; |
319 | case 'd': FDC_HIT_DROP_FRACTION=atof(&ptr[2]); break; |
320 | case 'p': FCAL_PHOT_STAT_COEF = atof(&ptr[2]); break; |
321 | case 'b': FCAL_BLOCK_THRESHOLD = atof(&ptr[2])*k_MeV; break; |
322 | case 'B': SMEAR_BCAL = false; break; |
323 | case 'R': BCAL_TDC_THRESHOLD = atof(&ptr[2]); break; |
324 | case 'F': NO_E_SMEAR = true; break; |
325 | case 'G': NO_T_SMEAR = true; break; |
326 | case 'H': NO_DARK_PULSES = true; break; |
327 | case 'K': NO_SAMPLING_FLUCTUATIONS = true; break; |
328 | case 'L': NO_SAMPLING_FLOOR_TERM = true; break; |
329 | case 'M': NO_POISSON_STATISTICS = true; break; |
330 | case 'Q': NO_TIME_JITTER = true; break; |
331 | case 'I': NO_THRESHOLD_CUT = true; break; |
332 | case 'J': BCAL_DEBUG_HISTS = true; break; |
333 | case 'f': TOF_SIGMA= atof(&ptr[2])*k_psec; break; |
334 | case 'S': START_SIGMA= atof(&ptr[2])*k_psec; break; |
335 | } |
336 | }else{ |
337 | INFILENAME = argv[i]; |
338 | } |
339 | } |
340 | |
341 | if(!INFILENAME){ |
| |
342 | cout<<endl<<"You must enter a filename!"<<endl<<endl; |
343 | Usage(); |
344 | } |
345 | |
346 | if(warn_obsolete){ |
| |
347 | cout<<endl; |
348 | cout<<"WARNING: Use of the \"-n\" option is obsolete. Random noise"<<endl; |
349 | cout<<" hits are disabled by default now. To turn them back"<<endl; |
350 | cout<<" on use the \"-N\" option."<<endl; |
351 | cout<<endl; |
352 | } |
353 | |
354 | |
355 | if(OUTFILENAME == NULL__null){ |
| |
356 | char *ptr, *path_stripped; |
357 | path_stripped = ptr = strdup(INFILENAME); |
| |
358 | while((ptr = strstr(ptr, "/")))path_stripped = ++ptr; |
| 7 | | Loop condition is false. Execution continues on line 359 | |
|
359 | ptr = strstr(path_stripped, ".hddm"); |
360 | if(ptr)*ptr=0; |
| |
361 | char str[256]; |
362 | sprintf(str, "%s_%ssmeared.hddm", path_stripped, ADD_NOISE ? "n":""); |
| 9 | | Assuming 'ADD_NOISE' is 0 | |
|
| |
363 | OUTFILENAME = strdup(str); |
| 11 | | Memory is never released; potential leak of memory pointed to by 'path_stripped' |
|
364 | } |
365 | |
366 | cout<<"BCAL values will "<< (SMEAR_BCAL ? "":"not") <<" be smeared"<<endl; |
367 | cout<<"BCAL values will "<< (SMEAR_BCAL ? "":"not") <<" be added"<<endl; |
368 | } |
369 | |
370 | |
371 | |
372 | |
373 | |
374 | void Usage(void) |
375 | { |
376 | cout<<endl<<"Usage:"<<endl; |
377 | cout<<" mcsmear [options] file.hddm"<<endl; |
378 | cout<<endl; |
379 | cout<<" Read the given, Geant-produced HDDM file as input and smear"<<endl; |
380 | cout<<"the truth values for \"hit\" data before writing out to a"<<endl; |
381 | cout<<"separate file. The truth values for the thrown particles are"<<endl; |
382 | cout<<"not changed. Noise hits can also be added using the -n option."<<endl; |
383 | cout<<"Note that all smearing is done using Gaussians, with the "<<endl; |
384 | cout<<"sigmas configurable with the options below."<<endl; |
385 | cout<<endl; |
386 | cout<<" options:"<<endl; |
387 | cout<<" -ofname Write output to a file named \"fname\" (default auto-generate name)"<<endl; |
388 | cout<<" -N Add random background hits to CDC and FDC (default is not to add)"<<endl; |
389 | cout<<" -s Don't smear real hits (see -B for BCAL, default is to smear)"<<endl; |
390 | cout<<" -i Ignore random number seeds found in input HDDM file"<<endl; |
391 | cout<<" -u# Sigma CDC anode drift time in ns (def:"<<CDC_TDRIFT_SIGMA*1.0E9<<"ns)"<<endl; |
392 | cout<<" (NOTE: this is only used if -y is also specified!)"<<endl; |
393 | cout<<" -y Do NOT apply drift distance dependence error to"<<endl; |
394 | cout<<" CDC (default is to apply)"<<endl; |
395 | cout<<" -Y Apply constant sigma smearing for FDC drift time. " <<endl; |
396 | cout<<" Default is to use a drift-distance dependent parameterization." <<endl; |
397 | cout<<" -t# CDC time window for background hits in ns (def:"<<CDC_TIME_WINDOW*1.0E9<<"ns)"<<endl; |
398 | cout<<" -U# Sigma FDC anode drift time in ns (def:"<<FDC_TDRIFT_SIGMA*1.0E9<<"ns)"<<endl; |
399 | cout<<" -C# Sigma FDC cathode strips in microns (def:"<<FDC_TDRIFT_SIGMA<<"ns)"<<endl; |
400 | cout<<" -t# FDC time window for background hits in ns (def:"<<FDC_TIME_WINDOW*1.0E9<<"ns)"<<endl; |
401 | cout<<" -e hdgeant was run with LOSS=0 so scale the FDC cathode"<<endl; |
402 | cout<<" pedestal noise (def:false)"<<endl; |
403 | cout<<" -d# Randomly drop this fraction of FDC hits (0=drop none 1=drop all)"<<endl; |
404 | cout<<" default is to drop none."<<endl; |
405 | cout<<" -p# FCAL photo-statistics smearing factor in GeV^3/2 (def:"<<FCAL_PHOT_STAT_COEF<<")"<<endl; |
406 | cout<<" -b# FCAL single block threshold in MeV (def:"<<FCAL_BLOCK_THRESHOLD/k_MeV<<")"<<endl; |
407 | cout<<" -B Don't process BCAL hits at all (def. process)"<<endl; |
408 | cout<<" -Rthresh BCAL TDC threshold (def. "<<BCAL_TDC_THRESHOLD<<")"<<endl; |
409 | cout<<" -F Don't smear BCAL energy (def. smear)"<<endl; |
410 | cout<<" -G Don't smear BCAL times (def. smear)"<<endl; |
411 | cout<<" -H Don't add BCAL dark hits (def. add)"<<endl; |
412 | cout<<" -K Don't apply BCAL sampling fluctuations (def. apply)"<<endl; |
413 | cout<<" -L Don't apply BCAL sampling floor term (def. apply)"<<endl; |
414 | cout<<" -M Don't apply BCAL Poisson statistics (def. apply)"<<endl; |
415 | cout<<" -Q Don't apply BCAL time jitter (def. apply)"<<endl; |
416 | cout<<" -I Don't apply discrim. thresh. to BCAL hits (def. cut)"<<endl; |
417 | cout<<" -J Create BCAL debug hists (only use with 1 event!)"<<endl; |
418 | cout<<" -f# TOF sigma in psec (def: "<< TOF_SIGMA/k_psec<<")"<<endl; |
419 | cout<<" -h Print this usage statement."<<endl; |
420 | cout<<endl; |
421 | cout<<" Example:"<<endl; |
422 | cout<<endl; |
423 | cout<<" mcsmear -u3.5 -t500 hdgeant.hddm"<<endl; |
424 | cout<<endl; |
425 | cout<<" This will produce a file named hdgeant_nsmeared.hddm that"<<endl; |
426 | cout<<" includes the hit information from the input file hdgeant.hddm"<<endl; |
427 | cout<<" but with the FDC and CDC hits smeared out. The CDC hits will"<<endl; |
428 | cout<<" have their drift times smeared via a gaussian with a 3.5ns width"<<endl; |
429 | cout<<" while the FDC will be smeared using the default values."<<endl; |
430 | cout<<" In addition, background hits will be added, the exact number of"<<endl; |
431 | cout<<" of which are determined by the time windows specified for the"<<endl; |
432 | cout<<" CDC and FDC. In this examplem the CDC time window was explicitly"<<endl; |
433 | cout<<" set to 500 ns."<<endl; |
434 | cout<<endl; |
435 | |
436 | exit(0); |
437 | } |
438 | |
439 | #if ! USE_JANA1 |
440 | |
441 | |
442 | |
443 | void ctrlCHandleMCSmear(int x) |
444 | { |
445 | QUIT++; |
446 | cerr<<endl<<"SIGINT received ("<<QUIT<<")....."<<endl; |
447 | } |
448 | #endif |
449 | |