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