Bug Summary

File:programs/Simulation/mcsmear/mcsmear.cc
Location:line 363, column 17
Description:Memory is never released; potential leak of memory pointed to by 'path_stripped'

Annotated Source Code

1// $Id: mcsmear.cc 9485 2012-08-14 13:26:04Z davidl $
2//
3// Created June 22, 2005 David Lawrence
4
5
6// The following flag can be used to switch from the classic mode where
7// the event loop is implemented in main() to a JANA based event-loop.
8#define USE_JANA1 1
9
10#include <iostream>
11#include <iomanip>
12using 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
30void Smear(s_HDDM_t *hddm_s);
31void ParseCommandLineArguments(int narg, char* argv[]);
32void Usage(void);
33
34#if ! USE_JANA1
35void ctrlCHandleMCSmear(int x);
36#endif
37
38char *INFILENAME = NULL__null;
39char *OUTFILENAME = NULL__null;
40int QUIT = 0;
41
42bool ADD_NOISE = false;
43bool SMEAR_HITS = true;
44bool SMEAR_BCAL = true;
45bool FDC_ELOSS_OFF = false;
46bool IGNORE_SEEDS = false;
47
48// setup response parameters
49double BCAL_DARKRATE_GHZ = 0.;// 0.0176 (from calibDB BCAL/bcal_parms) for 4x4 array
50double BCAL_SIGMA_SIG_RELATIVE = 0.;// 0.105 (from calibDB BCAL/bcal_parms)
51double BCAL_SIGMA_PED_RELATIVE = 0.;// 0.139 (from calibDB BCAL/bcal_parms)
52double BCAL_SIPM_GAIN_VARIATION = 0.;// 0.04 (from calibDB BCAL/bcal_parms)
53double BCAL_XTALK_FRACT = 0.;// 0.157 (from calibDB BCAL/bcal_parms)
54double BCAL_INTWINDOW_NS = 0.;// 100 (from calibDB BCAL/bcal_parms)
55double BCAL_DEVICEPDE = 0.;// 0.21 (from calibDB BCAL/bcal_parms)
56double BCAL_SAMPLING_FRACT = 0.;// 0.095 (from calibDB BCAL/bcal_parms)
57double BCAL_PHOTONSPERSIDEPERMEV_INFIBER = 0.0;// 75 (from calibDB BCAL/bcal_parms)
58double BCAL_AVG_DARK_DIGI_VALS_PER_EVENT = 0.0; // 240 used to set thresholds
59double BCAL_SAMPLINGCOEFA = 0.0; // 0.042 (from calibDB BCAL/bcal_parms)
60double BCAL_SAMPLINGCOEFB = 0.0; // 0.013 (from calibDB BCAL/bcal_parms)
61double BCAL_TIMEDIFFCOEFA = 0.0; // 0.07 * sqrt( 2 ) (from calibDB BCAL/bcal_parms)
62double BCAL_TIMEDIFFCOEFB = 0.0; // 0.00 * sqrt( 2 ) (from calibDB BCAL/bcal_parms)
63
64double BCAL_TDC_THRESHOLD = 44.7; // mV
65
66// BCAL flags
67bool NO_E_SMEAR = false;
68bool NO_T_SMEAR = false;
69bool NO_DARK_PULSES = false;
70bool NO_SAMPLING_FLUCTUATIONS = false;
71bool NO_SAMPLING_FLOOR_TERM = false;
72bool NO_POISSON_STATISTICS = false;
73bool NO_TIME_JITTER = false;
74bool NO_THRESHOLD_CUT = false;
75bool BCAL_DEBUG_HISTS = false;
76
77
78double FCAL_PHOT_STAT_COEF = 0.0; //0.035;
79double FCAL_BLOCK_THRESHOLD = 0.0; //20.0*k_MeV;
80
81double CDC_TDRIFT_SIGMA = 0.0; // 150.0/55.0*1E-9 seconds
82double CDC_TIME_WINDOW = 0.0; // 1000.0E-9 seconds
83double CDC_PEDESTAL_SIGMA = 0.0; // 0.06*k_keV
84
85double FDC_TDRIFT_SIGMA = 0.0; // 200.0/55.0*1.0E-9 seconds
86double FDC_CATHODE_SIGMA = 0.0; // 150.0 microns
87double FDC_PED_NOISE = 0.0; // in pC calculated in SmearFDC
88double FDC_HIT_DROP_FRACTION = 0.0; // 1000.0E-9
89double FDC_TIME_WINDOW = 0.0; // 1000.0E-9 in seconds
90
91double START_SIGMA = 0.0; // 300ps
92double START_PHOTONS_PERMEV = 0.0; // used to be 8000 should be more like 200
93
94// TOF parameters will be read from data base later
95double TOF_SIGMA = 100.*k_psec;
96double TOF_PHOTONS_PERMEV = 400.;
97
98#include <JANA/JCalibrationFile.h>
99using namespace jana;
100static JCalibration *jcalib=NULL__null;
101
102// histogram
103pthread_mutex_t root_mutex = PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, 0, 0, { 0, 0 } } };
104TH2F *fdc_drift_time_smear_hist;
105TH2F *fdc_drift_dist_smear_hist;
106TH2F *fdc_drift_time;
107TH2F *cdc_drift_time;
108TH1F *fdc_anode_mult;
109TH2F *cdc_drift_smear;
110
111//-----------
112// main
113//-----------
114int main(int narg,char* argv[])
115{
116#if ! USE_JANA1
117 // Set up to catch SIGINTs for graceful exits
118 signal(SIGINT2,ctrlCHandleMCSmear);
119#endif
120 ParseCommandLineArguments(narg, argv);
121
122 // hist file
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 // Create a JCalibration object using the JANA_CALIB_URL environment variable
139 // Right now, we hardwire this to use JCalibrationFile.
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 // Make sure jcalib is set
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 // get the TOF parameters
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 // get the BCAL parameters
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 // Open Input file
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 // Output file
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 // Loop over events in input file
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 // Smear values
265 Smear(hddm_s);
266
267 // Write event to output file
268 flush_s_HDDM(hddm_s, fout);
269
270 if(QUIT)break;
271 }
272 cout<<endl;
273
274 // close input and output files
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// ParseCommandLineArguments
296//-----------
297void 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){
3
Taking false branch
342 cout<<endl<<"You must enter a filename!"<<endl<<endl;
343 Usage();
344 }
345
346 if(warn_obsolete){
4
Taking false branch
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 // Generate output filename based on input filename
355 if(OUTFILENAME == NULL__null){
5
Taking true branch
356 char *ptr, *path_stripped;
357 path_stripped = ptr = strdup(INFILENAME);
6
Memory is allocated
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;
8
Taking false branch
361 char str[256];
362 sprintf(str, "%s_%ssmeared.hddm", path_stripped, ADD_NOISE ? "n":"");
9
Assuming 'ADD_NOISE' is 0
10
'?' condition is false
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// Usage
373//-----------
374void 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// ctrlCHandleMCSmear
442//-----------------------------------------------------------------
443void ctrlCHandleMCSmear(int x)
444{
445 QUIT++;
446 cerr<<endl<<"SIGINT received ("<<QUIT<<")....."<<endl;
447}
448#endif
449