]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/UPGRADE/AliITSUSegmentationPix.cxx
c83d288ec8a27e8449c39202b150b349b8bfb966
[u/mrichter/AliRoot.git] / ITS / UPGRADE / AliITSUSegmentationPix.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id: AliITSUSegmentationPix.cxx 47180 2011-02-08 09:42:29Z masera $ */
17 #include <TGeoManager.h>
18 #include <TGeoVolume.h>
19 #include <TGeoBBox.h>
20 #include <TObjArray.h>
21 #include <TString.h>
22 #include <TSystem.h>
23 #include <TFile.h>
24 #include "AliITSUGeomTGeo.h"
25 #include "AliITSUSegmentationPix.h"
26
27 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Segmentation class for pixels                                                                          //
29 // Questions to solve: are guardrings needed and do they belong to the sensor or to the module in TGeo    //
30 //                     At the moment assume that the local coord syst. is located at bottom left corner   //
31 //                     of the ACTIVE matrix. If the guardring to be accounted in the local coords, in     //
32 //                     the Z and X conversions one needs to first subtract the  fGuardLft and fGuardBot   //
33 //                     from the local Z,X coordinates                                                     //
34 //                                                                                                        //
35 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
36
37 ClassImp(AliITSUSegmentationPix)
38
39 const char* AliITSUSegmentationPix::fgkSegmListName = "ITSUpgradeSegmentations";
40
41 //_____________________________________________________________________________RS
42 AliITSUSegmentationPix::AliITSUSegmentationPix(UInt_t id, int nchips,int ncol,int nrow,
43                                                    double pitchX,double pitchZ,
44                                                    double thickness,
45                                                    double pitchLftC,double pitchRgtC,
46                                                    double edgL,double edgR,double edgT,double edgB)
47 : AliITSsegmentation()
48   ,fGuardLft(edgL)
49   ,fGuardRgt(edgR)
50   ,fGuardTop(edgT)
51   ,fGuardBot(edgB)
52   ,fPitchX(pitchX)
53   ,fPitchZ(pitchZ)
54   ,fPitchZLftCol(pitchLftC<0 ? pitchZ:pitchLftC)
55   ,fPitchZRgtCol(pitchRgtC<0 ? pitchZ:pitchRgtC)
56   ,fChipDZ(0)
57   ,fNChips(nchips)
58   ,fNColPerChip(nchips>0 ? ncol/nchips:0)
59   ,fNRow(nrow)
60   ,fNCol(ncol)
61 {
62   // Default constructor, sizes in microns
63   if (nchips) SetUniqueID( AliITSUGeomTGeo::ComposeDetTypeID(id) );
64   fChipDZ = (fNColPerChip-2)*fPitchZ + fPitchZLftCol + fPitchZRgtCol;
65   SetDetSize( fNRow*fPitchX /*+fGuardTop+fGuardBot*/,
66               fNChips*fChipDZ /*+fGuardLft+fGuardRgt*/,
67               thickness);
68   //
69 }
70
71 //_____________________________________________________________________________RS
72 void AliITSUSegmentationPix::GetPadIxz(Float_t x,Float_t z,Int_t &ix,Int_t &iz) const 
73 {
74   //  Returns pixel coordinates (ix,iz) for given real local coordinates (x,z)
75   //  expects x, z in microns
76   ix = int(x/fPitchX) + 1;     
77   iz = int(Z2Col(z)) + 1;
78   //  
79   if (iz >  fNCol) iz= fNCol;
80   if (ix >  fNRow) ix= fNRow;
81   //
82 }
83
84 //_____________________________________________________________________________RS
85 void AliITSUSegmentationPix::GetPadTxz(Float_t &x,Float_t &z) const
86 {
87   //  local transformation of real local coordinates (x,z)
88   //  expects x, z in microns
89   x /= fPitchX;
90   z = Z2Col(z);
91   //
92 }
93
94 //_____________________________________________________________________________RS
95 void AliITSUSegmentationPix::GetPadCxz(Int_t ix,Int_t iz,Float_t &x,Float_t&z) const
96 {
97   // Transform from pixel to real local coordinates
98   // returns x, z in microns
99   x = (ix>0) ? Float_t((ix-0.5)*fPitchX) : Float_t((ix+0.5)*fPitchX);
100   z = Col2Z(iz);
101   //
102 }
103
104 //_____________________________________________________________________________RS
105 Float_t AliITSUSegmentationPix::Z2Col(Float_t z) const 
106 {
107   // get column number (from 0) from local Z
108   int chip = z/fChipDZ;
109   float col = chip*fNColPerChip;
110   z -= chip*fChipDZ;
111   if (z<fPitchZLftCol) col += z/fPitchZLftCol;
112   else {
113     z = fPitchZLftCol;
114     col += 1;
115     if (z<(fChipDZ-fPitchZRgtCol)) col += 1+z/fPitchZ;
116     else col += 1+(z - (fNColPerChip-2)*fPitchZ)/fPitchZRgtCol;
117   }
118   return col;
119 }
120
121 //_____________________________________________________________________________RS
122 Float_t AliITSUSegmentationPix::Col2Z(Int_t col) const 
123 {
124   // convert column number (from 0) to Z coordinate
125   int nchip = col/fNColPerChip;
126   col %= fNColPerChip;
127   float z = nchip*fChipDZ;
128   if (!col) z -= fPitchZRgtCol/2;
129   else if (col==1) z += fPitchZLftCol/2;
130   else if (col==fNColPerChip-1) z += fChipDZ - fPitchZRgtCol/2;
131   else    z += fPitchZLftCol + (col-0.5)*fChipDZ;
132   return z;
133   //
134 }
135
136 //______________________________________________________________________RS
137 AliITSUSegmentationPix& AliITSUSegmentationPix::operator=(const AliITSUSegmentationPix &src)
138 {
139   // = operator
140   if(this==&src) return *this;
141   AliITSsegmentation::operator=(src);
142   fNCol  = src.fNCol;
143   fNRow  = src.fNRow;
144   fNColPerChip  = src.fNColPerChip;
145   fNChips = src.fNChips;
146   fChipDZ = src.fChipDZ;
147   fPitchZRgtCol = src.fPitchZRgtCol;
148   fPitchZLftCol = src.fPitchZLftCol;
149   fPitchZ = src.fPitchZ;
150   fPitchX = src.fPitchX;
151   //
152   fGuardBot = src.fGuardBot;
153   fGuardTop = src.fGuardTop;
154   fGuardRgt = src.fGuardRgt;
155   fGuardLft = src.fGuardLft;
156   //
157   return *this;
158 }
159
160 //____________________________________________________________________________RS
161 AliITSUSegmentationPix::AliITSUSegmentationPix(const AliITSUSegmentationPix &src) :
162   AliITSsegmentation(src)
163   ,fGuardLft(src.fGuardLft)
164   ,fGuardRgt(src.fGuardRgt)
165   ,fGuardTop(src.fGuardTop)
166   ,fGuardBot(src.fGuardBot)
167   ,fPitchX(src.fPitchX)
168   ,fPitchZ(src.fPitchZ)
169   ,fPitchZLftCol(src.fPitchZLftCol)
170   ,fPitchZRgtCol(src.fPitchZRgtCol)
171   ,fChipDZ(src.fChipDZ)
172   ,fNChips(src.fNChips)
173   ,fNColPerChip(src.fNColPerChip)
174   ,fNRow(src.fNRow)
175   ,fNCol(src.fNCol)  
176 {
177 }
178
179 //____________________________________________________________________________RS
180 Float_t AliITSUSegmentationPix::Dpx(Int_t ) const 
181 {
182   //returs x pixel pitch for a give pixel
183   return fPitchX;
184 }
185
186 //____________________________________________________________________________RS
187 Float_t AliITSUSegmentationPix::Dpz(Int_t col) const 
188 {
189   // returns z pixel pitch for a given pixel (cols starts from 0)
190   col %= fNColPerChip;
191   if (!col) return fPitchZLftCol;
192   if (col==fNColPerChip-1) return fPitchZRgtCol;
193   return fPitchZ;
194   //
195 }
196
197 //------------------------------
198 void AliITSUSegmentationPix::Neighbours(Int_t iX, Int_t iZ, Int_t* nlist, Int_t xlist[8], Int_t zlist[8]) const 
199 {
200   // returns the neighbouring pixels for use in Cluster Finders and the like.
201   //
202   *nlist=8;
203   xlist[0]=xlist[1]=iX;
204   xlist[2]=iX-1;
205   xlist[3]=iX+1;
206   zlist[0]=iZ-1;
207   zlist[1]=iZ+1;
208   zlist[2]=zlist[3]=iZ;
209   // Diagonal elements
210   xlist[4]=iX+1;
211   zlist[4]=iZ+1;
212   //  
213   xlist[5]=iX-1;
214   zlist[5]=iZ-1;
215   //
216   xlist[6]=iX-1;
217   zlist[6]=iZ+1;
218   //
219   xlist[7]=iX+1;
220   zlist[7]=iZ-1;
221   //
222 }
223
224 //______________________________________________________________________
225 Bool_t AliITSUSegmentationPix::LocalToDet(Float_t x,Float_t z,Int_t &ix,Int_t &iz) const 
226 {
227   // Transformation from Geant detector centered local coordinates (cm) to
228   // Pixel cell numbers ix and iz.
229   // Input:
230   //    Float_t   x        detector local coordinate x in cm with respect to
231   //                       the center of the sensitive volume.
232   //    Float_t   z        detector local coordinate z in cm with respect to
233   //                       the center of the sensitive volulme.
234   // Output:
235   //    Int_t    ix        detector x cell coordinate. Has the range 
236   //                       0<=ix<fNRow.
237   //    Int_t    iz        detector z cell coordinate. Has the range 
238   //                       0<=iz<fNCol.
239   // Return:
240   //   kTRUE if point x,z is inside sensitive volume, kFALSE otherwise.
241   //   A value of -1 for ix or iz indecates that this point is outside of the
242   //   detector segmentation as defined.
243   x = x*kCM2MC+0.5*Dx();
244   z = z*kCM2MC+0.5*Dz();
245   ix = iz = -1;
246   if(x<0 || x>Dx()) return kFALSE; // outside x range.
247   if(z<0 || z>Dz()) return kFALSE; // outside z range.
248   ix = int(x/fPitchX);
249   iz = Z2Col(z);
250   return kTRUE; // Found ix and iz, return.
251 }
252
253 //______________________________________________________________________
254 void AliITSUSegmentationPix::DetToLocal(Int_t ix,Int_t iz,Float_t &x,Float_t &z) const
255 {
256 // Transformation from Detector cell coordiantes to Geant detector centerd 
257 // local coordinates (cm).
258 // Input:
259 // Int_t    ix        detector x cell coordinate. Has the range 0<=ix<fNRow.
260 // Int_t    iz        detector z cell coordinate. Has the range 0<=iz<fNCol.
261 // Output:
262 // Float_t   x        detector local coordinate x in cm with respect to the
263 //                    center of the sensitive volume.
264 // Float_t   z        detector local coordinate z in cm with respect to the
265 //                    center of the sensitive volulme.
266 // If ix and or iz is outside of the segmentation range a value of -0.5*Dx()
267 // or -0.5*Dz() is returned.
268   //
269   x = -0.5/kCM2MC*Dx(); // default value.
270   z = -0.5/kCM2MC*Dz(); // default value.
271   // RS: to check: why we don't do strict check for [0:n)
272   if(ix<0 || ix>=fNRow) return; // outside of detector 
273   if(iz<0 || iz>=fNCol) return; // outside of detctor
274   x += (ix+0.5)*fPitchX*(1./kCM2MC);       // RS: we go to the center of the pad, i.e. + pitch/2, not to the boundary as in SPD
275   z += Col2Z(iz)*(1./kCM2MC); 
276   return; // Found x and z, return.
277 }
278
279 //______________________________________________________________________
280 void AliITSUSegmentationPix::CellBoundries(Int_t ix,Int_t iz,Double_t &xl,Double_t &xu,Double_t &zl,Double_t &zu) const
281 {
282   // Transformation from Detector cell coordiantes to Geant detector centerd 
283   // local coordinates (cm).
284   // Input:
285   // Int_t    ix        detector x cell coordinate. Has the range 0<=ix<fNRow.
286   // Int_t    iz        detector z cell coordinate. Has the range 0<=iz<fNCol.
287   // Output:
288   // Double_t   xl       detector local coordinate cell lower bounds x in cm
289   //                    with respect to the center of the sensitive volume.
290   // Double_t   xu       detector local coordinate cell upper bounds x in cm 
291   //                    with respect to the center of the sensitive volume.
292   // Double_t   zl       detector local coordinate lower bounds z in cm with
293   //                    respect to the center of the sensitive volulme.
294   // Double_t   zu       detector local coordinate upper bounds z in cm with 
295   //                    respect to the center of the sensitive volulme.
296   // If ix and or iz is outside of the segmentation range a value of -0.5*Dx()
297   // and -0.5*Dx() or -0.5*Dz() and -0.5*Dz() are returned.
298   Float_t x,z;
299   DetToLocal(ix,iz,x,z);
300   //
301   if( ix<0 || ix>=fNRow || iz<0 || iz>=fNCol) {
302     xl = xu = -0.5/kCM2MC*Dx(); // default value.
303     zl = zu = -0.5/kCM2MC*Dz(); // default value.
304     return; // outside of detctor
305   }
306   float zpitchH = Dpz(iz)/2./kCM2MC;
307   float xpitchH = fPitchX/2./kCM2MC;
308   xl -= xpitchH;
309   xu += xpitchH;
310   zl -= zpitchH;
311   zu += zpitchH;
312   return; // Found x and z, return.
313 }
314
315 //______________________________________________________________________
316 Int_t AliITSUSegmentationPix::GetChipFromChannel(Int_t, Int_t iz) const 
317 {
318   // returns chip number (in range 0-4) starting from channel number
319   if(iz>=fNCol  || iz<0 ){
320     AliWarning("Bad cell number");
321     return -1;
322   }
323   return iz/fNColPerChip;
324 }
325
326 //______________________________________________________________________
327 Int_t AliITSUSegmentationPix::GetChipFromLocal(Float_t, Float_t zloc) const 
328 {
329   // returns chip number (in range 0-4) starting from local coordinates
330   Int_t ix0,iz;
331   if (!LocalToDet(0,zloc,ix0,iz)) {
332     AliWarning("Bad local coordinate");
333     return -1;
334   } 
335   return GetChipFromChannel(ix0,iz);
336 }
337
338 //______________________________________________________________________
339 Int_t AliITSUSegmentationPix::GetChipsInLocalWindow(Int_t* array, Float_t zmin, Float_t zmax, Float_t, Float_t) const 
340 {
341   // returns the number of chips containing a road defined by given local coordinate limits
342   //
343   const Float_t kconv = 1./kCM2MC; // converts microns to cm.
344   //
345   if (zmin>zmax) {
346     AliWarning("Bad coordinate limits: zmin>zmax!");
347     return -1;
348   } 
349   //
350   Int_t nChipInW = 0;
351   //
352   Float_t zminDet = -0.5*kconv*Dz();
353   Float_t zmaxDet =  0.5*kconv*Dz();
354   if(zmin<zminDet) zmin=zminDet;
355   if(zmax>zmaxDet) zmax=zmaxDet;
356
357   Int_t n1 = GetChipFromLocal(0,zmin);
358   array[nChipInW] = n1;
359   nChipInW++;
360
361   Int_t n2 = GetChipFromLocal(0,zmax);
362
363   if(n2!=n1){
364     Int_t imin=TMath::Min(n1,n2);
365     Int_t imax=TMath::Max(n1,n2);
366     for(Int_t ichip=imin; ichip<=imax; ichip++){
367       if(ichip==n1) continue;
368       array[nChipInW]=ichip;
369       nChipInW++;
370     }
371   }
372   //
373   return nChipInW;
374 }
375
376 //______________________________________________________________________
377 void AliITSUSegmentationPix::Init()
378 {
379   // init settings
380 }
381
382 //______________________________________________________________________
383 Bool_t AliITSUSegmentationPix::Store(const char* outf)
384 {
385   // store in the special list under given ID
386   TString fns = outf;
387   gSystem->ExpandPathName(fns);
388   if (fns.IsNull()) {AliFatal("No file name provided"); return kFALSE;}
389   TFile* fout = TFile::Open(fns.Data(),"update");
390   if (!fout) {AliFatal(Form("Failed to open output file %s",outf)); return kFALSE;}
391   TObjArray* arr = (TObjArray*)fout->Get(fgkSegmListName);
392   int id = GetUniqueID();
393   if (!arr) arr = new TObjArray();
394   else if (arr->At(id)) {AliFatal(Form("Segmenation %d already exists in file %s",id,outf));return kFALSE;}
395   //
396   arr->AddAtAndExpand(this,id);
397   arr->SetOwner(kTRUE);
398   fout->WriteObject(arr,fgkSegmListName,"kSingleKey");
399   fout->Close();
400   delete fout;
401   arr->RemoveAt(id);
402   delete arr;
403   AliInfo(Form("Stored segmentation %d in %s",id,outf));
404   return kTRUE;
405   //
406 }
407
408 //______________________________________________________________________
409 AliITSUSegmentationPix* AliITSUSegmentationPix::LoadWithID(UInt_t id, const char* inpf)
410 {
411   // store in the special list under given ID
412   TString fns = inpf;
413   gSystem->ExpandPathName(fns);
414   if (fns.IsNull()) {AliFatalGeneral("LoadWithID","No file name provided"); return 0;}
415   TFile* finp = TFile::Open(fns.Data());
416   if (!finp) {AliFatalGeneral("LoadWithID",Form("Failed to open file %s",inpf)); return 0;}
417   TObjArray* arr = (TObjArray*)finp->Get(fgkSegmListName);
418   if (!arr) {
419     AliFatalGeneral("LoadWithID",Form("Failed to find segmenation array %s in %s",fgkSegmListName,inpf)); 
420     return 0;
421   }
422   AliITSUSegmentationPix* segm = dynamic_cast<AliITSUSegmentationPix*>(arr->At(id));
423   if (!segm || segm->GetUniqueID()!=id) {AliFatalGeneral("LoadWithID",Form("Failed to find segmenation %d in %s",id,inpf)); return 0;}
424   //
425   arr->RemoveAt(id);
426   arr->SetOwner(kTRUE); // to not leave in memory other segmenations
427   finp->Close();
428   delete finp;
429   delete arr;
430   //
431   return segm;
432 }
433
434 //______________________________________________________________________
435 void AliITSUSegmentationPix::LoadSegmentations(TObjArray* dest, const char* inpf)
436 {
437   // store in the special list under given ID
438   if (!dest) return;
439   TString fns = inpf;
440   gSystem->ExpandPathName(fns);
441   if (fns.IsNull()) AliFatalGeneral("LoadWithID","No file name provided");
442   TFile* finp = TFile::Open(fns.Data());
443   if (!finp) AliFatalGeneral("LoadWithID",Form("Failed to open file %s",inpf));
444   TObjArray* arr = (TObjArray*)finp->Get(fgkSegmListName);
445   if (!arr) AliFatalGeneral("LoadWithID",Form("Failed to find segmenation array %s in %s",fgkSegmListName,inpf)); 
446   int nent = arr->GetEntriesFast();
447   TObject *segm = 0;
448   for (int i=nent;i--;) if ((segm=arr->At(i))) dest->AddAtAndExpand(segm,segm->GetUniqueID());
449   AliInfoGeneral("LoadSegmentations",Form("Loaded %d segmantions from %s",arr->GetEntries(),inpf));
450   arr->SetOwner(kFALSE);
451   arr->Delete();
452   finp->Close();
453   delete finp;
454   delete arr;
455   //
456 }