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