Changes for report #76063 Commit to trunk + v4-20-Release
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerRawDigitMaker.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 /*
17  
18
19
20  
21 Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
22 */
23
24 #include "AliEMCALTriggerRawDigitMaker.h"
25 #include "AliEMCALTriggerSTURawStream.h"
26 #include "AliCaloRawAnalyzerFakeALTRO.h"
27 #include "AliEMCALTriggerRawDigit.h"
28 #include "AliCaloRawStreamV3.h"
29 #include "AliRun.h"
30 #include "AliRunLoader.h"
31 #include "AliEMCAL.h"
32 #include "AliCaloBunchInfo.h"
33 #include "AliRawReader.h"
34 #include "AliEMCALTriggerDCSConfigDB.h"
35 #include "AliEMCALTriggerData.h"
36 #include "AliEMCALTriggerPatch.h"
37 #include "AliLog.h"
38
39 #include "AliRawDataHeader.h"
40 #include "AliRawVEvent.h"
41 #include "AliRawEventHeaderBase.h"
42 #include "AliRawEvent.h"
43 #include "AliRawVEquipment.h"
44 #include "AliRawEquipmentHeader.h"
45
46 namespace
47 {
48         const Int_t kSTUEqId = 4652;
49 }
50
51 ClassImp(AliEMCALTriggerRawDigitMaker)
52
53 //_______________
54 AliEMCALTriggerRawDigitMaker::AliEMCALTriggerRawDigitMaker() : TObject(),
55 fGeometry(0x0),
56 fRawReader(0x0),
57 fCaloRawStream(0x0),
58 fSTURawStream(0x0),
59 fRawDigits(0x0),
60 fRawAnalyzer(0x0),
61 fDCSConfig(0x0),
62 fTriggerData(0x0)
63 {
64   // def ctor
65   
66   AliRunLoader* rl = AliRunLoader::Instance();
67   if (rl && rl->GetAliRun()){
68     AliEMCAL * emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
69     if(emcal) fGeometry = emcal->GetGeometry();
70   }
71   
72   if(!fGeometry)
73     {
74       AliDebug(1, Form("Using default geometry"));
75       fGeometry =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
76     }
77   
78   fRawAnalyzer = new AliCaloRawAnalyzerFakeALTRO();
79   
80   fDCSConfig = AliEMCALTriggerDCSConfigDB::Instance();
81   
82   for (Int_t i=0; i<3072; i++) fRawDigitIndex[i] = -1;
83 }       
84
85 //_______________
86 AliEMCALTriggerRawDigitMaker::~AliEMCALTriggerRawDigitMaker()
87 {
88         // dtor
89 }
90
91 //_______________
92 void AliEMCALTriggerRawDigitMaker::SetIO(AliRawReader* reader, AliCaloRawStreamV3& in, AliEMCALTriggerSTURawStream& inSTU, TClonesArray* digits, AliEMCALTriggerData* data)
93 {
94         //
95         fRawReader     = reader;
96         fCaloRawStream = &in;
97         fRawDigits     = digits;
98         fSTURawStream  = &inSTU;
99         fTriggerData   = data;
100 }
101
102 //_______________
103 void AliEMCALTriggerRawDigitMaker::Add(const std::vector<AliCaloBunchInfo> &bunchlist)
104 {
105         //
106         Int_t    hwAdd   = fCaloRawStream->GetHWAddress();
107         UShort_t iRCU    = fCaloRawStream->GetDDLNumber() % 2; // 0/1
108         UShort_t iBranch = ( hwAdd >> 11 ) & 0x1;              // 0/1
109         
110         // TRU id       
111         Int_t iTRU = ( (iRCU << 1) | iBranch ) - 1; // 0..2
112         
113         iTRU  = (fCaloRawStream->GetModule() % 2) ? 2 * (2 - iTRU) + 1 : 2 * iTRU;
114         
115         iTRU += 6 * int(fCaloRawStream->GetModule()/2);
116         
117         if (AliDebugLevel())
118         {
119                 printf("===\n");
120                 printf("| Hw Adress: 0x%x => SM# %2d / RCU# %d / Branch# %d / TRU# %2d / ADC# %2d\n",
121                            hwAdd, fCaloRawStream->GetModule(), iRCU, iBranch, iTRU, fCaloRawStream->GetColumn());
122         }
123         
124         Int_t idx;
125         
126         AliEMCALTriggerRawDigit* dig = 0x0;     
127         
128         Int_t timeSamples[256]; for (Int_t j=0; j<256; j++) timeSamples[j] = 0;
129         Int_t nSamples = 0;
130         
131         UInt_t iBin   = bunchlist.at(0).GetStartBin();
132          Int_t iBunch = 0;
133         
134         for (UInt_t i = 0; i < bunchlist.size(); i++)
135         {
136                 AliCaloBunchInfo bunch = bunchlist.at(i);
137                 
138                 if (iBin > bunch.GetStartBin()) 
139                 {
140                         iBin   = bunch.GetStartBin();
141                         iBunch = i;
142                 }
143                 
144                 if (fCaloRawStream->GetColumn() < 96)
145                 {
146                         const UShort_t* sig = bunch.GetData();
147                         Int_t startBin = bunch.GetStartBin();
148                         
149                         for (Int_t iS = 0; iS < bunch.GetLength(); iS++) 
150                         {
151                                 Int_t time = startBin--;
152                                 Int_t amp  = sig[iS];
153                                 
154                                 if (amp) timeSamples[nSamples++] = ((time << 12) & 0xFF000) | (amp & 0xFFF);
155                                 
156                                 if (AliDebugLevel())
157                                 {
158                                         printf("ADC# %2d / time: %2d amplitude: %d\n", fCaloRawStream->GetColumn(), time, amp);
159                                 }
160                         }
161                 }
162         }
163         
164         if (fCaloRawStream->GetColumn() > 95 && fCaloRawStream->GetColumn() < 106)
165         {
166                 Int_t nBits = (fCaloRawStream->GetColumn() == 105) ? 6 : 10;
167                 
168                 const UShort_t* sig = bunchlist.at(iBunch).GetData();
169                 
170                 if (AliDebugLevel()) printf("| L0 id in F-ALTRO => bunch length is: %d\n", bunchlist.at(iBunch).GetLength());
171                 
172                 for (Int_t i = 0; i < bunchlist.at(iBunch).GetLength(); i++) 
173                 {
174                         if (AliDebugLevel()) printf("| sig[%3d]: %x\n",i,sig[i]);
175                                                                                 
176                         for (Int_t j = 0; j < nBits; j++)
177                         {
178                                 if (sig[i] & ( 1 << j ))
179                                 {
180                                         if (AliDebugLevel()) 
181                                         {
182                                                 printf("| Add L0 patch index in TRU# %2d position %2d\n",iTRU,(fCaloRawStream->GetColumn() - 96) * 10 + j);
183                                         }
184                                         
185                                         if (fGeometry->GetAbsFastORIndexFromTRU(iTRU, (fCaloRawStream->GetColumn() - 96) * 10 + j, idx))
186                                         {
187                                                 if (fRawDigitIndex[idx] >= 0)
188                                                 {
189                                                         dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
190                                                 }
191                                                 else
192                                                 {
193                                                         AliDebug(100,"L0: Trying to update trigger info of a non-existent digit!");
194                                                         
195                                                         fRawDigitIndex[idx] = fRawDigits->GetEntriesFast();
196                                                         new((*fRawDigits)[fRawDigits->GetEntriesFast()]) AliEMCALTriggerRawDigit(idx, 0x0, 0);
197                                                         
198                                                         dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
199                                                 }
200                                                 
201                                                 dig->SetL0Time(iBin);
202                                         }
203                                 }
204                         }
205                         
206                         if (fCaloRawStream->GetColumn() == 105 && (sig[i] & (1 << 6))) 
207                         {
208                                 fTriggerData->SetL0Trigger(1, iTRU, 1);
209                                                                                    
210                                 if (AliDebugLevel()) printf("=======TRU# %2d has issued a L0\n",iTRU);
211                         }
212                         
213                         iBin--;
214                 }
215         } 
216         else
217         {
218                 if (nSamples && fGeometry->GetAbsFastORIndexFromTRU(iTRU, fCaloRawStream->GetColumn(), idx)) 
219                 {
220                         if (fRawDigitIndex[idx] < 0)
221                         {
222                                 fRawDigitIndex[idx] = fRawDigits->GetEntriesFast();
223                                 new((*fRawDigits)[fRawDigits->GetEntriesFast()]) AliEMCALTriggerRawDigit(idx, timeSamples, nSamples);
224                         }
225                         else
226                         {
227                                 dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
228                                 dig->SetTimeSamples(timeSamples, nSamples);
229                         }
230                         
231                         if (AliDebugLevel())
232                         {
233                                 printf("| Add TRG digit of id# %4d from TRU# %2d ADC# %2d\n", idx, iTRU, fCaloRawStream->GetColumn());
234
235                                 dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
236                                 dig->Print("");
237
238                                 Int_t iSm, iTru, iEta, iPhi, iD[4], iFor;
239                                 if (fGeometry->GetPositionInTRUFromAbsFastORIndex(idx, iTru, iEta, iPhi))
240                                 {
241                                         printf("| Position => TRU: %2d Eta: %2d Phi: %2d\n", iTru, iEta, iPhi);
242                                 }
243                                 
244                                 if (fGeometry->GetPositionInSMFromAbsFastORIndex(idx, iSm, iEta, iPhi))
245                                 {
246                                         printf("| Position =>  SM: %2d Eta: %2d Phi: %2d\n", iSm, iEta, iPhi);
247                                 }
248                                                                 
249                                 if (fGeometry->GetCellIndexFromFastORIndex(idx, iD))
250                                 {
251                                         printf("| tower iDs: ");
252                                         for (Int_t i = 0; i < 4; i++)
253                                         {
254                                                 printf("%5d ",iD[i]); 
255                                         }
256                                         printf("\n");
257                                         
258                                         for (Int_t i = 0; i < 4; i++)
259                                         {
260                                                 if (fGeometry->GetFastORIndexFromCellIndex(iD[i], iFor))
261                                                 {
262                                                         printf("| tower %d to F-OR %d\n",iD[i],iFor);
263                                                 }
264                                         }
265                                 }                               
266                         }
267                 }
268         }
269 }
270
271 //_______________
272 void AliEMCALTriggerRawDigitMaker::PostProcess()
273 {       
274         //
275         Int_t idx;
276         
277         AliEMCALTriggerRawDigit* dig = 0x0;
278         
279         Int_t sizeL1gsubr[2], sizeL1gpatch[2], sizeL1jsubr[2], sizeL1jpatch[2];
280         
281         fDCSConfig->GetSTUSegmentation(sizeL1gsubr, sizeL1gpatch, sizeL1jsubr, sizeL1jpatch);
282         
283         fRawReader->Reset();
284         fRawReader->Select("EMCAL",44); 
285
286         Bool_t STUin = kFALSE;
287         
288         Int_t nSubEv = fRawReader->GetEvent()->GetNSubEvents();
289         
290         for ( Int_t iSubEv=0; iSubEv<nSubEv; iSubEv++)
291         {
292                 AliRawVEvent *SubEv = ((AliRawEvent*)fRawReader->GetEvent())->GetSubEvent(iSubEv);
293                 if ( !SubEv ) continue;
294                 
295                 for (Int_t iEquip = 0; iEquip < SubEv->GetNEquipments(); iEquip++)
296                 {
297                         Int_t eqId = SubEv->GetEquipment(iEquip)->GetEquipmentHeader()->GetId();
298                         
299                         if (eqId == kSTUEqId) STUin = kTRUE;
300                 }
301         }
302         
303         fRawReader->Reset();
304         
305         if (STUin && fSTURawStream && fSTURawStream->ReadPayLoad())
306         {
307                 fTriggerData->SetL1GammaThreshold(fSTURawStream->GetL1GammaThreshold());
308                 fTriggerData->SetL1JetThreshold(  fSTURawStream->GetL1JetThreshold()  );
309                 
310                 Int_t iTRU, x, y;
311
312                 if (fSTURawStream->GetRawData())
313                 {
314                         if (AliDebugLevel()) printf("| STU => TRU raw data are there!\n");
315                         
316                         for (Int_t i = 0; i < 32; i++)
317                         {
318                                 iTRU = fGeometry->GetTRUIndexFromSTUIndex(i);
319                                 
320                                 UInt_t adc[96]; for (Int_t j = 0; j < 96; j++) adc[j] = 0;
321                                 
322                                 fSTURawStream->GetADC(i, adc);
323                                 
324                                 for (Int_t j = 0; j < 96; j++)
325                                 {
326                                         //if (adc[j] < 5) continue;
327                                         
328                                         if (AliDebugLevel()) printf("| STU => TRU# %2d raw data: ADC# %2d: %d\n", iTRU, j, adc[j]);
329                                         
330                                         fGeometry->GetAbsFastORIndexFromTRU(iTRU, j, idx);
331                                         
332                                         if (fRawDigitIndex[idx] >= 0)
333                                         {
334                                                 dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
335                                                 
336                                                 if (!dig->GetNSamples()) AliDebug(10,Form("TRG digit of id: %4d found in STU but has no time sample in F-ALTRO!",idx));
337                                         }
338                                         else
339                                         {
340                                                 AliDebug(10,Form("TRG digit of id: %4d found in STU but not in F-ALTRO! Create a new digit!",idx));
341                                                 
342                                                 fRawDigitIndex[idx] = fRawDigits->GetEntriesFast();
343                                                 new((*fRawDigits)[fRawDigits->GetEntriesFast()]) AliEMCALTriggerRawDigit(idx, 0x0, 0);
344                                                 
345                                                 dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
346                                         }
347                                         
348                                         dig->SetL1TimeSum(adc[j]);
349                                 }
350                         }
351                 }
352                 
353                 // List of patches in EMCal coordinate system
354                 
355                 for (Int_t i = 0; i < fSTURawStream->GetNL0GammaPatch(); i++)
356                 {
357                         fSTURawStream->GetL0GammaPatch(i, iTRU, x);
358                         
359                         iTRU = fGeometry->GetTRUIndexFromSTUIndex(iTRU);
360                         
361                         if (AliDebugLevel()) printf("| STU => Found L0 patch id: %2d in TRU# %2d\n", x, iTRU);
362                         
363                         const Int_t sizePatchL0 = fDCSConfig->GetTRUSegmentation(iTRU) * fDCSConfig->GetTRUSegmentation(iTRU);
364                         
365                         Int_t* idFastOR = new Int_t[sizePatchL0]; 
366                         
367                         for (Int_t j = 0; j < sizePatchL0; j++) idFastOR[j] = -1;
368                         
369                         if (fGeometry->GetFastORIndexFromL0Index(iTRU, x, idFastOR, sizePatchL0))
370                         {
371                                 idx = idFastOR[1];
372                                 
373                                 Int_t px, py;
374                                 if (fGeometry->GetPositionInEMCALFromAbsFastORIndex(idx, px, py))
375                                 {
376                                         if (AliDebugLevel()) printf("| STU => Add L0 patch at (%2d , %2d)\n", px, py);
377                                                                                 
378                                         if (fRawDigitIndex[idx] >= 0)
379                                         {
380                                                 dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
381                                 }
382                                         else
383                                         {
384                                                 fRawDigitIndex[idx] = fRawDigits->GetEntriesFast();
385                                                 new((*fRawDigits)[fRawDigits->GetEntriesFast()]) AliEMCALTriggerRawDigit(idx, 0x0, 0);
386                         
387                                                 dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
388                 }
389                 
390                                         dig->SetTriggerBit(kL0,1);
391                                 }
392                         }
393                 
394                         delete [] idFastOR;
395                 }
396                 
397                 for (Int_t i = 0; i < fSTURawStream->GetNL1GammaPatch(); i++)
398                 {
399                         if (fSTURawStream->GetL1GammaPatch(i, iTRU, x, y)) // col (0..23), row (0..3)
400                         {
401                                 iTRU = fGeometry->GetTRUIndexFromSTUIndex(iTRU);
402                         
403                                 if (AliDebugLevel()) printf("| STU => Found L1 gamma patch at (%2d , %2d) in TRU# %2d\n", x, y, iTRU);
404                                 
405                                 Int_t vx = 23 - x, vy = y + 4 * int(iTRU / 2); // Position in EMCal frame
406                                 
407                                 if (iTRU % 2) vx += 24; // C side
408                                 
409                                 vx = vx - sizeL1gsubr[0] * sizeL1gpatch[0] + 1;
410                                 
411                                 if (vx >= 0) 
412                                 {
413                                         if (fGeometry->GetAbsFastORIndexFromPositionInEMCAL(vx, vy, idx))
414                                         {
415                                         if (AliDebugLevel()) printf("| STU => Add L1 gamma patch at (%2d , %2d)\n", vx, vy);
416                                                 
417                                                 if (fRawDigitIndex[idx] >= 0)
418                                                 {
419                                                         dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
420                                 }
421                                                 else
422                                                 {
423                                                         fRawDigitIndex[idx] = fRawDigits->GetEntriesFast();
424                                                         new((*fRawDigits)[fRawDigits->GetEntriesFast()]) AliEMCALTriggerRawDigit(idx, 0x0, 0);
425
426                                                         dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
427                                                 }
428                 
429                                                 dig->SetTriggerBit(kL1Gamma,1);
430                                         }
431                                 }
432                         }
433                 }
434                 
435                 for (Int_t i = 0; i < fSTURawStream->GetNL1JetPatch(); i++)
436                 {
437                         if (fSTURawStream->GetL1JetPatch(i, x, y)) // col (0,15), row (0,11)
438                         {
439                                 if (AliDebugLevel()) printf("| STU => Found L1 jet patch at (%2d , %2d)\n", x, y);
440                                 
441                                 Int_t ix = sizeL1jsubr[0] * (11 - y - sizeL1jpatch[0] + 1);
442
443                                 Int_t iy = sizeL1jsubr[1] * (15 - x - sizeL1jpatch[1] + 1);
444                                 
445                                 // FIXME: x = 0 || y = 0 (Olivier's CS) patches a lost?
446                                 
447                                 if (ix >= 0 && iy >= 0)
448                                 {       
449                                         if (fGeometry->GetAbsFastORIndexFromPositionInEMCAL(ix, iy, idx))
450                                         {
451                                         if (AliDebugLevel()) printf("| STU => Add L1 jet patch at (%2d , %2d)\n", ix, iy);
452                 
453                                                 if (fRawDigitIndex[idx] >= 0)
454                                                 {
455                                                         dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
456                                                 }
457                                                 else
458                                                 {
459                                                         fRawDigitIndex[idx] = fRawDigits->GetEntriesFast();
460                                                         new((*fRawDigits)[fRawDigits->GetEntriesFast()]) AliEMCALTriggerRawDigit(idx, 0x0, 0);
461                 
462                                                         dig = (AliEMCALTriggerRawDigit*)fRawDigits->At(fRawDigitIndex[idx]);
463                                                 }
464                 
465                                                 dig->SetTriggerBit(kL1Jet,1);
466                                         }
467                                 }
468                         }
469                 }               
470         }
471 }
472
473 //_______________
474 void AliEMCALTriggerRawDigitMaker::Reset()
475 {
476         //      
477         for (Int_t i = 0; i < 3072; i++) fRawDigitIndex[i] = -1;
478 }
479
480