coverity fix
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerElectronics.cxx
CommitLineData
916f1e76 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
19EMCal trigger electronics manager L0/L1
20can handle both simulated digits and raw data
21Author: 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"
fff39dd1 31#include "AliEMCALTriggerDCSConfig.h"
916f1e76 32#include "AliEMCALTriggerData.h"
33#include "AliEMCALDigit.h"
34#include "AliCaloRawStreamV3.h"
916f1e76 35#include "AliEMCALTriggerSTURawStream.h"
36#include "AliEMCALDigit.h"
de39a0ff 37#include "AliEMCALTriggerRawDigit.h"
38#include "AliEMCALTriggerPatch.h"
804b828a 39#include "AliEMCALTriggerSTUDCSConfig.h"
916f1e76 40
41#include <TVector2.h>
916f1e76 42
43namespace
44{
8cf986c8 45 const Int_t kNTRU = 30;
916f1e76 46}
47
48ClassImp(AliEMCALTriggerElectronics)
49
50//__________________
fff39dd1 51AliEMCALTriggerElectronics::AliEMCALTriggerElectronics(const AliEMCALTriggerDCSConfig *dcsConf) : TObject(),
916f1e76 52fTRU(new TClonesArray("AliEMCALTriggerTRU",32)),
53fSTU(0x0)
54{
79b05051 55 // Ctor
56
916f1e76 57 TVector2 rSize;
58
59 rSize.Set( 24., 4. );
60
61 // 32 TRUs
fff39dd1 62 for (Int_t i=0;i<kNTRU;i++)
63 {
64 AliEMCALTriggerTRUDCSConfig* truConf = dcsConf->GetTRUDCSConfig(i);
de39a0ff 65 new ((*fTRU)[i]) AliEMCALTriggerTRU(truConf, rSize, i % 2);
fff39dd1 66 }
67
916f1e76 68 rSize.Set( 48., 64. );
69
70 // 1 STU
fff39dd1 71 AliEMCALTriggerSTUDCSConfig* stuConf = dcsConf->GetSTUDCSConfig();
72 fSTU = new AliEMCALTriggerSTU(stuConf, rSize);
916f1e76 73
de39a0ff 74 TString str = "map";
75 for (Int_t i=0;i<kNTRU;i++) fSTU->Build(str,
76 i,
77 (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Map(),
78 (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize()
79 );
916f1e76 80}
81
82//________________
83AliEMCALTriggerElectronics::~AliEMCALTriggerElectronics()
84{
79b05051 85 // Dtor
86
916f1e76 87 fTRU->Delete();
88 delete fSTU;
89}
90
91//__________________
de39a0ff 92void AliEMCALTriggerElectronics::Digits2Trigger(TClonesArray* digits, const Int_t V0M[], AliEMCALTriggerData* data)
916f1e76 93{
79b05051 94 // Digits to trigger
804b828a 95
916f1e76 96 AliEMCALGeometry* geom = 0x0;
97
98 AliRunLoader *rl = AliRunLoader::Instance();
a51e676d 99 if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")){
100 AliEMCAL* emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
101 if(emcal)geom = emcal->GetGeometry();
102 }
103
104 if(!geom) geom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
105
106 if(!geom) AliError("Cannot access geometry!");
916f1e76 107
804b828a 108 Int_t pos, px, py, id;
de39a0ff 109
110 Int_t region[48][64], posMap[48][64];
111 for (Int_t i = 0; i < 48; i++) for (Int_t j = 0; j < 64; j++)
112 {
113 region[i][j] = 0;
114 posMap[i][j] = -1;
115 }
116
117 for (Int_t i = 0; i < digits->GetEntriesFast(); i++)
916f1e76 118 {
de39a0ff 119 AliEMCALTriggerRawDigit* digit = (AliEMCALTriggerRawDigit*)digits->At(i);
120
804b828a 121 id = digit->GetId();
de39a0ff 122
123 Int_t iTRU, iADC;
124
125 Bool_t isOK1 = geom->GetTRUFromAbsFastORIndex(id, iTRU, iADC);
126
c33e980f 127 if ((isOK1 && iTRU >= kNTRU) || !isOK1) continue;
128
de39a0ff 129 for (Int_t j = 0; j < digit->GetNSamples(); j++)
916f1e76 130 {
de39a0ff 131 Int_t time, amp;
132 Bool_t isOK2 = digit->GetTimeSample(j, time, amp);
916f1e76 133
de39a0ff 134 if (isOK1 && isOK2 && amp) (static_cast<AliEMCALTriggerTRU*>(fTRU->At(iTRU)))->SetADC(iADC, time, amp);
135 }
136
804b828a 137 if (geom->GetPositionInEMCALFromAbsFastORIndex(id, px, py)) posMap[px][py] = i;
916f1e76 138 }
916f1e76 139
de39a0ff 140 Int_t iL0 = 0;
916f1e76 141
804b828a 142 Int_t timeL0[kNTRU] = {0}, timeL0min = 999;
143
916f1e76 144 for (Int_t i=0; i<kNTRU; i++)
145 {
de39a0ff 146 AliDebug(999, Form("===========< TRU %2d >============\n", i));
916f1e76 147
de39a0ff 148 AliEMCALTriggerTRU* iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
804b828a 149
150 if (iTRU->L0()) // L0 recomputation: *ALWAYS* done from FALTRO
916f1e76 151 {
804b828a 152 iL0++;
de39a0ff 153
804b828a 154 timeL0[i] = iTRU->GetL0Time();
de39a0ff 155
804b828a 156 if (!timeL0[i]) AliWarning(Form("TRU# %d has 0 trigger time",i));
de39a0ff 157
804b828a 158 if (timeL0[i] < timeL0min) timeL0min = timeL0[i];
159
160 data->SetL0Trigger(0, i, 1); // TRU# i has issued a L0
161 }
162 else
163 data->SetL0Trigger(0, i, 0);
164 }
165
166 AliDebug(999, Form("=== %2d TRU (out of %2d) has issued a L0 / Min L0 time: %d\n", iL0, kNTRU, timeL0min));
167
168 AliEMCALTriggerRawDigit* dig = 0x0;
169
170 if (iL0 && (!data->GetMode() || !fSTU->GetDCSConfig()->GetRawData()))
171 {
172 // Update digits after L0 calculation
173 for (Int_t i = 0; i < kNTRU; i++)
174 {
175 AliEMCALTriggerTRU *iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
176
177 Int_t reg[24][4];
178 iTRU->GetL0Region(timeL0min, reg);
179
180 for (int j = 0; j < iTRU->RegionSize()->X(); j++)
181 {
182 for (int k = 0; k < iTRU->RegionSize()->Y(); k++)
183 {
184 if (geom->GetAbsFastORIndexFromPositionInTRU(i, j, k, id)
185 &&
186 geom->GetPositionInEMCALFromAbsFastORIndex(id, px, py))
187 {
188 pos = posMap[px][py];
189
190 if (pos == -1)
191 {
192 // Add a new digit
193 new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
194
195 dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
196 }
197 else
198 {
199 dig = (AliEMCALTriggerRawDigit*)digits->At(pos);
200 }
201
202 dig->SetL1TimeSum(reg[j][k]);
203 }
204 }
205 }
206 }
207 }
208
209 if (iL0 && !data->GetMode())
210 {
de39a0ff 211 // transform local to global
804b828a 212
213 for (Int_t i = 0; i < kNTRU; i++)
214 {
215 AliEMCALTriggerTRU* iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
216
de39a0ff 217 TIter Next(&iTRU->Patches());
218 while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)Next())
219 {
804b828a 220 p->Position(px, py);
221
222 // Local 2 Global
223 if (geom->GetAbsFastORIndexFromPositionInTRU(i, px, py, id) &&
224 geom->GetPositionInEMCALFromAbsFastORIndex(id, px, py)) p->SetPosition(px, py);
de39a0ff 225
804b828a 226 Int_t peaks = p->Peaks();
227
228 Int_t sizeX = (Int_t) ((iTRU->PatchSize())->X() * (iTRU->SubRegionSize())->X());
229 Int_t sizeY = (Int_t) ((iTRU->PatchSize())->Y() * (iTRU->SubRegionSize())->Y());
a61738e1 230
804b828a 231 for (Int_t j = 0; j < sizeX * sizeY; j++)
232 {
233 if (peaks & (1 << j))
de39a0ff 234 {
804b828a 235 pos = posMap[px + j % sizeX][py + j / sizeX];
de39a0ff 236
804b828a 237 if (pos == -1)
238 {
a61738e1 239 // Add a new digit
804b828a 240 new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
de39a0ff 241
804b828a 242 dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
243 }
244 else
245 {
246 dig = (AliEMCALTriggerRawDigit*)digits->At(pos);
de39a0ff 247 }
804b828a 248
249 dig->SetL0Time(timeL0min);
a61738e1 250 }
804b828a 251 }
a61738e1 252
804b828a 253 pos = posMap[px][py];
a61738e1 254
804b828a 255 if (pos == -1)
256 {
a61738e1 257 // Add a new digit
804b828a 258 new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
a61738e1 259
804b828a 260 dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
261 }
262 else
263 {
264 dig = (AliEMCALTriggerRawDigit*)digits->At(pos);
de39a0ff 265 }
804b828a 266
267 dig->SetTriggerBit(kL0, 0);
de39a0ff 268 }
de39a0ff 269 }
916f1e76 270 }
804b828a 271
272 //
273 // Prepare STU for L1 calculation
274
275 for (int i = 0; i < (fSTU->RegionSize())->X(); i++)
916f1e76 276 {
804b828a 277 for (int j = 0; j < (fSTU->RegionSize())->Y(); j++)
de39a0ff 278 {
804b828a 279 pos = posMap[i][j];
916f1e76 280
804b828a 281 if (pos >= 0)
282 {
283 AliEMCALTriggerRawDigit *digit = (AliEMCALTriggerRawDigit*)digits->At(pos);
284
285 if (digit->GetL1TimeSum() > -1) region[i][j] = digit->GetL1TimeSum();
286 }
de39a0ff 287 }
804b828a 288 }
289
290 fSTU->SetRegion(region);
291
292 if (data->GetMode())
293 {
294 fSTU->SetThreshold(kL1Gamma, data->GetL1GammaThreshold());
295 fSTU->SetThreshold(kL1Jet, data->GetL1JetThreshold() );
296 }
297 else
298 {
299 fSTU->ComputeThFromV0(kL1Gamma, V0M);
300 data->SetL1GammaThreshold( fSTU->GetThreshold(kL1Gamma));
301 fSTU->ComputeThFromV0(kL1Jet, V0M);
302 data->SetL1JetThreshold( fSTU->GetThreshold(kL1Jet) );
303 }
916f1e76 304
804b828a 305 fSTU->L1(kL1Gamma);
a61738e1 306
804b828a 307 TIterator* nP = 0x0;
a61738e1 308
804b828a 309 nP = (fSTU->Patches()).MakeIterator();
310
311 while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)nP->Next())
312 {
313 p->Position(px, py);
a61738e1 314
804b828a 315 if (geom->GetAbsFastORIndexFromPositionInEMCAL(px, py, id))
316 {
317 if (posMap[px][py] == -1)
a61738e1 318 {
a61738e1 319 // Add a new digit
804b828a 320 new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
a61738e1 321
804b828a 322 dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
323 }
324 else
325 {
326 dig = (AliEMCALTriggerRawDigit*)digits->At(posMap[px][py]);
a61738e1 327 }
804b828a 328
329 dig->SetTriggerBit(kL1Gamma,0);
a61738e1 330 }
804b828a 331 }
916f1e76 332
804b828a 333 fSTU->Reset();
916f1e76 334
804b828a 335 fSTU->L1(kL1Jet);
916f1e76 336
804b828a 337 nP = (fSTU->Patches()).MakeIterator();
338
339 while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)nP->Next())
340 {
341 p->Position(px, py);
a61738e1 342
804b828a 343 px *= (Int_t)((fSTU->SubRegionSize())->X());
a61738e1 344
804b828a 345 py *= (Int_t)((fSTU->SubRegionSize())->Y());
a61738e1 346
804b828a 347 if (geom->GetAbsFastORIndexFromPositionInEMCAL(px, py, id))
348 {
349 if (posMap[px][py] == -1)
a61738e1 350 {
a61738e1 351 // Add a new digit
804b828a 352 new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
a61738e1 353
804b828a 354 dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
355 }
356 else
de39a0ff 357 {
804b828a 358 dig = (AliEMCALTriggerRawDigit*)digits->At(posMap[px][py]);
de39a0ff 359 }
804b828a 360
361 dig->SetTriggerBit(kL1Jet, 0);
de39a0ff 362 }
916f1e76 363 }
364
de39a0ff 365 if (AliDebugLevel() >= 999) data->Scan();
39c05eac 366
916f1e76 367 // Now reset the electronics for a fresh start with next event
368 Reset();
369}
370
371//__________________
372void AliEMCALTriggerElectronics::Reset()
373{
79b05051 374 // Reset
375
916f1e76 376 TIter NextTRU(fTRU);
377 while ( AliEMCALTriggerTRU *TRU = (AliEMCALTriggerTRU*)NextTRU() ) TRU->Reset();
378
379 fSTU->Reset();
380}