CbmRoot
CbmL1MuchFinder.cxx
Go to the documentation of this file.
1 #include "CbmL1MuchFinder.h"
2 
3 #include "CbmL1MuchHit.h"
4 #include "CbmL1MuchTrack.h"
5 
6 #include "CbmKF.h"
7 #include "CbmKFHit.h"
8 #include "CbmKFMaterial.h"
9 #include "CbmKFMath.h"
10 #include "CbmKFPixelMeasurement.h"
11 #include "CbmKFTrackInterface.h"
12 #include "CbmMCTrack.h"
13 #include "CbmMuchHit.h"
14 #include "CbmMuchPoint.h"
15 #include "CbmMuchTrack.h"
16 #include "CbmStsKFTrackFitter.h"
17 #include "CbmStsTrack.h"
18 #include "CbmStsTrackMatch.h"
19 #include "CbmVertex.h"
20 #include "FairRootManager.h"
21 
22 #include "TClonesArray.h"
23 #include "TFile.h"
24 #include "TLorentzVector.h"
25 #include "TVector3.h"
26 
27 #include <cmath>
28 #include <iostream>
29 #include <vector>
30 
31 using std::cout;
32 using std::endl;
33 using std::fabs;
34 using std::vector;
35 
37 
38 CbmL1MuchFinder::CbmL1MuchFinder(const char* name, Int_t iVerbose)
39  : FairTask(name, iVerbose) {
40  fTrackCollection = new TClonesArray("CbmMuchTrack", 100);
41  histodir = 0;
42 }
43 
44 
46 
47 InitStatus CbmL1MuchFinder::Init() { return ReInit(); }
48 
50  fMuchPoints =
51  (TClonesArray*) FairRootManager::Instance()->GetObject("MuchPoint");
52  fMuchHits = (TClonesArray*) FairRootManager::Instance()->GetObject("MuchHit");
53  fStsTracks =
54  (TClonesArray*) FairRootManager::Instance()->GetObject("StsTrack");
55  fMCTracks = (TClonesArray*) FairRootManager::Instance()->GetObject("MCTrack");
57  (TClonesArray*) FairRootManager::Instance()->GetObject("StsTrackMatch");
58  //fPrimVtx = (CbmVertex *) FairRootManager::Instance() ->GetObject("PrimaryVertex");
59  // Get pointer to PrimaryVertex object from IOManager if it exists
60  // The old name for the object is "PrimaryVertex" the new one
61  // "PrimaryVertex." Check first for the new name
62  fPrimVtx = dynamic_cast<CbmVertex*>(
63  FairRootManager::Instance()->GetObject("PrimaryVertex."));
64  if (nullptr == fPrimVtx) {
65  fPrimVtx = dynamic_cast<CbmVertex*>(
66  FairRootManager::Instance()->GetObject("PrimaryVertex"));
67  }
68  if (nullptr == fPrimVtx) {
69  Error("CbmL1MuchFinder::ReInit", "vertex not found!");
70  return kERROR;
71  }
72 
73  fStsFitter.Init();
74 
75  FairRootManager::Instance()->Register("MuchTrack",
76  "Much",
78  IsOutputBranchPersistent("MuchTrack"));
79 
80  return kSUCCESS;
81 }
82 
84 
86 
87 void CbmL1MuchFinder::Exec(Option_t* /*option*/) {
88  const int MaxBranches = 50;
89 
90  static bool first_call_murec = 1;
91  fTrackCollection->Clear();
92  static int EventCounter = 0;
93  EventCounter++;
94  cout << " MuRec event " << EventCounter << endl;
95 
96  int MuNStations = CbmKF::Instance()->MuchStation2MCIDMap.size();
97 
98  if (first_call_murec) {
99  first_call_murec = 0;
100  TDirectory* curdir = gDirectory;
101  histodir = gDirectory->mkdir("MuRec");
102  histodir->cd();
103  fhNBranches =
104  new TH1F("NBranches", "N Branches", MaxBranches, 0, MaxBranches);
105  curdir->cd();
106  }
107 
108  int NHits = fMuchHits->GetEntriesFast();
109  vector<CbmL1MuchHit> vMuchHits;
110 
111  for (int ih = 0; ih < NHits; ih++) {
112  CbmMuchHit* h = (CbmMuchHit*) fMuchHits->At(ih);
113  CbmL1MuchHit m(h, ih);
114  vMuchHits.push_back(m);
115  }
116 
117  vector<CbmL1MuchTrack> vTracks;
118 
119  int NStsTracks = fStsTracks->GetEntries();
120 
121  CbmL1MuchTrack Branches[MaxBranches];
122 
123  for (int itr = 0; itr < NStsTracks; itr++) {
124 
125  CbmStsTrack* sts = (CbmStsTrack*) fStsTracks->At(itr);
126  if (sts->GetNStsHits() + sts->GetNMvdHits() < 4) continue;
127 
128  // MC
129  if (0 && fSTSTrackMatch && fMCTracks) {
130  CbmStsTrackMatch* m = (CbmStsTrackMatch*) fSTSTrackMatch->At(itr);
131  if (!m) continue;
132  Int_t mcTrackID = m->GetMCTrackId();
133  if (mcTrackID < 0) continue;
134  CbmMCTrack* mcTrack = (CbmMCTrack*) fMCTracks->At(mcTrackID);
135  if (!mcTrack) continue;
136  if (abs(mcTrack->GetPdgCode()) != 13) continue;
137  }
138 
139  fStsFitter.DoFit(sts, 13); // refit with muon mass
140 
141  int NBranches = 1;
142 
143  Branches[0].SetStsTrack(sts);
144  Branches[0].StsID = itr;
145  Branches[0].NHits = 0;
146  Branches[0].NMissed = 0;
147  Branches[0].NMissedStations = 0;
148  Branches[0].ok = 1;
149  Branches[0].stopped = 0;
150  Branches[0].vHits.clear();
151  //cout<<"Sts track N "<<itr<<" with initial mom="<<1./Branches[0].T[4]<<endl;
152  for (int ist = 0; ist < MuNStations; ist++) {
153 
154  int NBranchesOld = NBranches;
155 
156  for (int ibr = 0; ibr < NBranchesOld; ibr++) {
157  CbmL1MuchTrack& tr = Branches[ibr];
158  if (tr.stopped) continue;
159  //if( ist%3 ==0 ) cout<<" | ";
160  double Zdet = CbmKF::Instance()->vMuchDetectors[ist].ZReference;
161  tr.Extrapolate(Zdet);
162  if (fabs(tr.T[4]) > 100.) tr.stopped = 1;
163  if (1. < 0.5 * fabs(tr.T[4]))
164  tr.stopped = 1; // 0.5 GeV, stop transport
165  //if( tr.stopped ) cout<<"Sts track N "<<itr<<" stopped at Mu station "<<ist
166  //<<" with mom="<<1./tr.T[4]<<endl;
167  if (tr.stopped) continue;
168  if (CbmKF::Instance()->vMuchDetectors[ist].IsOutside(tr.T[0],
169  tr.T[1])) {
170  //cout<<" out ";
171  tr.NMissedStations++;
172  continue;
173  }
174 
175  vector<int> NewHits;
176  for (int ih = 0; ih < NHits; ih++) {
177  CbmL1MuchHit& h = vMuchHits[ih];
178  if (h.iStation != ist) continue;
179  if (0) { // !!! Cut for the time of flight (ns)
180  double hl =
181  sqrt(h.FitPoint.x * h.FitPoint.x + h.FitPoint.y * h.FitPoint.y
182  + h.FitPoint.z * h.FitPoint.z);
183  double hp = 1. / fabs(tr.T[4]);
184  double texp =
185  hl * sqrt(1. - 0.1057 * 0.1057 / (hp * hp)) / 29.9792458; //ns
186  if (h.time - texp > 1.0) continue;
187  }
188  double dx = tr.T[0] - h.FitPoint.x;
189  double dy = tr.T[1] - h.FitPoint.y;
190  double c0 = tr.C[0] + h.FitPoint.V[0];
191  double c1 = tr.C[1] + h.FitPoint.V[1];
192  double c2 = tr.C[2] + h.FitPoint.V[2];
193  double chi2 = 0.5 * (dx * dx * c0 - 2 * dx * dy * c1 + dy * dy * c2)
194  / (c0 * c2 - c1 * c1);
195  if (chi2 <= 20.) NewHits.push_back(ih);
196  }
197  int nnew = NewHits.size();
198  for (int ih = 1; ih < nnew; ih++) {
199  if (NBranches >= MaxBranches) break;
200  CbmL1MuchTrack& t = Branches[NBranches++];
201  t = tr;
202  CbmL1MuchHit& h = vMuchHits[NewHits[ih]];
203  t.vHits.push_back(&h);
204  t.NHits++;
205  double qp0 = t.T[4];
206  h.Filter(t, 1, qp0);
207  }
208  if (nnew > 0) {
209  CbmL1MuchHit& h = vMuchHits[NewHits[0]];
210  tr.vHits.push_back(&h);
211  tr.NHits++;
212  double qp0 = tr.T[4];
213  h.Filter(tr, 1, qp0);
214  } else
215  tr.NMissed++;
216  }
217  }
218  int bestbr = 0;
219  for (int ibr = 1; ibr < NBranches; ibr++) {
220  if ((Branches[ibr].NHits > Branches[bestbr].NHits)
221  || (Branches[ibr].NHits == Branches[bestbr].NHits)
222  && (Branches[ibr].chi2 < Branches[bestbr].chi2))
223  bestbr = ibr;
224  }
225  vTracks.push_back(Branches[bestbr]);
226  // MC
227  if (fSTSTrackMatch && fMCTracks) {
228  CbmStsTrackMatch* m = (CbmStsTrackMatch*) fSTSTrackMatch->At(itr);
229  if (!m) continue;
230  Int_t mcTrackID = m->GetMCTrackId();
231  if (mcTrackID < 0) continue;
232  CbmMCTrack* mcTrack = (CbmMCTrack*) fMCTracks->At(mcTrackID);
233  if (!mcTrack) continue;
234  if (abs(mcTrack->GetPdgCode()) == 13) fhNBranches->Fill(NBranches);
235  }
236  } // tracks
237 
238  int NTracks = vTracks.size();
239  cout << "NTracks=" << NTracks << endl;
240  //sort tracks and check for common muon hits
241 
242  vector<CbmL1MuchTrack*> vpTracks;
243  for (int i = 0; i < NTracks; i++)
244  vpTracks.push_back(&(vTracks[i]));
245  sort(vpTracks.begin(), vpTracks.end(), CbmL1MuchTrack::Compare);
246 
247  int NOutTracks = fTrackCollection->GetEntries();
248 
249  for (int it = 0; it < NTracks; it++) {
250  CbmL1MuchTrack& tr = *vpTracks[it];
251  if (tr.NDF <= 0 || tr.chi2 > 10. * tr.NDF) {
252  tr.ok = 0;
253  continue;
254  }
255  int nbusy = 0;
256  for (int ih = 0; ih < tr.NHits; ih++)
257  if (tr.vHits[ih]->busy) nbusy++;
258  if (0 && nbusy > 2) {
259  tr.ok = 0;
260  continue;
261  }
262  {
263  new ((*fTrackCollection)[NOutTracks]) CbmMuchTrack();
264  CbmMuchTrack* MuchTrack =
265  (CbmMuchTrack*) fTrackCollection->At(NOutTracks++);
266  MuchTrack->SetChi2(tr.GetRefChi2());
267  MuchTrack->SetNDF(tr.GetRefNDF());
268  FairTrackParam tp;
269  CbmKFMath::CopyTC2TrackParam(&tp, tr.T, tr.C);
270  MuchTrack->SetMuchTrack(&tp);
271  MuchTrack->SetStsTrackID(tr.StsID);
272  int nh = 0;
273  for (vector<CbmL1MuchHit*>::iterator i = tr.vHits.begin();
274  i != tr.vHits.end();
275  i++) {
276  if (++nh > 30) break;
277  MuchTrack->AddHitIndex((*i)->index);
278  }
279  MuchTrack->SetNMissedHits(tr.NMissed);
280  MuchTrack->SetNMissedStations(tr.NMissedStations);
281  }
282  for (int ih = 0; ih < tr.NHits; ih++)
283  tr.vHits[ih]->busy = 1;
284  }
285 
286  if (EventCounter < 100 && EventCounter % 10 == 0
287  || EventCounter >= 100 && EventCounter % 100 == 0)
288  Write();
289  //cout<<"end of MuRec"<<endl;
290 }
291 
292 
294  TFile HistoFile("MuRec.root", "RECREATE");
296  HistoFile.Close();
297 }
298 
300  if (!obj->IsFolder())
301  obj->Write();
302  else {
303  TDirectory* cur = gDirectory;
304  TDirectory* sub = cur->mkdir(obj->GetName());
305  sub->cd();
306  TList* listSub = ((TDirectory*) obj)->GetList();
307  TIter it(listSub);
308  while (TObject* obj1 = it())
309  writedir2current(obj1);
310  cur->cd();
311  }
312 }
CbmKF::MuchStation2MCIDMap
std::map< Int_t, Int_t > MuchStation2MCIDMap
Definition: CbmKF.h:94
CbmL1MuchFinder::ReInit
InitStatus ReInit()
Definition: CbmL1MuchFinder.cxx:49
CbmVertex.h
CbmL1MuchFinder::writedir2current
void writedir2current(TObject *obj)
Definition: CbmL1MuchFinder.cxx:299
CbmKF.h
CbmL1MuchTrack::SetStsTrack
void SetStsTrack(CbmStsTrack *track)
Definition: CbmL1MuchTrack.cxx:8
CbmL1MuchFinder::fStsTracks
TClonesArray * fStsTracks
Much Hits.
Definition: CbmL1MuchFinder.h:45
CbmL1MuchTrack::NMissed
int NMissed
Definition: CbmL1MuchTrack.h:32
sqrt
friend F32vec4 sqrt(const F32vec4 &a)
Definition: L1/vectors/P4_F32vec4.h:41
CbmL1MuchFinder::Exec
void Exec(Option_t *option)
Definition: CbmL1MuchFinder.cxx:87
CbmMCTrack::GetPdgCode
Int_t GetPdgCode() const
Definition: CbmMCTrack.h:70
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmL1MuchFinder::Finish
void Finish()
Definition: CbmL1MuchFinder.cxx:85
CbmL1MuchTrack.h
CbmL1MuchFinder::fMuchHits
TClonesArray * fMuchHits
Much MC points.
Definition: CbmL1MuchFinder.h:44
CbmL1MuchFinder::SetParContainers
void SetParContainers()
Definition: CbmL1MuchFinder.cxx:83
CbmTrack::SetNDF
void SetNDF(Int_t ndf)
Definition: CbmTrack.h:71
CbmStsKFTrackFitter::DoFit
Int_t DoFit(CbmStsTrack *track, Int_t pidHypo=211)
Definition: CbmStsKFTrackFitter.cxx:79
CbmMuchTrack
Definition: CbmMuchTrack.h:16
CbmL1MuchTrack::T
double T[6]
Definition: CbmL1MuchTrack.h:29
CbmKFTrackInterface.h
CbmL1MuchTrack::NHits
int NHits
Definition: CbmL1MuchTrack.h:32
CbmL1MuchTrack::vHits
std::vector< CbmL1MuchHit * > vHits
Definition: CbmL1MuchTrack.h:31
CbmKFMath.h
CbmL1MuchFinder::Init
InitStatus Init()
Definition: CbmL1MuchFinder.cxx:47
CbmL1MuchHit
Definition: CbmL1MuchHit.h:10
CbmL1MuchTrack::GetRefNDF
int & GetRefNDF()
Chi^2 after fit.
Definition: CbmL1MuchTrack.h:21
CbmKF::Instance
static CbmKF * Instance()
Definition: CbmKF.h:39
CbmStsTrack.h
Data class for STS tracks.
CbmMuchPoint.h
h
Data class with information on a STS local track.
CbmMuchTrack.h
CbmL1MuchFinder::fStsFitter
CbmStsKFTrackFitter fStsFitter
Definition: CbmL1MuchFinder.h:51
CbmL1MuchFinder::CbmL1MuchFinder
CbmL1MuchFinder(const char *name="CbmL1MuchFinder", Int_t iVerbose=1)
Definition: CbmL1MuchFinder.cxx:38
CbmL1MuchTrack::ok
bool ok
Definition: CbmL1MuchTrack.h:33
CbmL1MuchTrack::Compare
static bool Compare(const CbmL1MuchTrack *p1, const CbmL1MuchTrack *p2)
Definition: CbmL1MuchTrack.h:37
CbmL1MuchTrack::NMissedStations
int NMissedStations
Definition: CbmL1MuchTrack.h:32
CbmL1MuchFinder::fMCTracks
TClonesArray * fMCTracks
Definition: CbmL1MuchFinder.h:46
CbmVertex
Definition: CbmVertex.h:26
CbmL1MuchFinder::Write
void Write()
Definition: CbmL1MuchFinder.cxx:293
CbmKFPixelMeasurement.h
CbmL1MuchFinder.h
CbmKF::vMuchDetectors
std::vector< CbmKFTube > vMuchDetectors
Definition: CbmKF.h:76
CbmStsKFTrackFitter::Init
void Init()
Definition: CbmStsKFTrackFitter.cxx:29
CbmL1MuchFinder::fMuchPoints
TClonesArray * fMuchPoints
Definition: CbmL1MuchFinder.h:43
CbmMCTrack.h
CbmL1MuchFinder::fTrackCollection
TClonesArray * fTrackCollection
Definition: CbmL1MuchFinder.h:48
CbmL1MuchTrack::NDF
int NDF
Definition: CbmL1MuchTrack.h:30
CbmKFMaterial.h
CbmL1MuchFinder::histodir
TDirectory * histodir
Definition: CbmL1MuchFinder.h:53
CbmMCTrack
Definition: CbmMCTrack.h:34
fabs
friend F32vec4 fabs(const F32vec4 &a)
Definition: L1/vectors/P4_F32vec4.h:60
CbmKFMath::CopyTC2TrackParam
static void CopyTC2TrackParam(FairTrackParam *par, Double_t T[], Double_t C[])
Definition: CbmKFMath.cxx:809
m
__m128 m
Definition: L1/vectors/P4_F32vec4.h:26
CbmL1MuchTrack
Definition: CbmL1MuchTrack.h:13
CbmL1MuchFinder::fPrimVtx
CbmVertex * fPrimVtx
Much tracks.
Definition: CbmL1MuchFinder.h:50
CbmL1MuchFinder
Definition: CbmL1MuchFinder.h:13
CbmKFHit.h
CbmL1MuchFinder::~CbmL1MuchFinder
~CbmL1MuchFinder()
Definition: CbmL1MuchFinder.cxx:45
CbmL1MuchFinder::fSTSTrackMatch
TClonesArray * fSTSTrackMatch
Definition: CbmL1MuchFinder.h:47
CbmStsTrack
Definition: CbmStsTrack.h:37
CbmL1MuchTrack::GetRefChi2
double & GetRefChi2()
array[15] of covariance matrix
Definition: CbmL1MuchTrack.h:20
CbmL1MuchFinder::fhNBranches
TH1F * fhNBranches
Definition: CbmL1MuchFinder.h:58
ClassImp
ClassImp(CbmL1MuchFinder)
CbmL1MuchTrack::C
double C[15]
Definition: CbmL1MuchTrack.h:29
CbmL1MuchHit.h
CbmL1MuchTrack::StsID
int StsID
Definition: CbmL1MuchTrack.h:35
CbmStsKFTrackFitter.h
CbmL1MuchTrack::chi2
double chi2
Definition: CbmL1MuchTrack.h:29
CbmL1MuchTrack::stopped
bool stopped
Definition: CbmL1MuchTrack.h:34
CbmKFTrackInterface::Extrapolate
Int_t Extrapolate(Double_t z, Double_t *QP0=0)
Access to i-th hit.
Definition: CbmKFTrackInterface.cxx:39