-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFitLayer.cxx
288 lines (236 loc) · 7.2 KB
/
FitLayer.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "FitLayer.h"
#include "util.h"
#include "TString.h"
#include "TFile.h"
#include "TH1.h"
#include "TNtuple.h"
#include <iostream>
#include "TrackModelPhysical.h"
#include "Tracker.h"
using namespace std;
constexpr double PI = 3.14159265359;
void FitLayerHit::Print() const
{
cout<<"fit hit x "<<x<<" y "<<y<<" z "<<z<<" r "<<r<<" phi "<<phi<<" t "<<t<<endl;
}
void FitLayer::Print()
{
cout<<"FitLayer v "<<mVolume<<" l "<< mLayer<<" r "<<mR<<" z "<<mZ<<endl;
}
void FitLayer::Create( int layerID, bool phiWindowInCm, double searchWindowPhi, double searchWindowT, bool prn )
{
//cout<<"Layer create : layer "<<layerID<<endl;
// SG!!!! remove some hits !!!!
Layer &geoLayer = Geo::layers[layerID];
bool *hitOK = new bool[ mTracker->mHits.size() ];
for( int ih=0; ih<mTracker->mHits.size(); ih++ ){
hitOK[ih] = 1;
if( !mTracker->mcFlag ) continue;
const HitMC &mc = mTracker->mHitsMC[ih];
//hitOK[ih] = 1;// ( mc.partID>=0 && mTracker->mParticles[mc.partID].pt>=1. );
//hitOK[ih] = ( mc.partID>=0 && 0.5<=pt0 && pt0 <1 );
//hitOK[ih] = ( mc.partID>=0 && pt0>=0.3 && pt0<0.5 );
//hitOK[ih] = !( mc.partID>=0 && pt0>=0.5 );
// if( mc.partID<0 ) hitOK[ih] = 0;
if( mc.partID>=0 ){
Particle &p = mTracker->mParticles[mc.partID];
//if( p.pt >= 0.5 ) hitOK[ih] = 0;
//if( p.pt < 0.3 || p.pt >= 0.5 ) hitOK[ih] = 0;
//if( p.baseV ) hitOK[ih] = 0;
//if( !p.baseA0 ) hitOK[ih] = 0;
//if( p.pt>=0.3 ) hitOK[ih] = 0;
//if( p.pt<0.2 ) hitOK[ih] = 0;
// if( p.baseI==0 && p.pt>=0.2 ) hitOK[ih] = 0;
//if( p.baseI==0 && p.pt<0.3 ) hitOK[ih] = 0;
//if( !( p.pt>=0.5 && p.baseI0==0 && !p.prim) ) hitOK[ih] = 0;
//if( p.pt<0.3 && p.baseI0==0 ) hitOK[ih] = 0;
//if( p.baseI0!=0 ) hitOK[ih] = 0;
}
//if( mc.partID!=498 ) hitOK[ih] =0;
}
// find detector geometry from hits
mType = Geo::layers[layerID].type;
mVolume = Geo::layers[layerID].volume;
mLayer = Geo::layers[layerID].layer;
mTimeMark = 0;
mTmin = mTmin = geoLayer.tMin;
mTmax = mTmax = geoLayer.tMax;
mR = 0;
mZ = 0;
double rmin = 0;
int nHits = 0;
for( int ih=0; ih<mTracker->mHits.size(); ih++ ){
const Hit &h = mTracker->mHits[ih];
if( h.layerID!= layerID ) continue;
if( h.isUsed!=0 ) continue;
double t = h.t;
if( mTmin > t ) mTmin = t;
if( mTmax < t ) mTmax = t;
if( rmin > h.r ) rmin = h.r;
mR+= h.r;
mZ+= h.z;
nHits++;
}
if( nHits>0 ){
mR/=nHits;
mZ/=nHits;
} else {
mR = 10;
mZ = 10;
rmin = 10;
}
mR = geoLayer.r;
mZ = geoLayer.z;
/*
cout<<"layer "<<layerID<<" geo min/maxT: "<<geoLayer.tMin<<" "<<geoLayer.tMax
<<" fitlayer: "<<mTmin<<" "<<mTmax<<endl;
*/
if( phiWindowInCm ){
searchWindowPhi/=rmin;
}
// add some margin for T and PI, that exact max values stays inside the intervals
mTmax+=.001;
mPhiMin = -PI;
double phiMax = PI+ 0.00001;
mNbinsPhi = int( (phiMax - mPhiMin)/searchWindowPhi );
mNbinsT = int( (mTmax - mTmin)/searchWindowT );
if( mNbinsPhi < 1 ) mNbinsPhi = 1;
if( mNbinsT < 1 ) mNbinsT = 1;
mBinPhi = (phiMax - mPhiMin)/mNbinsPhi;
mBinT = (mTmax - mTmin)/mNbinsT;
mBinPhiInv = 1./mBinPhi;
mBinTInv = 1./mBinT;
mNbinsPhi+=2;
mNbinsT+=2;
//
mNbinsTotal = mNbinsPhi*mNbinsT;
mBins.resize(mNbinsTotal);
for( int ib=0; ib<mNbinsTotal; ib++ ){
mBins[ib].firstHit=0;
mBins[ib].nHits=0;
}
vector<int> vHitIDs;
vector<int> vBinInd;
for( int ih=0; ih<mTracker->mHits.size(); ih++ ){
const Hit &h = mTracker->mHits[ih];
if( h.layerID!= layerID ) continue;
if( !hitOK[ih] ) continue; //SG!!!
if( h.isUsed!=0 ) continue;
int it = int ( (h.t - mTmin)*mBinTInv ) ;
int iphi = int ( (h.phi - mPhiMin )*mBinPhiInv );
if( it<0 || it>=mNbinsT-2 || iphi<0 || iphi>= mNbinsPhi-2 ){
cout<<"fit layer: wrong indexing: mark 1"<<endl;
cout<<" it "<<it<<" of "<<mNbinsT<<" iphi "<<iphi<<" of "<<" "<<mNbinsPhi<<endl;
cout<<"phi "<<h.phi<<" +pi="<< h.phi + PI <<endl;
cout<<"t "<<h.t<<" tmin "<<mTmin<<" tmax "<<mTmax<<" tbin "<<mBinT<<endl;
cout<<"it float = "<<(h.t - mTmin)*mBinTInv<<endl;
exit(0);
}
it+=1;
iphi+=1;
if(prn) cout<<"create layer: hit at bins "<<iphi<<" "<<it<<endl;
vHitIDs.push_back(ih);
vBinInd.push_back( it*mNbinsPhi + iphi );
// duplicate hits at the phi edges (phi=+-PI)
if( h.phi > PI-mBinPhi ){ // put to the 0 phi bin
if(prn) cout<<"create layer: add clone hit at bins "<<0<<" "<<it<<endl;
vHitIDs.push_back(ih);
vBinInd.push_back( it*mNbinsPhi + 0 );
}
else if( h.phi < -PI + mBinPhi ){ // put to the last phi bin
if(prn) cout<<"create layer: add clone hit at bins "<<mNbinsPhi-1<<" "<<it<<endl;
vHitIDs.push_back(ih);
vBinInd.push_back( it*mNbinsPhi + mNbinsPhi-1 );
}
}
nHits = vHitIDs.size();
for( int ih=0; ih<nHits; ih++ ){
FitLayerBin &bin = mBins[vBinInd[ih]];
bin.nHits++;
}
int nHits1=0;
for( int ib=0; ib<mNbinsTotal; ib++ ){
FitLayerBin &bin = mBins[ib];
bin.firstHit=nHits1;
nHits1+=bin.nHits;
bin.nHits = 0;
}
if(prn) cout<<"n hits to store:"<<nHits<<" "<<nHits1<<endl;
if( nHits1!=nHits ){
cout<<" wrong fit layer indexing: mark 2"<<endl;
exit(0);
}
// store hits
mFitHits.resize( nHits );
for( int ih=0; ih<nHits; ih++ ){
const Hit &h = mTracker->mHits[vHitIDs[ih]];
//assert( h.layerID == layerID );
//assert( h.isUsed==0 );
FitLayerBin &bin = mBins[vBinInd[ih]];
int ind = bin.firstHit + bin.nHits;
bin.nHits++;
FitLayerHit &rh = mFitHits[ind];
rh.x = h.x;
rh.y = h.y;
rh.z = h.z;
rh.r = h.r;
rh.phi = h.phi;
rh.searchPhi = rh.phi;
rh.BzFwd = h.BzFwd;
rh.BzMid = h.BzMid;
rh.BzBck = h.BzBck;
rh.isClone=0;
rh.t = h.t;
rh.id = vHitIDs[ih];
rh.module = h.module;
rh.timeMark=0;
}
// correct phi for the first and for the last phi bins
for(int it=0; it<mNbinsT; it++){
{
FitLayerBin &bin = mBins[it*mNbinsPhi];
for( int ih = 0; ih<bin.nHits; ih++){
FitLayerHit &rh = mFitHits[bin.firstHit+ih];
if( rh.phi < PI - mBinPhi ){
cout<<"wrong phi in the first search bin: phi="<<rh.phi<<endl;
cout<<"border "<<-PI + mBinPhi<<endl;
cout<<"mBinPhi "<< mBinPhi<<endl;
exit(1);
}
if( rh.phi > PI - mBinPhi){
rh.searchPhi = -2*PI+rh.phi;
rh.isClone=1;
}
}
}
{
FitLayerBin &bin = mBins[it*mNbinsPhi + mNbinsPhi - 1];
for( int ih = 0; ih<bin.nHits; ih++){
FitLayerHit &rh = mFitHits[bin.firstHit + ih];
if( rh.phi > -PI + mBinPhi ){
cout<<"wrong phi in the last search bin: phi="<<rh.phi<<endl;
exit(1);
}
if( rh.phi < -PI + mBinPhi ){
rh.searchPhi = 2*PI+rh.phi;
rh.isClone=1;
}
}
}
}
// check again
int nHits2=0;
for( int ib=0; ib<mNbinsTotal; ib++ ){
FitLayerBin &bin = mBins[ib];
if( bin.firstHit!= nHits2){
cout<<" wrong fit layer indexing: mark 3 "<<endl;
exit(0);
}
nHits2+=bin.nHits;
}
if( nHits2!=nHits ){
cout<<" wrong fit layer indexing: mark 4 "<<endl;
exit(0);
}
delete[] hitOK;
}