26b525c244fcdfb95544edac34b481b296bb685f
[u/mrichter/AliRoot.git] / EVE / EveDet / AliEveITSDigitsInfo.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 "AliEveITSDigitsInfo.h"
11 #include <EveBase/AliEveEventManager.h>
12
13 #include <AliITS.h>
14 #include <AliITSInitGeometry.h>
15 #include <AliITSgeomTGeo.h>
16 #include <AliITSsegmentationSPD.h>
17 #include <AliITSsegmentationSDD.h>
18 #include <AliITSsegmentationSSD.h>
19 #include <AliITSDDLModuleMapSDD.h>
20
21 #include <AliITSCalibrationSDD.h>
22 #include <AliITSCalibrationSPD.h>
23 #include <AliITSdigit.h>
24 #include <AliITSdigitSPD.h>
25
26 #include <AliCDBEntry.h>
27 #include <AliCDBManager.h>
28
29 #include <AliRawReader.h>
30 #include <AliITSRawStreamSPD.h>
31 #include <AliITSRawStreamSDD.h>
32 #include <AliITSRawStreamSDDCompressed.h>
33 #include <AliITSRawStreamSSD.h>
34
35 #include <TGeoMatrix.h>
36 #include <TEveTrans.h>
37
38 #include <TMath.h>
39 #include <TVector3.h>
40
41 //==============================================================================
42 //==============================================================================
43 // AliEveITSModuleSelection
44 //==============================================================================
45
46 //______________________________________________________________________________
47 //
48 // Helper for selecting a range of ITS modules by type, layer, phi and
49 // theta. Taken as an argument to AliEveITSDigitsInfo::GetModuleIDs().
50
51 ClassImp(AliEveITSModuleSelection)
52
53 AliEveITSModuleSelection::AliEveITSModuleSelection():
54   fType(-1),
55   fLayer(-1),
56   fMinPhi(-TMath::Pi()),
57   fMaxPhi(TMath::Pi()),
58   fMinTheta(-TMath::Pi()),
59   fMaxTheta(TMath::Pi())
60 {
61   // Constructor.
62 }
63
64
65 //==============================================================================
66 //==============================================================================
67 // AliEveITSDigitsInfo
68 //==============================================================================
69
70 //______________________________________________________________________________
71 //
72 // Stores ITS geometry information and event-data in format suitable
73 // for visualization.
74
75 ClassImp(AliEveITSDigitsInfo)
76
77 AliITSDDLModuleMapSDD *AliEveITSDigitsInfo::fgDDLMapSDD  = 0;
78 TObjArray             *AliEveITSDigitsInfo::fgDeadModSPD = 0;
79
80 /******************************************************************************/
81
82 AliEveITSDigitsInfo::AliEveITSDigitsInfo() :
83   TObject(),
84   TEveRefCnt(),
85   fTree       (0),
86   fSegSPD     (0), fSegSDD     (0), fSegSSD     (0),
87   fSPDMinVal  (0), fSSDMinVal  (0), fSDDMinVal  (0),
88   fSPDMaxVal  (0), fSSDMaxVal  (0), fSDDMaxVal  (0),
89   fSPDHighLim (0), fSDDHighLim (0), fSSDHighLim (0),
90   fSPDmap     (),  fSDDmap     (),  fSSDmap     ()
91 {
92   // Default constructor.
93
94   InitInternals();
95 }
96
97 void AliEveITSDigitsInfo::InitInternals()
98 {
99   // Initialize internal geometry structures, in particular the
100   // module-id to transformation-matrix mapping and segmentation
101   // classes and data-structures.
102
103   static const TEveException kEH("AliEveITSDigitsInfo::InitInternals ");
104
105   AliEveEventManager::AssertGeometry();
106
107   SetITSSegmentation();
108
109   // create tables for scaling
110   fSPDMinVal = 0;
111   fSDDMinVal = 5;
112   fSSDMinVal = 2;
113
114   fSPDMaxVal = 1;
115   fSDDMaxVal = 80;
116   fSSDMaxVal = 100;
117
118   fSPDHighLim = 1;
119   fSDDHighLim = 512;
120   fSSDHighLim = 1024;
121
122   // lowest scale factor refers to unscaled ITS module
123   fSPDScaleX[0] = 1;
124   fSPDScaleZ[0] = 1;
125   fSDDScaleX[0] = 1;
126   fSDDScaleZ[0] = 1;
127   fSSDScale [0] = 1;
128
129   // spd lowest resolution
130   Int_t nx = 8; // fSegSPD->Npx()/8; // 32
131   Int_t nz = 6; // fSegSPD->Npz()/2; // 128
132   fSPDScaleX[1] = Int_t(nx);
133   fSPDScaleZ[1] = Int_t(nz);
134   fSPDScaleX[2] = Int_t(nx*2);
135   fSPDScaleZ[2] = Int_t(nz*2);
136   fSPDScaleX[3] = Int_t(nx*3);
137   fSPDScaleZ[3] = Int_t(nz*3);
138   fSPDScaleX[4] = Int_t(nx*4);
139   fSPDScaleZ[4] = Int_t(nz*4);
140
141   fSDDScaleX[1] = 2;
142   fSDDScaleZ[1] = 2;
143   fSDDScaleX[2] = 8;
144   fSDDScaleZ[2] = 8;
145   fSDDScaleX[3] = 16;
146   fSDDScaleZ[3] = 16;
147   fSDDScaleX[4] = 25;
148   fSDDScaleZ[4] = 25;
149
150   fSSDScale[1] = 3;
151   fSSDScale[2] = 9;
152   fSSDScale[3] = 20;
153   fSSDScale[4] = 30;
154
155   if (fgDDLMapSDD == 0)
156   {
157     fgDDLMapSDD = new AliITSDDLModuleMapSDD();
158     AliCDBManager *man = AliCDBManager::Instance();
159     Bool_t cacheStatus = man->GetCacheFlag();
160     AliCDBEntry   *ddlMapSDD = man->Get("ITS/Calib/DDLMapSDD");
161
162     if (!ddlMapSDD)
163     {
164       AliWarning("SDD DDL map file retrieval from OCDB failed! - Use default DDL map");
165     }
166     else
167     {
168       AliITSDDLModuleMapSDD *ddlsdd = (AliITSDDLModuleMapSDD*)ddlMapSDD->GetObject();
169       if (!ddlsdd)
170       {
171         AliWarning("SDD DDL map object not found in OCDB file! - Use default DDL map");
172       }
173       else
174       {
175         if (!cacheStatus)
176           ddlMapSDD->SetObject(0);
177         ddlMapSDD->SetOwner(kTRUE);
178         fgDDLMapSDD->SetDDLMap(ddlsdd);
179       }
180     }
181
182     if (!cacheStatus)
183       delete ddlMapSDD;
184   }
185
186   if (fgDeadModSPD == 0)
187   {
188     AliCDBManager *cdb = AliCDBManager::Instance();
189
190     AliCDBEntry *deadSPD = cdb->Get("ITS/Calib/SPDDead");
191
192     if (!deadSPD)
193     {
194       AliWarning("SPD Calibration object retrieval failed!");
195     }
196     else
197     {
198       fgDeadModSPD = (TObjArray*)deadSPD->GetObject();
199       fgDeadModSPD->SetOwner(kTRUE);
200     }
201   }
202 }
203
204 /******************************************************************************/
205
206 AliEveITSDigitsInfo::~AliEveITSDigitsInfo()
207 {
208   // Destructor.
209   // Deletes the data-maps.
210   // The tree, if it was passed into the object, is not touched.
211
212   std::map<Int_t, TClonesArray*>::iterator j;
213   for (j = fSPDmap.begin(); j != fSPDmap.end(); ++j)
214     delete j->second;
215   for (j = fSDDmap.begin(); j != fSDDmap.end(); ++j)
216     delete j->second;
217   for (j = fSSDmap.begin(); j != fSSDmap.end(); ++j)
218     delete j->second;
219
220   delete fSegSPD; delete fSegSDD; delete fSegSSD;
221 }
222
223 /******************************************************************************/
224
225 void AliEveITSDigitsInfo::SetTree(TTree* tree)
226 {
227   // Set digit-tree to be used for digit retrieval. Data is loaded on
228   // demand.
229
230   fTree = tree;
231 }
232
233 void AliEveITSDigitsInfo::ReadRaw(AliRawReader* raw, Int_t mode)
234 {
235   // Read raw-data into internal structures. AliITSdigit is used to
236   // store raw-adata for all sub-detectors.
237
238   if ((mode & 1) || (mode & 2))
239   {
240     AliITSRawStreamSPD inputSPD(raw);
241     TClonesArray* digits = 0;
242     while (inputSPD.Next())
243     {
244       Int_t module = inputSPD.GetModuleID();
245       Int_t column = inputSPD.GetColumn();
246       Int_t row    = inputSPD.GetRow();
247
248       if (inputSPD.IsNewModule())
249       {
250         digits = fSPDmap[module];
251         if (digits == 0)
252           fSPDmap[module] = digits = new TClonesArray("AliITSdigit", 16);
253       }
254
255       AliITSdigit* d = new ((*digits)[digits->GetEntriesFast()]) AliITSdigit();
256       d->SetCoord1(column);
257       d->SetCoord2(row);
258       d->SetSignal(1);
259
260       // printf("SPD: %d %d %d\n",module,column,row);
261     }
262     raw->Reset();
263   }
264
265   if ((mode & 4) || (mode & 8))
266   {
267     AliITSRawStream* inputSDD = AliITSRawStreamSDD::CreateRawStreamSDD(raw);
268     inputSDD->SetDDLModuleMap(fgDDLMapSDD);
269     TClonesArray* digits = 0;
270     while (inputSDD->Next())
271     {
272       Int_t module = inputSDD->GetModuleID();
273
274       digits = fSDDmap[module];
275       if (digits == 0)
276         fSDDmap[module] = digits = new TClonesArray("AliITSdigit", 0);      
277
278       if (inputSDD->IsCompletedModule()==kFALSE && inputSDD->IsCompletedDDL()==kFALSE)
279       {
280         Int_t anode  = inputSDD->GetCoord1()+inputSDD->GetChannel()*AliITSsegmentationSDD::GetNAnodesPerHybrid();
281         Int_t time   = inputSDD->GetCoord2();
282         Int_t signal = inputSDD->GetSignal();
283         AliITSdigit* d = new ((*digits)[digits->GetEntriesFast()]) AliITSdigit();
284         d->SetCoord1(anode);
285         d->SetCoord2(time);
286         d->SetSignal(signal);
287       }
288       // printf("SDD: %d %d %d %d\n",module,anode,time,signal);
289     }
290     delete inputSDD;
291     raw->Reset();
292   }
293
294   if ((mode & 16) || (mode & 32))
295   {
296     AliITSRawStreamSSD input(raw);
297     TClonesArray* digits = 0;
298     while (input.Next())
299     {
300       Int_t module  = input.GetModuleID();
301       Int_t side    = input.GetSideFlag();
302       Int_t strip   = input.GetStrip();
303       Int_t signal  = input.GetSignal();
304
305       // !!!! IsNewModule() is false in the beginning of the stream, so we also
306       // !!!! check for digits == 0. Should be fixed in SSD stream.
307       if (input.IsNewModule() || digits == 0)
308       {
309         digits = fSSDmap[module];
310         if (digits == 0)
311           fSSDmap[module] = digits = new TClonesArray("AliITSdigit", 0);
312       }
313
314       AliITSdigit* d = new ((*digits)[digits->GetEntriesFast()]) AliITSdigit();
315       d->SetCoord1(side);
316       d->SetCoord2(strip);
317       d->SetSignal(signal);
318
319       // printf("SSD: %d %d %d %d\n",module,side,strip,signal);
320     }
321     raw->Reset();
322   }
323 }
324
325 /******************************************************************************/
326
327 void AliEveITSDigitsInfo::SetITSSegmentation()
328 {
329   // Create the segmentation objects and fill internal
330   // data-structures.
331
332   // SPD
333   fSegSPD = new AliITSsegmentationSPD("TGeo");
334
335   Float_t fNzSPD = 160;
336   Float_t fHlSPD = 3.48;
337   Float_t fZ1pitchSPD = 0.0425, fZ2pitchSPD = 0.0625;
338
339   fSPDZCoord[0] = fZ1pitchSPD - fHlSPD;
340   for (Int_t m=1; m<fNzSPD; m++)
341   {
342     Float_t dz = fZ1pitchSPD;
343     if (m==31 || m==32 || m==63 || m==64 || m==95 || m==96 || m==127 || m==128)
344     {
345       dz = fZ2pitchSPD;
346     }
347     fSPDZCoord[m] = fSPDZCoord[m-1] + dz;
348   }
349
350   for (Int_t m=0; m<fNzSPD; m++)
351   {
352     Float_t dz = fZ1pitchSPD;
353     if (m==31 || m==32 || m==63 || m==64 || m==95 || m==96 || m==127 || m==128)
354     {
355       dz = fZ2pitchSPD;
356     }
357     fSPDZCoord[m] -= dz;
358   }
359
360   // SDD
361   fSegSDD = new AliITSsegmentationSDD("TGeo");
362   // !!!! Set default drift speed, eventually need to get it from CDB.
363   fSegSDD->SetDriftSpeed(7.3);
364
365   // SSD
366   fSegSSD = new AliITSsegmentationSSD("TGeo");
367 }
368
369 /******************************************************************************/
370
371 TClonesArray* AliEveITSDigitsInfo::GetDigits(Int_t mod, Int_t subdet) const
372 {
373   // Return TClonesArray of digits for specified module and sub-detector-id.
374
375   switch(subdet)
376   {
377     case 0:
378     {
379       std::map<Int_t, TClonesArray*>::iterator i = fSPDmap.find(mod);
380       if (i != fSPDmap.end())
381       {
382         return i->second;
383       }
384       else if (fTree)
385       {
386         TClonesArray *digitsSPD = 0;
387         TBranch *br = fTree->GetBranch("ITSDigitsSPD");
388         br->SetAddress(&digitsSPD);
389         br->GetEntry(mod);
390         fSPDmap[mod] = digitsSPD;
391         return digitsSPD;
392       }
393       break;
394     }
395
396     case 1:
397     {
398       std::map<Int_t, TClonesArray*>::iterator i = fSDDmap.find(mod);
399       if (i != fSDDmap.end())
400       {
401         return i->second;
402       }
403       else if (fTree)
404       {
405         TClonesArray *digitsSDD = 0;
406         TBranch *br = fTree->GetBranch("ITSDigitsSDD");
407         br->SetAddress(&digitsSDD);
408         br->GetEntry(mod);
409         fSDDmap[mod] = digitsSDD;
410         return digitsSDD;
411       }
412       break;
413     }
414
415     case 2:
416     {
417       std::map<Int_t, TClonesArray*>::iterator i = fSSDmap.find(mod);
418       if (i != fSSDmap.end())
419       {
420         return i->second;
421       }
422       else if (fTree)
423       {
424         TClonesArray *digitsSSD = 0;
425         TBranch *br = fTree->GetBranch("ITSDigitsSSD");
426         br->SetAddress(&digitsSSD);
427         br->GetEntry(mod);
428         fSSDmap[mod] = digitsSSD;
429         return digitsSSD;
430       }
431       break;
432     }
433   }
434
435   return 0;
436 }
437
438 /******************************************************************************/
439
440 void AliEveITSDigitsInfo::GetModuleIDs(AliEveITSModuleSelection* sel,
441                                        std::vector<UInt_t>& ids) const
442 {
443   // Fill the id-vector with ids of modules that satisfy conditions
444   // given by the AliEveITSModuleSelection object.
445
446   Int_t idx0 = 0, idx1 = 0;
447   switch(sel->GetType())
448   {
449     case 0:
450       idx0 = 0;
451       idx1 = AliITSgeomTGeo::GetModuleIndex(3, 1, 1) - 1;
452       break;
453     case 1:
454       idx0 = AliITSgeomTGeo::GetModuleIndex(3, 1, 1);
455       idx1 = AliITSgeomTGeo::GetModuleIndex(5, 1, 1) - 1;
456       break;
457     case 2:
458       idx0 = AliITSgeomTGeo::GetModuleIndex(5, 1, 1);
459       idx1 = AliITSgeomTGeo::GetNModules() - 1;
460       break;
461     default:
462       idx1 = 0;
463       idx1 = AliITSgeomTGeo::GetNModules() - 1;
464       break;
465   }
466
467   TVector3  v;
468   TEveTrans mx;
469   Int_t     lay, lad, det;
470   for (Int_t id = idx0; id < idx1; ++id)
471   {
472     AliITSgeomTGeo::GetModuleId(id, lay, lad, det);
473     if (sel->GetLayer() == lay || sel->GetLayer() == -1)
474     {
475       // check data from matrix
476       mx.SetFrom(*AliITSgeomTGeo::GetMatrix(id));
477       mx.GetPos(v);
478       if (v.Phi()   <= sel->GetMaxPhi()   && v.Phi()   >= sel->GetMinPhi()   &&
479           v.Theta() <= sel->GetMaxTheta() && v.Theta() >= sel->GetMinTheta())
480       {
481         ids.push_back(id);
482       }
483     }
484   }
485 }
486
487 /******************************************************************************/
488
489 Bool_t AliEveITSDigitsInfo::HasData(Int_t module, Int_t det_id) const
490 {
491   // Return true if given module has data.
492
493   TClonesArray *digits = GetDigits(module, det_id);
494   return digits && digits->GetEntriesFast() > 0;
495 }
496
497 Bool_t AliEveITSDigitsInfo::IsDead (Int_t module, Int_t det_id) const
498 {
499   // Return true if given module is dead.
500   // Only implemented for SPD.
501
502   if (det_id == 0 && fgDeadModSPD)
503     return ((AliITSCalibrationSPD*) fgDeadModSPD->At(module))->IsBad();
504   return kFALSE;
505 }
506
507 /******************************************************************************/
508
509 void AliEveITSDigitsInfo::Print(Option_t* ) const
510 {
511   // Print information about stored geometry and segmentation.
512
513   printf("*********************************************************\n");
514   printf("SPD module dimension (%f,%f)\n",           fSegSPD->Dx()*0.0001, fSegSPD->Dz()*0.0001);
515   printf("SPD first,last module: %d, %d\n",
516          AliITSgeomTGeo::GetModuleIndex(1,1,1), 
517          AliITSgeomTGeo::GetModuleIndex(3,1,1) - 1);
518   printf("SPD num cells per module (x::%d,z::%d)\n", fSegSPD->Npx(), fSegSPD->Npz());
519   Int_t iz = 0, ix = 0;
520   printf("SPD dimesion of (%d,%d) in pixel(%f,%f)\n",   ix, iz, fSegSPD->Dpx(ix), fSegSPD->Dpz(iz));
521   iz = 32;
522   printf("SPD dimesion of pixel (%d,%d) are (%f,%f)\n", ix, iz, fSegSPD->Dpx(ix)*0.001, fSegSPD->Dpz(iz)*0.001);
523
524   printf("*********************************************************\n");
525   printf("SDD module dimension (%f,%f)\n",           fSegSDD->Dx()*0.0001, fSegSDD->Dz()*0.0001);
526   printf("SDD first,last module: %d, %d\n",
527          AliITSgeomTGeo::GetModuleIndex(3,1,1),
528          AliITSgeomTGeo::GetModuleIndex(5,1,1) - 1);
529   printf("SDD num cells per module (x::%d,z::%d)\n", fSegSDD->Npx(), fSegSDD->Npz());
530   printf("SDD dimesion of pixel are (%f,%f)\n",      fSegSDD->Dpx(1)*0.001, fSegSDD->Dpz(1)*0.001);
531
532   Float_t ap, an;
533   printf("*********************************************************\n");
534   printf("SSD module dimension (%f,%f)\n",  fSegSSD->Dx()*0.0001, fSegSSD->Dz()*0.0001);
535   printf("SSD first, last module: %d, %d\n",
536          AliITSgeomTGeo::GetModuleIndex(5,1,1),
537          AliITSgeomTGeo::GetNModules() - 1);
538   printf("SSD strips in module %d\n",       fSegSSD->Npx());
539   printf("SSD strip sizes are (%f,%f)\n",   fSegSSD->Dpx(1), fSegSSD->Dpz(1));
540   fSegSSD->SetLayer(5);  fSegSSD->Angles(ap,an);
541   printf("SSD layer 5 stereoP %f stereoN %f angle\n", ap, an);
542   fSegSSD->SetLayer(6);  fSegSSD->Angles(ap,an);
543   printf("SSD layer 6 stereoP %f stereoN %f angle\n", ap, an);
544 }
545
546
547 /*
548   printf("num cells %d,%d scaled %d,%d \n",fSegSPD->Npz(),fSegSPD->Npx(),Nz,Nx);
549   printf("%d digits in AliEveITSModule %d\n",ne, module);
550   Float_t zn = i*(3.48*2)/Nz - 3.48 ;
551   Float_t xo =  -fSegSPD->Dx()*0.00005 + fSegSPD->Dpx(0)*od->GetCoord2()*0.0001;
552   Float_t xn =  -fSegSPD->Dx()*0.00005 + j*0.0001*fSegSPD->Dx()/Nx;
553   Float_t dpx = 0.0001*fSegSPD->Dx()/Nx;
554   Float_t dpz = 3.48*2/Nz;
555   printf("Z::original (%3f) scaled (%3f, %3f) \n", zo, zn-dpz/2, zn+dpz/2);
556   printf("X::original (%3f) scaled (%3f, %3f) \n", xo, xn-dpx/2, xn+dpx/2);
557   printf("%d,%d maped to %d,%d \n", od->GetCoord1(), od->GetCoord2(), i,j );
558 */