]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveDet/AliEveTRDData.cxx
2489f50dc83f5144327021b1d0b31661e3f0a95b
[u/mrichter/AliRoot.git] / EVE / EveDet / AliEveTRDData.cxx
1 // $Id$
2 // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3
4 /**************************************************************************
5  * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6  * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for          *
7  * full copyright notice.                                                 *
8  **************************************************************************/
9
10 #include "TVector.h"
11 #include "TLinearFitter.h"
12
13 #include "TEveTrans.h"
14 #include "TEveManager.h"
15
16 #include "EveBase/AliEveEventManager.h"
17
18 #include "AliEveTRDData.h"
19 #include "AliEveTRDModuleImp.h"
20 #include "AliEveTRDLoader.h"
21 #include "AliEveTRDLoaderImp.h"
22
23 #include "AliLog.h"
24 #include "AliPID.h"
25 #include "AliTrackPointArray.h"
26 #include "AliRieman.h"
27
28 #include "AliTRDhit.h"
29 #include "AliTRDcluster.h"
30 #include "AliTRDseedV1.h"
31 #include "AliTRDtrackV1.h"
32 #include "AliTRDtrackerV1.h"
33 #include "AliTRDpadPlane.h"
34 #include "AliTRDdigitsManager.h"
35 #include "AliTRDdataArrayDigits.h"
36 #include "AliTRDarrayADC.h"
37 #include "AliTRDSignalIndex.h"
38 #include "AliTRDgeometry.h"
39 #include "AliTRDtransform.h"
40 #include "AliTRDReconstructor.h"
41 #include "AliTRDrecoParam.h"
42
43 ClassImp(AliEveTRDHits)
44 ClassImp(AliEveTRDDigits)
45 ClassImp(AliEveTRDClusters)
46 ClassImp(AliEveTRDTracklet)
47 ClassImp(AliEveTRDTrack)
48
49 ///////////////////////////////////////////////////////////
50 /////////////   AliEveTRDDigits       /////////////////////
51 ///////////////////////////////////////////////////////////
52
53 //______________________________________________________________________________
54 AliEveTRDDigits::AliEveTRDDigits(AliEveTRDChamber *p) :
55   TEveQuadSet("digits", ""), fParent(p), fBoxes(), fData()
56 {
57   // Constructor.
58 }
59
60 //______________________________________________________________________________
61 AliEveTRDDigits::~AliEveTRDDigits()
62 {
63 //  AliInfo(GetTitle());
64 }
65
66 //______________________________________________________________________________
67 void AliEveTRDDigits::ComputeRepresentation()
68 {
69   // Calculate digits representation according to user settings. The
70   // user can set the following parameters:
71   // - digits scale (log/lin)
72   // - digits threshold
73   // - digits apparence (quads/boxes)
74
75   if(!fData.HasData()){
76     return;
77   }
78
79   TEveQuadSet::Reset(TEveQuadSet::kQT_RectangleYZ, kTRUE, 64);
80
81   Double_t scale, dy, dz;
82   Int_t q, color;
83   Int_t nrows = fData.GetNrow(),
84         ncols = fData.GetNcol(),
85         ntbs  = fData.GetNtime(),
86         det   = fParent->GetID();
87   Float_t threshold = fParent->GetDigitsThreshold();
88
89   AliTRDtransform transform(det);
90   AliTRDgeometry *geo = fParent->fGeo;
91   AliTRDpadPlane *pp = geo->GetPadPlane(geo->GetLayer(det), geo->GetStack(det));
92
93   // express position in tracking coordinates
94   fData.Expand();
95   for (Int_t ir = 0; ir < nrows; ir++) {
96     dz = pp->GetRowSize(ir);
97     for (Int_t ic = 0; ic < ncols; ic++) {
98       dy = pp->GetColSize(ic);
99       for (Int_t it = 0; it < ntbs; it++) {
100         q = fData.GetDataUnchecked(ir, ic, it);
101         if (q < threshold) continue;
102
103         Double_t x[6] = {0., 0., Double_t(q), 0., 0., 0.}; 
104         Int_t  roc[3] = {ir, ic, 0}; 
105         Bool_t    out = kTRUE;
106         transform.Transform(&x[0], &roc[0], UInt_t(it), out, 0);
107
108         scale = q < 512 ? q/512. : 1.;
109         color  = 50+int(scale*50.);
110     
111         AddQuad(x[1]-.45*dy, x[2]-.5*dz*scale, x[0], .9*dy, dz*scale);
112         QuadValue(q);
113         QuadColor(color);
114         QuadId(new TNamed(Form("Charge%d", q), "dummy title"));
115       }  // end time loop
116     }  // end col loop
117   }  // end row loop
118   fData.Compress(1);
119   
120   // rotate to global coordinates
121   //RefitPlex();
122   TEveTrans& t = RefMainTrans();
123   t.SetRotByAngles((geo->GetSector(det)+.5)*AliTRDgeometry::GetAlpha(), 0.,0.);
124 }
125
126 //______________________________________________________________________________
127 void AliEveTRDDigits::SetData(AliTRDdigitsManager *digits)
128 {
129   // Set data source.
130
131   Int_t det = fParent->GetID();
132   AliTRDarrayADC *data = digits->GetDigits(det);
133   if(!data->GetDim()) return;
134   data->Expand();
135
136   AliTRDSignalIndex *indexes = digits->GetIndexes(det);
137   if(!indexes->IsAllocated()) digits->BuildIndexes(det);
138
139   if(!fData.HasData()) fData.Allocate(data->GetNrow(), data->GetNcol(), data->GetNtime());
140   fData.Expand();
141
142   Int_t row, col, time, adc;
143   indexes->ResetCounters();
144   while (indexes->NextRCIndex(row, col)){
145     indexes->ResetTbinCounter();
146     while (indexes->NextTbinIndex(time)){
147       if(data->IsPadCorrupted(row, col, time)){
148         // we should mark this position
149         break;
150       }
151       adc = data->GetData(row, col, time);
152       if(adc <= 1) continue;
153       fData.SetDataUnchecked(row, col, time, adc);
154       //fIndex->AddIndexTBin(row,col,time);
155       //printf("\tr[%d] c[%d] t[%d] ADC[%d]\n", row, col, time, adc);
156     } 
157   }
158   fData.Compress(1);
159 }
160
161
162 //______________________________________________________________________________
163 void AliEveTRDDigits::Paint(Option_t *option)
164 {
165   // Paint the object.
166
167   if(fParent->GetDigitsBox()) fBoxes.Paint(option);
168   else TEveQuadSet::Paint(option);
169 }
170
171 //______________________________________________________________________________
172 void AliEveTRDDigits::Reset()
173 {
174   // Reset raw and visual data.
175
176   TEveQuadSet::Reset(TEveQuadSet::kQT_RectangleYZ, kTRUE, 64);
177   // MT fBoxes.fBoxes.clear();
178   fData.Reset();
179 }
180
181 ///////////////////////////////////////////////////////////
182 /////////////   AliEveTRDHits         /////////////////////
183 ///////////////////////////////////////////////////////////
184
185 //______________________________________________________________________________
186 AliEveTRDHits::AliEveTRDHits() : TEvePointSet("hits", 20)
187 {
188   // Constructor.
189   SetMarkerSize(.1);
190   SetMarkerColor(kGreen);
191   SetOwnIds(kTRUE);
192 }
193
194 //______________________________________________________________________________
195 AliEveTRDHits::~AliEveTRDHits()
196 {
197   //AliInfo(GetTitle());
198 }
199
200 //______________________________________________________________________________
201 void AliEveTRDHits::PointSelected(Int_t n)
202 {
203   // Handle an individual point selection from GL.
204
205   AliTRDhit *h = 0x0;
206   if(!(h = dynamic_cast<AliTRDhit*>(GetPointId(n)))) return;
207   printf("Id[%3d] Det[%3d] Reg[%c] TR[%c] Q[%3d] MC[%d] t[%f]\n", 
208     n, h->GetDetector(), 
209     h->FromAmplification() ? 'A' : 'D', 
210     h->FromTRphoton() ? 'y' : 'n', 
211     h->GetCharge(), h->GetTrack(), h->GetTime());
212 }
213
214
215 ///////////////////////////////////////////////////////////
216 /////////////   AliEveTRDClusters         /////////////////////
217 ///////////////////////////////////////////////////////////
218
219 //______________________________________________________________________________
220 AliEveTRDClusters::AliEveTRDClusters():AliEveTRDHits()
221 {
222   // Constructor.
223   SetName("clusters");
224
225   SetMarkerSize(.4);
226   SetMarkerStyle(24);
227   SetMarkerColor(kGray);
228   SetOwnIds(kTRUE);
229 }
230
231 //______________________________________________________________________________
232 void AliEveTRDClusters::PointSelected(Int_t n)
233 {
234   // Handle an individual point selection from GL.
235
236   AliTRDcluster *c = dynamic_cast<AliTRDcluster*>(GetPointId(n));
237   printf("\nDetector             : %d\n", c->GetDetector());
238   printf("Charge               : %f\n", c->GetQ());
239   printf("Sum S                : %4.0f\n", c->GetSumS());
240   printf("Time bin             : %d\n", c->GetLocalTimeBin());
241   printf("Signals              : ");
242   Short_t *cSignals = c->GetSignals();
243   for(Int_t ipad=0; ipad<7; ipad++) printf("%d ", cSignals[ipad]); printf("\n");
244   printf("Central pad          : %d\n", c->GetPadCol());
245   printf("MC track labels      : ");
246   for(Int_t itrk=0; itrk<3; itrk++) printf("%d ", c->GetLabel(itrk)); printf("\n");
247   // Bool_t     AliCluster::GetGlobalCov(Float_t* cov) const
248   // Bool_t     AliCluster::GetGlobalXYZ(Float_t* xyz) const
249   // Float_t    AliCluster::GetSigmaY2() const
250   // Float_t    AliCluster::GetSigmaYZ() const
251   // Float_t    AliCluster::GetSigmaZ2() const
252 }
253
254 //______________________________________________________________________________
255 void AliEveTRDClusters::Print(Option_t *o) const
256 {
257   AliTRDcluster *c = 0x0;
258
259   for(Int_t n = GetN(); n--;){
260     if(!(c = dynamic_cast<AliTRDcluster*>(GetPointId(n)))) continue;
261     c->Print(o);
262   }
263 }
264
265 //______________________________________________________________________________
266 void AliEveTRDClusters::Load(Char_t *w, Bool_t stk) const
267 {
268   Int_t typ = -1;
269   if(strcmp(w, "hit")==0) typ = 0;
270   else if(strcmp(w, "dig")==0) typ = 1;
271   else if(strcmp(w, "cls")==0) typ = 2;
272   else if(strcmp(w, "all")==0) typ = 3;
273   else{
274     AliInfo("The following arguments are accepted:");
275     AliInfo("   \"hit\" : loading of MC hits");
276     AliInfo("   \"dig\" : loading of digits");
277     AliInfo("   \"cls\" : loading of reconstructed clusters");
278     AliInfo("   \"all\" : loading of MC hits+digits+clusters");
279     return;
280   }
281
282   AliTRDcluster *c = 0x0;
283   Int_t n = 0;
284   while((n = GetN() && !(c = dynamic_cast<AliTRDcluster*>(GetPointId(n))))) n++;
285   if(!c) return;
286
287   Int_t det = c->GetDetector();
288   AliEveTRDLoader *loader = 0x0;
289   switch(typ){
290   case 0:  
291     loader = new AliEveTRDLoader("Hits");
292     if(!loader->Open("TRD.Hits.root")){ 
293       delete loader;
294       return;
295     }
296     loader->SetDataType(AliEveTRDLoader::kTRDHits);
297     break;
298   case 1:
299     loader = new AliEveTRDLoader("Digits");
300     if(!loader->Open("TRD.Digits.root")){ 
301       delete loader;
302       return;
303     }
304     loader->SetDataType(AliEveTRDLoader::kTRDDigits);
305     break;
306   case 2:
307     loader = new AliEveTRDLoader("Clusters");
308     if(!loader->Open("TRD.RecPoints.root")){ 
309       delete loader;
310       return;
311     }
312     loader->SetDataType(AliEveTRDLoader::kTRDClusters);
313     break;
314   case 3:
315     loader = new AliEveTRDLoaderSim("MC");
316     if(!loader->Open("galice.root")){ 
317       delete loader;
318       return;
319     }
320     loader->SetDataType(AliEveTRDLoader::kTRDHits | AliEveTRDLoader::kTRDDigits | AliEveTRDLoader::kTRDClusters);
321     break;
322   default: return;
323   }
324
325   loader->AddChambers(AliTRDgeometry::GetSector(det),AliTRDgeometry::GetStack(det), stk ? -1 : AliTRDgeometry::GetLayer(det));
326   // load first event
327   loader->GoToEvent(AliEveEventManager::GetCurrent()->GetEventId());
328   
329   // register loader with alieve
330   gEve->AddElement(loader);
331   //loader->SpawnEditor();
332   gEve->Redraw3D();
333 }
334
335 ///////////////////////////////////////////////////////////
336 /////////////   AliEveTRDTracklet         /////////////////////
337 ///////////////////////////////////////////////////////////
338
339 //______________________________________________________________________________
340 AliEveTRDTracklet::AliEveTRDTracklet(AliTRDseedV1 *trklt):TEveLine()
341   ,fClusters(0x0)
342 {
343   // Constructor.
344   SetName("tracklet");
345   
346   SetUserData(trklt);
347   Float_t dx;
348   Float_t x0   = trklt->GetX0();
349   Float_t y0   = trklt->GetYref(0);
350   Float_t z0   = trklt->GetZref(0);
351   Float_t dydx = trklt->GetYref(1);
352   Float_t dzdx = trklt->GetZref(1);
353   Float_t tilt = trklt->GetTilt();
354   Float_t g[3];
355   AliTRDcluster *c = 0x0;
356   for(Int_t ic=0; ic<AliTRDseed::knTimebins; ic++){
357     if(!(c = trklt->GetClusters(ic))) continue;
358     if(!fClusters) AddElement(fClusters = new AliEveTRDClusters());
359     dx = x0 - c->GetX();
360     //Float_t yt = y0 - dx*dydx;
361     Float_t zt = z0 - dx*dzdx;
362     // backup yc - for testing purposes
363     Float_t yc = c->GetY(); 
364     c->SetY(yc-tilt*(c->GetZ()-zt));
365     c->GetGlobalXYZ(g); 
366     Int_t id = fClusters->SetNextPoint(g[0], g[1], g[2]);    
367     //Int_t id = fClusters->SetNextPoint(c->GetX(), c->GetY(), c->GetZ());    
368     c->SetY(yc);
369     fClusters->SetPointId(id, new AliTRDcluster(*c));
370   } 
371   if(fClusters){
372     fClusters->SetTitle(Form("N[%d]", trklt->GetN2()));
373     fClusters->SetMarkerColor(kMagenta);
374   }
375
376   SetTitle(Form("Det[%d] Plane[%d] P[%7.3f]", trklt->GetDetector(), trklt->GetPlane(), trklt->GetMomentum()));
377   SetLineColor(kRed);
378   //SetOwnIds(kTRUE);
379   
380   // init tracklet line
381   Int_t sec = AliTRDgeometry::GetSector(trklt->GetDetector());
382   Double_t alpha = AliTRDgeometry::GetAlpha() * (sec<9 ? sec + .5 : sec - 17.5); 
383
384   trklt->Fit(kTRUE);
385   y0   = trklt->GetYfit(0);
386   dydx = trklt->GetYfit(1);
387   Double_t xg =  x0 * TMath::Cos(alpha) - y0 * TMath::Sin(alpha); 
388   Double_t yg = x0 * TMath::Sin(alpha) + y0 * TMath::Cos(alpha);
389   SetPoint(0, xg, yg, z0); 
390   //SetPoint(0, x0, y0, z0);
391
392
393   dx = .5*AliTRDgeometry::CamHght()+AliTRDgeometry::CdrHght();
394   x0 -= dx; 
395   y0 -= dydx*dx,
396   z0 -= dzdx*dx; 
397   xg = x0 * TMath::Cos(alpha) - y0 * TMath::Sin(alpha); 
398   yg = x0 * TMath::Sin(alpha) + y0 * TMath::Cos(alpha);
399   SetPoint(1, xg, yg, z0);
400   //SetPoint(1, x0, y0, z0);
401 }
402
403 //______________________________________________________________________________
404 void AliEveTRDTracklet::Print(Option_t *o) const
405 {
406   AliTRDseedV1 *tracklet = (AliTRDseedV1*)GetUserData();
407   if(!tracklet) return;
408   tracklet->Print(o);
409 }
410
411 ///////////////////////////////////////////////////////////
412 /////////////   AliEveTRDTrack         /////////////////////
413 ///////////////////////////////////////////////////////////
414
415 //______________________________________________________________________________
416 AliEveTRDTrack::AliEveTRDTrack(AliTRDtrackV1 *trk) 
417   :TEveLine()
418   ,fTrackState(0)
419   ,fESDStatus(0)
420   ,fAlpha(0.)
421   ,fPoints(0x0)
422   ,fRim(0x0)
423 {
424   // Constructor.
425   SetUserData(trk);
426   SetName("");
427
428   AliTRDtrackerV1::SetNTimeBins(24);
429
430
431   fRim = new AliRieman(trk->GetNumberOfClusters());
432   AliTRDseedV1 *tracklet = 0x0;
433   for(Int_t il=0; il<AliTRDgeometry::kNlayer; il++){
434     if(!(tracklet = trk->GetTracklet(il))) continue;
435     if(!tracklet->IsOK()) continue;
436     AddElement(new AliEveTRDTracklet(tracklet));
437
438     AliTRDcluster *c = 0x0;
439     tracklet->ResetClusterIter(kFALSE);
440     while((c = tracklet->PrevCluster())){
441       Float_t xc = c->GetX();
442       Float_t yc = c->GetY();
443       Float_t zc = c->GetZ();
444       Float_t zt = tracklet->GetZref(0) - (tracklet->GetX0()-xc)*tracklet->GetZref(1); 
445       yc -= tracklet->GetTilt()*(zc-zt);
446       fRim->AddPoint(xc, yc, zc, 1, 10);
447     }
448   }
449   fRim->Update();
450   SetStatus(fTrackState);
451 }
452
453 //______________________________________________________________________________
454 AliEveTRDTrack::~AliEveTRDTrack()
455 {
456   if(fPoints) delete [] fPoints; fPoints = 0x0;
457   //delete dynamic_cast<AliTRDtrackV1*>(GetUserData());
458 }
459
460 //______________________________________________________________________________
461 void AliEveTRDTrack::Print(Option_t *o) const
462 {
463   AliTRDtrackV1 *track = (AliTRDtrackV1*)GetUserData();
464   if(!track) return;
465   track->Print(o);
466 }
467
468 //______________________________________________________________________________
469 void AliEveTRDTrack::SetStatus(UChar_t s)
470 {
471   // nothing to be done
472   if(fPoints && fTrackState == s) return;
473
474   const Int_t nc = AliTRDtrackV1::kMAXCLUSTERSPERTRACK;
475   AliTRDtrackV1 *trk = (AliTRDtrackV1*)GetUserData();
476
477   Bool_t BUILD = kFALSE;
478   if(!fPoints){ 
479     fPoints = new AliTrackPoint[nc];
480
481     AliTRDcluster *c = trk->GetCluster(0);
482     Double_t x = c->GetX();
483     Int_t sec = c->GetDetector()/30;
484     fAlpha = AliTRDgeometry::GetAlpha() * (sec<9 ? sec + .5 : sec - 17.5); 
485
486     Double_t dx = (trk->GetCluster(trk->GetNumberOfClusters()-1)->GetX()-x)/nc;
487     for(Int_t ip=0; ip<nc; ip++){
488       fPoints[ip].SetXYZ(x, 0., 0.);
489       x+=dx;
490     }
491     BUILD = kTRUE;
492   }
493
494   // select track model
495   if(BUILD || ((s&12) != (fTrackState&12))){
496     if(TESTBIT(s, kTrackCosmics)){
497       //printf("Straight track\n");
498       AliTRDtrackerV1::FitLine(trk, 0x0, kFALSE, nc, fPoints);
499     } else {
500       if(TESTBIT(s, kTrackModel)){
501         //printf("Kalman track\n");
502         if(trk->GetNumberOfTracklets() >=4) AliTRDtrackerV1::FitKalman(trk, 0x0, kFALSE, nc, fPoints);
503       } else { 
504         //printf("Rieman track\n");
505         // if(trk->GetNumberOfTracklets() >=4) AliTRDtrackerV1::FitRiemanTilt(trk, 0x0, kTRUE, nc, fPoints);
506         Float_t x = 0.;
507         for(Int_t ip = nc; ip--;){
508           x = fPoints[ip].GetX();
509           fPoints[ip].SetXYZ(x, fRim->GetYat(x), fRim->GetZat(x));
510         }
511       }
512     }
513   
514     Float_t global[3];
515     for(Int_t ip=0; ip<nc; ip++){
516       fPoints[ip].Rotate(-fAlpha).GetXYZ(global);
517       SetPoint(ip, global[0], global[1], global[2]);
518     }
519     SetSmooth(kTRUE);
520   }
521
522   // set color
523   if(BUILD || ((s&3) != (fTrackState&3))){
524     if(TESTBIT(s, kSource)){
525       //printf("Source color\n");
526       if(fESDStatus&AliESDtrack::kTRDin){
527         SetMarkerColor(kGreen);
528         SetLineColor(kGreen);
529       } else {
530         SetMarkerColor(kMagenta);
531         SetLineColor(kMagenta);
532       }
533     } else {
534       if(TESTBIT(s, kPID) == AliTRDReconstructor::kLQPID){
535         //printf("PID color kLQPID\n");
536         //trk->GetReconstructor()->SetOption("!nn");
537       } else {
538         //printf("PID color kNNPID\n");
539         //trk->GetReconstructor()->SetOption("nn");
540       }
541       trk->CookPID();
542   
543       Int_t species = 0; Float_t pid = 0.;
544       for(Int_t is=0; is<AliPID::kSPECIES; is++) 
545         if(trk->GetPID(is) > pid){
546           pid = trk->GetPID(is);
547           species = is;
548         }
549       switch(species){
550       case AliPID::kElectron:
551         SetMarkerColor(kRed);
552         SetLineColor(kRed);
553         break;
554       default:
555         SetMarkerColor(kBlue);
556         SetLineColor(kBlue);
557         break;
558       }
559     }
560     SetLineWidth(2);
561   }
562   
563   const Char_t *model = "line";
564   if(!TESTBIT(s, kTrackCosmics)){
565     if(TESTBIT(s, kTrackModel)) model = "kalman";
566     else model = "rieman";
567   }
568   Int_t species = 0; Float_t pid = 0.;
569   for(Int_t is=0; is<AliPID::kSPECIES; is++) 
570     if(trk->GetPID(is) > pid){
571       pid = trk->GetPID(is);
572       species = is;
573     }
574
575   SetTitle(Form(
576     "Tracklets[%d] Clusters[%d]\n"
577     "Reconstruction Source[%s]\n"
578     "PID[%4.1f %4.1f %4.1f %4.1f %4.1f]\n"
579     "MC[%d]", trk->GetNumberOfTracklets(), trk->GetNumberOfClusters(), fESDStatus&AliESDtrack::kTRDin ? "barrel" : "sa",
580     1.E2*trk->GetPID(0), 1.E2*trk->GetPID(1),
581     1.E2*trk->GetPID(2), 1.E2*trk->GetPID(3), 1.E2*trk->GetPID(4), trk->GetLabel()));
582
583   if(GetName()){
584     char id[6]; strncpy(id, GetName(), 6); 
585     SetName(Form("%s %s", id, AliPID::ParticleName(species)));
586   }
587
588   // save track status
589   fTrackState = s;
590 }