]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/macros/GeneratePedestalScript.C
pdf figures to compile with pdflatex
[u/mrichter/AliRoot.git] / EMCAL / macros / GeneratePedestalScript.C
1 // some global var/constants
2 const Int_t kNSM = 4; // for first LHC run
3 const Int_t kNRCU = 2;
4 AliCaloAltroMapping *fMapping[4]; // 1 for each side (A/C) and each RCU (0/1), i.e. 2*2 total
5 const Int_t kNBranch = 2;
6 const Int_t kNFEC = 10; // 0..9, when including LED Ref
7 const Int_t kNChip = 5; // really 0,2..4, i.e. skip #1
8 const Int_t kNChan = 16;
9 Float_t fMeanPed[kNSM][kNRCU][kNBranch][kNFEC][kNChip][kNChan];
10 Float_t fRmsPed[kNSM][kNRCU][kNBranch][kNFEC][kNChip][kNChan];
11 //
12 const int kNStrips = 24; // per SM
13 Int_t fHWAddrLEDRef[kNStrips][2]; // [2] is for Low/High gain
14
15 const Bool_t kDebug = kFALSE;
16 const Float_t kBadRMS = 20;
17
18 // help methods
19 void GetPedVal(const Int_t iSM, const Int_t igain, const TProfile2D *h2);
20 void GetPedValLEDRef(const Int_t iSM, const Int_t igain, const TProfile *h);
21 void PrintScript();
22 void Clear();
23 Int_t GetHWAddress(Int_t iside, Int_t icol, Int_t irow, Int_t igain);
24 Int_t GetHWAddressLEDRef(Int_t istrip, Int_t igain);
25 void DecodeHWAddress(Int_t hwAddr, Int_t & branch, Int_t & FEC, Int_t & chip, Int_t & chan);
26 void GetMapping();
27 void CreateMappingLEDRef();
28
29 // main method
30 void 
31 //GeneratePedestalScript(const char * filename = "alien/Run113790_113790_v1_s0.root") // 1st set
32 GeneratePedestalScript(const char * filename = "Run117756_117756_v1_s0.root") // 2nd set
33 {
34   // get the DA info/object
35   TFile *file = TFile::Open(filename); 
36   //TMP  AliCaloCalibPedestal *emcCalibPedestal = AliCDBEntry->GetObject();
37   if (kDebug) {
38     file->ls();
39   }
40
41   // Get mapping file info, and clear arrays
42   Clear();
43   GetMapping();
44   CreateMappingLEDRef();
45
46   // Store the pedestal info
47   for (Int_t iSM=0; iSM<kNSM; iSM++) {
48     GetPedVal( iSM, 0, emcCalibPedestal->GetPedProfileLowGain(iSM) );
49     GetPedVal( iSM, 1, emcCalibPedestal->GetPedProfileHighGain(iSM) );
50     GetPedValLEDRef( iSM, 0, emcCalibPedestal->GetPedLEDRefProfileLowGain(iSM) );
51     GetPedValLEDRef( iSM, 1, emcCalibPedestal->GetPedLEDRefProfileHighGain(iSM) );
52   }
53
54   // Generate the needed scripts
55   PrintScript();
56
57 }
58
59 void 
60 GetPedVal(const Int_t iSM, const Int_t igain, const TProfile2D *h2)
61 {
62   Int_t isect = iSM / 2; //
63   Int_t iside = iSM % 2; // A or C side
64   Int_t nCols = h2->GetNbinsX();
65   Int_t nRows = h2->GetNbinsY();
66   if (kDebug) {
67     printf("GetPedVal: iSM %d isect %d iside %d igain %d nRows %d nCols %d\n",
68            iSM, isect, iside, igain, nRows, nCols);
69   }
70   Int_t hwAddress = 0;
71   Int_t iRCU = 0;
72   Int_t branch = 0;
73   Int_t FEC = 0;
74   Int_t chip = 0;
75   Int_t chan = 0;
76
77   Int_t icol = 0;
78   Int_t irow = 0;
79   Int_t bin = 0;
80
81   for (icol=0; icol<nCols; icol++) {
82     for (irow=0; irow<nRows; irow++) {
83
84       hwAddress = GetHWAddress(iside, icol, irow, igain, iRCU);
85       DecodeHWAddress(hwAddress, branch, FEC, chip, chan);  
86       bin = h2->FindBin(icol, irow);
87             
88       // store the values
89       fMeanPed[iSM][iRCU][branch][FEC][chip][chan] = h2->GetBinContent(bin);
90       fRmsPed[iSM][iRCU][branch][FEC][chip][chan] = h2->GetBinError(bin);
91
92       // report bad RMS channels:
93       if (h2->GetBinError(bin) > kBadRMS) {
94         printf(" bad pedestal RMS: iSM %d icol %d irow %d igain %d iRCU %d branch %d FEC %d chip %d chan %d - mean %4.1f rms %4.1f\n", 
95                iSM, icol, irow, igain, iRCU, branch, FEC, chip, chan,
96                h2->GetBinContent(bin), h2->GetBinError(bin));
97       }
98     }
99   }
100
101   return;
102 }
103
104
105 void 
106 GetPedValLEDRef(const Int_t iSM, const Int_t igain, const TProfile *h)
107 {
108   Int_t isect = iSM / 2; //
109   Int_t iside = iSM % 2; // A or C side
110   Int_t nStrips = h->GetNbinsX();
111
112   if (kDebug) {
113     printf("GetPedValLEDRef: iSM %d isect %d iside %d igain %d nStrips %d\n",
114            iSM, isect, iside, igain, nStrips);
115   }
116   Int_t hwAddress = 0;
117   Int_t iRCU = 0; // always true for LED Ref FEE
118   Int_t branch = 0;
119   Int_t FEC = 0;
120   Int_t chip = 0;
121   Int_t chan = 0;
122
123   Int_t icol = 0;
124   Int_t irow = 0;
125   Int_t bin = 0;
126
127   for (int istrip=0; istrip<nStrips; istrip++) {
128
129     hwAddress = GetHWAddressLEDRef(istrip, igain);
130     DecodeHWAddress(hwAddress, branch, FEC, chip, chan);  
131     bin = h->FindBin(istrip);
132             
133     // store the values
134     fMeanPed[iSM][iRCU][branch][FEC][chip][chan] = h->GetBinContent(bin);
135     fRmsPed[iSM][iRCU][branch][FEC][chip][chan] = h->GetBinError(bin);
136   }
137
138   return;
139 }
140
141 void 
142 PrintScript()
143 {
144   const char * sideStr[] = {"A","C"};
145   const char * branchStr[] = {"A","B"};
146   int VFPED = 0x06;
147   int RCUWrite = 0x200000;
148
149   char filename[100];
150   char scriptLine[200];
151
152   Int_t iSM = 0;
153   Int_t iRCU = 0;
154   Int_t ibranch = 0;
155   Int_t iFEC = 0;
156   Int_t ichip = 0;
157   Int_t ichan = 0;
158   Int_t Ped = 0;
159
160   for (iSM=0; iSM<kNSM; iSM++) {
161     int iside = iSM % 2;
162     int isect = iSM / 2;
163     for (iRCU=0; iRCU<kNRCU; iRCU++) {
164       sprintf(filename, "setSM%1s%dRCU%d.scr", 
165               sideStr[iside], isect, iRCU);
166       ofstream fout(filename);
167       int nscriptLines = 0;
168
169       for (ibranch=0; ibranch<kNBranch; ibranch++) {
170         int firstFEC = 1; 
171         if (ibranch==0 && iRCU==0) { // extra LED Ref FEE in this location
172           firstFEC = 0;
173         }
174
175         for (iFEC=firstFEC; iFEC<kNFEC; iFEC++) { // FEC 1..9 (or 0..9)
176           for (ichip=0; ichip<kNChip; ichip++) { // ALTRO 0,2,3,4
177             if (ichip!=1) {
178               for (ichan=0; ichan<kNChan; ichan++) {
179
180                 if (iFEC!=0 || (ichan<8 || ichan>11)) {
181
182                   Ped = TMath::Nint(fMeanPed[iSM][iRCU][ibranch][iFEC][ichip][ichan]);
183                   // raise Ped value to max for channels with exceptionally large RMS
184                   if (fRmsPed[iSM][iRCU][ibranch][iFEC][ichip][ichan] > kBadRMS) {
185                     printf(" bad pedestal RMS: iSM %d iRCU %d ibranch %d iFEC %d ichip %d ichan %d - raising from %d to 0x3ff\n", 
186                            iSM, iRCU, ibranch, iFEC, ichip, ichan, Ped);
187                     Ped = 0x3ff;
188                   }
189                   // 
190                   int writeAddr = (ibranch << 16) | (iFEC << 12) | (ichip << 9) |
191                     (ichan << 5) | VFPED | RCUWrite;  
192                   sprintf(scriptLine, "w 0x%04x 0x%06x  # Branch %s, Card %d, Altro %d, Chan %d", 
193                           nscriptLines, writeAddr, branchStr[ibranch],
194                           iFEC, ichip, ichan);
195                   fout << scriptLine << endl;
196                   nscriptLines++;
197                   
198                   int writeVal = (Ped | RCUWrite);
199                   sprintf(scriptLine, "w 0x%04x 0x%06x  # Pedestal 0x%x = %d", 
200                           nscriptLines, writeVal, Ped, Ped);
201                   fout << scriptLine << endl;
202                   nscriptLines++;
203                   
204                 }
205               }
206             }
207           }
208         }
209       }
210       // ending, with execute and update step..     
211       sprintf(scriptLine, "w 0x%04x 0x380000  # End of the sequence", 
212               nscriptLines);
213       fout << scriptLine << endl;
214       nscriptLines++;
215
216       sprintf(scriptLine, "w 0x%04x 0x3F0000  # End of the instruction memory", 
217               nscriptLines);
218       fout << scriptLine << endl;
219       nscriptLines++;
220
221       fout << "wait 100 us" << endl;
222       fout << "w 0x5304 0x0       \# execute and update registers" << endl; 
223       fout.close();
224     } // iRCU
225   }// iSM
226
227   return;
228 }
229
230 void 
231 Clear()
232 {
233   for (Int_t iSM=0; iSM<kNSM; iSM++) {
234     for (Int_t iRCU=0; iRCU<kNRCU; iRCU++) {
235       for (Int_t ibranch=0; ibranch<kNBranch; ibranch++) {
236         for (Int_t iFEC=0; iFEC<kNFEC; iFEC++) {
237           for (Int_t ichip=0; ichip<kNChip; ichip++) {
238             for (Int_t ichan=0; ichan<kNChan; ichan++) {
239               fMeanPed[iSM][iRCU][ibranch][iFEC][ichip][ichan] = 0;
240               fRmsPed[iSM][iRCU][ibranch][iFEC][ichip][ichan] = 0;
241             }
242           }
243         }
244       }
245     }
246   }
247
248   for (int istrip=0; istrip<kNStrips; istrip++) {
249     fHWAddrLEDRef[istrip][0] = 0; 
250     fHWAddrLEDRef[istrip][1] = 0; 
251   }
252
253   return;
254 }
255
256 void 
257 DecodeHWAddress(Int_t hwAddr, Int_t & branch, Int_t & FEC, Int_t & chip, Int_t & chan)
258 {
259   chan = hwAddr & 0xf;
260   chip = (hwAddr >> 4) & 0x7;
261   FEC = (hwAddr >> 7) & 0xf;
262   branch = (hwAddr >> 11) & 0x1;
263   return;
264 }
265
266 Int_t 
267 GetHWAddress(Int_t iside, Int_t icol, Int_t irow, Int_t igain, Int_t & iRCU)
268 {
269   iRCU = -111;
270
271   //RCU0
272   if (0<=irow&&irow<8) iRCU=0; // first cable row
273   else if (8<=irow&&irow<16 && 0<=icol&&icol<24) iRCU=0; // first half; 
274   //second cable row
275   //RCU1
276   else if(8<=irow&&irow<16 && 24<=icol&&icol<48) iRCU=1; // second half; 
277   //second cable row
278   else if(16<=irow&&irow<24) iRCU=1; // third cable row
279
280   // swap for odd=C side, to allow us to cable both sides the same
281   Int_t iRCUSide = iRCU; 
282  if (iside == 1) {
283     iRCU = 1 - iRCU;
284     iRCUSide = iRCU + 2; // to make it map file index
285   }
286   Int_t hwAddress = fMapping[iRCUSide]->GetHWAddress(irow, icol, igain);
287
288   return hwAddress;
289 }
290
291 Int_t 
292 GetHWAddressLEDRef(Int_t istrip, Int_t igain)
293 {
294   Int_t iRCU = 0; // for both sides; LED ref info is the same for both sides
295   Int_t caloflag = 3; // AliCaloRawStreamV3::kLEDMonData;
296  
297   Int_t hwAddress = fHWAddrLEDRef[istrip][igain];
298
299   return hwAddress;
300 }
301
302 void 
303 GetMapping()
304 {  
305   TString sides[]={"A","C"};
306   // Read mapping files from $ALICE_ROOT/CALO/mapping/*.data
307   TString path = gSystem->Getenv("ALICE_ROOT");
308   path += "/EMCAL/mapping/RCU";
309   TString path2;
310   for(Int_t j = 0; j < 2; j++){ // sides
311     for(Int_t i = 0; i < 2; i++) { // RCU
312       path2 = path;
313       path2 += i;
314       path2 += sides[j];
315       path2 += ".data";
316       if (kDebug) { printf("Mapping file: %s\n",path2.Data()); }
317       fMapping[j*2 + i] = new AliCaloAltroMapping(path2.Data());
318     }
319   }
320   return;
321 }
322
323 void 
324 CreateMappingLEDRef()
325
326   Int_t iRCU = 0; // for both sides; LED ref info is the same for both sides
327   Int_t caloflag = 3; // AliCaloRawStreamV3::kLEDMonData;
328
329   Int_t maxAddr = 1 << 7; // LED Ref FEE is in FEC pos 0, i.e. addr space 0..127
330
331   int nLEDRefFEEChan = 0;
332
333   Int_t branch = 0;
334   Int_t FEC = 0;
335   Int_t chip = 0;
336   Int_t chan = 0;
337   for (int hwaddr = 0; hwaddr<maxAddr; hwaddr++) {
338
339     DecodeHWAddress(hwaddr, branch, FEC, chip, chan);  
340     if ( (chip!=1 && chip<kNChip) &&  // ALTROs 0,2,3,4
341          (chan<8 || chan>11) ) { // actual installed LED Ref FEE channels
342
343       int istrip = fMapping[iRCU]->GetPad(hwaddr);
344       int igain = fMapping[iRCU]->GetPadRow(hwaddr);
345       int iflag = fMapping[iRCU]->GetSector(hwaddr);
346       if (iflag == caloflag) {
347         fHWAddrLEDRef[istrip][igain] = hwaddr; 
348         nLEDRefFEEChan++;
349       }
350     }
351   }
352
353   if (kDebug) { cout << " nLEDRefFEEChan " << nLEDRefFEEChan << endl; }
354 }