cb183ab0f986e61715f75530596e61f1dd427305
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerElectronics.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 EMCal trigger electronics manager L0/L1
20 can handle both simulated digits and raw data
21 Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
22 */
23
24 #include "AliEMCALTriggerElectronics.h"
25 #include "AliEMCALTriggerTRU.h"
26 #include "AliEMCALTriggerSTU.h"
27 #include "AliEMCALGeometry.h"
28 #include "AliRunLoader.h"
29 #include "AliEMCAL.h" 
30 #include "AliRun.h" 
31 #include "AliEMCALTriggerDCSConfig.h"
32 #include "AliEMCALTriggerData.h"
33 #include "AliEMCALDigit.h"
34 #include "AliCaloRawStreamV3.h"
35 #include "AliEMCALTriggerSTURawStream.h"
36 #include "AliEMCALDigit.h"
37 #include "AliEMCALTriggerRawDigit.h"
38 #include "AliEMCALTriggerPatch.h"
39
40 #include <TVector2.h>
41 #include <TClonesArray.h>
42
43 namespace
44 {
45         const Int_t kNTRU = 32;
46 }
47
48 ClassImp(AliEMCALTriggerElectronics)
49
50 //__________________
51 AliEMCALTriggerElectronics::AliEMCALTriggerElectronics(const AliEMCALTriggerDCSConfig *dcsConf) : TObject(),
52 fTRU(new TClonesArray("AliEMCALTriggerTRU",32)),
53 fSTU(0x0)
54 {
55         TVector2 rSize;
56         
57         rSize.Set( 24.,  4. );
58
59         // 32 TRUs
60         for (Int_t i=0;i<kNTRU;i++) 
61         {
62                 AliEMCALTriggerTRUDCSConfig* truConf = dcsConf->GetTRUDCSConfig(i);
63                 new ((*fTRU)[i]) AliEMCALTriggerTRU(truConf, rSize, i % 2);
64         }
65         
66         rSize.Set( 48., 64. );
67         
68         // 1 STU
69         AliEMCALTriggerSTUDCSConfig* stuConf = dcsConf->GetSTUDCSConfig();
70         fSTU = new AliEMCALTriggerSTU(stuConf, rSize);
71         
72         TString str = "map";
73         for (Int_t i=0;i<kNTRU;i++) fSTU->Build(str,
74                                                                                         i,
75                                                                                         (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Map(),
76                                                                                         (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize() 
77                                                                                     );
78 }
79
80 //________________
81 AliEMCALTriggerElectronics::~AliEMCALTriggerElectronics()
82 {
83         //
84         fTRU->Delete();
85         delete fSTU;
86 }
87
88 //__________________
89 void AliEMCALTriggerElectronics::Digits2Trigger(TClonesArray* digits, const Int_t V0M[], AliEMCALTriggerData* data)
90 {
91         //
92         AliEMCALGeometry* geom = 0x0;
93         
94         AliRunLoader *rl = AliRunLoader::Instance();
95         if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")){
96           AliEMCAL* emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
97           if(emcal)geom = emcal->GetGeometry();
98         }
99         
100         if(!geom) geom =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
101         
102         if(!geom) AliError("Cannot access geometry!");
103         
104         //      digits->Sort();
105         
106         Int_t region[48][64], posMap[48][64];
107         for (Int_t i = 0; i < 48; i++) for (Int_t j = 0; j < 64; j++) 
108         {
109                 region[i][j] =  0;
110                 posMap[i][j] = -1;
111         }
112         
113         for (Int_t i = 0; i < digits->GetEntriesFast(); i++)
114         {
115                 AliEMCALTriggerRawDigit* digit = (AliEMCALTriggerRawDigit*)digits->At(i);
116                 
117                 Int_t id = digit->GetId();
118                 
119                 Int_t iTRU, iADC;
120                 
121                 Bool_t isOK1 = geom->GetTRUFromAbsFastORIndex(id, iTRU, iADC);
122                 
123                 for (Int_t j = 0; j < digit->GetNSamples(); j++)
124                 {
125                         Int_t time, amp;
126                         Bool_t isOK2 = digit->GetTimeSample(j, time, amp);
127                         
128                         if (isOK1 && isOK2 && amp) (static_cast<AliEMCALTriggerTRU*>(fTRU->At(iTRU)))->SetADC(iADC, time, amp);
129                 }
130                 
131                 Int_t px, py;
132                 if (geom->GetPositionInEMCALFromAbsFastORIndex(id, px, py))
133                 {
134                         posMap[px][py] = i;
135                         
136                         if (fSTU->GetRawData() && digit->GetL1TimeSum() >= 0) 
137                         {
138                                 region[px][py] = digit->GetL1TimeSum();
139                         }
140                 }
141         }
142
143         Int_t iL0 = 0;
144
145         for (Int_t i=0; i<kNTRU; i++) 
146         {
147                 AliDebug(999, Form("===========< TRU %2d >============\n", i));
148                 
149                 AliEMCALTriggerTRU* iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
150
151                 // L0 is always computed from F-ALTRO
152                 if (iTRU->L0()) 
153                 {
154                         iL0 += iTRU->L0();
155                         
156                         Int_t sizeX = (Int_t) ((iTRU->PatchSize())->X() * (iTRU->SubRegionSize())->X());
157                         
158                         Int_t sizeY = (Int_t) ((iTRU->PatchSize())->Y() * (iTRU->SubRegionSize())->Y());
159                         
160                         // transform local to global 
161                         TIter Next(&iTRU->Patches());
162                         while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)Next())
163                         {
164                                 Int_t px, py, id; p->Position(px, py);
165                                 
166                                 if (geom->GetAbsFastORIndexFromPositionInTRU(i, px, py, id) 
167                                         && 
168                                         geom->GetPositionInEMCALFromAbsFastORIndex(id, px, py)) p->SetPosition(px, py);
169                                 
170                                 if (!data->GetMode())
171                                 {
172                                         Int_t peaks = p->Peaks();
173                                         
174                                         for (Int_t j = 0; j < sizeX * sizeY; j++)
175                                         {
176                                                 if (peaks & (1 << j))
177                                                 {
178                                                         
179                                                         Int_t pos = posMap[px + j % sizeX][py + j / sizeX];
180                                                         
181 //                                                      cout << "px: " << px << " py: " << py << " j: " << j << " mod: " << j%sizeX << " ratio: " << j / sizeX << " pos: " << pos << endl;
182                                                         
183                                                         AliEMCALTriggerRawDigit* dig = 0x0;
184                                                         
185                                                         if (pos == -1)
186                                                         {
187                                                                 new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
188                                                                 
189                                                                 dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
190                                                         }
191                                                         else
192                                                         {
193                                                                 dig = (AliEMCALTriggerRawDigit*)digits->At(pos);
194                                                         }
195                                                         
196                                                         dig->SetL0Time(p->Time());
197                                                 }
198                                         }                                                               
199                                 }
200                         }
201                         
202                         data->SetL0Trigger(0, i, 1);
203                                                 
204                 data->SetPatches(kL0, 0, iTRU->Patches());
205                 }
206                 else
207                         data->SetL0Trigger(0, i, 0);
208         }
209
210         // A L0 has been issued, run L1
211         // Depending on raw data enabled or not in STU data: L1 computation 
212         // should be done from F-ALTRO or directly on TRU time sums in STU raw data
213         if (iL0) 
214         {
215                 // Use L1 threshold from raw data when reconstructing raw data
216                 if (data->GetMode())
217                 {
218                         fSTU->SetThreshold(kL1Gamma, data->GetL1GammaThreshold());
219                         fSTU->SetThreshold(kL1Jet,   data->GetL1JetThreshold()  );                      
220                 }
221                 else
222                 {
223                         fSTU->ComputeThFromV0(V0M); // C/A
224                         data->SetL1GammaThreshold(fSTU->GetThreshold(kL1Gamma));
225                         data->SetL1JetThreshold(  fSTU->GetThreshold(kL1Jet)  );
226                 }
227                 
228                 if (fSTU->GetRawData())
229                 {
230                         // Compute L1 from STU raw data
231                         fSTU->SetRegion(region);
232                 }
233                 else
234                 {
235                         // Build STU raw data from F-ALTRO
236                         TString str = "region";
237                         for (Int_t i = 0; i < kNTRU; i++) fSTU->Build(str,
238                                                                                                                   i, 
239                                                                                                                   (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Region(), 
240                                                                                                                   (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize());
241                 }
242
243                 fSTU->L1(kL1Gamma);
244                 
245                 data->SetPatches(kL1Gamma, 0, fSTU->Patches());
246
247                 fSTU->Reset();
248
249                 fSTU->L1(kL1Jet);
250                 
251                 data->SetPatches(kL1Jet,   0, fSTU->Patches());
252                 
253                 Int_t** reg = fSTU->Region();
254                 
255                 if (!fSTU->GetRawData())
256                 {
257                         // Update digits w/ L1 time sum
258                         // Done in raw digit maker when raw data enabled
259                         for (Int_t i = 0; i < 48; i++)
260                         {
261                                 for (Int_t j = 0; j < 64; j++)
262                                 {
263                                         if (reg[i][j])
264                                         {
265                                                 Int_t id;
266                                                 if (geom->GetAbsFastORIndexFromPositionInEMCAL(i, j, id))
267                                                 {
268                                                         AliEMCALTriggerRawDigit* dig = 0x0;
269
270                                                         if (posMap[i][j] == -1)
271                                                         {
272                                                                 // Add a new digit with L1 time sum
273                                                                 new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
274                                                                 
275                                                                 dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
276                                                         } 
277                                                         else
278                                                         {
279                                                                 dig = (AliEMCALTriggerRawDigit*)digits->At(posMap[i][j]);                                                               
280                                                         }
281                                                         
282                                                         dig->SetL1TimeSum(reg[i][j]);
283                                                 }
284                                         }
285                                 }
286                         }
287                 }
288         }
289
290         if (AliDebugLevel() >= 999) data->Scan();
291         
292         // Now reset the electronics for a fresh start with next event
293         Reset();
294 }
295
296 //__________________
297 void AliEMCALTriggerElectronics::Reset()
298 {
299         //
300         TIter NextTRU(fTRU);
301         while ( AliEMCALTriggerTRU *TRU = (AliEMCALTriggerTRU*)NextTRU() ) TRU->Reset();
302         
303         fSTU->Reset();
304 }