BmnRoot
Loading...
Searching...
No Matches
CbmL1SttTrackFinder.cxx
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// ----- CbmL1SttTrackFinder source file -----
3// ----- Created 8/03/08 by A. Zinchenko -----
4// -------------------------------------------------------------------------
5
7
8#include "CbmL1SttHit.h"
9#include "CbmL1SttTrack.h"
10
11#include "CbmSttHit.h"
12#include "CbmSttPoint.h"
13#include "CbmSttTrack.h"
14#include "CbmKF.h"
15#include "CbmKFMath.h"
16#include "FairRootManager.h"
17#include "CbmKFHit.h"
18#include "CbmKFPixelMeasurement.h"
19#include "CbmKFMaterial.h"
20#include "CbmKFTrackInterface.h"
21#include "CbmMuchHit.h"
22#include "CbmMuchTrack.h"
23#include "CbmMuchPoint.h"
24#include "CbmStsTrack.h"
25#include "CbmMCTrack.h"
26#include "CbmStsKFTrackFitter.h"
27#include "CbmVertex.h"
28#include "CbmStsTrackMatch.h"
29
30#include "TFile.h"
31#include "TLorentzVector.h"
32#include "TVector3.h"
33#include "TClonesArray.h"
34
35#include <iostream>
36#include <vector>
37#include <cmath>
38
39using std::cout;
40using std::endl;
41using std::vector;
42using std::fabs;
43
44CbmL1SttTrackFinder::CbmL1SttTrackFinder(const char *name, Int_t iVerbose ):FairTask(name, iVerbose)
45{
46 fTrackCollection = new TClonesArray("CbmSttTrack", 100);
47 histodir = 0;
48}
49
50
54
56{
57 return ReInit();
58}
59
61{
62 fSttPoints=(TClonesArray *) FairRootManager::Instance()->GetObject("SttPoint");
63 fSttHits =(TClonesArray *) FairRootManager::Instance()->GetObject("SttHit");
64 fMuchTracks =(TClonesArray *) FairRootManager::Instance()->GetObject("MuchTrack");
65 fStsTracks =(TClonesArray *) FairRootManager::Instance()->GetObject("StsTrack");
66 fMCTracks =(TClonesArray *) FairRootManager::Instance()->GetObject("MCTrack");
67 fSTSTrackMatch = (TClonesArray*) FairRootManager::Instance()->GetObject("StsTrackMatch");
68 fPrimVtx = (CbmVertex *) FairRootManager::Instance() ->GetObject("PrimaryVertex");
69 fStsFitter.Init();
70
71 FairRootManager::Instance()->Register("SttTrack", "Stt", fTrackCollection, kTRUE);
72
73 cout << " **************************************************" << endl;
74 if (fMuchTracks) cout << " *** Using Much tracks for Stt tracking *** " << endl;
75 else cout << " *** Using Sts tracks for Stt tracking *** " << endl;
76 cout << " **************************************************" << endl;
77
78 return kSUCCESS;
79}
80
84
86{
87 Write();
88}
89
90void CbmL1SttTrackFinder::Exec(Option_t * option)
91{
92 const int MaxBranches = 50;
93
94 static bool first = 1;
95 fTrackCollection->Clear();
96 static int EventCounter = 0;
97 EventCounter++;
98 cout<<" SttRec event "<<EventCounter<<endl;
99
100 //int MuNStations = CbmKF::Instance()->MuchStation2MCIDMap.size();
101 static const Int_t nStations = CbmKF::Instance()->SttStationIDMap.size();
102 //const int nStations = 18; // to be taken elsewhere !!!
103
104 if ( first ){
105 first = 0;
106 TDirectory *curdir = gDirectory;
107 histodir = gDirectory->mkdir("SttRec");
108 histodir->cd();
109 fhNBranches = new TH1F("NBranches","N Branches",MaxBranches,0,MaxBranches);
110 curdir->cd();
111 }
112
113 int NHits = fSttHits->GetEntriesFast();
114 vector<CbmL1SttHit> vSttHits;
115
116 for( int ih = 0; ih < NHits; ++ih ){
117 CbmSttHit *h = (CbmSttHit*) fSttHits->UncheckedAt(ih);
118 CbmL1SttHit m( h, ih );
119 vSttHits.push_back(m);
120 }
121
122 vector<CbmL1SttTrack> vTracks;
123
124 Int_t nStsTracks;
125 TClonesArray *seedTracks;
126 if (fMuchTracks) seedTracks = fMuchTracks;
127 else seedTracks = fStsTracks;
128 nStsTracks = seedTracks->GetEntriesFast();
129 cout << " Seed tracks: " << nStsTracks << endl;
130
131 CbmL1SttTrack Branches[MaxBranches];
132 Double_t scatAng[MaxBranches] = {0.};
133
134 for( int itr = 0; itr < nStsTracks; ++itr ){
135
136 Int_t nOK = 0;
137 TObject *sts = seedTracks->UncheckedAt(itr);
138 if (!fMuchTracks) {
139 if ( ((CbmStsTrack*)sts)->GetNStsHits()+((CbmStsTrack*)sts)->GetNMvdHits()<4 ) continue;
140 }
141
142 // MC
143 /*
144 if( 0&&fSTSTrackMatch && fMCTracks ){
145 CbmStsTrackMatch *m = (CbmStsTrackMatch*) fSTSTrackMatch->At(itr);
146 if( !m ) continue;
147 Int_t mcTrackID = m->GetMCTrackId();
148 if (mcTrackID<0) continue;
149 CbmMCTrack* mcTrack= (CbmMCTrack*) fMCTracks->At(mcTrackID);
150 if( !mcTrack ) continue;
151 if( abs(mcTrack->GetPdgCode())!=13 ) continue;
152 }
153 */
154 // Check if track passes all the planes
155 if (1&&fSTSTrackMatch && fMCTracks ){
156 Int_t itr1 = itr;
157 if (fMuchTracks) itr1 = ((CbmMuchTrack*)sts)->GetStsTrackID();
158 CbmStsTrackMatch *m = (CbmStsTrackMatch*) fSTSTrackMatch->At(itr1);
159 if( !m ) continue;
160 Int_t mcTrackID = m->GetMCTrackId();
161 //if (mcTrackID<0) continue;
162 //CbmMCTrack* mcTrack= (CbmMCTrack*) fMCTracks->At(mcTrackID);
163 //if( !mcTrack ) continue;
164 //if( abs(mcTrack->GetPdgCode())!=13 ) continue;
165 Int_t hitPlanes[20];
166 TVector3 mom0(-1e+7), mom1;
167 for (Int_t i = 0; i < nStations; ++i) hitPlanes[i] = -1;
168 for( int ih = 0; ih < NHits; ++ih ){
169 CbmL1SttHit &h = vSttHits[ih];
170 CbmSttHit *hit = (CbmSttHit*) fSttHits->UncheckedAt(h.index);
171 CbmSttPoint *p = (CbmSttPoint*) fSttPoints->UncheckedAt(hit->GetRefIndex());
172 if (p->GetTrackID() != mcTrackID) continue;
173 if (p->GetStationNo() == 1 &&
174 TMath::Sqrt(p->GetX()*p->GetX()+p->GetY()*p->GetY()) > 220) continue;
175 if (hitPlanes[h.iStation] < 0) {
176 hitPlanes[h.iStation] = 1;
177 ++nOK;
178 }
179 if (mom0[0] < -1e+5) p->Momentum(mom0);
180 else p->Momentum(mom1);
181 if (itr < MaxBranches) scatAng[itr] = TMath::Max (scatAng[itr], mom1.Angle(mom0) * TMath::RadToDeg());
182 }
183 if (nOK < nStations) {
184 //cout << " Track " << mcTrackID << " has " << nOK << " points " << endl;
185 //continue;
186 }
187 }
188
189 if (!fMuchTracks) fStsFitter.DoFit( (CbmStsTrack*)sts, 13 ); // refit with muon mass
190
191 int NBranches = 1;
192
193 fMuchTracks == 0x0 ? Branches[0].SetStsTrack((CbmStsTrack*)sts) :
194 Branches[0].SetMuchTrack((CbmMuchTrack*)sts);
195 Branches[0].StsID = itr;
196 Branches[0].NHits = 0;
197 Branches[0].NMissed = 0;
198 Branches[0].NMissedStations = 0;
199 Branches[0].ok = 1;
200 Branches[0].stopped = 0;
201 Branches[0].vHits.clear();
202 //cout<<"Sts track N "<<itr<<" with initial mom="<<1./Branches[0].T[4]<<endl;
203 for( Int_t ist=0; ist<nStations; ++ist ){
204
205 int NBranchesOld = NBranches;
206
207 for( Int_t ibr=0; ibr<NBranchesOld; ++ibr ){
208 CbmL1SttTrack &tr = Branches[ibr];
209 if( tr.stopped ) continue;
210 //if( ist%3 ==0 ) cout<<" | ";
211 //double Zdet = CbmKF::Instance()->vMuchDetectors[ist].ZReference;
212 double Zdet = CbmKF::Instance()->vSttMaterial[ist].ZReference;
213 //cout << Zdet << endl;
214 //double Zdet = zPlanes[ist];
215 tr.Extrapolate(Zdet);
216 if( fabs(tr.T[4])>100.) tr.stopped = 1;
217 if( 1.<0.5 *fabs(tr.T[4]) ) tr.stopped = 1; // 0.5 GeV, stop transport
218 //if( tr.stopped ) cout<<"Sts track N "<<itr<<" stopped at Mu station "<<ist
219 //<<" with mom="<<1./tr.T[4]<<endl;
220 if( tr.stopped ) continue;
221 /*
222 if( CbmKF::Instance()->vMuchDetectors[ist].IsOutside( tr.T[0], tr.T[1] ) ){
223 //cout<<" out ";
224 tr.NMissedStations++;
225 continue;
226 }
227 */
228
229 vector<int> NewHits;
230 Int_t firstHit = -1;
231 Double_t uTr = 0.;
232 for( int ih=0; ih<NHits; ++ih ){
233 CbmL1SttHit &h = vSttHits[ih];
234 if( h.iStation != ist ) continue;
235
236 if (firstHit < 0) {
237 // Track coordinate transformation
238 uTr = tr.T[0] * h.FitPoint.phi_c + tr.T[1] * h.FitPoint.phi_s;
239 //uTr = tr.T[0] * h.FitPoint.phi_c - tr.T[1] * h.FitPoint.phi_s;
240 firstHit = 1;
241 }
242 CbmSttHit *hit = (CbmSttHit*) fSttHits->UncheckedAt(h.index);
243 //cout << tr.T[0] << " " << tr.T[1] << " " << hit->GetX() << " " << hit->GetY() << " " << uTr << " " << h.FitPoint.u << " " << h.FitPoint.phi_c << " " << h.FitPoint.phi_s << endl;
244
245 /*
246 if(0){ // !!! Cut for the time of flight (ns)
247 double hl = sqrt(h.FitPoint.x*h.FitPoint.x+h.FitPoint.y*h.FitPoint.y+h.FitPoint.z*h.FitPoint.z);
248 double hp = 1./fabs(tr.T[4]);
249 double texp = hl*sqrt(1. - 0.1057*0.1057/(hp*hp))/29.9792458; //ns
250 if( h.time - texp > 1.0 ) continue;
251 }
252 */
253 /*
254 double dx = tr.T[0] - h.FitPoint.x;
255 double dy = tr.T[1] - h.FitPoint.y;
256 double c0 = tr.C[0] + h.FitPoint.V[0];
257 double c1 = tr.C[1] + h.FitPoint.V[1];
258 double c2 = tr.C[2] + h.FitPoint.V[2];
259 double chi2 = 0.5*(dx*dx*c0-2*dx*dy*c1+dy*dy*c2)/(c0*c2-c1*c1);
260 */
261 Double_t du = uTr - h.FitPoint.u;
262 //Double_t c0 = tr.C[0] + h.FitPoint.sigma2;
263 //Double_t chi2 = du * du / c0; // w/out correlations at the moment !!!
264 Double_t w = h.FitPoint.sigma2 + h.FitPoint.phi_cc*tr.C[0] +
265 h.FitPoint.phi_2sc*tr.C[1] + h.FitPoint.phi_ss*tr.C[2];
266 Double_t chi21 = du * du / w;
267 //cout << " chi2 " << ist << " " << du << " " << chi21 << " " << chi21 << endl;
268 if ( chi21 <= 20. ) NewHits.push_back( ih );
269 //if ( chi21 <= 100. ) NewHits.push_back( ih );
270 }
271 int nnew = NewHits.size();
272 for( int ih=1; ih<nnew; ++ih){
273 if( NBranches >= MaxBranches ) break;
274 CbmL1SttTrack &t = Branches[NBranches++];
275 t = tr;
276 CbmL1SttHit &h = vSttHits[NewHits[ih]];
277 t.vHits.push_back(&h);
278 t.NHits++;
279 double qp0 = t.T[4];
280 h.Filter(t, 1, qp0);
281 }
282 if( nnew >0 ){
283 CbmL1SttHit &h = vSttHits[NewHits[0]];
284 tr.vHits.push_back(&h);
285 tr.NHits++;
286 double qp0 = tr.T[4];
287 h.Filter(tr, 1, qp0);
288 }else tr.NMissed++;
289 } // for( int ibr=0; ibr<NBranchesOld;
290 } // for( int ist=0; ist<nStations;
291 int bestbr=0;
292 for( int ibr=1; ibr<NBranches; ++ibr ){
293 if( (Branches[ibr].NHits > Branches[bestbr].NHits) ||
294 (Branches[ibr].NHits == Branches[bestbr].NHits)&&
295 (Branches[ibr].chi2<Branches[bestbr].chi2)
296 ) bestbr = ibr;
297 }
298 vTracks.push_back(Branches[bestbr]);
299 // MC
300 /*
301 if( fSTSTrackMatch && fMCTracks ){
302 CbmStsTrackMatch *m = (CbmStsTrackMatch*) fSTSTrackMatch->At(itr);
303 if( !m ) continue;
304 Int_t mcTrackID = m->GetMCTrackId();
305 if (mcTrackID<0) continue;
306 CbmMCTrack* mcTrack= (CbmMCTrack*) fMCTracks->At(mcTrackID);
307 if( !mcTrack ) continue;
308 if( abs(mcTrack->GetPdgCode())==13 ) fhNBranches->Fill(NBranches);
309 }
310 */
311 if (nOK == nStations) fhNBranches->Fill(NBranches);
312 else (vTracks.back()).ok = kFALSE;
313 //cout << itr << " " << nOK << " " << (vTracks.back()).ok << endl;
314 } // for( int itr=0; itr<nStsTracks;
315
316 int NTracks = vTracks.size();
317 cout<<"NTracks="<<NTracks<<endl;
318 //sort tracks and check for common muon hits
319
320 vector<CbmL1SttTrack*> vpTracks;
321 for( int i=0; i<NTracks; ++i ) vpTracks.push_back(&(vTracks[i]));
322 sort( vpTracks.begin(), vpTracks.end(), CbmL1SttTrack::Compare );
323
324 int NOutTracks = fTrackCollection->GetEntries();
325
326 for( int it=0; it<NTracks; ++it ){
327 CbmL1SttTrack &tr = *vpTracks[it];
328 if( tr.NDF<=0 || tr.chi2 > 10.*tr.NDF ){
329 //tr.ok = 0;
330 //continue;
331 }
332 int nbusy = 0;
333 for( int ih=0; ih<tr.NHits; ++ih ) if( tr.vHits[ih]->busy ) nbusy++;
334 if( 0 && nbusy>2 ){
335 tr.ok = 0;
336 continue;
337 }
338 // Check if track contains hit from all doublets in all stations
339 Int_t nDoubl[20] = {0};
340 for (Int_t ih = 0; ih < tr.NHits; ++ih) {
341 Int_t i2 = tr.vHits[ih]->iStation / 2;
342 if (nDoubl[i2] == 0) nDoubl[i2]++;
343 }
344 Int_t nHit = 0;
345 for (Int_t i = 0; i < nStations/2; ++i) nHit += nDoubl[i];
346 if (nHit < nStations/2) continue;
347 if (!(tr.ok)) continue;
348
349 {
350 new((*fTrackCollection)[NOutTracks]) CbmSttTrack();
351 CbmSttTrack* track = (CbmSttTrack*) fTrackCollection->At(NOutTracks++);
352 track->SetChi2(tr.GetRefChi2());
353 track->SetNDF(tr.GetRefNDF());
354 FairTrackParam tp;
355 CbmKFMath::CopyTC2TrackParam( &tp, tr.T, tr.C );
356 track->SetSttTrack( &tp );
357 track->SetStsTrackID(tr.StsID);
358 int nh=0;
359 for( vector<CbmL1SttHit* >::iterator i= tr.vHits.begin(); i!=tr.vHits.end(); i++){
360 if( ++nh>30 ) break;
361 track->AddHitIndex( (*i)->index );
362 }
363 track->SetNMissedHits( tr.NMissed );
364 track->SetNMissedStations( tr.NMissedStations );
365 if (tr.StsID < MaxBranches) track->SetScatAng(scatAng[tr.StsID]);
366 }
367 for( int ih=0; ih<tr.NHits; ++ih ) tr.vHits[ih]->busy=1;
368 }
369
370 if ( EventCounter < 100 && EventCounter % 10 == 0 ||
371 EventCounter >= 100 && EventCounter % 100 == 0
372 ) Write();
373 cout << "end of SttRec " << fTrackCollection->GetEntriesFast() << endl;
374}
375
376
377void CbmL1SttTrackFinder::Write()
378{
379 TFile HistoFile("SttRec.root","RECREATE");
380 writedir2current(histodir);
381 HistoFile.Close();
382}
383
384void CbmL1SttTrackFinder::writedir2current( TObject *obj ){
385 if( !obj->IsFolder() ) obj->Write();
386 else{
387 TDirectory *cur = gDirectory;
388 TDirectory *sub = cur->mkdir(obj->GetName());
389 sub->cd();
390 TList *listSub = ((TDirectory*)obj)->GetList();
391 TIter it(listSub);
392 while( TObject *obj1=it() ) writedir2current(obj1);
393 cur->cd();
394 }
395}
friend F32vec4 fabs(const F32vec4 &a)
Definition P4_F32vec4.h:52
int i
Definition P4_F32vec4.h:22
__m128 m
Definition P4_F32vec4.h:27
static void CopyTC2TrackParam(FairTrackParam *par, Double_t T[], Double_t C[])
Int_t Extrapolate(Double_t z, Double_t *QP0=0, Bool_t line=false)
Access to i-th hit.
static CbmKF * Instance()
Definition CbmKF.h:35
std::vector< CbmKFWall > vSttMaterial
Definition CbmKF.h:62
std::map< Int_t, Int_t > SttStationIDMap
Definition CbmKF.h:77
CbmKFUMeasurement FitPoint
Definition CbmL1SttHit.h:23
Int_t Filter(CbmKFTrackInterface &track, Bool_t downstream, Double_t &QP0)
CbmL1SttTrackFinder(const char *name="CbmL1SttTrackFinder", Int_t iVerbose=1)
void Exec(Option_t *option)
void SetStsTrack(CbmStsTrack *track)
double & GetRefChi2()
array[15] of covariance matrix
double C[15]
static bool Compare(const CbmL1SttTrack *p1, const CbmL1SttTrack *p2)
std::vector< CbmL1SttHit * > vHits
int & GetRefNDF()
Chi^2 after fit.
void SetMuchTrack(CbmMuchTrack *track)
Int_t DoFit(CbmStsTrack *track, Int_t pidHypo=211)