HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HarpoKalmanNew.cxx
Go to the documentation of this file.
1 //
2 // File HarpoKalman.cxx
3 //
10 #include "HarpoKalmanNew.h"
11 #include "HarpoConfig.h"
12 #include "HarpoDetSet.h"
13 #include "HarpoDebug.h"
14 #include "HarpoDccEvent.h"
15 #include "Pmm2Event.h"
16 #include "HarpoEvent.h"
17 #include "HarpoRecoEvent.h"
18 #include "MakeNiceHisto.h"
19 
20 #include "TFile.h"
21 #include "TStyle.h"
22 #include "TCanvas.h"
23 #include "TLatex.h"
24 #include "TGraph.h"
25 #include "TF1.h"
26 #include "TArrow.h"
27 #include "TEllipse.h"
28 #include "TLinearFitter.h"
29 #include "TMath.h"
30 #include "TSystem.h"
31 
32 #include <cstdlib>
33 #include <cstring>
34 #include <cassert>
35 #include <fstream>
36 #include <iostream>
37 
38 
39 ClassImp(HarpoKalmanNew);
40 
42  {
43  unsigned int nd; // number of detectors
45 
46  assert(hdr != NULL);
47  hdr->print();
48 
49  for (nd = 0; nd < gkNDetectors; nd++) {
50  // if (fEvt->isdataExist(nd)) {
51  HarpoDccMap *plane = fEvt->GetDccMap(nd);
52  if (plane != NULL )plane->print();
53  }
54  }
55 
57 {
58  // Bool_t drawEvent = kFALSE;
59  nEvents++;
60  if(gHarpoDebug>0)
61  Info("process","Event %d",nEvents);
62 
63  // for(Int_t i = 0; i< NTRACK; i++)
64  // fNclTrack[i] = 0;
65  if(!fEvt->GetRecoEvent()) // Reconstructed data (clusters, tracks, matched tracks)
66  return;
67  HarpoRecoEvent* reco = fEvt->GetRecoEvent();
68  reco->SetTrackType(KALMAN);
69  // Clusters in the event (both X and Y directions)
70  TClonesArray* clArray = reco->GetClustersArray();
71  Int_t nCl = clArray->GetEntries();
72  TClonesArray* vArray = reco->GetVertexArray();
73  if(!vArray) return;
74  Int_t nV = vArray->GetEntries();
75  if(nV<=0) return;
76 
77  //Info("process","A");
78 
79  // TMatrixD Xorig(4,1);
80  Double_t inc = fResKalman; // Space resolution
81  Double_t theta = fScatKalman; // Multiple scattering
82  TMatrixD Corig(4,4);
83  for(int a = 0;a<4;a++){
84  for(int b =0;b<4;b++){
85  if(a==b){
86  if(a<2)
87  Corig[a][b]=inc*inc; //initialisation de C
88  else
89  Corig[a][b]=(2*inc)*(2*inc)+theta*theta;
90  }
91  else
92  Corig[a][b]=0;
93  }
94  }
95 
96  //Info("process","nV = %d",nV);
97  for(Int_t plane = 0; plane<2; plane++){
98  // Info("process","______________PLANE %d _________________________________",plane);
99  InitPlane(clArray, plane);
100  //Info("process","C %d",plane);
101  Int_t iTr = 0;
102  TArrayI* arr[NTRACK];
103  Double_t xStart[NTRACK][4];
104  Double_t xEnd[NTRACK][4];
105  Int_t ncl[NTRACK];
106  // Int_t track[nV][2];
107  Int_t vert[NTRACK][2];
108  for(Int_t iV = 0; iV<nV; iV++){
109  HarpoRecoVertex* vertex = (HarpoRecoVertex*)vArray->At(iV);
110  if(vertex->GetPlane() != plane) continue;
111  //Info("process","D %d",iV);
112  Double_t x = vertex->GetXvertex();
113  Double_t z = vertex->GetZvertex();
114  Int_t color = kRed, fill = 0;//3001;
115  //TArrayI * arr = new TArrayI(nCl);
116  //Double_t angle = 0;
117  TMatrixD Xorig(4,1);
118  Xorig[0][0] = x;
119  Xorig[1][0] = z;
120  Xorig[2][0] = vertex->GetPxVertex();
121  Xorig[3][0] = vertex->GetPzVertex();
122  // Info("process","%g %g %g %g",Xorig[0][0],Xorig[1][0],Xorig[2][0],Xorig[3][0]);
123  Int_t nTr = 2;
124  if(vertex->GetThetaVertex()==0) nTr = 1;
125  Double_t cos = TMath::Cos(vertex->GetThetaVertex()*0.5);
126  Double_t sin = TMath::Sin(vertex->GetThetaVertex()*0.5);
127  // track[iV][0] = -1;
128  // track[iV][1] = -1;
129  for(Int_t j = 0; j<nTr; j++){
130  color = iTr+2;
131  Xorig[2][0] = cos*vertex->GetPxVertex()-sin*vertex->GetPzVertex();
132  Xorig[3][0] = cos*vertex->GetPzVertex()+sin*vertex->GetPxVertex();
133  if(vertex->GetThetaVertex()>0){
134  Xorig[0][0] = x+20*Xorig[2][0];
135  Xorig[1][0] = z+20*Xorig[3][0];
136  }
137  sin *= -1;
138  if(fCanvas[plane]){
139  TArrow* arrow = new TArrow();
140  arrow->SetLineColor(kGray);
141  arrow->SetLineWidth(2);
142  arrow->SetFillColor(color);
143  fCanvas[plane]->cd();
144  arrow->DrawArrow(z,x,z+30*Xorig[3][0],x+30*Xorig[2][0],0.01);
145  // fHist[plane]->GetYaxis()->SetRangeUser(Xorig[0][0] - 10,Xorig[0][0] + 10);
146  // fHist[plane]->GetXaxis()->SetRangeUser(Xorig[1][0] - 10,Xorig[1][0] + 10);
147  fCanvas[plane]->Update();
148  }
149  //Info("process","_________________________________________________________");
150  for(Int_t i = 0; i<4; i++) xStart[iTr][i] = Xorig[i][0];
151 
152  arr[iTr] = new TArrayI(nCl);
153  for(Int_t i = 0; i<nCl; i++) arr[iTr]->AddAt(-1,i);
154  // FindNextClosest(Xorig,Corig,angle,0,iTr,clArray,arr[iTr],0,plane,color,fill,0,0);
155  color = iTr+2;
156  std::vector<TMatrixD> output = NextStep(Xorig,Corig,iTr,clArray,arr[iTr],0,plane,color,fill,0,0,kTRUE);
157  TMatrixD Xend = output[1];
158  for(Int_t i = 0; i<2; i++) xEnd[iTr][i] = Xend[i][0];
159  for(Int_t i = 2; i<4; i++) xEnd[iTr][i] = -Xend[i][0];
160  // track[iV][j] = iTr;
161  vert[iTr][0] = iV;
162  vert[iTr][1] = j;
163  ncl[iTr] = 0;
164  while(arr[iTr]->At(ncl[iTr])>=0) ncl[iTr]++;
165  //if(ncl[iTr]<5) continue;
166  // HarpoRecoKalmanTracks* tr = new HarpoRecoKalmanTracks(iTr,100000,0,0,0,0,0,100,plane,0,0,0,1,0,0,0,-1);
167  // // Info("SpliceTracks","Add graph %p",fGraphs[plane][itr]);
168  // fEvt->GetRecoEvent()->AddTracks(tr);
169  iTr++;
170  }
171  }
172 
173  Int_t nTracks = 0;
174  Int_t usedTr[iTr];
175  for(Int_t i1 = 0; i1<iTr; i1++) usedTr[i1] = 0;
176  for(Int_t i1 = 0; i1<iTr; i1++){
177  if(!arr[i1]) continue;
178  if(usedTr[i1]) continue;
179  Int_t ncl1 = ncl[i1];
180  Int_t dFirstMin = 1000;//, dFirstMax = -1000, dLastMin = 1000, dLastMax = -1000;
181  Int_t iFirstMin = -1;//, iFirstMax = -1, iLastMin = -1, iLastMax = -1;
182  Bool_t endMin = kFALSE;
183  Int_t nMerge = 1;
184  Int_t merge[iTr];
185  for(Int_t i = 1; i<iTr; i++) merge[i] = -1;
186  merge[0] = i1;
187  for(Int_t i2 = i1+1; i2<iTr; i2++){
188  if(!arr[i2]) continue;
189  Int_t ncl2 = ncl[i2];
190  Bool_t first = kTRUE;
191  Int_t nComm = 0; // number of common clusters
192  Int_t j1first = -1, j2first = -1;
193  // Int_t j1last = -1;
194  Int_t j2last = -1;
195  for(Int_t j1 = 0; j1<ncl1; j1++){
196  if(arr[i1]->At(j1)<0){
197  Info("process","???????????????");
198  ncl1 = j1;
199  break;
200  }
201  for(Int_t j2 = 0; j2<ncl2; j2++){
202  if(arr[i2]->At(j2)<0){
203  ncl2 = j2;
204  break;
205  }
206  if(arr[i2]->At(j2)==arr[i1]->At(j1)){
207  if(first){j2first = j2; j1first = j1; first = kFALSE;}
208  j2last = j2;
209  //j1last = j1;
210  nComm++;
211  //Info("process","track %d, %d %d %d %d",i2,j1first,j2first,j1last,j2last);
212  break;
213  }
214  }
215  }
216  // Info("process","** ncl[%d] = %d, ncl[%d] = %d => nComm = %d",i1,ncl1,i2,ncl2,nComm);
217  if(nComm>=8 || nComm>0.3*ncl1 || nComm>0.3*ncl2){
218  // Merge tracks i1 and i2
219  usedTr[i2] = 1; // remove track i2
220  Int_t dFirst = -1; //, dLast = -1;
221  //Info("process","** track %d (%d), %d %d %d %d",i2,nComm,j1first,j2first,j1last,j2last);
222  if(j2first<j2last){ // both tracks in same direction
223  dFirst = j1first - j2first;
224  // dLast = (ncl1-j1last) - (ncl2-j2last);
225  //Info("process","track %d, dFirst = %d, (%g %g)",i2,dFirst,xStart[i2][1],xStart[i2][0]);
226  }else{ // opposite directions
227  dFirst = j1first - (ncl2-j2first);
228  // dLast = (ncl1-j1last) - j2last;
229  //Info("process","track %d, dFirst = %d, (%g %g)",i2,dFirst,xEnd[i2][1],xEnd[i2][0]);
230  }
231  if(dFirst<dFirstMin){ dFirstMin = dFirst; iFirstMin = i2; endMin = (j2first<j2last);}
232  // if(dFirst>dFirstMax){ dFirstMax = dFirst; iFirstMax = i2; endMin = (j2first<j2last)}
233  // if(dLast<dLastMin){ dLastMin = dLast; iLastMin = i2;}
234  // if(dLast>dLastMax){ dLastMax = dLast; iLastMax = i2;}
235  merge[nMerge] = i2; nMerge++;
236  }
237  }
238  // Info("process","Merge tracks with %d => %d",i1,nTracks);
239  for(Int_t i = 0; i<nMerge; i++){
240  Int_t iTrack = merge[i];
241  //Info("process"," track %d (%d)",iTrack,ncl[iTrack]);
242  for(Int_t j = 0; j<ncl[iTrack]; j++){
243  if(arr[iTrack]->At(j)<0) break;
244  //Info("process"," %d",arr[iTrack]->At(j));
245  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(arr[iTrack]->At(j));
246  cluster->AddIdClusterTrack(nTracks);
247  }
248  }
249 
250  TMatrixD Xorig(4,1);
251  if(dFirstMin>=0 || nMerge==1){
252  //Info("process","track %d, dFirst = %d",i1,dFirstMin);
253  for(Int_t i = 0; i<4; i++) Xorig[i][0] = xStart[i1][i];
254  }else{
255  //Info("process","track %d, dFirst = %d => %d %d",i1,dFirstMin,iFirstMin,endMin);
256  if(endMin)
257  for(Int_t i = 0; i<4; i++) Xorig[i][0] = xStart[iFirstMin][i];
258  else
259  for(Int_t i = 0; i<4; i++) Xorig[i][0] = xEnd[iFirstMin][i];
260  }
261  // Info("process","Start: (%g %g %g %g)",Xorig[0][0],Xorig[1][0],Xorig[2][0],Xorig[3][0]);
262  Int_t color = 2+nTracks, fill = 3001+nTracks;
263  for(Int_t icl = 0; icl<nCl; icl++)
264  used[icl] = -1000;
265  for(Int_t i = 0; i<2; i++) Xorig[i][0] = Xorig[i][0]-10*Xorig[2+i][0];
266  fill = 3002;
267 
268 
269  if(fCanvas[plane]){
270  TArrow* arrow = new TArrow();
271  arrow->SetLineColor(kGray+2);
272  arrow->SetLineWidth(4);
273  arrow->SetLineStyle(2);
274  arrow->SetFillColor(color);
275  fCanvas[plane]->cd();
276  arrow->DrawArrow(Xorig[1][0],Xorig[0][0],Xorig[1][0]+30*Xorig[3][0],Xorig[0][0]+30*Xorig[2][0],0.01);
277  fCanvas[plane]->Update();
278  }
279 
280 
281  std::vector<TMatrixD> output = NextStep(Xorig,Corig,nTracks,clArray,0,0,plane,color,fill,1,0,kFALSE);
282 
283  // Info("process","size = %ld",output.size());
284  if(output.size()<5) continue;
285  TMatrixD extra = output[4];
286  Int_t nCl = Int_t(extra[0][0]);
287  if(nCl<5) continue;
288 
289  Double_t angle = extra[1][0]/nCl;
290 
291  Double_t rms[8];
292  for(Int_t i = 0; i<8; i++){
293  if(nCl-i < 1) break;
294  // Double_t angle = extra[3*i+1][0]/(nCl-i);
295  Double_t dangle = extra[3*i+2][0]/(nCl-i);
296  Double_t dangleSq = extra[3*i+3][0]/(nCl-i);
297  if(dangleSq-dangle*dangle<1e-6) rms[i] = -1;
298  else rms[i] = TMath::Sqrt(dangleSq-dangle*dangle);
299  //Info("process","%g %g %g %g",angle,dangle,dangleSq, dangleSq-dangle*dangle);
300  }
301  //Info("process","%g %g %g %g %g %g %g %g",rms[0],rms[1],rms[2],rms[3],rms[4],rms[5],rms[6],rms[7]);
302  // Info("process","Angles: %g %g %g",angle,rmsAngle,rmsAngle2);
303  Double_t rmsAngle = rms[0];
304  Double_t rmsAngle2 = rms[1];
305 
306  Double_t qTot = 0;
307  //HarpoRecoKalmanTracks* tr = new HarpoRecoKalmanTracks(nTracks,qTot,angle,rmsAngle,rmsAngle2,0,0,nCl,plane,0,0,0,1,0,0,0,-1);
308  HarpoRecoKalmanTracks* tr = new HarpoRecoKalmanTracks(nTracks,qTot,angle,rmsAngle,rmsAngle2,0,0,nCl,plane,rms[0],rms[1],rms[2],rms[3],rms[4],rms[5],rms[6],rms[7]);
309  fEvt->GetRecoEvent()->AddTracks(tr);
310 
311 
312  for(Int_t i = 0; i<nMerge; i++){
313  Int_t iTrack = merge[i];
314  Int_t iV = vert[iTrack][0];
315  Int_t j = vert[iTrack][1];
316  HarpoRecoVertex* vertex = (HarpoRecoVertex*)vArray->At(iV);
317  if(j==0) vertex->SetTr1Vertex(nTracks);
318  if(j==1) vertex->SetTr2Vertex(nTracks);
319  }
320  nTracks++;
321 
322 
323  }
324 
325  if(fCanvas[plane]){
326  fHist[plane]->GetXaxis()->SetRangeUser(0,NADC);
327  fHist[plane]->GetYaxis()->SetRangeUser(0,NCHAN);
328  fCanvas[plane]->Update();
329  }
330  }
331  // Info("process","DONE");
332 }
333 
334 Int_t HarpoKalmanNew::InitPlane(TClonesArray* clArray, Int_t plane)
335 {
336 
337  for(Int_t i = 0; i<NTRACK; i++){
338  if(fGraph[plane][i]) fGraph[plane][i]->Delete();
339  if(fGraph2[plane][i]) fGraph2[plane][i]->Delete();
340  fGraph[plane][i] = new TGraph();
341  fGraph2[plane][i] = new TGraph();
342  }
343 
344  fNtr[plane] = 0;
345  Int_t nCl = clArray->GetEntries();
346  if(nCl>=4000){
347  Warning("InitPlane","Too many clusters %d",nCl);
348  return 0;
349  }
350  TGraph* gCl = 0;
351  if(fCanvas[plane]){
352  gCl = new TGraph();
353  gCl->SetMarkerStyle(7);
354  }
355  // Organise Clusters in layers
356  for(Int_t icl = 0; icl<nCl; icl++){
357  reused[icl] = 0;
358  used[icl] = -1000;
359  fTrack[icl] = -1000;
360  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(icl);
361  // Int_t index = cluster->GetIndex();
362  if(cluster->GetPlane() != plane) continue;
363  if(cluster->GetQ() < fQmin) continue;
364  if(cluster->GetType() != BLOCCLUSTER) continue;
365  if(fCanvas[plane])
366  gCl->SetPoint(gCl->GetN(),cluster->GetZ(),cluster->GetX());
367  }
368  if(fCanvas[plane]){
369  fCanvas[plane]->cd();
370  gCl->Draw("Psame");
371  }
372 
373  return 1;
374 }
375 
376 
377 Int_t HarpoKalmanNew::GetNclCommon(TClonesArray* clArray, Int_t plane, Int_t iTr1, Int_t iTr2)
378 {
379 
380  Int_t nComm = 0;
381  Int_t nCl = clArray->GetEntries();
382  for(Int_t icl = 0; icl<nCl; icl++){ // For all clusters
383  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(icl);
384  if(cluster->GetPlane() != plane) continue;
385  if(!cluster->CheckIdClusterTrack(iTr1)) continue;
386  if(!cluster->CheckIdClusterTrack(iTr2)) continue;
387  nComm++;
388  }
389  return nComm;
390 }
391 
392 
393 
394 
395 std::vector<TMatrixD> HarpoKalmanNew::NextStep(TMatrixD X, TMatrixD C, Int_t iTr, TClonesArray* clArray, TArrayI* arr, Int_t ncl, Int_t plane, Int_t color, Int_t fill, Int_t smooth, Int_t qold, Bool_t finder)
396 {
397  if(gHarpoDebug>2) Info("NextStep","SMOOTH = %d, FINDER = %d",smooth,finder);
398  std::vector<TMatrixD> output;
399  // Double_t deltaRes = fResFinder;
400  // Double_t deltaScat = fScatFinder;
401  Double_t theta = fScatKalman; // Multiple scattering
402 
403  // Int_t ntr = 0;
404 
405  Double_t norm = TMath::Sqrt(X[2][0]*X[2][0]+X[3][0]*X[3][0]);
406  X[2][0] /= norm;
407  X[3][0] /= norm;
408 
409  if(fCanvas[plane]){
410  TArrow* arrow = new TArrow();
411  arrow->SetLineColor(kBlack);
412  arrow->SetFillColor(color);
413  fCanvas[plane]->cd();
414  arrow->DrawArrow(X[1][0],X[0][0],X[1][0]+5*X[3][0],X[0][0]+5*X[2][0],0.01);
415  // fHist[plane]->GetYaxis()->SetRangeUser(X[0][0] - 10,X[0][0] + 10);
416  // fHist[plane]->GetXaxis()->SetRangeUser(X[1][0] - 10,X[1][0] + 10);
417  fCanvas[plane]->Update();
418  }
419 
420 
421  Int_t iclMin = FindClosestNeighbour(X,iTr,clArray, plane, color, fill, finder);
422  if(iclMin==-1){ // END OF TRACK
423  output.push_back(C);
424  output.push_back(X);
425  return output;
426  }
427  //Info("NextStep","arr->AddAt(%d,%d)",iclMin,ncl);
428  if(arr) arr->AddAt(iclMin,ncl);
429  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(iclMin);
430  Double_t xMin = cluster->GetX();
431  Double_t zMin = cluster->GetZ();
432  Int_t qMin = cluster->GetQ();
433  Double_t resMin = GetResolution(cluster);
434 
435  // Position of the cluster (x,z) along the line of propagation
436  Double_t lambdaMin = (xMin-X[0][0])*X[2][0]+(zMin-X[1][0])*X[3][0];
437 
438 
439  if(gHarpoDebug>2)
440  Info("FindNextClosest","Add New Cluster");
441 
442  TMatrixD F(4,4);
443  for(int i = 0;i<4;i++){
444  for(int j =0;j<4;j++){
445  F[i][j]=0;
446  if(i==j) F[i][j]=1;
447  }
448  }
449  TMatrixD Xproj(4,1);
450  TMatrixD H(2,4);
451  TMatrixD Ht(2,4);
452  TMatrixD Q(4,4);
453  TMatrixD G(2,2);
454  TMatrixD V(2,2);
455 
456  Double_t inc = resMin;//fResKalman; // Space resolution
457  if(inc==0) inc = 0.000001;
458  Double_t v = X[2][0]*X[2][0] + X[3][0]*X[3][0];
459  for(int i = 0;i<4;i++){
460  for(int j =0;j<4;j++){
461  if(i==j){
462  if(i>1)
463  Q[i][j]=theta*theta*(1-X[i][0]*X[i][0]/v); //initialisation de Q
464  else
465  Q[i][j]=0;
466  }
467  }
468  }
469  for(int i = 0;i<2;i++){
470  for(int j =0;j<2;j++){
471  if(i==j){
472  H[i][j]=1;
473  V[i][j]=inc*inc;
474  G[i][j]=1./inc*inc;
475  }else{
476  H[i][j]=0;
477  V[i][j]=0;
478  G[i][j]=0;
479  }
480  }
481  }
482  Ht=H;
483  Ht.T();
484  // G=V;
485  // G.Invert();
486 
487 
488  TMatrixD Ft(4,4);
489  TMatrixD Cinv(4,4);
490  TMatrixD M(2,1);
491  TMatrixD Cproj(4,4);
492  TMatrixD Xfilter(4,1);
493  TMatrixD Cfilter(4,4);
494  TMatrixD Cprojinv(4,4);
495 
496  F[0][2]=lambdaMin;
497  F[1][3]=lambdaMin;
498  Xproj=F*X;
499  // Kalman matrices calculations
500  Ft = F;
501  Ft.T();
502  M[0][0] = xMin;
503  M[1][0] = zMin;
504  Cproj=F*C*Ft+Q;
505  Cprojinv=Cproj;
506  Cprojinv.Invert();
507  Cinv=Cprojinv+Ht*G*H;
508  Cfilter=Cinv;
509  Cfilter.Invert();
510  Xfilter=Cfilter*((Cprojinv*Xproj)+(Ht*G*M));
511 
512  used[iclMin] = iTr;
513 
514  if(gHarpoDebug>2)
515  Info("FindNextClosest","Add cluster %d, (%d %p) %d",iclMin,iTr,arr,fStartIndex[iTr]%10);
516 
517  //ncl++;
518  if(gHarpoDebug>2)
519  Info("FindNextClosest","Test %d %p %p %d %d %d %d %d", iTr, clArray, arr, ncl, plane, color, fill,smooth);
520  // Xproj.Print();
521  // Info("NextStep","Xproj[%d] = (%g %g %g %g)",ncl,Xproj[0][0],Xproj[1][0],Xproj[2][0],Xproj[3][0]);
522  //Info("NextStep","Xfilter[%d] = (%g %g %g %g)",ncl,Xfilter[0][0],Xfilter[1][0],Xfilter[2][0],Xfilter[3][0]);
523 
524  std::vector<TMatrixD> matrices =
525  NextStep(Xfilter, Cfilter, iTr, clArray, arr, ncl+1, plane, color, fill,smooth,qMin+qold,finder);
526 
527  if(smooth==0) fGraph2[plane][iTr]->SetPoint(ncl,Xfilter[1][0],Xfilter[0][0]);
528  if(smooth==0){
529  cluster->SetXfit(0,Xfilter[0][0]);
530  cluster->SetZfit(0,Xfilter[1][0]);
531  return matrices; // no smoothing
532  }
533 
534  TMatrixD Xsmooth(4,1);
535  TMatrixD Csmooth(4,4);
536  Int_t nExtra = 1+3*8;
537  TMatrixD extraold(nExtra,1);
538  if(matrices.size()<5){
539  //Info("NextStep","******* Initialize smoothing (%ld) ********",matrices.size());
540  Xsmooth = Xfilter;
541  Csmooth = Cfilter;
542  extraold[0][0] = 0;
543  for(Int_t i = 0; i<8; i++){
544  extraold[3*i+1][0] = -1000;
545  extraold[3*i+2][0] = 0; // mean deflection angle
546  extraold[3*i+3][0] = 0; // rms deflection angle
547  }
548  extraold[1][0] = TMath::ATan2(Xfilter[2][0],Xfilter[3][0]); // angle[k]
549  }else{
550  TMatrixD Cprojnew = matrices[0];
551  TMatrixD Xprojnew = matrices[1];
552  TMatrixD Csmoothold = matrices[2];
553  TMatrixD Xsmoothold = matrices[3];
554  extraold = matrices[4];
555 
556  TMatrixD A(4,4);
557  //Info("NextStep","ncl = %d (%d,%d)*(%d,%d)*(%d,%d)",ncl,Cfilter.GetNcols(),Cfilter.GetNrows(),Ft.GetNcols(),Ft.GetNrows(),(Cprojnew).GetNcols(),(Cprojnew).GetNrows());
558  TMatrixD Cprojnewinv(4,4);
559  Cprojnewinv = Cprojnew;
560  Cprojnewinv.Invert();
561  A = Cfilter*Ft*Cprojnewinv;
562  TMatrixD At(4,4);
563  At = A;
564  At.T();
565  Xsmooth = Xfilter + A*(Xsmoothold-Xprojnew);
566  Csmooth = Cfilter + A*(Csmoothold-Cprojnew)*At;
567  // Info("NextStep","Xsmoothold[%d] = (%g %g %g %g)",ncl,Xsmoothold[0][0],Xsmoothold[1][0],Xsmoothold[2][0],Xsmoothold[3][0]);
568  // Info("NextStep","Xprojnew[%d] = (%g %g %g %g)",ncl,Xprojnew[0][0],Xprojnew[1][0],Xprojnew[2][0],Xprojnew[3][0]);
569  }
570  // Info("NextStep","Xfilter[%d] = (%g %g %g %g)",ncl,Xfilter[0][0],Xfilter[1][0],Xfilter[2][0],Xfilter[3][0]);
571  // Info("NextStep","Xsmooth[%d] = (%g %g %g %g)",ncl,Xsmooth[0][0],Xsmooth[1][0],Xsmooth[2][0],Xsmooth[3][0]);
572 
573  output.push_back(Cproj);
574  output.push_back(Xproj);
575  output.push_back(Csmooth);
576  output.push_back(Xsmooth);
577 
578  // TMatrixD residual(2,1);
579  // TMatrixD residualt(2,1);
580  // TMatrixD Residual(2,2);
581  // residual = M - H*Xsmooth;
582  // residualt = residual;
583  // residualt.T();
584  // Residual = V - H*Csmooth*Ht;
585  // Residual.Invert();
586  // TMatrixD chi2m(1,1);
587  // chi2m = residualt*(Residual*residual);
588  // Double_t chi2 = chi2m[0][0];
589  // hChi2->Fill(chi2);
590 
591  // HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(iclMin);
592  // cluster->AddIdClusterTrack(iTr);
593  cluster->SetXfit(0,Xsmooth[0][0]);
594  cluster->SetZfit(0,Xsmooth[1][0]);
595 
596  fGraph[plane][iTr]->SetPoint(ncl,Xsmooth[1][0],Xsmooth[0][0]);
597 
598  // Double_t angleTmp = TMath::ATan2(Xsmooth[2][0], Xsmooth[3][0]);
599  // Double_t angleOld = extraold[1][0];
600  // Double_t angleOld2 = extraold[4][0];
601  // Double_t dangle = angleTmp - angleOld;
602  // Double_t dangle2 = 0;
603  // if(angleOld2>-100) dangle2 = angleTmp - angleOld2;
604  // if(dangle>TMath::Pi()) dangle -= TMath::Pi();
605  // if(dangle<-TMath::Pi()) dangle += TMath::Pi();
606  // if(dangle2>TMath::Pi()) dangle2 -= TMath::Pi();
607  // if(dangle2<-TMath::Pi()) dangle2 += TMath::Pi();
608  // Info("NextStep","(%g %g %g) => %g %g / %g %g",angleTmp,angleOld,angleOld2,dangle,dangle2,extraold[3][0],extraold[6][0]);
609  // TMatrixD extra(nExtra,1);
610  // extra[0][0] = extraold[0][0]+1;
611  // extra[1][0] = angleTmp;
612  // extra[2][0] = extraold[2][0] + dangle;
613  // extra[3][0] = extraold[3][0] + dangle*dangle;
614  // extra[4][0] = angleOld;
615  // extra[5][0] = extraold[5][0] + dangle2;
616  // extra[6][0] = extraold[6][0] + dangle2*dangle2;
617  // output.push_back(extra);
618  // Double_t angleOld = TMath::ATan2(Xsmoothold[2][0], Xsmoothold[3][0]);
619  // Double_t angleTmp = TMath::ATan2(Xsmooth[0][0] - Xsmoothold[0][0], Xsmooth[1][0] - Xsmoothold[1][0]);
620 
621  Double_t angleTmp = TMath::ATan2(Xsmooth[2][0], Xsmooth[3][0]);
622  TMatrixD extra(nExtra,1);
623  extra[0][0] = extraold[0][0]+1;
624  extra[1][0] = angleTmp;
625  for(Int_t i = 0; i<8; i++){
626  Double_t angleOld = extraold[3*i+1][0];
627  Double_t dangle = 0;
628  if(angleOld>-100) dangle = angleTmp - angleOld;
629  if(dangle>TMath::Pi()) dangle -= TMath::Pi();
630  if(dangle<-TMath::Pi()) dangle += TMath::Pi();
631  extra[3*i+2][0] = extraold[3*i+2][0] + dangle;
632  extra[3*i+3][0] = extraold[3][0] + dangle*dangle;
633  if(i>0) extra[3*i+1][0] = extra[3*i-2][0];
634  }
635  output.push_back(extra);
636  return output;
637 }
638 
639 
640 Int_t HarpoKalmanNew::FindClosestNeighbour(TMatrixD X, Int_t iTr, TClonesArray* clArray, Int_t plane, Int_t color, Int_t fill, Bool_t finder)
641 {
642 
643  if(gHarpoDebug>2) Info("FindNextClosest","FINDER = %d",finder);
644 
645  Double_t fRangeMax = 15;
646  Double_t deltaRes = fResFinder;
647  Double_t deltaScat = fScatFinder;
648  Double_t theta = fScatKalman; // Multiple scattering
649 
650  //Double_t distMin = 1000, dMin = 100000, xMin = -10000, zMin = -10000, rangeMin = 0, angleMin = -1000, resMin = -1, lambdaMin = 10000;
651  // Double_t distMin = 1000;
652  Double_t dMin = 100000, xMin = -10000, zMin = -10000, rangeMin = 0, lambdaMin = 10000;
653  //Int_t iclMin = -1, typeMin = -1, iMin = -1, kMin = -1, qMin = -1;
654  Int_t iclMin = -1; //, qMin = -1;
655 
656  Int_t nCl = clArray->GetEntries();
657  for(Int_t i = 0; i<nCl; i++){ // For all clusters
658  Int_t icl = i;
659  if(used[icl]==iTr) continue;
660  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(icl);
661  if(cluster->GetPlane() != plane) continue;
662  if(cluster->GetType() != BLOCCLUSTER) continue;
663  if(!finder && !cluster->CheckIdClusterTrack(iTr)) continue;
664  Double_t x = cluster->GetX();
665  Double_t z = cluster->GetZ();
666  // Int_t q = cluster->GetQ();
667 
668  // Position of the cluster (x,z) along the line of propagation
669  Double_t lambda = (x-X[0][0])*X[2][0]+(z-X[1][0])*X[3][0];
670  if(lambda<fLambdaMin) continue;
671  //if(finder && lambda>50) continue;
672  if(lambda>fLambdaMax) continue;
673  // Distance from new point (x,z) to the line of propagation
674  Double_t dist = TMath::Abs((x-X[0][0])*X[3][0]-(z-X[1][0])*X[2][0]);
675 
676  //Double_t d = lambda*lambda+dist*dist;
677  Double_t d = lambda*lambda*dist;
678 
679  Double_t res = GetResolution(cluster);
680  Double_t range = deltaRes*res + deltaScat*theta*lambda;
681  if(range>fRangeMax) range = fRangeMax;
682 
683  if(finder && TMath::Abs(dist)>range) continue;
684  if(d < dMin){
685  dMin = d;
686  // distMin = dist;
687  iclMin = icl;
688  lambdaMin = lambda;
689  rangeMin = range;
690  xMin = x;
691  zMin = z;
692  // qMin = q;
693  }
694  }
695 
696  if(fCanvas[plane]){ // Display for Reco monitor
697  // Float_t z[3] = {X[1][0],X[1][0]+lambdaMin*X[3][0]-rangeMin*X[2][0],X[1][0]+lambdaMin*X[3][0]+rangeMin*X[2][0]};
698  // Float_t x[3] = {X[0][0],X[0][0]+lambdaMin*X[2][0]+rangeMin*X[3][0],X[0][0]+lambdaMin*X[2][0]-rangeMin*X[3][0]};
699  Double_t w1 = rangeMin; //, w2 = rangeMin;
700  if(finder) w1 *= 5./lambdaMin;
701  Float_t z[4] = {(Float_t) (X[1][0]+5*X[3][0]+w1*X[2][0]),
702  (Float_t) (X[1][0]+5*X[3][0]-w1*X[2][0]),
703  (Float_t) (X[1][0]+lambdaMin*X[3][0]-rangeMin*X[2][0]),
704  (Float_t) (X[1][0]+lambdaMin*X[3][0]+rangeMin*X[2][0])};
705  Float_t x[4] = {(Float_t) (X[0][0]+5*X[2][0]-w1*X[3][0]),
706  (Float_t) (X[0][0]+5*X[2][0]+w1*X[3][0]),
707  (Float_t) (X[0][0]+lambdaMin*X[2][0]+rangeMin*X[3][0]),
708  (Float_t) (X[0][0]+lambdaMin*X[2][0]-rangeMin*X[3][0])};
709  if(gHarpoDebug>2)
710  Info("FindNextClosest","%g %g %g %g %g %g",x[0],z[0],x[1],z[1],x[2],z[2]);
711  TPolyLine* triangle;
712  triangle = new TPolyLine(4,z,x,"FL");
713  triangle->SetLineColor(kBlack);
714  triangle->SetFillColor(color);
715  triangle->SetFillStyle(fill);
716  fCanvas[plane]->cd();
717  triangle->Draw("FL");
718 
719  fHist[plane]->GetXaxis()->SetRangeUser(zMin-20,zMin+20);
720  fHist[plane]->GetYaxis()->SetRangeUser(xMin-20,xMin+20);
721  fCanvas[plane]->Update();
722  gSystem->Sleep(100);
723  }
724 
725  return iclMin;
726 
727 }
728 
729 
730 
732 {
733 
734  return 5;
735 
736  if((cl->GetQuality() & 3) == 3) return 2;
737  if(cl->GetQuality() & 4) return 1;
738  if(cl->GetQuality() & 3) return 1;//0.5;
739 
740 
741  if(cl->GetType() == CCLUSTER) return 1;
742  if(cl->GetType() == TCLUSTER) return 0.2;
743 
744  return 10;
745 }
746 
747 
749  {
750 
751  nEvents = 0;
752 
753  // Initialise histograms here
754 
755  fCanvas[0] = NULL;
756  fCanvas[1] = NULL;
757  fHist[0] = NULL;
758  fHist[1] = NULL;
759 
760  fLambdaMin = 5;
761  fLambdaMax = 50;
762  fNclMin = 10;
763  fNclMin2 = 15;
764  fMaxSlopeType = 1.5;
765  fResKalman = 0.2;
766  fScatKalman = 0.1;
767  fResFinder = 1;
768  fScatFinder = 3;//2;
769 
770  fQmin = 200;
771  fWidthMinT = 2;
772  fWidthMinC = 5;
773  fWidthMax = 30;
774 
775  for(Int_t plane = 0; plane<2; plane++){
776  for(Int_t i = 0; i<NTRACK; i++){
777  fGraph[plane][i] = 0;
778  fGraph2[plane][i] = 0;
779  }
780  }
781 
782 
783  // fNtr = 0;
784  hNcl = new TH1F("hNcl","hNcl",500,0,500);
785 
786  const Int_t nbinsDist = 500;
787  Double_t xminDist = 1e-6;
788  Double_t xmaxDist = 1e5;
789  Double_t logxminDist = TMath::Log(xminDist);
790  Double_t logxmaxDist = TMath::Log(xmaxDist);
791  Double_t binwidthDist = (logxmaxDist-logxminDist)/nbinsDist;
792  Double_t xbinsDist[nbinsDist+1];
793  xbinsDist[0] = xminDist;
794  for (Int_t i=1;i<=nbinsDist;i++)
795  xbinsDist[i] = TMath::Exp(logxminDist+i*binwidthDist);
796  hChi2 = new TH1F("hChi2","hChi2",nbinsDist,xbinsDist);
797 
798 
799  const Int_t nbinsTheta = 100;
800  Double_t xminTheta = 1e-5;
801  Double_t xmaxTheta = 2;
802  Double_t logxminTheta = TMath::Log(xminTheta);
803  Double_t logxmaxTheta = TMath::Log(xmaxTheta);
804  Double_t binwidthTheta = (logxmaxTheta-logxminTheta)/nbinsTheta;
805  Double_t xbinsTheta[nbinsTheta+1];
806  xbinsTheta[0] = xminTheta;
807  for (Int_t i=1;i<=nbinsTheta;i++)
808  xbinsTheta[i] = TMath::Exp(logxminTheta+i*binwidthTheta);
809 
810  hDistTracks = new TH1F("hDistTracks","",nbinsDist,xbinsDist);
811  hDistMinTracks = new TH1F("hDistMinTracks","",nbinsDist,xbinsDist);
812  hThetaTracks = new TH1F("hThetaTracks","",nbinsTheta,xbinsTheta);//,100,-1,1);
813  // hThetaTracksNtr = new TH2F("hThetaTracksNtr","",NTRACK,0,NTRACK,100,-1,1);
814  hThetaTracksNtr = new TH2F("hThetaTracksNtr","",nbinsDist,xbinsDist,nbinsTheta,xbinsTheta);
815  for(Int_t i = 0; i<10; i++)
816  hThetaDist[i] = new TH2F(Form("hThetaDist%d",i),"",nbinsDist,xbinsDist,nbinsTheta,xbinsTheta);
817 
818 
819  const Int_t nbinsQ = 100;
820  Double_t xminQ = 1e-3;
821  Double_t xmaxQ = 1e3;
822  Double_t logxminQ = TMath::Log(xminQ);
823  Double_t logxmaxQ = TMath::Log(xmaxQ);
824  Double_t binwidthQ = (logxmaxQ-logxminQ)/nbinsQ;
825  Double_t xbinsQ[nbinsQ+1];
826  xbinsQ[0] = xminQ;
827  for (Int_t i=1;i<=nbinsQ;i++)
828  xbinsQ[i] = TMath::Exp(logxminQ+i*binwidthQ);
829  fHistQQtestVsDist = new TH2F("fHistQQtestVsDist","",nbinsQ,xbinsQ,nbinsDist,xbinsDist);
830  fHistQQmin = new TH2F("fHistQQmin","",nbinsQ,xbinsQ,nbinsDist,xbinsDist);
831 
832  }
833 
834 void HarpoKalmanNew::Save(char * /* mode */)
835  {
836 
837  // TString * hstFile = gHConfig->GetHistFile();
838  // if ( hstFile == NULL ) {
839  // std::cout << gHConfig->GetProgramName() << " " <<
840  // "No Hist File name given, use default" << std::endl;
841  // // const char* dir = gSystem->ExpandPathName("$HARPO_ANA_DIR/reco");
842  // const char* dir = "$HARPO_ANA_DIR/reco";
843  // if(gSystem->AccessPathName(dir))
844  // hstFile = new TString(Form("trackingKalman%lli.root",gHConfig->GetRunNo()));
845  // else
846  // hstFile = new TString(Form("%s/trackingKalman%lli.root",dir,gHConfig->GetRunNo()));
847  // // hstFile = new TString(Form("outputs/anaph%lli.root",gHConfig->GetRunNo()));
848  // }
849 
850  // TFile *hf = new TFile(hstFile->Data(),"RECREATE");
851  OpenHistFile("trackingKalman");
852  // Save histograms here
853  hDistTracks->Write();
854  hDistMinTracks->Write();
855  hThetaTracks->Write();
856  hThetaTracksNtr->Write();
857  for(Int_t i = 0; i<10; i++){
858  if(hThetaDist[i])
859  hThetaDist[i]->Write();
860  }
861  fHistQQtestVsDist->Write();
862  fHistQQmin->Write();
864  hChi2->Write();
865 
866  // hf->Close();
867  // printf("fNewFile %s closed\n", hstFile->Data() );
868 
869  }
870 
871 
872 
873 void HarpoKalmanNew::DisplayAnalysis(TRootEmbeddedCanvas* ecTab, TGListBox* /* infobox */ )
874 {
875 
876  TCanvas* cTab = ecTab->GetCanvas();
877  HarpoRecoEvent* reco = fEvt->GetRecoEvent();
878  TClonesArray* clArray = reco->GetClustersArray();
879  Int_t nCl = clArray->GetEntries();
880 
881  TGraph* g[2];
882  g[0] = new TGraph();
883  g[0]->SetMarkerStyle(7);
884  g[1] = new TGraph();
885  g[1]->SetMarkerStyle(7);
886  for(Int_t icl = 0; icl<nCl; icl++){
887  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(icl);
888  Int_t plane = cluster->GetPlane();
889  Double_t x = cluster->GetX();
890  Double_t z = cluster->GetZ();
891  g[plane]->SetPoint(g[plane]->GetN(),z,x);
892  }
893 
894  for(Int_t plane = 0; plane<2; plane++){
895  cTab->cd(plane+1);
896  if(g[plane]->GetN()>0) g[plane]->Draw("Psame");
897  for(Int_t i = 0; i<NTRACK; i++){
898  if(!fGraph[plane][i]) continue;
899  fGraph[plane][i]->SetMarkerStyle(24);
900  fGraph[plane][i]->SetMarkerColor(i+2);
901  fGraph[plane][i]->SetLineColor(i+2);
902  if(fGraph[plane][i]->GetN()>0) fGraph[plane][i]->Draw("LPsame");
903  fGraph2[plane][i]->SetMarkerStyle(6);
904  fGraph2[plane][i]->SetMarkerColor(i+2);
905  fGraph2[plane][i]->SetLineColor(i+2);
906  if(fGraph2[plane][i]->GetN()>5) fGraph2[plane][i]->Draw("LPsame");
907  }
908  }
909  cTab->Update();
910 }
911 
912 
913 
Double_t fScatKalman
#define NTRACK
void SetTr2Vertex(Int_t val)
TH1F * hNcl
Redefine empty default.
HarpoRecoEvent * GetRecoEvent()
Definition: HarpoEvent.h:46
TH2F * hThetaTracksNtr
#define NCHAN
Definition: HarpoDccMap.h:16
Double_t fWidthMax
Double_t fWidthMinT
Int_t FindClosestNeighbour(TMatrixD X, Int_t iTr, TClonesArray *clArray, Int_t plane, Int_t color, Int_t fill, Bool_t finder=kTRUE)
void SetTr1Vertex(Int_t val)
Double_t GetZvertex()
void AddIdClusterTrack(Int_t val)
Int_t GetNclCommon(TClonesArray *clArray, Int_t plane, Int_t iTr1, Int_t iTr2)
TGraph * fGraph2[2][NTRACK]
Double_t GetResolution(HarpoRecoClusters *cl)
Int_t reused[4000]
Double_t GetThetaVertex()
Int_t fTrack[4000]
Object containing the reconstruction information for one event (with array of HarpoRecoClusters Harpo...
Double_t fLambdaMax
#define BLOCCLUSTER
void AddTracks(HarpoRecoTracks *val)
2D vertex object, containing position, angle and associated track numbers, and quality info ...
TH2F * fHistQQtestVsDist
virtual void print()
FullEvent Header not scecific to the detectors The class is ....
Double_t GetPzVertex()
void NormaliseBins(TH1 *h)
TClonesArray * GetVertexArray()
virtual void print()
HarpoRecoTracks object, Obtained with Kalman filter.
void print()
Ovreloaded medod whic do all job.
Cluster object, containing position, charge and quality information.
TFile * OpenHistFile(const char *ananame)
Int_t used[4000]
unpacked dcc data The class contains the data map for DCC or Feminos The data is stored as a 2D TMatr...
Definition: HarpoDccMap.h:29
Double_t fMaxSlopeType
void Save(char *mode=NULL)
Double_t fResKalman
Int_t fStartIndex[NTRACK]
Double_t fWidthMinC
Double_t fLambdaMin
void DisplayAnalysis(TRootEmbeddedCanvas *ecTab, TGListBox *infobox)
void SetXfit(Int_t i, Double_t val)
Double_t fResFinder
#define NADC
Definition: HarpoDccMap.h:18
#define KALMAN
std::vector< TMatrixD > NextStep(TMatrixD X, TMatrixD C, Int_t Ntr, TClonesArray *clArray, TArrayI *arr, Int_t ncl, Int_t plane, Int_t color, Int_t fill, Int_t smooth, Int_t q, Bool_t finder=kTRUE)
#define TCLUSTER
Long64_t gHarpoDebug
Definition: HarpoDebug.cxx:9
TVirtualPad * fCanvas[2]
HarpoEventHeader * GetHeader()
Definition: HarpoEvent.cxx:80
HarpoEvent * fEvt
Definition: HarpoAnalyse.h:70
unsigned int res
Definition: Pmm2Status.h:428
Double_t GetPxVertex()
TH2F * hThetaDist[10]
const ULong_t gkNDetectors
Definition: HarpoDet.h:14
#define CCLUSTER
HarpoDccMap * GetDccMap(Long_t plane=XDCC)
Definition: HarpoEvent.cxx:108
Double_t GetXvertex()
void SetZfit(Int_t i, Double_t val)
TGraph * fGraph[2][NTRACK]
TClonesArray * GetClustersArray()
Double_t fScatFinder
void SetTrackType(Int_t val)
Bool_t CheckIdClusterTrack(Int_t val)
Int_t InitPlane(TClonesArray *clArray, Int_t plane)