- moved AliHLTDisplay files from TPCLib to TPCLib/OnlineDisplay
[u/mrichter/AliRoot.git] / HLT / TPCLib / OnlineDisplay / AliHLTTPCDisplay3D.cxx
1 // $Id$
2
3 /** \class AliHLTTPCDisplayPadRow
4 <pre>
5 //_____________________________________________________________
6 // AliHLTTPCDisplay3D
7 //
8 // Display class for the HLT TPC-3D events.
9 </pre>
10 */
11 // Author: Jochen Thaeder <mailto:thaeder@kip.uni-heidelberg.de>
12 //*-- Copyright &copy ALICE HLT Group 
13
14 #define TRACKHELIX 0       // use THelix for tracks
15 #define TRACKPOLYMARKER 0  // use TPolymarker3D for tracks
16 #define FIRSTLASTPOINT 0   // show first / last point of tracks
17
18 #define DRAWSTEP 0.2
19
20 #define UNUSEDCLUSTERCOLOR 2
21 #define USEDCLUSTERCOLOR 3
22 #define TRACKCOLOR 4
23 #define TRACKPOLYMARKERCOLOR 5
24 #define TRACKHELIXCOLOR 6
25
26 #include "AliHLTTPCDisplay3D.h"
27 #include "AliHLTTPCDisplayPadRow.h"
28
29 #include "AliHLTStdIncludes.h"
30 #include <TView.h>
31 #include <TPolyMarker3D.h>
32 #include <TPolyLine3D.h>
33 #include <TH2.h>
34 #include <TTree.h>
35 #include <TNode.h>
36 #include <TGeometry.h>
37 #include <TShape.h>
38 #include <TParticle.h>
39 #include <TFile.h>
40 #include <THelix.h>
41 #include <TStyle.h>
42 #include <TGraph.h>
43 #include <TMultiGraph.h>
44 #include <TAttText.h>
45 #include <TAxis.h>
46 #include <TCanvas.h>
47
48 #ifdef use_aliroot
49 #include <TClonesArray.h>
50 #include <AliRun.h>
51 #include <AliSimDigits.h>
52 #include <AliTPCParam.h>
53 #endif
54
55 #include "AliHLTTPCDefinitions.h"
56 #include "AliHLTDataTypes.h"
57 #include "AliHLTTPCSpacePointData.h"
58 #include "AliHLTTPCClusterDataFormat.h"
59 #include "AliHLTTPCTrackletDataFormat.h"
60
61
62 #include "AliHLTTPCDigitReader.h"
63 #include "AliHLT_C_Component_WrapperInterface.h"
64
65 #include "AliHLTTPCDisplayMain.h"
66
67 #include "AliHLTTPCLogging.h"
68 #include "AliHLTTPCDisplay.h"
69 #include "AliHLTTPCTransform.h"
70 #include "AliHLTTPCTrack.h"
71 #include "AliHLTTPCTrackArray.h"
72 #include "AliHLTTPCMemHandler.h"
73 #include "AliHLTTPCDigitReaderPacked.h"
74
75
76 #if __GNUC__ >= 3
77 using namespace std;
78 #endif
79
80 ClassImp(AliHLTTPCDisplay3D)
81
82 //____________________________________________________________________________________________________
83 AliHLTTPCDisplay3D::AliHLTTPCDisplay3D(AliHLTTPCDisplayMain* display, Char_t* gfile ) {
84     // constructor
85     fDisplay = display;
86     
87     fGeom = NULL;
88     LoadGeometrie(gfile);
89 }
90
91 //____________________________________________________________________________________________________
92 AliHLTTPCDisplay3D::~AliHLTTPCDisplay3D() {
93     // destructor   
94
95 }
96
97 //____________________________________________________________________________________________________
98 void AliHLTTPCDisplay3D::Save(){
99   fDisplay->GetCanvas3D()->SaveAs("HLT-3D-View.eps");
100 }
101
102 //____________________________________________________________________________________________________
103 void AliHLTTPCDisplay3D::Draw(){
104     fDisplay->GetCanvas3D()->cd();
105     fDisplay->GetCanvas3D()->Clear();
106
107     TView *v = new TView(1);
108     //    TView v(1);
109     v->SetRange(-800,-800,-800,800,800,800);
110
111     Float_t* etaRange = NULL;   // ------  STILL TO FIX
112
113
114     //--------------------------------------------------------------------------------------------
115     // DRAW 3D GEOMETRY
116     //--------------------------------------------------------------------------------------------
117     if (fDisplay->Get3DSwitchGeometry()){
118
119         TList* masterNodeList = fGeom->GetListOfNodes();
120         TNode* masterNode=0;
121         TIter next(masterNodeList);
122         
123         while ((masterNode = static_cast<TNode*> (next()))) {  
124         
125             TList* nodeList = masterNode->GetListOfNodes();
126             TNode* node=0;
127             TIter next(nodeList);
128             
129             while ((node = static_cast<TNode*> (next()))) {  
130                 
131                 ULong_t tmpslice = atol(node->GetName() + 2);
132
133                 if (fDisplay->GetDisplaySlice(tmpslice)) {
134                     node->SetVisibility(1);
135                     node->SetFillColor(0);
136                     node->SetLineColor(fDisplay->GetLineColor());
137                 }
138                 else node->SetVisibility(0);
139             } // end while son Nodes
140         } // end while master Node
141
142         fGeom->Draw("");
143     }   // END - DRAW 3D GEOMETRY
144
145     
146     //--------------------------------------------------------------------------------------------
147     // DRAW 3D CLUSTER
148     //--------------------------------------------------------------------------------------------
149     if (fDisplay->Get3DSwitchCluster() && fDisplay->ExistsClusterData()){
150
151         for (Int_t slice=0; slice <= 35; slice++){
152
153             Int_t currenttrack = -1;
154
155             if (fDisplay->ExistsTrackData() && fDisplay->GetSelectTrackSwitch()) currenttrack = fDisplay->GetGlobalTrack(slice);
156
157             if (!fDisplay->GetDisplaySlice(slice)) continue;
158             
159             for(Int_t patch=0;patch<6;patch++){
160
161                 AliHLTTPCSpacePointData *points = fDisplay->GetSpacePointDataPointer(slice,patch);
162                 if(!points) continue;
163
164                 TPolyMarker3D *pmUsed = new TPolyMarker3D(1,6);
165                 TPolyMarker3D *pmUnused = new TPolyMarker3D(1,6);
166                 pmUnused->SetBit(kCanDelete);
167                 pmUsed->SetBit(kCanDelete);
168
169
170                 Int_t nUsedCluster = 0;
171                 Int_t nUnusedCluster = 0;
172
173                 Float_t xyz[3];    
174                 for(Int_t i=0; i< fDisplay->GetNumberSpacePoints(slice,patch); i++){
175                     // Used  cluster only
176                     if (fDisplay->GetSelectCluster() == 1  && points[i].fUsed == kFALSE) continue; 
177                     // Unused cluster only
178                     if (fDisplay->GetSelectCluster() == 2  && points[i].fUsed == kTRUE) continue; 
179                     
180                     // if single track is selcted draw only cluster for this track
181                     if (fDisplay->GetSelectCluster() == 1 && fDisplay->GetSelectTrackSwitch() && points[i].fTrackN != currenttrack) continue;
182                     
183                     xyz[0] = points[i].fX;
184                     xyz[1] = points[i].fY;
185                     xyz[2] = points[i].fZ;
186                     
187                     if ( etaRange ){              
188                         // Do this before the transform, because the tracker also uses
189                         // local coordinates when using this limit to determine 
190                         // which clusters to use for tracking
191                         Double_t pointEta = AliHLTTPCTransform::GetEta( xyz );
192                         if ( pointEta<etaRange[0] || pointEta>etaRange[1] )
193                             continue;
194                     }
195                     
196                     AliHLTTPCTransform::Local2Global(xyz,slice);
197                     
198                     if (points[i].fUsed == kTRUE){
199                         pmUsed->SetPoint(nUsedCluster,xyz[0],xyz[1],xyz[2]);
200                         nUsedCluster++;
201                     }
202                     else {
203                         pmUnused->SetPoint(nUnusedCluster,xyz[0],xyz[1],xyz[2]);
204                         nUnusedCluster++;
205                     }
206                     
207                 }
208                 pmUsed->SetMarkerSize(1);
209                 pmUsed->SetMarkerColor(USEDCLUSTERCOLOR); 
210                 pmUsed->Draw("same");
211
212                 pmUnused->SetMarkerSize(1);
213                 pmUnused->SetMarkerColor(UNUSEDCLUSTERCOLOR); 
214                 pmUnused->Draw("same");
215
216                 fDisplay->GetCanvas3D()->Modified();
217                 fDisplay->GetCanvas3D()->Update();
218
219             } // END - PATCH LOOP           
220         }  // END - SLICE LOOP  
221     }   // END - DRAW 3D CLUSTER 
222
223     //--------------------------------------------------------------------------------------------
224     // DRAW 3D TRACKS
225     //--------------------------------------------------------------------------------------------
226     if (fDisplay->Get3DSwitchTracks() && fDisplay->ExistsTrackData()){
227
228       AliHLTTPCTransform::SetBField( 0.5 );  // ++++++
229
230
231         AliHLTTPCTrackArray* tracks = fDisplay->GetTrackArrayPointer();
232         Int_t ntracks = tracks->GetNTracks();
233
234         //      TPolyLine3D **line = new (TPolyLine3D*)[ntracks];
235         //      for(Int_t j=0; j<ntracks; j++) line[j] = 0;
236 #if TRACKHELIX
237         //      THelix **helix = new (THelix*)[ntracks];
238         //      for(Int_t j=0; j<ntracks; j++) helix[j] = 0;
239
240 #endif
241         for(Int_t j=0; j<ntracks; j++) {        
242
243             AliHLTTPCTrack *gtrack = tracks->GetCheckedTrack(j); 
244             if(!gtrack) continue;
245
246             Int_t nHits = gtrack->GetNHits();  // Number of associated hits to track
247             Int_t slice = gtrack->GetSector();
248
249             // --- CHECK if track is should be drawn
250             // select if slice should be displayed or not
251             if (!fDisplay->GetDisplaySlice(slice)) continue;
252
253             if (fDisplay->GetSelectTrackSwitch() && fDisplay->GetGlobalTrack(slice) != j) continue;
254
255             Double_t radius = gtrack->GetRadius();      // radius
256             Double_t kappa = gtrack->GetKappa();        // curvature = 1/R , signed
257             Double_t lambda = atan( gtrack->GetTgl() ); // dipAngle lambda
258             Double_t phi0 = gtrack->GetPsi() + (gtrack->GetCharge() * AliHLTTPCTransform::PiHalf() ); // azimuthal angle of startingpoint, with respect to helix axis
259
260             if (kappa == 0 && AliHLTTPCTransform::GetBFieldValue() > 0.) {
261                 printf("================================KAPPA == 0");
262                 continue;
263             }
264
265             Double_t xyzL[3];      // lastpoint of track
266             Double_t xyzF[3];      // firstpoint of track
267
268             xyzF[0] = gtrack->GetFirstPointX();
269             xyzF[1] = gtrack->GetFirstPointY();
270             xyzF[2] = gtrack->GetFirstPointZ();
271
272             xyzL[0] = gtrack->GetLastPointX();
273             xyzL[1] = gtrack->GetLastPointY();
274             xyzL[2] = gtrack->GetLastPointZ();
275
276 #if FIRSTLASTPOINT          
277             //      TPolyMarker3D *pmL = new TPolyMarker3D(1,2);
278             //TPolyMarker3D *pmF = new TPolyMarker3D(1,2);
279
280             TPolyMarker3D pmL(1,2);
281             TPolyMarker3D pmF(1,2);
282
283
284             pmF.SetPoint(0,xyzF[0],xyzF[1],xyzF[2]);
285             pmL.SetPoint(0,xyzL[0],xyzL[1],xyzL[2]);
286 #endif
287
288             Double_t s = 0.;       // length of the track
289
290
291             if (  AliHLTTPCTransform::GetBFieldValue() == 0.) 
292                 s = sqrt ( (xyzL[0] - xyzF[0])*(xyzL[0] - xyzF[0]) + (xyzL[1] - xyzF[1])*(xyzL[1] - xyzF[1]) ); 
293             else {
294                 // Calculate the length of the track. If it is to flat in in s,z plane use sxy, otherwise use sz
295                 if (fabs(lambda) > 0.05){
296                     // length of track calculated out of z
297                     s = fabs( (xyzL[2] - xyzF[2]) / sin(lambda) ); // length of track calculated out of z
298                 }
299                 else {
300                     Double_t d = (xyzL[0] - xyzF[0])*(xyzL[0] - xyzF[0]) + (xyzL[1] - xyzF[1])*(xyzL[1] - xyzF[1]); 
301                     // length of track calculated out of xy
302                     s = fabs ( acos( 0.5 * (2 - (d / (radius*radius)))) / ( kappa * cos(lambda) ) );            
303                 }
304             }
305
306
307             // CUTS on tracks
308 /*
309             if (nHits < fDisplay->GetCutHits() ) continue;
310             if (s < fDisplay->GetCutS() ) continue;
311             if (gtrack->GetPsi() < fDisplay->GetCutPsi() ) continue;
312             if (lambda < fDisplay->GetCutLambda() ) continue; 
313             if (gtrack->GetPt() < fDisplay->GetCutPt()  &&  AliHLTTPCTransform::GetBFieldValue() != 0. ) continue;
314             if ( AliHLTTPCTransform::GetPadRow((Float_t)xyzF[0]) >   fDisplay->GetIncidentPadrow() ) continue;
315 */    
316             Int_t nTrackPoints = 2 + (Int_t) floor(s / DRAWSTEP);
317                             
318 #if TRACKPOLYMARKER
319             //    TPolyMarker3D *pmT = new TPolyMarker3D(nTrackPoints,6);
320             TPolyMarker3D pmT(nTrackPoints,6);
321
322 #endif
323             Double_t *xT = new Double_t[nTrackPoints];
324             Double_t *yT = new Double_t[nTrackPoints];
325             Double_t *zT = new Double_t[nTrackPoints];
326
327             Int_t trackPointCounter = 0;
328
329             //Write Track Parameters for single track
330             if (fDisplay->GetSelectTrackSwitch() ){
331                 fDisplay->fTrackParam.id = j;
332                 fDisplay->fTrackParam.nHits = nHits;
333                 fDisplay->fTrackParam.charge = gtrack->GetCharge();
334                 fDisplay->fTrackParam.lambda = lambda;
335                 fDisplay->fTrackParam.kappa = kappa;
336                 fDisplay->fTrackParam.radius = radius;
337                 fDisplay->fTrackParam.slice = slice;
338                 fDisplay->fTrackParam.phi0 = phi0;
339                 fDisplay->fTrackParam.pt = gtrack->GetPt();
340                 fDisplay->fTrackParam.bfield = AliHLTTPCTransform::GetBFieldValue();
341                 fDisplay->fTrackParam.psi = gtrack->GetPsi();
342                 fDisplay->fTrackParam.s = s;
343             }
344
345
346             if (  AliHLTTPCTransform::GetBFieldValue() == 0.) {
347
348                 for (Double_t ds = 0.; ds < s; ds = ds + DRAWSTEP){
349                     // FILL ARRAYS IN ORDER TO DRAW THE TRACKPOINTS, OUT OF THE PARAMETER
350                     xT[trackPointCounter] = xyzF[0] + ds * cos(phi0); 
351                     yT[trackPointCounter] = xyzF[1] + ds * sin(phi0);
352                     zT[trackPointCounter] = xyzF[2] + ds * sin(lambda);
353 #if TRACKPOLYMARKER
354                     pmT.SetPoint(trackPointCounter,xT[trackPointCounter],yT[trackPointCounter],zT[trackPointCounter]);
355 #endif
356                     trackPointCounter++;
357                 }
358                 
359                 if (trackPointCounter > nTrackPoints) printf("N=%d  n=%d", nTrackPoints,trackPointCounter);
360                 else {
361                     xT[trackPointCounter] = xyzF[0] + s * cos(phi0); 
362                     yT[trackPointCounter] = xyzF[1] + s * sin(phi0);
363                     zT[trackPointCounter] = xyzF[2] + s * sin(lambda);
364 #if TRACKPOLYMARKER       
365                     pmT.SetPoint(trackPointCounter,xT[trackPointCounter],yT[trackPointCounter],zT[trackPointCounter]);
366 #endif
367                 }
368
369             }
370             else {
371
372                 for (Double_t ds = 0.; ds < s; ds = ds + DRAWSTEP){
373                     // FILL ARRAYS IN ORDER TO DRAW THE TRACKPOINTS, OUT OF THE PARAMETER
374                     xT[trackPointCounter] = xyzF[0] + radius * ( cos( phi0 + (ds*kappa*cos(lambda)) ) - cos(phi0) );
375                     yT[trackPointCounter] = xyzF[1] + radius * ( sin( phi0 + (ds*kappa*cos(lambda)) ) - sin(phi0) );
376                     zT[trackPointCounter] = xyzF[2] + ds * sin(lambda);
377 #if TRACKPOLYMARKER
378                     pmT.SetPoint(trackPointCounter,xT[trackPointCounter],yT[trackPointCounter],zT[trackPointCounter]);
379 #endif
380                     trackPointCounter++;
381                 }
382                 
383                 if (trackPointCounter > nTrackPoints) printf("N=%d  n=%d", nTrackPoints,trackPointCounter);
384                 else {
385                     xT[trackPointCounter] = xyzF[0] + radius * ( cos( phi0 + (s*kappa*cos(lambda)) ) - cos(phi0) );
386                     yT[trackPointCounter] = xyzF[1] + radius * ( sin( phi0 + (s*kappa*cos(lambda)) ) - sin(phi0) );
387                     zT[trackPointCounter] = xyzF[2] + s * sin(lambda);
388 #if TRACKPOLYMARKER       
389                     pmT.SetPoint(trackPointCounter,xT[trackPointCounter],yT[trackPointCounter],zT[trackPointCounter]);
390 #endif
391                 }
392             }
393
394             // Draw Track -- as line
395             //line[j] = new TPolyLine3D(nTrackPoints,xT,yT,zT,"");
396             //TPolyLine3D *currentline = line[j];
397             //* currentline = new TPolyLine3D(nTrackPoints,xT,yT,zT,"");
398             //      TPolyLine3D currentline(nTrackPoints,xT,yT,zT,"");
399
400             TPolyLine3D *currentline = new TPolyLine3D(nTrackPoints,xT,yT,zT,"");
401             currentline->SetBit(kCanDelete);
402             currentline->SetLineColor(TRACKCOLOR);   
403             currentline->SetLineWidth(2);
404             currentline->Draw("same");
405
406
407
408             // ---  ADDITIONAL DRAW OPTIONS
409 #if FIRSTLASTPOINT
410             // Draw last point of Track
411             pmL.SetMarkerSize(3);
412             pmL.SetMarkerColor(4); 
413             pmL.Draw();
414
415             // Draw first point of Track
416             pmF.SetMarkerSize(3);
417             pmF.SetMarkerColor(5); 
418             pmF.Draw();
419 #endif
420 #if TRACKPOLYMARKER
421             // Draw Track -- as polymarker
422             pmT.SetMarkerSize(3);
423             pmT.SetMarkerColor(TRACKPOLYMARKERCOLOR); 
424             pmT.Draw();
425 #endif
426 #if TRACKHELIX
427             // Draw Track -- as helix
428             // works ok, execpt for very small dipangles -> track almost horizontal
429             Double_t hrange[2];
430             Double_t v0[3];
431             Double_t omega;
432             hrange[0] = xyzF[2];
433             hrange[1] = xyzL[2];
434             v0[0] = gtrack->GetPx();
435             v0[1] = gtrack->GetPy();
436             v0[2] = gtrack->GetPz();
437             omega = AliHLTTPCTransform::GetBFieldValue() * gtrack->GetCharge();
438
439             //      helix[j] = new THelix(xyzF,v0,omega,hrange,kHelixZ,0);
440             //      THelix *currenthelix = helix[j];
441             //      currenthelix = new THelix(xyzF,v0,omega,hrange,kHelixZ,0);
442             THelix currenthelix(xyzF,v0,omega,hrange,kHelixZ,0);
443             currenthelix.SetLineColor(TRACKHELIXCOLOR);   
444             currenthelix.SetLineWidth(1);
445             currenthelix.Draw("same");              
446 #endif
447
448
449             // delete[]
450             //Double_t *xT = new Double_t[nTrackPoints];
451             //      Double_t *yT = new Double_t[nTrackPoints];
452             //      Double_t *zT = new Double_t[nTrackPoints];
453             if (xT){ 
454               delete[] xT; 
455               xT = NULL;
456             }
457             if (yT){ 
458               delete[] yT; 
459               yT = NULL;
460             }
461             if (zT){ 
462               delete[] zT; 
463               zT = NULL;
464             }
465
466         } // END for track loop
467
468
469         // NO !!! DELETE line #ifdef helix delete helix
470
471
472     }   // END - DRAW 3D Tracks
473
474     //--------------------------------------------------------------------------------------------
475     // DRAW 3D PadRow
476     //--------------------------------------------------------------------------------------------
477     if (fDisplay->Get3DSwitchPadRow() && fDisplay->GetDisplaySlice(fDisplay->GetSlicePadRow())){
478         fDisplay->GetPadRowPointer()->Draw3D();
479     }
480
481     //--------------------------------------------------------------------------------------------
482     // DRAW 3D 
483     //--------------------------------------------------------------------------------------------
484     // v->ZoomView(0,3);
485     // v->Draw();   
486
487     fDisplay->GetCanvas3D()->SetFillColor(fDisplay->GetBackColor());
488
489     if ( !fDisplay->GetKeepView() ){ 
490         fDisplay->GetCanvas3D()->SetTheta(fDisplay->GetTheta());
491         fDisplay->GetCanvas3D()->SetPhi(fDisplay->GetPhi());
492     }
493
494     fDisplay->GetCanvas3D()->Modified();
495     fDisplay->GetCanvas3D()->Update();
496 }
497
498 //____________________________________________________________________________________________________
499 void AliHLTTPCDisplay3D::DrawGeomSector(Int_t sector) {  
500   /*
501     Char_t fname[256];
502     Int_t realsector = sector;// % 18;
503   
504     if (realsector < 10){
505         sprintf(fname,"LS0%d",realsector);
506         fGeom->GetNode(fname)->SetLineColor(fDisplay->GetLineColor());
507         fGeom->GetNode(fname)->Draw("same");
508         sprintf(fname,"US0%d",realsector);
509         fGeom->GetNode(fname)->SetLineColor(fDisplay->GetLineColor()); 
510         fGeom->GetNode(fname)->Draw("same");
511     }
512     else {
513         sprintf(fname,"LS%d",realsector);
514         fGeom->GetNode(fname)->SetLineColor(fDisplay->GetLineColor());
515         fGeom->GetNode(fname)->Draw("same");
516         sprintf(fname,"US%d",realsector);
517         fGeom->GetNode(fname)->SetLineColor(fDisplay->GetLineColor()); 
518         fGeom->GetNode(fname)->Draw("same");
519     }
520
521   */   
522 }
523
524 //____________________________________________________________________________________________________
525 void AliHLTTPCDisplay3D::LoadGeometrie(Char_t *gfile) {
526     if (gfile) {
527         TFile *file = TFile::Open(gfile);
528         if(!file) {
529             LOG(AliHLTTPCLog::kError,"AliHLTTPCDisplay3D::AliHLTDisplay","File Open") 
530               <<"Geometry file " << gfile << " does not exist!"<<ENDLOG;
531             exit(-1);
532         }
533         
534         fGeom = (TGeometry*)file->Get("AliceGeom");
535
536         file->Close();
537         //delete file;  ####
538     }
539 }