Bug Summary

File:programs/Utilities/hddm_cull_events/hddm_cull_events.cc
Location:line 59, column 26
Description:Dereference of null pointer

Annotated Source Code

1// $Id$
2//
3// Created Dec 22, 2007 David Lawrence
4
5#include <iostream>
6#include <iomanip>
7#include <vector>
8using namespace std;
9
10#include <signal.h>
11#include <time.h>
12
13#include "HDDM/hddm_s.h"
14
15void Smear(s_HDDM_t *hddm_s);
16void ParseCommandLineArguments(int narg, char* argv[]);
17void Usage(void);
18void ctrlCHandle(int x);
19
20vector<char*> INFILENAMES;
21char *OUTFILENAME = NULL__null;
22int QUIT = 0;
23unsigned int EVENTS_TO_SKIP = 0;
24unsigned int EVENTS_TO_KEEP = 1;
25unsigned int SPECIFIC_OFFSET_TO_KEEP = 0;
26unsigned int SPECIFIC_EVENT_TO_KEEP = 0;
27bool EVENT_TO_KEEP_MODE = false;
28
29#define _DBG_cout<<"hddm_cull_events.cc"<<":"<<29<<
" "
cout<<__FILE__"hddm_cull_events.cc"<<":"<<__LINE__29<<" "
30#define _DBG__cout<<"hddm_cull_events.cc"<<":"<<30<<
endl
cout<<__FILE__"hddm_cull_events.cc"<<":"<<__LINE__30<<endl
31
32
33//-----------
34// main
35//-----------
36int main(int narg,char* argv[])
37{
38 // Set up to catch SIGINTs for graceful exits
39 signal(SIGINT2,ctrlCHandle);
40
41 ParseCommandLineArguments(narg, argv);
42
43 cout<<"Skipping "<<EVENTS_TO_SKIP<<endl;
44 cout<<"Keeping "<<EVENTS_TO_KEEP<<endl;
45
46 // Output file
47 cout<<" output file: "<<OUTFILENAME<<endl;
48 s_iostream_t *fout = init_s_HDDM(OUTFILENAME);
49 if(!fout){
1
Taking false branch
50 cout<<" Error opening output file \""<<OUTFILENAME<<"\"!"<<endl;
51 exit(-1);
52 }
53
54 // Loop over input files
55 unsigned int NEvents = 0;
56 unsigned int NEvents_read = 0;
57 time_t last_time = time(NULL__null);
58 for(unsigned int i=0; i<INFILENAMES.size(); i++){
2
Loop condition is true. Entering loop body
59 cout<<" input file: "<<INFILENAMES[i]<<endl;
3
Dereference of null pointer
60 s_iostream_t *fin = open_s_HDDM(INFILENAMES[i]);
61 if(!fin){
62 cout<<" Error opening input file \""<<INFILENAMES[i]<<"\"!"<<endl;
63 exit(-1);
64 }
65
66 // Loop over all events in input
67 while(true){
68 s_HDDM_t *hddm_s = read_s_HDDM(fin);
69 if(!hddm_s)break;
70 NEvents_read++;
71
72 bool write_this_event = false;
73
74 // Loop over physics events within this event and see if one
75 // has the event number of interest
76 if(EVENT_TO_KEEP_MODE && hddm_s->physicsEvents!=HDDM_NULL(void*)&hddm_s_nullTarget){
77 for(unsigned int i=0; i<hddm_s->physicsEvents->mult; i++){
78 int eventNo = hddm_s->physicsEvents->in[i].eventNo;
79 if((unsigned int)eventNo == SPECIFIC_EVENT_TO_KEEP){
80 write_this_event = true;
81 QUIT = true;
82 }
83 }
84 }
85
86 // Check if we're in the range of offsets to write out
87 if(NEvents_read>EVENTS_TO_SKIP)write_this_event = true;
88
89 // Write this output event to file and free its memory
90 if(write_this_event){
91 flush_s_HDDM(hddm_s, fout);
92 NEvents++;
93 }else{
94 flush_s_HDDM(hddm_s, NULL__null);
95 }
96
97 // Update ticker
98 time_t now = time(NULL__null);
99 if(now != last_time){
100 cout<<" "<<NEvents_read<<" events read ("<<NEvents<<" event written) \r";cout.flush();
101 last_time = now;
102 }
103
104 // Quit as soon as we wrote all of the events we're going to
105 if(NEvents_read>=(EVENTS_TO_SKIP+EVENTS_TO_KEEP))break;
106
107 if(QUIT)break;
108 }
109
110 // Close input file
111 close_s_HDDM(fin);
112 }
113
114 // Close output file
115 close_s_HDDM(fout);
116
117 cout<<endl;
118 cout<<" "<<NEvents_read<<" events read, "<<NEvents<<" events written"<<endl;
119
120 return 0;
121}
122
123//-----------
124// ParseCommandLineArguments
125//-----------
126void ParseCommandLineArguments(int narg, char* argv[])
127{
128 INFILENAMES.clear();
129
130 for(int i=1; i<narg; i++){
131 char *ptr = argv[i];
132
133 if(ptr[0] == '-'){
134 switch(ptr[1]){
135 case 'h': Usage(); break;
136 case 'o': OUTFILENAME=&ptr[2]; break;
137 case 's': EVENTS_TO_SKIP=atoi(&ptr[2]); break;
138 case 'k': EVENTS_TO_KEEP=atoi(&ptr[2]); break;
139 case 'e': SPECIFIC_OFFSET_TO_KEEP=atoi(&ptr[2]); break;
140 case 'E': SPECIFIC_EVENT_TO_KEEP=atoi(&ptr[2]); EVENT_TO_KEEP_MODE=true; break;
141 }
142 }else{
143 INFILENAMES.push_back(argv[i]);
144 }
145 }
146
147 if(INFILENAMES.size()==0){
148 cout<<endl<<"You must enter a filename!"<<endl<<endl;
149 Usage();
150 }
151
152 if(SPECIFIC_OFFSET_TO_KEEP>0){
153 EVENTS_TO_KEEP=1;
154 EVENTS_TO_SKIP=SPECIFIC_OFFSET_TO_KEEP-1;
155 }
156
157 if(SPECIFIC_EVENT_TO_KEEP>0){
158 EVENTS_TO_KEEP=1;
159 EVENTS_TO_SKIP=1000000000; // Large number of events to read in while looking for the specified event
160 }
161
162 if(OUTFILENAME==NULL__null){
163 if(SPECIFIC_OFFSET_TO_KEEP>0){
164 OUTFILENAME = new char[256];
165 sprintf(OUTFILENAME,"evt%d.hddm", SPECIFIC_OFFSET_TO_KEEP);
166 }else{
167 OUTFILENAME = (char*)"culled.hddm";
168 }
169 }
170}
171
172
173//-----------
174// Usage
175//-----------
176void Usage(void)
177{
178 cout<<endl<<"Usage:"<<endl;
179 cout<<" hddm_cull_events [-oOutputfile] [-sNeventsToSkip] [-kNeventsToKeep] file1.hddm file2.hddm ..."<<endl;
180 cout<<endl;
181 cout<<"options:"<<endl;
182 cout<<" -oOutputfile Set output filename (def. culled.hddm)"<<endl;
183 cout<<" -sNeventsToSkip Set number of events to skip (def. 0)"<<endl;
184 cout<<" -kNeventsToKeep Set number of events to keep (def. 1)"<<endl;
185 cout<<" -eSingleEvent Keep only the single, specified event (file pos.)"<<endl;
186 cout<<" -ESingleEvent Keep only the single, specified event (event number)"<<endl;
187 cout<<endl;
188 cout<<" This will copy a continguous set of events from the combined event streams"<<endl;
189 cout<<" into a seperate output file. The primary use for this would be to copy"<<endl;
190 cout<<" a single, problematic event into a seperate file for easier debugging."<<endl;
191 cout<<endl;
192 cout<<" If the -eNNN option is used then only a single event is extracted"<<endl;
193 cout<<" (the NNN-th event) and written to a file with the name evtNNN.hddm."<<endl;
194 cout<<" Note that the event is counted from the begining of the file, starting"<<endl;
195 cout<<" with \"1\". This does NOT look at the event number stored in the event itself."<<endl;
196 cout<<" "<<endl;
197 cout<<" If the -ENNN option is used then only a single event is extracted"<<endl;
198 cout<<" (the specified event number) and written to a file with the name evtNNN.hddm."<<endl;
199 cout<<" "<<endl;
200 cout<<endl;
201
202 exit(0);
203}
204
205//-----------------------------------------------------------------
206// ctrlCHandle
207//-----------------------------------------------------------------
208void ctrlCHandle(int x)
209{
210 QUIT++;
211 cerr<<endl<<"SIGINT received ("<<QUIT<<")....."<<endl;
212}