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