]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliCaloAltroMapping.cxx
Changes needed in order to enable the root i/o for the altro mapping objects.
[u/mrichter/AliRoot.git] / RAW / AliCaloAltroMapping.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 /* $Id$ */
16
17 // This class handles the mapping of the Altro channels in the PHOS/EMCAL
18 // The mapping is read from an external mapping files
19 // Author: C.Cheshkov
20
21 /// Exported from PHOS to be used also by EMCAL
22 /// November 2006 Gustavo Conesa Balbastre
23
24 #include "AliCaloAltroMapping.h"
25 #include "AliLog.h"
26 #include <Riostream.h>
27 //#include <stdlib.h>
28
29
30 ClassImp(AliCaloAltroMapping)
31
32 //_____________________________________________________________________________
33 AliCaloAltroMapping::AliCaloAltroMapping():
34   AliAltroMapping(),
35   fMinRow(0),
36   fMaxRow(0),
37   fMinCol(0),
38   fMaxCol(0),
39   fMapping(NULL),
40   fInvMappingLow(NULL),
41   fInvMappingHigh(NULL)
42 {
43   // Default constructor
44 }
45
46 //_____________________________________________________________________________
47 AliCaloAltroMapping::AliCaloAltroMapping(const char *mappingFile):
48   AliAltroMapping(mappingFile),
49   fMinRow(0),
50   fMaxRow(0),
51   fMinCol(0),
52   fMaxCol(0),
53   fMapping(NULL),
54   fInvMappingLow(NULL),
55   fInvMappingHigh(NULL)
56 {
57   // Constructor
58   ReadMapping();
59   CloseMappingFile();
60 }
61
62 //_____________________________________________________________________________
63 AliCaloAltroMapping::~AliCaloAltroMapping()
64 {
65   // destructor
66   DeleteMappingArrays();
67 }
68
69 //_____________________________________________________________________________
70 Bool_t AliCaloAltroMapping::ReadMapping()
71 {
72   // Initalizes the ALTRO mapping from a file
73   // Look at the Calo module for the format of
74   // the mapping file
75   if (!fIn) {
76     AliFatal("Mapping file has not been opened !");
77     return kFALSE;
78   }
79
80   fMinRow = 0x7fffffff;
81   fMaxRow = 0;
82   fMinCol = 0x7fffffff;
83   fMaxCol = 0;
84   fMappingSize = 3*(fMaxHWAddress+1);
85   fMapping = new Short_t[fMappingSize];
86   for (Int_t i = 0; i <= fMaxHWAddress; i++) {
87     fMapping[3*i] = fMapping[3*i+1] = fMapping[3*i+2] = -1;
88   }
89  
90   for(Int_t i = 0; i < fNumberOfChannels ; i++) { // 1792 = 2*896 channels connected to each RCU
91     Int_t hwAddress;
92     if (!(*fIn >> hwAddress)) {
93       AliFatal("Syntax of the mapping file is wrong !");
94       return kFALSE;
95     }
96     if (hwAddress > fMaxHWAddress) {
97       AliFatal(Form("Hardware (ALTRO) adress (%d) outside the range (0 -> %d) !",hwAddress,fMaxHWAddress));
98       return kFALSE;
99     }
100     Int_t row,col,gain;
101     if (!(*fIn >> row >> col >> gain)) {
102       AliFatal("Syntax of the mapping file is wrong !");
103       return kFALSE;
104     }
105
106     if (gain < 0 || gain > 1) {
107       AliFatal(Form("Wrong gain value found (%d)! Should be 0 or 1 !",gain));
108       return kFALSE;
109     }
110  
111     fMapping[3*hwAddress] = row;
112     fMapping[3*hwAddress+1] = col;
113     fMapping[3*hwAddress+2] = gain;
114
115     if (row > fMaxRow) fMaxRow = row;
116     if (row < fMinRow) fMinRow = row;
117     if (col > fMaxCol) fMaxCol = col;
118     if (col < fMinCol) fMinCol = col;
119
120   }
121
122   Int_t nRows = fMaxRow - fMinRow + 1;
123   Int_t nCols = fMaxCol - fMinCol + 1;
124   fInvMappingSize = nRows*nCols;
125
126
127   fInvMappingLow  = new Short_t[fInvMappingSize];
128   fInvMappingHigh = new Short_t[fInvMappingSize];
129   for (Int_t i = 0; i < nRows; i++) {
130     for (Int_t j = 0; j < nCols; j++) {
131       fInvMappingLow[nCols*i+j]  = -1;
132       fInvMappingHigh[nCols*i+j] = -1;
133     }
134   }
135
136   for(Int_t i = 0; i <= fMaxHWAddress; i++) {
137     Int_t row = fMapping[3*i];
138     Int_t col = fMapping[3*i+1];
139     Int_t gain = fMapping[3*i+2];
140     if(row != -1 && col != -1) {
141       if (gain == 0)
142         fInvMappingLow[nCols*(row-fMinRow)+(col-fMinCol)] = i;
143       if (gain == 1)
144         fInvMappingHigh[nCols*(row-fMinRow)+(col-fMinCol)] = i;
145     }
146   }
147
148   return kTRUE;
149 }
150
151 //_____________________________________________________________________________
152 Int_t AliCaloAltroMapping::GetHWAddress(Int_t row, Int_t col, Int_t gain) const
153 {
154   // Get the content of the mapping array
155   // return -1 in case there is no hardware
156   // adress defined for these row-column-gain
157   if (!fInvMappingLow || !fInvMappingHigh) {
158     AliWarning("Mapping array was not initalized correctly !");
159     return -1;
160   }
161   if (row < fMinRow || row > fMaxRow) {
162     AliWarning(Form("Index of row (%d) outside the range (%d -> %d) !",row,fMinRow,fMaxRow));
163     return -1;
164   }
165   if (col < fMinCol || col > fMaxCol) {
166     AliWarning(Form("Index of column (%d) outside the range (0 -> %d) !",col,fMinCol,fMaxCol));
167     return -1;
168   }
169   if (gain < 0 || gain > 1) {
170     AliWarning(Form("Invalid gain (%d)! Should be 0 or 1 !",gain));
171     return -1;
172   }
173   Int_t hwAddress = -1;
174   if (gain == 0)
175     hwAddress = fInvMappingLow[(fMaxCol - fMinCol + 1)*(row-fMinRow)+(col-fMinCol)];
176   if (gain == 1)
177     hwAddress = fInvMappingHigh[(fMaxCol - fMinCol + 1)*(row-fMinRow)+(col-fMinCol)];
178
179   if (hwAddress == -1)
180     AliWarning(Form("Hardware (ALTRO) adress is not defined for these row (%d), column (%d) and gain (%d) !",row,col,gain));
181
182   return hwAddress;
183 }
184
185 //_____________________________________________________________________________
186 Int_t AliCaloAltroMapping::GetPadRow(Int_t hwAddress) const
187 {
188   // Return the row index
189   // Note the difference w.r.t to the base class notation
190   if (!fMapping) {
191     AliWarning("Mapping array was not initalized correctly !");
192     return -1;
193   }
194   if (hwAddress > fMaxHWAddress) {
195     AliWarning(Form("Hardware (ALTRO) adress (%d) outside the range (0 -> %d) !",hwAddress,fMaxHWAddress));
196     return -1;
197   }
198   Int_t row = fMapping[3*hwAddress];
199   if (row == -1)
200     AliWarning(Form("Hardware (ALTRO) adress (%d) is not defined !",hwAddress));
201
202   return row;
203 }
204
205 //_____________________________________________________________________________
206 Int_t AliCaloAltroMapping::GetPad(Int_t hwAddress) const
207 {
208   // Return the column index
209   // Note the difference w.r.t to the base class notation
210   if (!fMapping) {
211     AliWarning("Mapping array was not initalized correctly !");
212     return -1;
213   }
214   if (hwAddress > fMaxHWAddress) {
215     AliWarning(Form("Hardware (ALTRO) adress (%d) outside the range (0 -> %d) !",hwAddress,fMaxHWAddress));
216     return -1;
217   }
218   Int_t col = fMapping[3*hwAddress+1];
219   if (col == -1)
220     AliWarning(Form("Hardware (ALTRO) adress (%d) is not defined !",hwAddress));
221
222   return col;
223 }
224
225 //_____________________________________________________________________________
226 Int_t AliCaloAltroMapping::GetSector(Int_t hwAddress) const
227 {
228   // Return the gain factor (0/1)
229   // Note the difference w.r.t to the base class notation
230   if (!fMapping) {
231     AliWarning("Mapping array was not initalized correctly !");
232     return -1;
233   }
234   if (hwAddress > fMaxHWAddress) {
235     AliWarning(Form("Hardware (ALTRO) adress (%d) outside the range (0 -> %d) !",hwAddress,fMaxHWAddress));
236     return -1;
237   }
238   Int_t gain = fMapping[3*hwAddress+2];
239   if (gain == -1)
240     AliWarning(Form("Hardware (ALTRO) adress (%d) is not defined !",hwAddress));
241
242   return gain;
243 }
244
245 //_____________________________________________________________________________
246 void AliCaloAltroMapping::DeleteMappingArrays()
247 {
248   // Deletes the arrays which have been
249   // allocated during the reading of the
250   // mapping file
251   if (fMapping) delete [] fMapping;
252
253   if (fInvMappingLow) delete [] fInvMappingLow;
254
255   if (fInvMappingHigh) delete [] fInvMappingHigh;
256 }