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