]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/macros/GeneratePedestalScriptSRU.C
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / EMCAL / macros / GeneratePedestalScriptSRU.C
1 // some global var/constants
2 const Int_t kNSM = 12; // 
3 const Int_t kNRCU = 2;
4 const Int_t kNDTC = 40; // links for full SRU (corresponding to two readout crates or RCUs)
5 AliCaloAltroMapping *fMapping[4]; // 1 for each side (A/C) and each RCU (0/1), i.e. 2*2 total
6 const Int_t kNBranch = 2;
7 const Int_t kNFEC = 10; // 0..9, when including LED Ref
8 const Int_t kNChip = 5; // really 0,2..4, i.e. skip #1
9 const Int_t kNChan = 16;
10 Float_t fMeanPed[kNSM][kNRCU][kNBranch][kNFEC][kNChip][kNChan];
11 Float_t fRmsPed[kNSM][kNRCU][kNBranch][kNFEC][kNChip][kNChan];
12 //
13 const int kNStrips = 24; // per SM
14 Int_t fHWAddrLEDRef[kNStrips][2]; // [2] is for Low/High gain
15
16 const Bool_t kDebug = kFALSE;
17 const Float_t kBadRMS = 20;
18
19 // help methods
20 void GetPedVal(const Int_t iSM, const Int_t igain, const TProfile2D *h2);
21 void GetPedValLEDRef(const Int_t iSM, const Int_t igain, const TProfile *h);
22 void PrintScript(const Int_t runno);
23 void Clear();
24 Int_t GetHWAddress(Int_t iside, Int_t icol, Int_t irow, Int_t igain);
25 Int_t GetHWAddressLEDRef(Int_t istrip, Int_t igain);
26 void DecodeHWAddress(Int_t hwAddr, Int_t & branch, Int_t & FEC, Int_t & chip, Int_t & chan);
27 void GetMapping();
28 void CreateMappingLEDRef();
29
30 // main method
31 void 
32 GeneratePedestalScriptSRU(const char * filename = "EMCALPED.root") // from DA node
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   int runno = emcCalibPedestal->GetRunNumber();
41
42   // Get mapping file info, and clear arrays
43   Clear();
44   GetMapping();
45   CreateMappingLEDRef();
46
47   // Store the pedestal info
48   for (Int_t iSM=0; iSM<kNSM; iSM++) {
49     GetPedVal( iSM, 0, emcCalibPedestal->GetPedProfileLowGain(iSM) );
50     GetPedVal( iSM, 1, emcCalibPedestal->GetPedProfileHighGain(iSM) );
51     GetPedValLEDRef( iSM, 0, emcCalibPedestal->GetPedLEDRefProfileLowGain(iSM) );
52     GetPedValLEDRef( iSM, 1, emcCalibPedestal->GetPedLEDRefProfileHighGain(iSM) );
53   }
54
55   // Generate the needed scripts
56   PrintScript(runno);
57
58 }
59
60 void 
61 GetPedVal(const Int_t iSM, const Int_t igain, const TProfile2D *h2)
62 {
63   Int_t isect = iSM / 2; //
64   Int_t iside = iSM % 2; // A or C side
65   Int_t nCols = h2->GetNbinsX();
66   Int_t nRows = h2->GetNbinsY();
67   if (kDebug) {
68     printf("GetPedVal: iSM %d isect %d iside %d igain %d nRows %d nCols %d\n",
69            iSM, isect, iside, igain, nRows, nCols);
70   }
71   Int_t hwAddress = 0;
72   Int_t iRCU = 0;
73   Int_t branch = 0;
74   Int_t FEC = 0;
75   Int_t chip = 0;
76   Int_t chan = 0;
77
78   Int_t icol = 0;
79   Int_t irow = 0;
80   Int_t bin = 0;
81
82   for (icol=0; icol<nCols; icol++) {
83     for (irow=0; irow<nRows; irow++) {
84
85       hwAddress = GetHWAddress(iside, icol, irow, igain, iRCU);
86       DecodeHWAddress(hwAddress, branch, FEC, chip, chan);  
87       bin = h2->FindBin(icol, irow);
88             
89       // store the values
90       fMeanPed[iSM][iRCU][branch][FEC][chip][chan] = h2->GetBinContent(bin);
91       fRmsPed[iSM][iRCU][branch][FEC][chip][chan] = h2->GetBinError(bin);
92
93       // report bad RMS channels:
94       if (h2->GetBinError(bin) > kBadRMS) {
95         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", 
96                iSM, icol, irow, igain, iRCU, branch, FEC, chip, chan,
97                h2->GetBinContent(bin), h2->GetBinError(bin));
98       }
99     }
100   }
101
102   return;
103 }
104
105
106 void 
107 GetPedValLEDRef(const Int_t iSM, const Int_t igain, const TProfile *h)
108 {
109   Int_t isect = iSM / 2; //
110   Int_t iside = iSM % 2; // A or C side
111   Int_t nStrips = h->GetNbinsX();
112
113   if (kDebug) {
114     printf("GetPedValLEDRef: iSM %d isect %d iside %d igain %d nStrips %d\n",
115            iSM, isect, iside, igain, nStrips);
116   }
117   Int_t hwAddress = 0;
118   Int_t iRCU = 0; // always true for LED Ref FEE
119   Int_t branch = 0;
120   Int_t FEC = 0;
121   Int_t chip = 0;
122   Int_t chan = 0;
123
124   Int_t icol = 0;
125   Int_t irow = 0;
126   Int_t bin = 0;
127
128   for (int istrip=0; istrip<nStrips; istrip++) {
129
130     hwAddress = GetHWAddressLEDRef(istrip, igain);
131     DecodeHWAddress(hwAddress, branch, FEC, chip, chan);  
132     bin = h->FindBin(istrip);
133             
134     // store the values
135     fMeanPed[iSM][iRCU][branch][FEC][chip][chan] = h->GetBinContent(bin);
136     fRmsPed[iSM][iRCU][branch][FEC][chip][chan] = h->GetBinError(bin);
137   }
138
139   return;
140 }
141
142 void 
143 PrintScript(const int runno)
144 {
145   const char * sideStr[] = {"A","C"};
146   const char * branchStr[] = {"A","B"};
147   int VFPED = 0x06;
148   int SRUAltroWrite = 0x40000000; 
149   int Port = 0x1001; // 4097
150
151   char dirname[40];
152   sprintf(dirname,"scriptsSRU_Run%d", runno);
153
154   char cmd[500];
155   sprintf(cmd,"mkdir -p %s/SM{A,C}{0,1,2,3,4,5}", dirname);
156   gSystem->Exec(cmd);
157
158   sprintf(cmd,"echo \"cp info.txt GeneratePedestalScriptSRU.C %s/.\" > scp.sh", dirname);
159   gSystem->Exec(cmd);
160   sprintf(cmd,"echo \"scp -r -p %s aldaqacr50:pedestals/.\" >> scp.sh", dirname);
161   gSystem->Exec(cmd);
162   sprintf(cmd,"echo \"ssh -t aldaqacr50 \'cd pedestals; scp -r -p %s emc@alidcscom702:srudcs/scripts/pedestals/.\'\" >> scp.sh", dirname);
163   gSystem->Exec(cmd);
164
165   char filename[100];
166   char scriptLine[200];
167
168   Int_t iSM = 0;
169   Int_t iRCU = 0;
170   Int_t ibranch = 0;
171   Int_t iFEC = 0;
172   Int_t ichip = 0;
173   Int_t ichan = 0;
174   Int_t Ped = 0;
175   Int_t iDTC = 0;
176
177   for (iSM=0; iSM<kNSM; iSM++) {
178     int iside = iSM % 2;
179     int isect = iSM / 2;
180
181     char IP[100];
182     if (iSM == 0) { sprintf(IP, "10.160.132.100"); } // SMA0 
183     else if (iSM == 1) { sprintf(IP, "10.160.132.102"); } // SMC0
184     else if (iSM == 2) { sprintf(IP, "10.160.132.104"); } // SMA1 
185     else if (iSM == 3) { sprintf(IP, "10.160.132.106"); } // SMC1
186     else if (iSM == 4) { sprintf(IP, "10.160.132.108"); } // SMA2 
187     else if (iSM == 5) { sprintf(IP, "10.160.132.110"); } // SMC2
188     else if (iSM == 6) { sprintf(IP, "10.160.132.112"); } // SMA3 
189     else if (iSM == 7) { sprintf(IP, "10.160.132.114"); } // SMC3
190     else if (iSM == 8) { sprintf(IP, "10.160.132.116"); } // SMA4 
191     else if (iSM == 9) { sprintf(IP, "10.160.132.118"); } // SMC4
192     else if (iSM == 10) { sprintf(IP, "10.160.36.155"); } // SMA5
193     else if (iSM == 11) { sprintf(IP, "10.160.36.156"); } // SMC5
194
195     // only do instrumented parts..
196     int activeDTC[kNDTC] = {0};
197     for (iDTC=0; iDTC<kNDTC; iDTC++) {
198       if (iDTC==10 || iDTC==20 || iDTC==30) { // skip TRU 
199         activeDTC[iDTC] = 0;
200       } 
201       else {
202         if (iSM<10) { // not special third SM
203           activeDTC[iDTC] = 1;
204         }
205         else {
206           if (iSM==10) { // SMA5
207             if (iDTC<14) { activeDTC[iDTC] = 1; }
208             else { activeDTC[iDTC] = 0; }
209           }
210           else if (iSM==11) { // SMC5
211             if (iDTC==0 || iDTC>=27) { activeDTC[iDTC] = 1; }
212             else { activeDTC[iDTC] = 0; }
213           }
214
215         }
216       }
217     }
218
219     // OK, let's generate the files for all active FECs/DTCs
220     for (iDTC=0; iDTC<kNDTC; iDTC++) {
221       if (activeDTC[iDTC] == 0) { continue; }
222       sprintf(filename, "%s/SM%1s%d/set_ped_DTC%02d.txt", 
223               dirname, sideStr[iside], isect, iDTC);
224       ofstream fout(filename);
225
226       iRCU = iDTC / 20;
227       ibranch = (iDTC % 20) / 10;
228       iFEC = iDTC % 10;
229       int ipos = iFEC + 10*ibranch;
230
231       // write DTC file header..
232       sprintf(scriptLine, "%s # IP\n%d       #UDP port", IP, Port);
233       fout << scriptLine << endl;
234
235       int dtcselUpper = 0;
236       int dtcselLower = 0;
237       if (iRCU == 0) {
238         dtcselLower = (1 << ipos);
239       }
240       else { // crate == 1
241         dtcselUpper = (1 << ipos);
242       }
243       sprintf(scriptLine, "%08x # DTC SEL Upper\n%08x # DTC SEL Lower",
244               dtcselUpper, dtcselLower);
245       fout << scriptLine << endl;
246       sprintf(scriptLine, "%08x # FEE GTL Address\n%08x # Branch %s, Card %d",
247               0x3, iFEC + 16*ibranch, branchStr[ibranch], iFEC);
248       fout << scriptLine << endl;
249       // end header
250
251       for (ichip=0; ichip<kNChip; ichip++) { // ALTRO 0,2,3,4
252         if (ichip!=1) {
253           for (ichan=0; ichan<kNChan; ichan++) {
254
255             if (iFEC!=0 || (ichan<8 || ichan>11)) {
256
257               Ped = TMath::Nint(fMeanPed[iSM][iRCU][ibranch][iFEC][ichip][ichan]);
258               // raise Ped value to max for channels with exceptionally large RMS
259               if (fRmsPed[iSM][iRCU][ibranch][iFEC][ichip][ichan] > kBadRMS) {
260                 printf(" bad pedestal RMS: iSM %d iRCU %d ibranch %d iFEC %d ichip %d ichan %d - raising from %d to 0x3ff\n", 
261                        iSM, iRCU, ibranch, iFEC, ichip, ichan, Ped);
262                 Ped = 0x3ff;
263               }
264               // 
265               int writeAddr = SRUAltroWrite | (ibranch << 16) | (iFEC << 12) | (ichip << 9) | (ichan << 5) | VFPED; 
266               sprintf(scriptLine, "%08x # Branch %s, Card %d, Altro %d, Chan %d", 
267                       writeAddr, branchStr[ibranch], iFEC, ichip, ichan);
268               fout << scriptLine << endl;
269               
270               int writeVal = Ped; 
271               sprintf(scriptLine, "%08x # Pedestal 0x%x = %d", 
272                        writeVal, Ped, Ped);
273               fout << scriptLine << endl;
274               
275             }
276           }
277         }
278       } // chip
279
280       fout.close();
281     } // iDTC
282   }// iSM
283
284   return;
285 }
286
287 void 
288 Clear()
289 {
290   for (Int_t iSM=0; iSM<kNSM; iSM++) {
291     for (Int_t iRCU=0; iRCU<kNRCU; iRCU++) {
292       for (Int_t ibranch=0; ibranch<kNBranch; ibranch++) {
293         for (Int_t iFEC=0; iFEC<kNFEC; iFEC++) {
294           for (Int_t ichip=0; ichip<kNChip; ichip++) {
295             for (Int_t ichan=0; ichan<kNChan; ichan++) {
296               fMeanPed[iSM][iRCU][ibranch][iFEC][ichip][ichan] = 0;
297               fRmsPed[iSM][iRCU][ibranch][iFEC][ichip][ichan] = 0;
298             }
299           }
300         }
301       }
302     }
303   }
304
305   for (int istrip=0; istrip<kNStrips; istrip++) {
306     fHWAddrLEDRef[istrip][0] = 0; 
307     fHWAddrLEDRef[istrip][1] = 0; 
308   }
309
310   return;
311 }
312
313 void 
314 DecodeHWAddress(Int_t hwAddr, Int_t & branch, Int_t & FEC, Int_t & chip, Int_t & chan)
315 {
316   chan = hwAddr & 0xf;
317   chip = (hwAddr >> 4) & 0x7;
318   FEC = (hwAddr >> 7) & 0xf;
319   branch = (hwAddr >> 11) & 0x1;
320   return;
321 }
322
323 Int_t 
324 GetHWAddress(Int_t iside, Int_t icol, Int_t irow, Int_t igain, Int_t & iRCU)
325 {
326   iRCU = -111;
327
328   //RCU0
329   if (0<=irow&&irow<8) iRCU=0; // first cable row
330   else if (8<=irow&&irow<16 && 0<=icol&&icol<24) iRCU=0; // first half; 
331   //second cable row
332   //RCU1
333   else if(8<=irow&&irow<16 && 24<=icol&&icol<48) iRCU=1; // second half; 
334   //second cable row
335   else if(16<=irow&&irow<24) iRCU=1; // third cable row
336
337   // swap for odd=C side, to allow us to cable both sides the same
338   Int_t iRCUSide = iRCU; 
339  if (iside == 1) {
340     iRCU = 1 - iRCU;
341     iRCUSide = iRCU + 2; // to make it map file index
342   }
343   Int_t hwAddress = fMapping[iRCUSide]->GetHWAddress(irow, icol, igain);
344
345   return hwAddress;
346 }
347
348 Int_t 
349 GetHWAddressLEDRef(Int_t istrip, Int_t igain)
350 {
351   Int_t iRCU = 0; // for both sides; LED ref info is the same for both sides
352   Int_t caloflag = 3; // AliCaloRawStreamV3::kLEDMonData;
353  
354   Int_t hwAddress = fHWAddrLEDRef[istrip][igain];
355
356   return hwAddress;
357 }
358
359 void 
360 GetMapping()
361 {  
362   TString sides[]={"A","C"};
363   // Read mapping files from $ALICE_ROOT/CALO/mapping/*.data
364   TString path = gSystem->Getenv("ALICE_ROOT");
365   path += "/EMCAL/mapping/RCU";
366   TString path2;
367   for(Int_t j = 0; j < 2; j++){ // sides
368     for(Int_t i = 0; i < 2; i++) { // RCU
369       path2 = path;
370       path2 += i;
371       path2 += sides[j];
372       path2 += ".data";
373       if (kDebug) { printf("Mapping file: %s\n",path2.Data()); }
374       fMapping[j*2 + i] = new AliCaloAltroMapping(path2.Data());
375     }
376   }
377   return;
378 }
379
380 void 
381 CreateMappingLEDRef()
382
383   Int_t iRCU = 0; // for both sides; LED ref info is the same for both sides
384   Int_t caloflag = 3; // AliCaloRawStreamV3::kLEDMonData;
385
386   Int_t maxAddr = 1 << 7; // LED Ref FEE is in FEC pos 0, i.e. addr space 0..127
387
388   int nLEDRefFEEChan = 0;
389
390   Int_t branch = 0;
391   Int_t FEC = 0;
392   Int_t chip = 0;
393   Int_t chan = 0;
394   for (int hwaddr = 0; hwaddr<maxAddr; hwaddr++) {
395
396     DecodeHWAddress(hwaddr, branch, FEC, chip, chan);  
397     if ( (chip!=1 && chip<kNChip) &&  // ALTROs 0,2,3,4
398          (chan<8 || chan>11) ) { // actual installed LED Ref FEE channels
399
400       int istrip = fMapping[iRCU]->GetPad(hwaddr);
401       int igain = fMapping[iRCU]->GetPadRow(hwaddr);
402       int iflag = fMapping[iRCU]->GetSector(hwaddr);
403       if (iflag == caloflag) {
404         fHWAddrLEDRef[istrip][igain] = hwaddr; 
405         nLEDRefFEEChan++;
406       }
407     }
408   }
409
410   if (kDebug) { cout << " nLEDRefFEEChan " << nLEDRefFEEChan << endl; }
411 }