Bug Summary

File:programs/Simulation/genpi/genpi.cc
Location:line 96, column 4
Description:Dereference of null pointer

Annotated Source Code

1
2
3#include <iostream>
4#include <fstream>
5#include <string>
6#include <cmath>
7#include <stdlib.h>
8#include <time.h>
9#include <vector>
10using namespace std;
11
12#include <TVector3.h>
13#include <TLorentzVector.h>
14
15double PI_CHARGED_MASS = 0.139568;
16unsigned int MAX_EVENTS=10000;
17double P_MIN=0.100;
18double P_MAX=6.000;
19double PHI_MIN = 0.0;
20double PHI_MAX = 2.0*M_PI3.14159265358979323846;
21double THETA_MIN = 0.0;
22double THETA_MAX = M_PI3.14159265358979323846;
23bool IS_POSITIVE = true;
24
25int RUN_NUMBER=100;
26string OUTPUT_FILENAME="genpi.ascii";
27
28#define GAMMA_TYPE1 1
29#define PI_PLUS_TYPE8 8
30#define PI_MINUS_TYPE9 9
31
32#define _DBG_cout<<"genpi.cc"<<":"<<32<<" " cout<<__FILE__"genpi.cc"<<":"<<__LINE__32<<" "
33#define _DBG__cout<<"genpi.cc"<<":"<<33<<" "<<
endl
_DBG_cout<<"genpi.cc"<<":"<<33<<" "<<endl
34
35class piX{
36 public:
37 double px,py,pz,E; // piX
38};
39
40
41extern "C"{
42double rtsafe(void (*funcd)(double, double *, double *), double x1, double x2, double xacc);
43double rtnewt(void (*funcd)(double, double *, double *), double x1, double x2, double xacc);
44double zbrent(double (*func)(double), double x1, double x2, double tol);
45void funcd(double, double *f, double *df);
46double func(double);
47}
48
49double SampleSin2Theta(void);
50void ParseCommandLineArguments(int narg, char* argv[]);
51void Usage(void);
52
53
54//----------------------------
55// main
56//----------------------------
57int main(int narg, char* argv[])
58{
59
60 // Parse the command line
61 ParseCommandLineArguments(narg, argv);
62
63 // Open file for output
64 ofstream of(OUTPUT_FILENAME.c_str());
65 if(!of.is_open()){
1
Taking false branch
66 cout<<"Unable to open \""<<OUTPUT_FILENAME<<"\" for writing!"<<endl;
67 exit(-1);
68 }
69
70 // Seed random number generator
71 srandom(time(NULL__null));
72
73
74 // Loop over events
75 unsigned int nevents;
76 for(nevents=1; nevents<=MAX_EVENTS; nevents++){
2
Loop condition is true. Entering loop body
5
Loop condition is true. Entering loop body
8
Loop condition is true. Entering loop body
77
78 vector<piX> piXs;
79 piX p;
80
81 // Randomly sample the energy and angles of the pion
82 double mom = (double)random()/(double)RAND_MAX2147483647*(P_MAX-P_MIN) + P_MIN;
83 double phi = (double)random()/(double)RAND_MAX2147483647*(PHI_MAX-PHI_MIN) + PHI_MIN;
84 double theta = (double)random()/(double)RAND_MAX2147483647*(THETA_MAX-THETA_MIN) + THETA_MIN;
85
86 p.E = sqrt(mom*mom + PI_CHARGED_MASS*PI_CHARGED_MASS);
87 p.px = mom*sin(theta)*cos(phi);
88 p.py = mom*sin(theta)*sin(phi);
89 p.pz = mom*cos(theta);
90 piXs.push_back(p);
91
92 // Write event to file
93 unsigned int type = PI_PLUS_TYPE8;
94 of<<RUN_NUMBER<<" "<<nevents<<" "<<piXs.size()<<endl;
95 for(unsigned int j=0; j<piXs.size(); j++){
3
Loop condition is false. Execution continues on line 107
6
Loop condition is false. Execution continues on line 107
9
Loop condition is true. Entering loop body
96 piX &p = piXs[j];
10
Dereference of null pointer
97 unsigned int index = j+1;
98
99 of<<index<<" "<<type<<" "<<0<<endl;
100 of<<" "<<0<<" "<<p.px<<" "<<p.py<<" "<<p.pz<<" "<<p.E<<endl;
101
102 // alternate bewtween pi+ and pi-
103 type = IS_POSITIVE ? PI_PLUS_TYPE8:PI_MINUS_TYPE9;
104 }
105
106 // Update screen
107 if(nevents%1000 == 0){
4
Taking false branch
7
Taking false branch
108 cout<<" "<<nevents<<" events generated"<<endl;
109 }
110
111 }
112
113 of.close();
114
115 cout<<endl<<""<<nevents-1<<" events written to "<<OUTPUT_FILENAME<<endl;
116
117 return 0;
118}
119
120
121//----------------------------
122// ParseCommandLineArguments
123//----------------------------
124void ParseCommandLineArguments(int narg, char* argv[])
125{
126 if(narg==1)Usage();
127
128 for(int i=1; i<narg; i++){
129 string arg = argv[i];
130 if(arg=="-h" || arg=="--help"){
131 Usage();
132 }else if(arg=="-M"){
133 if(i==narg-1){cout<<"-M requires an argument!"<<endl; Usage();}
134 MAX_EVENTS = atoi(argv[++i]);
135 }else if(arg=="-Pmin"){
136 if(i==narg-1){cout<<"-Pmin requires an argument!"<<endl; Usage();}
137 P_MIN = atof(argv[++i]);
138 }else if(arg=="-Pmax"){
139 if(i==narg-1){cout<<"-Pmax requires an argument!"<<endl; Usage();}
140 P_MAX = atof(argv[++i]);
141 }else if(arg=="-Phimin"){
142 if(i==narg-1){cout<<"-Phimin requires an argument!"<<endl; Usage();}
143 PHI_MIN = M_PI3.14159265358979323846/180.0*atof(argv[++i]);
144 }else if(arg=="-Phimax"){
145 if(i==narg-1){cout<<"-Phimax requires an argument!"<<endl; Usage();}
146 PHI_MAX = M_PI3.14159265358979323846/180.0*atof(argv[++i]);
147 }else if(arg=="-Thetamin"){
148 if(i==narg-1){cout<<"-Thetamin requires an argument!"<<endl; Usage();}
149 THETA_MIN = M_PI3.14159265358979323846/180.0*atof(argv[++i]);
150 }else if(arg=="-Thetamax"){
151 if(i==narg-1){cout<<"-Thetamax requires an argument!"<<endl; Usage();}
152 THETA_MAX = M_PI3.14159265358979323846/180.0*atof(argv[++i]);
153 }else if(arg=="-o"){
154 if(i==narg-1){cout<<"-o requires an argument!"<<endl; Usage();}
155 OUTPUT_FILENAME = argv[++i];
156 }else if(arg=="-n"){
157 IS_POSITIVE = false;
158 }
159 }
160
161 cout<<"---- genpiX will use the following settings: ----"<<endl;
162 cout<<"MAX_EVENTS = "<<MAX_EVENTS<<endl;
163 cout<<"P_MIN = "<<P_MIN<<endl;
164 cout<<"P_MAX = "<<P_MAX<<endl;
165 cout<<"PHI_MIN = "<<PHI_MIN<<endl;
166 cout<<"PHI_MAX = "<<PHI_MAX<<endl;
167 cout<<"THETA_MIN = "<<THETA_MIN<<endl;
168 cout<<"THETA_MAX = "<<THETA_MAX<<endl;
169 cout<<"PI_CHARGE = "<<(IS_POSITIVE ? "+1":"-1")<<endl;
170 cout<<"OUTPUT_FILENAME = \""<<OUTPUT_FILENAME<<"\""<<endl;
171 cout<<"-------------------------------------------------"<<endl;
172
173}
174
175//----------------------------
176// Usage
177//----------------------------
178void Usage(void)
179{
180 cout<<endl;
181 cout<<"Usage:"<<endl;
182 cout<<" genpiX -M numEvents [options]"<<endl;
183 cout<<endl;
184 cout<<" options:"<<endl;
185 cout<<" -h print this help message"<<endl;
186 cout<<" -M numEvents set the number of events to produced (required)"<<endl;
187 cout<<" -N numpiXs set the number of piXs to generate per event."<<endl;
188 cout<<" -Pmin p set the lower pion momentum in GeV/c"<<endl;
189 cout<<" -Pmax p set the upper pion momentum in GeV/c"<<endl;
190 cout<<" -Phimin phi set the lower pion phi angle in degrees"<<endl;
191 cout<<" -Phimax phi set the upper pion phi angle in degrees"<<endl;
192 cout<<" -Thetamin theta set the lower pion theta angle in degrees"<<endl;
193 cout<<" -Thetamax theta set the upper pion theta angle in degrees"<<endl;
194 cout<<" -n set the particle type to a pi-"<<endl;
195 cout<<" -o filename set the output filename"<<endl;
196 cout<<endl;
197 cout<<"This program is essentially a single pion particle gun."<<endl;
198 cout<<"It can produce single pion events that can be converted using"<<endl;
199 cout<<"genr8_2_hddm into a format suitable as input to hdgeant."<<endl;
200 cout<<"\"Why go to this trouble\" you ask, \"when hdgeant already has a"<<endl;
201 cout<<"built-in particle gun?\" The reason is because I want to overlay"<<endl;
202 cout<<"EM background events on these and hdgeant will only do that if the"<<endl;
203 cout<<"event is read from an external source."<<endl;
204
205 cout<<endl;
206 exit(0);
207}
208
209