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