1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | #include <iostream> |
9 | #include <fstream> |
10 | #include <math.h> |
11 | #include <DVector3.h> |
12 | using namespace std; |
13 | |
14 | #include <JANA/JEvent.h> |
15 | using namespace jana; |
16 | |
17 | #include "DFCALCluster_factory.h" |
18 | #include "DFCALHit.h" |
19 | |
20 | |
21 | bool FCALHitsSort_C(const DFCALHit* const &thit1, const DFCALHit* const &thit2) { |
22 | return thit1->E > thit2->E; |
23 | } |
24 | |
25 | |
26 | int ambiguous_events = 0; |
27 | |
28 | |
29 | |
30 | |
31 | DFCALCluster_factory::DFCALCluster_factory() |
32 | { |
33 | |
34 | MIN_CLUSTER_BLOCK_COUNT = 1; |
35 | MIN_CLUSTER_SEED_ENERGY = 0.035; |
36 | |
37 | gPARMS->SetDefaultParameter("FCAL:MIN_CLUSTER_BLOCK_COUNT", MIN_CLUSTER_BLOCK_COUNT); |
38 | gPARMS->SetDefaultParameter("FCAL:MIN_CLUSTER_SEED_ENERGY", MIN_CLUSTER_SEED_ENERGY); |
39 | |
40 | } |
41 | |
42 | |
43 | |
44 | |
45 | jerror_t DFCALCluster_factory::brun(JEventLoop *eventLoop, int runnumber) |
46 | { |
47 | |
48 | vector<const DFCALGeometry*> fcalGeoms; |
49 | eventLoop->Get(fcalGeoms); |
50 | if(fcalGeoms.size()<1){ |
51 | _DBG_std::cerr<<"DFCALCluster_factory.cc"<<":"<< 51<<" "<<"fcalGeoms.size()<1 !!!"<<endl; |
52 | throw JException("fcalGeoms.size()<1 !!!"); |
53 | } |
54 | fcalGeom = fcalGeoms[0]; |
55 | |
56 | return NOERROR; |
57 | } |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | jerror_t DFCALCluster_factory::evnt(JEventLoop *eventLoop, int eventnumber) |
64 | { |
65 | |
66 | vector<const DFCALHit*> fcalhits; |
67 | eventLoop->Get(fcalhits); |
68 | |
69 | |
70 | |
71 | sort(fcalhits.begin(), fcalhits.end(), FCALHitsSort_C); |
72 | |
73 | |
74 | int nhits = 0; |
| 1 | Variable 'nhits' initialized to 0 | |
|
75 | DFCALCluster::userhits_t* hits = (DFCALCluster::userhits_t*) malloc(sizeof(DFCALCluster::userhits_t)*FCAL_USER_HITS_MAX2800); |
76 | |
77 | |
78 | for (vector<const DFCALHit*>::const_iterator hit = fcalhits.begin(); |
| 2 | | Loop condition is false. Execution continues on line 94 | |
|
79 | hit != fcalhits.end(); hit++ ) { |
80 | if ( (**hit).E < 1e-6 ) continue; |
81 | hits->hit[nhits].id = (**hit).id; |
82 | hits->hit[nhits].x = (**hit).x; |
83 | hits->hit[nhits].y = (**hit).y; |
84 | hits->hit[nhits].E = (**hit).E; |
85 | hits->hit[nhits].t = (**hit).t; |
86 | nhits++; |
87 | |
88 | if (nhits >= (int) FCAL_USER_HITS_MAX2800) { |
89 | cout << "ERROR: FCALCluster_factory: number of hits " << nhits << " larger than " << FCAL_USER_HITS_MAX2800 << endl; |
90 | break; |
91 | } |
92 | |
93 | } |
94 | hits->nhits = nhits; |
95 | |
96 | |
97 | |
98 | int hitUsed[nhits]; |
| 3 | | Declared variable-length array (VLA) has zero size |
|
99 | for ( int i = 0; i < nhits; i++ ) { hitUsed[i] = 0; } |
100 | |
101 | const unsigned int max = 999; |
102 | DFCALCluster* clusterList[max]; |
103 | unsigned int clusterCount = 0; |
104 | int iter; |
105 | for ( iter=0; iter < 99; iter++ ) { |
106 | |
107 | |
108 | |
109 | |
110 | bool something_changed = false; |
111 | for ( unsigned int c = 0; c < clusterCount; c++ ) { |
112 | |
113 | something_changed |= clusterList[c]->update( hits ); |
114 | } |
115 | if (something_changed) { |
116 | for ( unsigned int c = 0; c < clusterCount; c++ ) { |
117 | clusterList[c]->resetClusterHits(); |
118 | } |
119 | |
120 | for ( int h = 0; h < nhits; h++ ) hitUsed[h] = 0; |
121 | } |
122 | else if (iter > 0) { |
123 | break; |
124 | } |
125 | |
126 | |
127 | |
128 | |
129 | for ( int ih = 0; ih < hits->nhits; ih++ ) { |
130 | double energy = hits->hit[ih].E; |
131 | |
132 | if (energy < MIN_CLUSTER_SEED_ENERGY) |
133 | break; |
134 | double totalAllowed = 0; |
135 | for ( unsigned int c = 0; c < clusterCount; c++ ) { |
136 | totalAllowed += clusterList[c]->getEallowed(ih); |
137 | |
138 | |
139 | } |
140 | if (energy > totalAllowed) { |
141 | clusterList[clusterCount] = new DFCALCluster( hits->nhits ); |
142 | hitUsed[ih] = -1; |
143 | clusterList[clusterCount]->addHit(ih,1.); |
144 | clusterList[clusterCount]->update( hits ); |
145 | ++clusterCount; |
146 | } |
147 | else if (iter > 0) { |
148 | for ( unsigned int c = 0; c < clusterCount; c++ ) { |
149 | int nh_clust = clusterList[c]->getHits(); |
150 | |
151 | if ( nh_clust ) |
152 | continue; |
153 | totalAllowed -= clusterList[c]->getEallowed(ih); |
154 | |
155 | if (energy > totalAllowed) { |
156 | if ( clusterList[c]->getHits() == 0 ) { |
157 | hitUsed[ih] = -1; |
158 | } |
159 | else { |
160 | ++hitUsed[ih]; |
161 | } |
162 | clusterList[c]->addHit(ih,1.); |
163 | break; |
164 | } |
165 | } |
166 | } |
167 | } |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | for ( int ih = 0; ih < hits->nhits; ih++ ) { |
174 | if ( hitUsed[ih] < 0) |
175 | continue; |
176 | double totalExpected = 0; |
177 | |
178 | for ( unsigned int c = 0; c < clusterCount; c++ ) { |
179 | if (clusterList[c]->getHits() > 0) { |
180 | totalExpected += clusterList[c]->getEexpected(ih); |
181 | } |
182 | } |
183 | |
184 | for ( unsigned int c = 0; c < clusterCount; c++ ) { |
185 | if (clusterList[c]->getHits() > 0) { |
186 | double expected = clusterList[c]->getEexpected(ih); |
187 | |
188 | if (expected > 1e-6) { |
189 | clusterList[c]->addHit(ih,expected/totalExpected); |
190 | ++hitUsed[ih]; |
191 | } |
192 | } |
193 | } |
194 | } |
195 | |
196 | } |
197 | |
198 | if (iter == 99) { |
199 | ++ambiguous_events; |
200 | } |
201 | |
202 | for ( unsigned int c = 0; c < clusterCount; c++) { |
203 | unsigned int blockCount = clusterList[c]->getHits(); |
204 | |
205 | if (blockCount < MIN_CLUSTER_BLOCK_COUNT) { |
206 | delete clusterList[c]; |
207 | continue; |
208 | } |
209 | else { |
210 | |
211 | clusterList[c]->saveHits( hits ); |
212 | |
213 | _data.push_back( clusterList[c] ); |
214 | |
215 | } |
216 | } |
217 | |
218 | |
219 | if (hits) { |
220 | free(hits); |
221 | hits=0; |
222 | } |
223 | |
224 | return NOERROR; |
225 | |
226 | } |
227 | |