Clean-up of include files
[u/mrichter/AliRoot.git] / TRD / AliTRDmcmSim.cxx
CommitLineData
dfd03fc3 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
5eba8ada 16/* $Id$ */
17
dfd03fc3 18///////////////////////////////////////////////////////////////////////////////
19// //
20// TRD MCM (Multi Chip Module) simulator //
ce4786b9 21// which simulates the TRAP processing after the AD-conversion. //
22// The relevant parameters (i.e. configuration settings of the TRAP) //
23// are taken from AliTRDtrapConfig. //
dfd03fc3 24// //
25///////////////////////////////////////////////////////////////////////////////
26
ce4786b9 27#include <iostream>
28#include <iomanip>
ecf39416 29
ce4786b9 30#include "TCanvas.h"
31#include "TH1F.h"
32#include "TH2F.h"
33#include "TGraph.h"
34#include "TLine.h"
35#include "TRandom.h"
36#include "TClonesArray.h"
ce51199c 37#include "TMath.h"
c93255fe 38#include <TTree.h>
0c349049 39
dfd03fc3 40#include "AliLog.h"
b0a41e80 41#include "AliRunLoader.h"
42#include "AliLoader.h"
0c349049 43
dfd03fc3 44#include "AliTRDfeeParam.h"
b0a41e80 45#include "AliTRDtrapConfig.h"
4cc89512 46#include "AliTRDdigitsManager.h"
b65e5048 47#include "AliTRDarrayADC.h"
40bd6ee4 48#include "AliTRDarrayDictionary.h"
52c19022 49#include "AliTRDtrackletMCM.h"
b0a41e80 50#include "AliTRDmcmSim.h"
1d93b218 51
dfd03fc3 52ClassImp(AliTRDmcmSim)
53
40bd6ee4 54Bool_t AliTRDmcmSim::fgApplyCut = kTRUE;
ce4786b9 55Int_t AliTRDmcmSim::fgAddBaseline = 0;
56
57const Int_t AliTRDmcmSim::fgkFormatIndex = std::ios_base::xalloc();
58
59const Int_t AliTRDmcmSim::fgkNADC = AliTRDfeeParam::GetNadcMcm();
60const UShort_t AliTRDmcmSim::fgkFPshifts[4] = {11, 14, 17, 21};
61
62
63AliTRDmcmSim::AliTRDmcmSim() :
64 TObject(),
65 fInitialized(kFALSE),
66 fDetector(-1),
67 fRobPos(-1),
68 fMcmPos(-1),
69 fRow (-1),
70 fNTimeBin(-1),
71 fADCR(NULL),
72 fADCF(NULL),
73 fMCMT(NULL),
74 fTrackletArray(NULL),
75 fZSMap(NULL),
6b094867 76 fTrklBranchName("mcmtrklbranch"),
ce4786b9 77 fFeeParam(NULL),
78 fTrapConfig(NULL),
79 fDigitsManager(NULL),
80 fPedAcc(NULL),
81 fGainCounterA(NULL),
82 fGainCounterB(NULL),
83 fTailAmplLong(NULL),
84 fTailAmplShort(NULL),
85 fNHits(0),
86 fFitReg(NULL)
dfd03fc3 87{
88 //
b0a41e80 89 // AliTRDmcmSim default constructor
dfd03fc3 90 // By default, nothing is initialized.
91 // It is necessary to issue Init before use.
54d34aac 92
93 for (Int_t iDict = 0; iDict < 3; iDict++)
94 fDict[iDict] = 0x0;
95
96 fFitPtr[0] = 0;
97 fFitPtr[1] = 0;
98 fFitPtr[2] = 0;
99 fFitPtr[3] = 0;
dfd03fc3 100}
101
dfd03fc3 102AliTRDmcmSim::~AliTRDmcmSim()
103{
104 //
105 // AliTRDmcmSim destructor
106 //
0c349049 107
b0a41e80 108 if(fInitialized) {
ce4786b9 109 for( Int_t iAdc = 0 ; iAdc < fgkNADC; iAdc++ ) {
110 delete [] fADCR[iAdc];
111 delete [] fADCF[iAdc];
dfd03fc3 112 }
16e077d0 113 delete [] fADCR;
114 delete [] fADCF;
ce4786b9 115 delete [] fZSMap;
1d93b218 116 delete [] fMCMT;
b0a41e80 117
118 delete [] fPedAcc;
119 delete [] fGainCounterA;
120 delete [] fGainCounterB;
121 delete [] fTailAmplLong;
122 delete [] fTailAmplShort;
123 delete [] fFitReg;
124
125 fTrackletArray->Delete();
126 delete fTrackletArray;
1d93b218 127 }
dfd03fc3 128}
129
b0a41e80 130void AliTRDmcmSim::Init( Int_t det, Int_t robPos, Int_t mcmPos, Bool_t /* newEvent */ )
dfd03fc3 131{
0c349049 132 //
ce4786b9 133 // Initialize the class with new MCM position information
134 // memory is allocated in the first initialization
0c349049 135 //
96e6312d 136
b0a41e80 137 if (!fInitialized) {
138 fFeeParam = AliTRDfeeParam::Instance();
139 fTrapConfig = AliTRDtrapConfig::Instance();
b0a41e80 140 }
141
142 fDetector = det;
0c349049 143 fRobPos = robPos;
144 fMcmPos = mcmPos;
ce4786b9 145 fNTimeBin = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kC13CPUA);
dfd03fc3 146 fRow = fFeeParam->GetPadRowFromMCM( fRobPos, fMcmPos );
23200400 147
b0a41e80 148 if (!fInitialized) {
ce4786b9 149 fADCR = new Int_t *[fgkNADC];
150 fADCF = new Int_t *[fgkNADC];
151 fZSMap = new Int_t [fgkNADC];
152 fGainCounterA = new UInt_t[fgkNADC];
153 fGainCounterB = new UInt_t[fgkNADC];
154 for( Int_t iAdc = 0 ; iAdc < fgkNADC; iAdc++ ) {
155 fADCR[iAdc] = new Int_t[fNTimeBin];
156 fADCF[iAdc] = new Int_t[fNTimeBin];
dfd03fc3 157 }
b0a41e80 158
159 // filter registers
ce4786b9 160 fPedAcc = new UInt_t[fgkNADC]; // accumulator for pedestal filter
161 fTailAmplLong = new UShort_t[fgkNADC];
162 fTailAmplShort = new UShort_t[fgkNADC];
b0a41e80 163
164 // tracklet calculation
ce4786b9 165 fFitReg = new FitReg_t[fgkNADC];
166 fTrackletArray = new TClonesArray("AliTRDtrackletMCM", fgkMaxTracklets);
b0a41e80 167
ce4786b9 168 fMCMT = new UInt_t[fgkMaxTracklets];
dfd03fc3 169 }
170
b0a41e80 171 fInitialized = kTRUE;
172
173 Reset();
174}
175
176void AliTRDmcmSim::Reset()
177{
178 // Resets the data values and internal filter registers
179 // by re-initialising them
180
ce4786b9 181 if( !CheckInitialized() )
182 return;
5896bc23 183
ce4786b9 184 for( Int_t iAdc = 0 ; iAdc < fgkNADC; iAdc++ ) {
185 for( Int_t it = 0 ; it < fNTimeBin ; it++ ) {
186 fADCR[iAdc][it] = 0;
187 fADCF[iAdc][it] = 0;
dfd03fc3 188 }
ce4786b9 189 fZSMap[iAdc] = -1; // Default unread, low active bit mask
190 fGainCounterA[iAdc] = 0;
191 fGainCounterB[iAdc] = 0;
dfd03fc3 192 }
ecf39416 193
ce4786b9 194 for(Int_t i = 0; i < fgkMaxTracklets; i++) {
1d93b218 195 fMCMT[i] = 0;
196 }
ce4786b9 197
198 for (Int_t iDict = 0; iDict < 3; iDict++)
199 fDict[iDict] = 0x0;
b0a41e80 200
201 FilterPedestalInit();
202 FilterGainInit();
ce4786b9 203 FilterTailInit();
b0a41e80 204}
1d93b218 205
4ff7ed2b 206void AliTRDmcmSim::SetNTimebins(Int_t ntimebins)
207{
ce4786b9 208 // Reallocate memory if a change in the number of timebins
209 // is needed (should not be the case for real data)
210
211 if( !CheckInitialized() )
212 return;
213
4ff7ed2b 214 fNTimeBin = ntimebins;
ce4786b9 215 for( Int_t iAdc = 0 ; iAdc < fgkNADC; iAdc++ ) {
216 delete fADCR[iAdc];
217 delete fADCF[iAdc];
218 fADCR[iAdc] = new Int_t[fNTimeBin];
219 fADCF[iAdc] = new Int_t[fNTimeBin];
4ff7ed2b 220 }
221}
222
ab9f7002 223Bool_t AliTRDmcmSim::LoadMCM(AliRunLoader* const runloader, Int_t det, Int_t rob, Int_t mcm)
b0a41e80 224{
ce4786b9 225 // loads the ADC data as obtained from the digitsManager for the specified MCM.
226 // This method is meant for rare execution, e.g. in the visualization. When called
227 // frequently use SetData(...) instead.
b0a41e80 228
64e3d742 229 Init(det, rob, mcm);
b0a41e80 230
231 if (!runloader) {
232 AliError("No Runloader given");
233 return kFALSE;
234 }
235
236 AliLoader *trdLoader = runloader->GetLoader("TRDLoader");
237 if (!trdLoader) {
238 AliError("Could not get TRDLoader");
239 return kFALSE;
240 }
241
5eba8ada 242 Bool_t retval = kTRUE;
b0a41e80 243 trdLoader->LoadDigits();
40bd6ee4 244 fDigitsManager = 0x0;
b0a41e80 245 AliTRDdigitsManager *digMgr = new AliTRDdigitsManager();
246 digMgr->SetSDigits(0);
247 digMgr->CreateArrays();
248 digMgr->ReadDigits(trdLoader->TreeD());
249 AliTRDarrayADC *digits = (AliTRDarrayADC*) digMgr->GetDigits(det);
5eba8ada 250 if (digits->HasData()) {
251 digits->Expand();
252
5896bc23 253 if (fNTimeBin != digits->GetNtime()) {
ce4786b9 254 AliWarning(Form("Changing no. of timebins from %i to %i", fNTimeBin, digits->GetNtime()));
4ff7ed2b 255 SetNTimebins(digits->GetNtime());
5896bc23 256 }
4ff7ed2b 257
ce4786b9 258 SetData(digits);
b0a41e80 259 }
5eba8ada 260 else
261 retval = kFALSE;
4ff7ed2b 262
b0a41e80 263 delete digMgr;
4ff7ed2b 264
265 return retval;
b0a41e80 266}
267
268void AliTRDmcmSim::NoiseTest(Int_t nsamples, Int_t mean, Int_t sigma, Int_t inputGain, Int_t inputTail)
269{
270 // This function can be used to test the filters.
271 // It feeds nsamples of ADC values with a gaussian distribution specified by mean and sigma.
272 // The filter chain implemented here consists of:
273 // Pedestal -> Gain -> Tail
274 // With inputGain and inputTail the input to the gain and tail filter, respectively,
275 // can be chosen where
276 // 0: noise input
277 // 1: pedestal output
278 // 2: gain output
279 // The input has to be chosen from a stage before.
280 // The filter behaviour is controlled by the TRAP parameters from AliTRDtrapConfig in the
281 // same way as in normal simulation.
282 // The functions produces four histograms with the values at the different stages.
283
ce4786b9 284 if( !CheckInitialized() )
285 return;
286
287 TString nameInputGain;
288 TString nameInputTail;
289
290 switch (inputGain) {
291 case 0:
292 nameInputGain = "Noise";
293 break;
294
295 case 1:
296 nameInputGain = "Pedestal";
297 break;
298
299 default:
300 AliError("Undefined input to tail cancellation filter");
301 return;
302 }
303
304 switch (inputTail) {
305 case 0:
306 nameInputTail = "Noise";
307 break;
308
309 case 1:
310 nameInputTail = "Pedestal";
311 break;
312
313 case 2:
314 nameInputTail = "Gain";
315 break;
316
317 default:
318 AliError("Undefined input to tail cancellation filter");
319 return;
320 }
321
b0a41e80 322 TH1F *h = new TH1F("noise", "Gaussian Noise;sample;ADC count",
323 nsamples, 0, nsamples);
ce4786b9 324 TH1F *hfp = new TH1F("ped", "Noise #rightarrow Pedestal filter;sample;ADC count", nsamples, 0, nsamples);
325 TH1F *hfg = new TH1F("gain",
326 (nameInputGain + "#rightarrow Gain;sample;ADC count").Data(),
327 nsamples, 0, nsamples);
328 TH1F *hft = new TH1F("tail",
329 (nameInputTail + "#rightarrow Tail;sample;ADC count").Data(),
330 nsamples, 0, nsamples);
b0a41e80 331 h->SetStats(kFALSE);
332 hfp->SetStats(kFALSE);
333 hfg->SetStats(kFALSE);
334 hft->SetStats(kFALSE);
335
336 Int_t value; // ADC count with noise (10 bit)
337 Int_t valuep; // pedestal filter output (12 bit)
338 Int_t valueg; // gain filter output (12 bit)
339 Int_t valuet; // tail filter value (12 bit)
340
341 for (Int_t i = 0; i < nsamples; i++) {
342 value = (Int_t) gRandom->Gaus(mean, sigma); // generate noise with gaussian distribution
343 h->SetBinContent(i, value);
344
345 valuep = FilterPedestalNextSample(1, 0, ((Int_t) value) << 2);
346
347 if (inputGain == 0)
348 valueg = FilterGainNextSample(1, ((Int_t) value) << 2);
349 else
350 valueg = FilterGainNextSample(1, valuep);
351
352 if (inputTail == 0)
353 valuet = FilterTailNextSample(1, ((Int_t) value) << 2);
354 else if (inputTail == 1)
355 valuet = FilterTailNextSample(1, valuep);
356 else
357 valuet = FilterTailNextSample(1, valueg);
358
359 hfp->SetBinContent(i, valuep >> 2);
360 hfg->SetBinContent(i, valueg >> 2);
361 hft->SetBinContent(i, valuet >> 2);
362 }
363
364 TCanvas *c = new TCanvas;
365 c->Divide(2,2);
366 c->cd(1);
367 h->Draw();
368 c->cd(2);
369 hfp->Draw();
370 c->cd(3);
371 hfg->Draw();
372 c->cd(4);
373 hft->Draw();
dfd03fc3 374}
375
ce4786b9 376Bool_t AliTRDmcmSim::CheckInitialized() const
ecf39416 377{
0c349049 378 //
379 // Check whether object is initialized
380 //
381
ce4786b9 382 if( ! fInitialized )
383 AliError(Form ("AliTRDmcmSim is not initialized but function other than Init() is called."));
384
ecf39416 385 return fInitialized;
386}
387
ab9f7002 388void AliTRDmcmSim::Print(Option_t* const option) const
b0a41e80 389{
390 // Prints the data stored and/or calculated for this MCM.
391 // The output is controlled by option which can be a sequence of any of
392 // the following characters:
393 // R - prints raw ADC data
394 // F - prints filtered data
395 // H - prints detected hits
396 // T - prints found tracklets
ce4786b9 397 // The later stages are only meaningful after the corresponding calculations
b0a41e80 398 // have been performed.
399
ce4786b9 400 if ( !CheckInitialized() )
401 return;
402
b0a41e80 403 printf("MCM %i on ROB %i in detector %i\n", fMcmPos, fRobPos, fDetector);
404
405 TString opt = option;
ce4786b9 406 if (opt.Contains("R") || opt.Contains("F")) {
407 std::cout << *this;
1d93b218 408 }
409
b0a41e80 410 if (opt.Contains("H")) {
411 printf("Found %i hits:\n", fNHits);
412 for (Int_t iHit = 0; iHit < fNHits; iHit++) {
413 printf("Hit %3i in timebin %2i, ADC %2i has charge %3i and position %3i\n",
ab9f7002 414 iHit, fHits[iHit].fTimebin, fHits[iHit].fChannel, fHits[iHit].fQtot, fHits[iHit].fYpos);
b0a41e80 415 }
1d93b218 416 }
1d93b218 417
b0a41e80 418 if (opt.Contains("T")) {
419 printf("Tracklets:\n");
420 for (Int_t iTrkl = 0; iTrkl < fTrackletArray->GetEntriesFast(); iTrkl++) {
421 printf("tracklet %i: 0x%08x\n", iTrkl, ((AliTRDtrackletMCM*) (*fTrackletArray)[iTrkl])->GetTrackletWord());
422 }
1d93b218 423 }
b0a41e80 424}
1d93b218 425
ab9f7002 426void AliTRDmcmSim::Draw(Option_t* const option)
b0a41e80 427{
428 // Plots the data stored in a 2-dim. timebin vs. ADC channel plot.
429 // The option selects what data is plotted and can be a sequence of
430 // the following characters:
431 // R - plot raw data (default)
432 // F - plot filtered data (meaningless if R is specified)
433 // In addition to the ADC values:
434 // H - plot hits
435 // T - plot tracklets
436
ce4786b9 437 if( !CheckInitialized() )
438 return;
439
b0a41e80 440 TString opt = option;
441
442 TH2F *hist = new TH2F("mcmdata", Form("Data of MCM %i on ROB %i in detector %i", \
443 fMcmPos, fRobPos, fDetector), \
ce4786b9 444 fgkNADC, -0.5, fgkNADC-.5, fNTimeBin, -.5, fNTimeBin-.5);
b0a41e80 445 hist->GetXaxis()->SetTitle("ADC Channel");
446 hist->GetYaxis()->SetTitle("Timebin");
447 hist->SetStats(kFALSE);
448
449 if (opt.Contains("R")) {
450 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
ce4786b9 451 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
b0a41e80 452 hist->SetBinContent(iAdc+1, iTimeBin+1, fADCR[iAdc][iTimeBin] >> fgkAddDigits);
453 }
1d93b218 454 }
b0a41e80 455 }
456 else {
457 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
ce4786b9 458 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
b0a41e80 459 hist->SetBinContent(iAdc+1, iTimeBin+1, fADCF[iAdc][iTimeBin] >> fgkAddDigits);
460 }
1d93b218 461 }
1d93b218 462 }
b0a41e80 463 hist->Draw("colz");
1d93b218 464
b0a41e80 465 if (opt.Contains("H")) {
466 TGraph *grHits = new TGraph();
467 for (Int_t iHit = 0; iHit < fNHits; iHit++) {
468 grHits->SetPoint(iHit,
ab9f7002 469 fHits[iHit].fChannel + 1 + fHits[iHit].fYpos/256.,
470 fHits[iHit].fTimebin);
b0a41e80 471 }
472 grHits->Draw("*");
473 }
1d93b218 474
b0a41e80 475 if (opt.Contains("T")) {
476 TLine *trklLines = new TLine[4];
64e3d742 477 for (Int_t iTrkl = 0; iTrkl < fTrackletArray->GetEntries(); iTrkl++) {
b0a41e80 478 AliTRDtrackletMCM *trkl = (AliTRDtrackletMCM*) (*fTrackletArray)[iTrkl];
ce4786b9 479 Float_t padWidth = 0.635 + 0.03 * (fDetector % 6);
480 Float_t offset = padWidth/256. * ((((((fRobPos & 0x1) << 2) + (fMcmPos & 0x3)) * 18) << 8) - ((18*4*2 - 18*2 - 3) << 7)); // revert adding offset in FitTracklet
5ac2e3b1 481 Int_t ndrift = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrNdrift, fDetector, fRobPos, fMcmPos) >> 5;
ce4786b9 482 Float_t slope = trkl->GetdY() * 140e-4 / ndrift;
483
484 Int_t t0 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFS);
485 Int_t t1 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFE);
486
487 trklLines[iTrkl].SetX1((offset - (trkl->GetY() - slope * t0)) / padWidth); // ??? sign?
488 trklLines[iTrkl].SetY1(t0);
489 trklLines[iTrkl].SetX2((offset - (trkl->GetY() - slope * t1)) / padWidth); // ??? sign?
490 trklLines[iTrkl].SetY2(t1);
b0a41e80 491 trklLines[iTrkl].SetLineColor(2);
492 trklLines[iTrkl].SetLineWidth(2);
493 printf("Tracklet %i: y = %f, dy = %f, offset = %f\n", iTrkl, trkl->GetY(), (trkl->GetdY() * 140e-4), offset);
494 trklLines[iTrkl].Draw();
495 }
496 }
1d93b218 497}
498
ce4786b9 499void AliTRDmcmSim::SetData( Int_t adc, Int_t* const data )
dfd03fc3 500{
0c349049 501 //
dfd03fc3 502 // Store ADC data into array of raw data
0c349049 503 //
dfd03fc3 504
ecf39416 505 if( !CheckInitialized() ) return;
dfd03fc3 506
ce4786b9 507 if( adc < 0 || adc >= fgkNADC ) {
508 AliError(Form ("Error: ADC %i is out of range (0 .. %d).", adc, fgkNADC-1));
dfd03fc3 509 return;
510 }
511
4ff7ed2b 512 for( Int_t it = 0 ; it < fNTimeBin ; it++ ) {
ce4786b9 513 fADCR[adc][it] = (Int_t) (data[it]) << fgkAddDigits;
514 fADCF[adc][it] = (Int_t) (data[it]) << fgkAddDigits;
dfd03fc3 515 }
516}
517
ce4786b9 518void AliTRDmcmSim::SetData( Int_t adc, Int_t it, Int_t data )
dfd03fc3 519{
0c349049 520 //
dfd03fc3 521 // Store ADC data into array of raw data
0c349049 522 //
dfd03fc3 523
ecf39416 524 if( !CheckInitialized() ) return;
dfd03fc3 525
ce4786b9 526 if( adc < 0 || adc >= fgkNADC ) {
527 AliError(Form ("Error: ADC %i is out of range (0 .. %d).", adc, fgkNADC-1));
dfd03fc3 528 return;
529 }
530
ce4786b9 531 fADCR[adc][it] = data << fgkAddDigits;
532 fADCF[adc][it] = data << fgkAddDigits;
b0a41e80 533}
534
6b094867 535void AliTRDmcmSim::SetData(AliTRDarrayADC* const adcArray, AliTRDdigitsManager * const digitsManager)
b0a41e80 536{
ab9f7002 537 // Set the ADC data from an AliTRDarrayADC
538
ce4786b9 539 if( !CheckInitialized() )
b0a41e80 540 return;
b0a41e80 541
40bd6ee4 542 fDigitsManager = digitsManager;
ce4786b9 543 if (fDigitsManager) {
544 for (Int_t iDict = 0; iDict < 3; iDict++) {
545 AliTRDarrayDictionary *newDict = (AliTRDarrayDictionary*) fDigitsManager->GetDictionary(fDetector, iDict);
546 if (fDict[iDict] != 0x0 && newDict != 0x0) {
547
548 if (fDict[iDict] == newDict)
549 continue;
40bd6ee4 550
ce4786b9 551 fDict[iDict] = newDict;
ce4786b9 552 fDict[iDict]->Expand();
553 }
554 else {
555 fDict[iDict] = newDict;
556 if (fDict[iDict])
557 fDict[iDict]->Expand();
27a030ab 558 }
559
560 // If there is no data, set dictionary to zero to avoid crashes
561 if (fDict[iDict]->GetDim() == 0) {
562 AliError(Form("Dictionary %i of det. %i has dim. 0", fDetector, iDict));
563 fDict[iDict] = 0x0;
ce4786b9 564 }
565 }
5896bc23 566 }
4ff7ed2b 567
ce4786b9 568 if (fNTimeBin != adcArray->GetNtime())
569 SetNTimebins(adcArray->GetNtime());
570
571 Int_t offset = (fMcmPos % 4 + 1) * 21 + (fRobPos % 2) * 84 - 1;
b0a41e80 572
573 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
ce4786b9 574 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
575 Int_t value = adcArray->GetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin);
576 if (value < 0 || (offset - iAdc < 1) || (offset - iAdc > 165)) {
577 fADCR[iAdc][iTimeBin] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP) + (fgAddBaseline << fgkAddDigits);
4ff7ed2b 578 fADCF[iAdc][iTimeBin] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFP) + (fgAddBaseline << fgkAddDigits);
b0a41e80 579 }
580 else {
ce4786b9 581 fZSMap[iAdc] = 0;
582 fADCR[iAdc][iTimeBin] = (value << fgkAddDigits) + (fgAddBaseline << fgkAddDigits);
583 fADCF[iAdc][iTimeBin] = (value << fgkAddDigits) + (fgAddBaseline << fgkAddDigits);
b0a41e80 584 }
585 }
586 }
dfd03fc3 587}
588
6b094867 589void AliTRDmcmSim::SetDataByPad(AliTRDarrayADC* const adcArray, AliTRDdigitsManager * const digitsManager)
ce51199c 590{
591 // Set the ADC data from an AliTRDarrayADC
592 // (by pad, to be used during initial reading in simulation)
593
594 if( !CheckInitialized() )
595 return;
596
597 fDigitsManager = digitsManager;
598 if (fDigitsManager) {
599 for (Int_t iDict = 0; iDict < 3; iDict++) {
600 AliTRDarrayDictionary *newDict = (AliTRDarrayDictionary*) fDigitsManager->GetDictionary(fDetector, iDict);
601 if (fDict[iDict] != 0x0 && newDict != 0x0) {
602
603 if (fDict[iDict] == newDict)
604 continue;
605
606 fDict[iDict] = newDict;
ce51199c 607 fDict[iDict]->Expand();
608 }
609 else {
610 fDict[iDict] = newDict;
611 if (fDict[iDict])
612 fDict[iDict]->Expand();
613 }
27a030ab 614
615 // If there is no data, set dictionary to zero to avoid crashes
616 if (fDict[iDict]->GetDim() == 0) {
617 AliError(Form("Dictionary %i of det. %i has dim. 0", fDetector, iDict));
618 fDict[iDict] = 0x0;
619 }
ce51199c 620 }
621 }
622
623 if (fNTimeBin != adcArray->GetNtime())
624 SetNTimebins(adcArray->GetNtime());
625
626 Int_t offset = (fMcmPos % 4 + 1) * 18 + (fRobPos % 2) * 72 + 1;
627
628 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
629 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
630 Int_t value = -1;
631 Int_t pad = offset - iAdc;
632 if (pad > -1 && pad < 144)
633 value = adcArray->GetData(GetRow(), offset - iAdc, iTimeBin);
634 // Int_t value = adcArray->GetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin);
635 if (value < 0 || (offset - iAdc < 1) || (offset - iAdc > 165)) {
636 fADCR[iAdc][iTimeBin] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP) + (fgAddBaseline << fgkAddDigits);
637 fADCF[iAdc][iTimeBin] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFP) + (fgAddBaseline << fgkAddDigits);
638 }
639 else {
640 fZSMap[iAdc] = 0;
641 fADCR[iAdc][iTimeBin] = (value << fgkAddDigits) + (fgAddBaseline << fgkAddDigits);
642 fADCF[iAdc][iTimeBin] = (value << fgkAddDigits) + (fgAddBaseline << fgkAddDigits);
643 }
644 }
645 }
646}
647
ce4786b9 648void AliTRDmcmSim::SetDataPedestal( Int_t adc )
dfd03fc3 649{
0c349049 650 //
dfd03fc3 651 // Store ADC data into array of raw data
0c349049 652 //
dfd03fc3 653
ce4786b9 654 if( !CheckInitialized() )
655 return;
dfd03fc3 656
ce4786b9 657 if( adc < 0 || adc >= fgkNADC ) {
dfd03fc3 658 return;
659 }
660
661 for( Int_t it = 0 ; it < fNTimeBin ; it++ ) {
ce4786b9 662 fADCR[adc][it] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP) + (fgAddBaseline << fgkAddDigits);
663 fADCF[adc][it] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFP) + (fgAddBaseline << fgkAddDigits);
dfd03fc3 664 }
665}
666
ce4786b9 667Bool_t AliTRDmcmSim::GetHit(Int_t index, Int_t &channel, Int_t &timebin, Int_t &qtot, Int_t &ypos, Float_t &y, Int_t &label) const
668{
669 // retrieve the MC hit information (not available in TRAP hardware)
670
671 if (index < 0 || index >= fNHits)
672 return kFALSE;
673
674 channel = fHits[index].fChannel;
675 timebin = fHits[index].fTimebin;
676 qtot = fHits[index].fQtot;
677 ypos = fHits[index].fYpos;
678 y = (Float_t) ((((((fRobPos & 0x1) << 2) + (fMcmPos & 0x3)) * 18) << 8) - ((18*4*2 - 18*2 - 1) << 7) -
679 (channel << 8) - ypos)
680 * (0.635 + 0.03 * (fDetector % 6))
681 / 256.0;
25b41f6f 682 label = fHits[index].fLabel[0];
ce4786b9 683
684 return kTRUE;
685}
686
687Int_t AliTRDmcmSim::GetCol( Int_t adc )
dfd03fc3 688{
0c349049 689 //
dfd03fc3 690 // Return column id of the pad for the given ADC channel
0c349049 691 //
692
f793c83d 693 if( !CheckInitialized() )
694 return -1;
dfd03fc3 695
ce4786b9 696 Int_t col = fFeeParam->GetPadColFromADC(fRobPos, fMcmPos, adc);
a6d08b7f 697 if (col < 0 || col >= fFeeParam->GetNcol())
698 return -1;
699 else
700 return col;
dfd03fc3 701}
702
ce4786b9 703Int_t AliTRDmcmSim::ProduceRawStream( UInt_t *buf, Int_t bufSize, UInt_t iEv) const
987ba9a3 704{
705 //
706 // Produce raw data stream from this MCM and put in buf
707 // Returns number of words filled, or negative value
708 // with -1 * number of overflowed words
709 //
710
ce4786b9 711 if( !CheckInitialized() )
712 return 0;
713
987ba9a3 714 UInt_t x;
7d619a80 715 UInt_t mcmHeader = 0;
716 UInt_t adcMask = 0;
987ba9a3 717 Int_t nw = 0; // Number of written words
718 Int_t of = 0; // Number of overflowed words
719 Int_t rawVer = fFeeParam->GetRAWversion();
720 Int_t **adc;
721 Int_t nActiveADC = 0; // number of activated ADC bits in a word
722
ce4786b9 723 if( !CheckInitialized() )
724 return 0;
987ba9a3 725
ce4786b9 726 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBSF) != 0) // store unfiltered data
987ba9a3 727 adc = fADCR;
ce4786b9 728 else
987ba9a3 729 adc = fADCF;
ce4786b9 730
987ba9a3 731 // Produce ADC mask : nncc cccm mmmm mmmm mmmm mmmm mmmm 1100
732 // n : unused , c : ADC count, m : selected ADCs
7d619a80 733 if( rawVer >= 3 &&
734 (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kC15CPUA) & (1 << 13))) { // check for zs flag in TRAP configuration
ce4786b9 735 for( Int_t iAdc = 0 ; iAdc < fgkNADC ; iAdc++ ) {
736 if( ~fZSMap[iAdc] != 0 ) { // 0 means not suppressed
7d619a80 737 adcMask |= (1 << (iAdc+4) ); // last 4 digit reserved for 1100=0xc
738 nActiveADC++; // number of 1 in mmm....m
987ba9a3 739 }
740 }
987ba9a3 741
7d619a80 742 if ((nActiveADC == 0) &&
743 (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kC15CPUA) & (1 << 8))) // check for DEH flag in TRAP configuration
744 return 0;
745
746 // assemble adc mask word
747 adcMask |= (1 << 30) | ( ( 0x3FFFFFFC ) & (~(nActiveADC) << 25) ) | 0xC; // nn = 01, ccccc are inverted, 0xc=1100
748 }
749
750 // MCM header
751 mcmHeader = (1<<31) | (fRobPos << 28) | (fMcmPos << 24) | ((iEv % 0x100000) << 4) | 0xC;
752 if (nw < bufSize)
753 buf[nw++] = mcmHeader;
754 else
755 of++;
756
757 // ADC mask
758 if( adcMask != 0 ) {
759 if (nw < bufSize)
760 buf[nw++] = adcMask;
761 else
987ba9a3 762 of++;
987ba9a3 763 }
764
765 // Produce ADC data. 3 timebins are packed into one 32 bits word
766 // In this version, different ADC channel will NOT share the same word
767
768 UInt_t aa=0, a1=0, a2=0, a3=0;
769
770 for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
ce4786b9 771 if( rawVer>= 3 && ~fZSMap[iAdc] == 0 ) continue; // Zero Suppression, 0 means not suppressed
b0a41e80 772 aa = !(iAdc & 1) + 2;
987ba9a3 773 for (Int_t iT = 0; iT < fNTimeBin; iT+=3 ) {
b0a41e80 774 a1 = ((iT ) < fNTimeBin ) ? adc[iAdc][iT ] >> fgkAddDigits : 0;
775 a2 = ((iT + 1) < fNTimeBin ) ? adc[iAdc][iT+1] >> fgkAddDigits : 0;
776 a3 = ((iT + 2) < fNTimeBin ) ? adc[iAdc][iT+2] >> fgkAddDigits : 0;
987ba9a3 777 x = (a3 << 22) | (a2 << 12) | (a1 << 2) | aa;
ce4786b9 778 if (nw < bufSize) {
b0a41e80 779 buf[nw++] = x;
987ba9a3 780 }
781 else {
b0a41e80 782 of++;
987ba9a3 783 }
784 }
785 }
786
787 if( of != 0 ) return -of; else return nw;
788}
789
ce4786b9 790Int_t AliTRDmcmSim::ProduceTrackletStream( UInt_t *buf, Int_t bufSize )
1d93b218 791{
792 //
793 // Produce tracklet data stream from this MCM and put in buf
794 // Returns number of words filled, or negative value
795 // with -1 * number of overflowed words
796 //
797
ce4786b9 798 if( !CheckInitialized() )
799 return 0;
800
1d93b218 801 Int_t nw = 0; // Number of written words
802 Int_t of = 0; // Number of overflowed words
803
1d93b218 804 // Produce tracklet data. A maximum of four 32 Bit words will be written per MCM
805 // fMCMT is filled continuously until no more tracklet words available
806
f793c83d 807 for (Int_t iTracklet = 0; iTracklet < fTrackletArray->GetEntriesFast(); iTracklet++) {
ce4786b9 808 if (nw < bufSize)
f793c83d 809 buf[nw++] = ((AliTRDtrackletMCM*) (*fTrackletArray)[iTracklet])->GetTrackletWord();
810 else
811 of++;
1d93b218 812 }
813
814 if( of != 0 ) return -of; else return nw;
815}
816
dfd03fc3 817void AliTRDmcmSim::Filter()
818{
0c349049 819 //
b0a41e80 820 // Filter the raw ADC values. The active filter stages and their
821 // parameters are taken from AliTRDtrapConfig.
822 // The raw data is stored separate from the filtered data. Thus,
823 // it is possible to run the filters on a set of raw values
824 // sequentially for parameter tuning.
0c349049 825 //
dfd03fc3 826
ce4786b9 827 if( !CheckInitialized() )
b0a41e80 828 return;
dfd03fc3 829
b0a41e80 830 // Apply filters sequentially. Bypass is handled by filters
831 // since counters and internal registers may be updated even
832 // if the filter is bypassed.
833 // The first filter takes the data from fADCR and
834 // outputs to fADCF.
835
836 // Non-linearity filter not implemented.
837 FilterPedestal();
838 FilterGain();
839 FilterTail();
840 // Crosstalk filter not implemented.
dfd03fc3 841}
842
ce4786b9 843void AliTRDmcmSim::FilterPedestalInit(Int_t baseline)
b0a41e80 844{
845 // Initializes the pedestal filter assuming that the input has
846 // been constant for a long time (compared to the time constant).
847
b0a41e80 848 UShort_t fptc = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPTC); // 0..3, 0 - fastest, 3 - slowest
b0a41e80 849
ce4786b9 850 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++)
851 fPedAcc[iAdc] = (baseline << 2) * (1 << fgkFPshifts[fptc]);
b0a41e80 852}
853
854UShort_t AliTRDmcmSim::FilterPedestalNextSample(Int_t adc, Int_t timebin, UShort_t value)
dfd03fc3 855{
b0a41e80 856 // Returns the output of the pedestal filter given the input value.
857 // The output depends on the internal registers and, thus, the
858 // history of the filter.
859
860 UShort_t fpnp = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP); // 0..511 -> 0..127.75, pedestal at the output
861 UShort_t fptc = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPTC); // 0..3, 0 - fastest, 3 - slowest
ce4786b9 862 UShort_t fpby = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPBY); // 0..1 bypass, active low
b0a41e80 863
864 UShort_t accumulatorShifted;
865 Int_t correction;
866 UShort_t inpAdd;
867
868 inpAdd = value + fpnp;
869
ce4786b9 870 accumulatorShifted = (fPedAcc[adc] >> fgkFPshifts[fptc]) & 0x3FF; // 10 bits
b0a41e80 871 if (timebin == 0) // the accumulator is disabled in the drift time
872 {
873 correction = (value & 0x3FF) - accumulatorShifted;
874 fPedAcc[adc] = (fPedAcc[adc] + correction) & 0x7FFFFFFF; // 31 bits
875 }
876
ce4786b9 877 if (fpby == 0)
878 return value;
879
b0a41e80 880 if (inpAdd <= accumulatorShifted)
881 return 0;
882 else
883 {
884 inpAdd = inpAdd - accumulatorShifted;
885 if (inpAdd > 0xFFF)
886 return 0xFFF;
887 else
888 return inpAdd;
889 }
890}
23200400 891
b0a41e80 892void AliTRDmcmSim::FilterPedestal()
893{
0c349049 894 //
dfd03fc3 895 // Apply pedestal filter
0c349049 896 //
b0a41e80 897 // As the first filter in the chain it reads data from fADCR
898 // and outputs to fADCF.
899 // It has only an effect if previous samples have been fed to
900 // find the pedestal. Currently, the simulation assumes that
901 // the input has been stable for a sufficiently long time.
dfd03fc3 902
b0a41e80 903 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
ce4786b9 904 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
b0a41e80 905 fADCF[iAdc][iTimeBin] = FilterPedestalNextSample(iAdc, iTimeBin, fADCR[iAdc][iTimeBin]);
dfd03fc3 906 }
907 }
908}
909
b0a41e80 910void AliTRDmcmSim::FilterGainInit()
dfd03fc3 911{
b0a41e80 912 // Initializes the gain filter. In this case, only threshold
913 // counters are reset.
0c349049 914
ce4786b9 915 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
b0a41e80 916 // these are counters which in hardware continue
917 // until maximum or reset
918 fGainCounterA[iAdc] = 0;
919 fGainCounterB[iAdc] = 0;
920 }
dfd03fc3 921}
922
b0a41e80 923UShort_t AliTRDmcmSim::FilterGainNextSample(Int_t adc, UShort_t value)
dfd03fc3 924{
b0a41e80 925 // Apply the gain filter to the given value.
926 // BEGIN_LATEX O_{i}(t) = #gamma_{i} * I_{i}(t) + a_{i} END_LATEX
927 // The output depends on the internal registers and, thus, the
928 // history of the filter.
dfd03fc3 929
b0a41e80 930 UShort_t fgby = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFGBY); // bypass, active low
931 UShort_t fgf = fTrapConfig->GetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGF0 + adc)); // 0x700 + (0 & 0x1ff);
932 UShort_t fga = fTrapConfig->GetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGA0 + adc)); // 40;
933 UShort_t fgta = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFGTA); // 20;
934 UShort_t fgtb = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFGTB); // 2060;
dfd03fc3 935
ce4786b9 936 UInt_t corr; // corrected value
dfd03fc3 937
b0a41e80 938 value &= 0xFFF;
ce4786b9 939 corr = (value * fgf) >> 11;
940 corr = corr > 0xfff ? 0xfff : corr;
941 corr = AddUintClipping(corr, fga, 12);
b0a41e80 942
943 // Update threshold counters
944 // not really useful as they are cleared with every new event
ce4786b9 945 if (!((fGainCounterA[adc] == 0x3FFFFFF) || (fGainCounterB[adc] == 0x3FFFFFF)))
946 // stop when full
b0a41e80 947 {
ce4786b9 948 if (corr >= fgtb)
b0a41e80 949 fGainCounterB[adc]++;
ce4786b9 950 else if (corr >= fgta)
b0a41e80 951 fGainCounterA[adc]++;
952 }
953
ce4786b9 954 if (fgby == 1)
955 return corr;
956 else
957 return value;
b0a41e80 958}
959
960void AliTRDmcmSim::FilterGain()
961{
962 // Read data from fADCF and apply gain filter.
963
ce4786b9 964 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
b0a41e80 965 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
966 fADCF[iAdc][iTimeBin] = FilterGainNextSample(iAdc, fADCF[iAdc][iTimeBin]);
1d93b218 967 }
b0a41e80 968 }
969}
1d93b218 970
b0a41e80 971void AliTRDmcmSim::FilterTailInit(Int_t baseline)
972{
973 // Initializes the tail filter assuming that the input has
974 // been at the baseline value (configured by FTFP) for a
975 // sufficiently long time.
976
977 // exponents and weight calculated from configuration
978 UShort_t alphaLong = 0x3ff & fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTAL); // the weight of the long component
979 UShort_t lambdaLong = (1 << 10) | (1 << 9) | (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTLL) & 0x1FF); // the multiplier
980 UShort_t lambdaShort = (0 << 10) | (1 << 9) | (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTLS) & 0x1FF); // the multiplier
981
982 Float_t lambdaL = lambdaLong * 1.0 / (1 << 11);
983 Float_t lambdaS = lambdaShort * 1.0 / (1 << 11);
984 Float_t alphaL = alphaLong * 1.0 / (1 << 11);
985 Float_t qup, qdn;
986 qup = (1 - lambdaL) * (1 - lambdaS);
987 qdn = 1 - lambdaS * alphaL - lambdaL * (1 - alphaL);
988 Float_t kdc = qup/qdn;
989
990 Float_t kt, ql, qs;
991 UShort_t aout;
ce4786b9 992
993 if (baseline < 0)
994 baseline = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP);
b0a41e80 995
b0a41e80 996 ql = lambdaL * (1 - lambdaS) * alphaL;
997 qs = lambdaS * (1 - lambdaL) * (1 - alphaL);
998
ce4786b9 999 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
1000 Int_t value = baseline & 0xFFF;
1001 Int_t corr = (value * fTrapConfig->GetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGF0 + iAdc))) >> 11;
1002 corr = corr > 0xfff ? 0xfff : corr;
1003 corr = AddUintClipping(corr, fTrapConfig->GetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGA0 + iAdc)), 12);
1004
1005 kt = kdc * baseline;
1006 aout = baseline - (UShort_t) kt;
1007
b0a41e80 1008 fTailAmplLong[iAdc] = (UShort_t) (aout * ql / (ql + qs));
1009 fTailAmplShort[iAdc] = (UShort_t) (aout * qs / (ql + qs));
1010 }
1011}
1012
1013UShort_t AliTRDmcmSim::FilterTailNextSample(Int_t adc, UShort_t value)
1014{
1015 // Returns the output of the tail filter for the given input value.
1016 // The output depends on the internal registers and, thus, the
1017 // history of the filter.
1018
1019 // exponents and weight calculated from configuration
ce4786b9 1020 UShort_t alphaLong = 0x3ff & fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTAL); // the weight of the long component
1021 UShort_t lambdaLong = (1 << 10) | (1 << 9) | (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTLL) & 0x1FF); // the multiplier of the long component
1022 UShort_t lambdaShort = (0 << 10) | (1 << 9) | (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTLS) & 0x1FF); // the multiplier of the short component
b0a41e80 1023
ce4786b9 1024 // intermediate signals
1025 UInt_t aDiff;
1026 UInt_t alInpv;
b0a41e80 1027 UShort_t aQ;
ce4786b9 1028 UInt_t tmp;
b0a41e80 1029
ab9f7002 1030 UShort_t inpVolt = value & 0xFFF; // 12 bits
b0a41e80 1031
ce4786b9 1032 // add the present generator outputs
1033 aQ = AddUintClipping(fTailAmplLong[adc], fTailAmplShort[adc], 12);
1034
1035 // calculate the difference between the input and the generated signal
1036 if (inpVolt > aQ)
1037 aDiff = inpVolt - aQ;
1038 else
1039 aDiff = 0;
1040
1041 // the inputs to the two generators, weighted
1042 alInpv = (aDiff * alphaLong) >> 11;
1043
1044 // the new values of the registers, used next time
1045 // long component
1046 tmp = AddUintClipping(fTailAmplLong[adc], alInpv, 12);
1047 tmp = (tmp * lambdaLong) >> 11;
1048 fTailAmplLong[adc] = tmp & 0xFFF;
1049 // short component
1050 tmp = AddUintClipping(fTailAmplShort[adc], aDiff - alInpv, 12);
1051 tmp = (tmp * lambdaShort) >> 11;
1052 fTailAmplShort[adc] = tmp & 0xFFF;
1053
1054 // the output of the filter
b0a41e80 1055 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTBY) == 0) // bypass mode, active low
1056 return value;
1057 else
b0a41e80 1058 return aDiff;
b0a41e80 1059}
dfd03fc3 1060
b0a41e80 1061void AliTRDmcmSim::FilterTail()
1062{
ce4786b9 1063 // Apply tail cancellation filter to all data.
fabf2e09 1064
b0a41e80 1065 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
ce4786b9 1066 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
b0a41e80 1067 fADCF[iAdc][iTimeBin] = FilterTailNextSample(iAdc, fADCF[iAdc][iTimeBin]);
1068 }
1069 }
dfd03fc3 1070}
1071
dfd03fc3 1072void AliTRDmcmSim::ZSMapping()
1073{
0c349049 1074 //
dfd03fc3 1075 // Zero Suppression Mapping implemented in TRAP chip
ce4786b9 1076 // only implemented for up to 30 timebins
dfd03fc3 1077 //
1078 // See detail TRAP manual "Data Indication" section:
1079 // http://www.kip.uni-heidelberg.de/ti/TRD/doc/trap/TRAP-UserManual.pdf
0c349049 1080 //
dfd03fc3 1081
ce4786b9 1082 if( !CheckInitialized() )
1083 return;
1084
1085 Int_t eBIS = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBIS);
1086 Int_t eBIT = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBIT);
1087 Int_t eBIL = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBIL);
1088 Int_t eBIN = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBIN);
ecf39416 1089
b0a41e80 1090 Int_t **adc = fADCF;
dfd03fc3 1091
ce4786b9 1092 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++)
1093 fZSMap[iAdc] = -1;
b0a41e80 1094
1095 for( Int_t it = 0 ; it < fNTimeBin ; it++ ) {
ce4786b9 1096 Int_t iAdc; // current ADC channel
1097 Int_t ap;
1098 Int_t ac;
1099 Int_t an;
1100 Int_t mask;
1101 Int_t supp; // suppression of the current channel (low active)
1102
1103 // ----- first channel -----
1104 iAdc = 0;
1105
1106 ap = 0; // previous
1107 ac = adc[iAdc ][it]; // current
1108 an = adc[iAdc+1][it]; // next
1109
1110 mask = ( ac >= ap && ac >= an ) ? 0 : 0x1; // peak center detection
1111 mask += ( ap + ac + an > eBIT ) ? 0 : 0x2; // cluster
1112 mask += ( ac > eBIS ) ? 0 : 0x4; // absolute large peak
1113
1114 supp = (eBIL >> mask) & 1;
1115
1116 fZSMap[iAdc] &= ~((1-supp) << it);
1117 if( eBIN == 0 ) { // neighbour sensitivity
1118 fZSMap[iAdc+1] &= ~((1-supp) << it);
dfd03fc3 1119 }
ce4786b9 1120
1121 // ----- last channel -----
1122 iAdc = fgkNADC - 1;
1123
1124 ap = adc[iAdc-1][it]; // previous
1125 ac = adc[iAdc ][it]; // current
1126 an = 0; // next
1127
1128 mask = ( ac >= ap && ac >= an ) ? 0 : 0x1; // peak center detection
1129 mask += ( ap + ac + an > eBIT ) ? 0 : 0x2; // cluster
1130 mask += ( ac > eBIS ) ? 0 : 0x4; // absolute large peak
1131
1132 supp = (eBIL >> mask) & 1;
1133
1134 fZSMap[iAdc] &= ~((1-supp) << it);
1135 if( eBIN == 0 ) { // neighbour sensitivity
1136 fZSMap[iAdc-1] &= ~((1-supp) << it);
ecf39416 1137 }
ce4786b9 1138
1139 // ----- middle channels -----
1140 for( iAdc = 1 ; iAdc < fgkNADC-1; iAdc++ ) {
1141 ap = adc[iAdc-1][it]; // previous
1142 ac = adc[iAdc ][it]; // current
1143 an = adc[iAdc+1][it]; // next
1144
1145 mask = ( ac >= ap && ac >= an ) ? 0 : 0x1; // peak center detection
1146 mask += ( ap + ac + an > eBIT ) ? 0 : 0x2; // cluster
1147 mask += ( ac > eBIS ) ? 0 : 0x4; // absolute large peak
1148
1149 supp = (eBIL >> mask) & 1;
1150
1151 fZSMap[iAdc] &= ~((1-supp) << it);
1152 if( eBIN == 0 ) { // neighbour sensitivity
1153 fZSMap[iAdc-1] &= ~((1-supp) << it);
1154 fZSMap[iAdc+1] &= ~((1-supp) << it);
ecf39416 1155 }
dfd03fc3 1156 }
ce4786b9 1157
dfd03fc3 1158 }
1159}
1160
25b41f6f 1161void AliTRDmcmSim::AddHitToFitreg(Int_t adc, UShort_t timebin, UShort_t qtot, Short_t ypos, Int_t label[])
dfd03fc3 1162{
b0a41e80 1163 // Add the given hit to the fit register which is lateron used for
1164 // the tracklet calculation.
1165 // In addition to the fit sums in the fit register MC information
1166 // is stored.
1167
1168 if ((timebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS0)) &&
1169 (timebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE0)))
ab9f7002 1170 fFitReg[adc].fQ0 += qtot;
b0a41e80 1171
1172 if ((timebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS1)) &&
1173 (timebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE1)))
ab9f7002 1174 fFitReg[adc].fQ1 += qtot;
b0a41e80 1175
1176 if ((timebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFS) ) &&
1177 (timebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFE)))
1178 {
ab9f7002 1179 fFitReg[adc].fSumX += timebin;
1180 fFitReg[adc].fSumX2 += timebin*timebin;
1181 fFitReg[adc].fNhits++;
1182 fFitReg[adc].fSumY += ypos;
1183 fFitReg[adc].fSumY2 += ypos*ypos;
1184 fFitReg[adc].fSumXY += timebin*ypos;
b0a41e80 1185 }
1186
1187 // register hits (MC info)
ab9f7002 1188 fHits[fNHits].fChannel = adc;
1189 fHits[fNHits].fQtot = qtot;
1190 fHits[fNHits].fYpos = ypos;
1191 fHits[fNHits].fTimebin = timebin;
25b41f6f 1192 fHits[fNHits].fLabel[0] = label[0];
1193 fHits[fNHits].fLabel[1] = label[1];
1194 fHits[fNHits].fLabel[2] = label[2];
b0a41e80 1195 fNHits++;
dfd03fc3 1196}
1197
b0a41e80 1198void AliTRDmcmSim::CalcFitreg()
dfd03fc3 1199{
b0a41e80 1200 // Preprocessing.
1201 // Detect the hits and fill the fit registers.
1202 // Requires 12-bit data from fADCF which means Filter()
1203 // has to be called before even if all filters are bypassed.
1204
b0a41e80 1205 //??? to be clarified:
64e3d742 1206 UInt_t adcMask = 0xffffffff;
b0a41e80 1207
ab9f7002 1208 UShort_t timebin, adcch, adcLeft, adcCentral, adcRight, hitQual, timebin1, timebin2, qtotTemp;
b0a41e80 1209 Short_t ypos, fromLeft, fromRight, found;
5ac2e3b1 1210 UShort_t qTotal[19+1]; // the last is dummy
ab9f7002 1211 UShort_t marked[6], qMarked[6], worse1, worse2;
b0a41e80 1212
1213 timebin1 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFS);
1214 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS0)
1215 < timebin1)
1216 timebin1 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS0);
1217 timebin2 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFE);
1218 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE1)
1219 > timebin2)
1220 timebin2 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE1);
1221
1222 // reset the fit registers
1223 fNHits = 0;
ce4786b9 1224 for (adcch = 0; adcch < fgkNADC-2; adcch++) // due to border channels
b0a41e80 1225 {
ab9f7002 1226 fFitReg[adcch].fNhits = 0;
1227 fFitReg[adcch].fQ0 = 0;
1228 fFitReg[adcch].fQ1 = 0;
1229 fFitReg[adcch].fSumX = 0;
1230 fFitReg[adcch].fSumY = 0;
1231 fFitReg[adcch].fSumX2 = 0;
1232 fFitReg[adcch].fSumY2 = 0;
1233 fFitReg[adcch].fSumXY = 0;
b0a41e80 1234 }
1235
1236 for (timebin = timebin1; timebin < timebin2; timebin++)
1237 {
ab9f7002 1238 // first find the hit candidates and store the total cluster charge in qTotal array
b0a41e80 1239 // in case of not hit store 0 there.
ce4786b9 1240 for (adcch = 0; adcch < fgkNADC-2; adcch++) {
ab9f7002 1241 if ( ( (adcMask >> adcch) & 7) == 7) //??? all 3 channels are present in case of ZS
b0a41e80 1242 {
ab9f7002 1243 adcLeft = fADCF[adcch ][timebin];
1244 adcCentral = fADCF[adcch+1][timebin];
1245 adcRight = fADCF[adcch+2][timebin];
b0a41e80 1246 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPVBY) == 1)
ab9f7002 1247 hitQual = ( (adcLeft * adcRight) <
1248 (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPVT) * adcCentral) );
b0a41e80 1249 else
ab9f7002 1250 hitQual = 1;
b0a41e80 1251 // The accumulated charge is with the pedestal!!!
ab9f7002 1252 qtotTemp = adcLeft + adcCentral + adcRight;
1253 if ( (hitQual) &&
1254 (qtotTemp >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPHT)) &&
1255 (adcLeft <= adcCentral) &&
1256 (adcCentral > adcRight) )
1257 qTotal[adcch] = qtotTemp;
b0a41e80 1258 else
ab9f7002 1259 qTotal[adcch] = 0;
1d93b218 1260 }
b0a41e80 1261 else
ab9f7002 1262 qTotal[adcch] = 0; //jkl
ce4786b9 1263 if (qTotal[adcch] != 0)
1264 AliDebug(10,Form("ch %2d qTotal %5d",adcch, qTotal[adcch]));
1d93b218 1265 }
b0a41e80 1266
1267 fromLeft = -1;
1268 adcch = 0;
1269 found = 0;
1270 marked[4] = 19; // invalid channel
1271 marked[5] = 19; // invalid channel
ab9f7002 1272 qTotal[19] = 0;
b0a41e80 1273 while ((adcch < 16) && (found < 3))
1274 {
ab9f7002 1275 if (qTotal[adcch] > 0)
b0a41e80 1276 {
1277 fromLeft = adcch;
1278 marked[2*found+1]=adcch;
1279 found++;
1d93b218 1280 }
b0a41e80 1281 adcch++;
1d93b218 1282 }
1d93b218 1283
b0a41e80 1284 fromRight = -1;
1285 adcch = 18;
1286 found = 0;
1287 while ((adcch > 2) && (found < 3))
1288 {
ab9f7002 1289 if (qTotal[adcch] > 0)
b0a41e80 1290 {
1291 marked[2*found]=adcch;
1292 found++;
1293 fromRight = adcch;
1294 }
1295 adcch--;
1d93b218 1296 }
1d93b218 1297
4ff7ed2b 1298 AliDebug(10,Form("Fromleft=%d, Fromright=%d",fromLeft, fromRight));
b0a41e80 1299 // here mask the hit candidates in the middle, if any
1300 if ((fromLeft >= 0) && (fromRight >= 0) && (fromLeft < fromRight))
1301 for (adcch = fromLeft+1; adcch < fromRight; adcch++)
ab9f7002 1302 qTotal[adcch] = 0;
1d93b218 1303
b0a41e80 1304 found = 0;
1305 for (adcch = 0; adcch < 19; adcch++)
ab9f7002 1306 if (qTotal[adcch] > 0) found++;
b0a41e80 1307 // NOT READY
1d93b218 1308
b0a41e80 1309 if (found > 4) // sorting like in the TRAP in case of 5 or 6 candidates!
1310 {
1311 if (marked[4] == marked[5]) marked[5] = 19;
1312 for (found=0; found<6; found++)
1313 {
ab9f7002 1314 qMarked[found] = qTotal[marked[found]] >> 4;
4ff7ed2b 1315 AliDebug(10,Form("ch_%d qTotal %d qTotals %d",marked[found],qTotal[marked[found]],qMarked[found]));
1d93b218 1316 }
b0a41e80 1317
1318 Sort6To2Worst(marked[0], marked[3], marked[4], marked[1], marked[2], marked[5],
ab9f7002 1319 qMarked[0],
1320 qMarked[3],
1321 qMarked[4],
1322 qMarked[1],
1323 qMarked[2],
1324 qMarked[5],
b0a41e80 1325 &worse1, &worse2);
1326 // Now mask the two channels with the smallest charge
1327 if (worse1 < 19)
1328 {
ab9f7002 1329 qTotal[worse1] = 0;
4ff7ed2b 1330 AliDebug(10,Form("Kill ch %d\n",worse1));
1d93b218 1331 }
b0a41e80 1332 if (worse2 < 19)
1333 {
ab9f7002 1334 qTotal[worse2] = 0;
4ff7ed2b 1335 AliDebug(10,Form("Kill ch %d\n",worse2));
1d93b218 1336 }
1d93b218 1337 }
1d93b218 1338
b0a41e80 1339 for (adcch = 0; adcch < 19; adcch++) {
ab9f7002 1340 if (qTotal[adcch] > 0) // the channel is marked for processing
b0a41e80 1341 {
ab9f7002 1342 adcLeft = fADCF[adcch ][timebin];
1343 adcCentral = fADCF[adcch+1][timebin];
1344 adcRight = fADCF[adcch+2][timebin];
b0a41e80 1345 // hit detected, in TRAP we have 4 units and a hit-selection, here we proceed all channels!
1346 // subtract the pedestal TPFP, clipping instead of wrapping
1347
ab9f7002 1348 Int_t regTPFP = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFP);
4ff7ed2b 1349 AliDebug(10, Form("Hit found, time=%d, adcch=%d/%d/%d, adc values=%d/%d/%d, regTPFP=%d, TPHT=%d\n",
1350 timebin, adcch, adcch+1, adcch+2, adcLeft, adcCentral, adcRight, regTPFP,
1351 fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPHT)));
b0a41e80 1352
ab9f7002 1353 if (adcLeft < regTPFP) adcLeft = 0; else adcLeft -= regTPFP;
1354 if (adcCentral < regTPFP) adcCentral = 0; else adcCentral -= regTPFP;
1355 if (adcRight < regTPFP) adcRight = 0; else adcRight -= regTPFP;
f793c83d 1356
b0a41e80 1357 // Calculate the center of gravity
f793c83d 1358 // checking for adcCentral != 0 (in case of "bad" configuration)
1359 if (adcCentral == 0)
1360 continue;
ab9f7002 1361 ypos = 128*(adcLeft - adcRight) / adcCentral;
b0a41e80 1362 if (ypos < 0) ypos = -ypos;
ce4786b9 1363 // make the correction using the position LUT
25b41f6f 1364 ypos = ypos + fTrapConfig->GetTrapReg((AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kTPL00 + (ypos & 0x7F)),
1365 fDetector, fRobPos, fMcmPos);
ab9f7002 1366 if (adcLeft > adcRight) ypos = -ypos;
40bd6ee4 1367
25b41f6f 1368 // label calculation (up to 3)
1369 Int_t mcLabel[] = {-1, -1, -1};
40bd6ee4 1370 if (fDigitsManager) {
25b41f6f 1371 const Int_t maxLabels = 9;
1372 Int_t label[maxLabels] = { 0 }; // up to 9 different labels possible
1373 Int_t count[maxLabels] = { 0 };
40bd6ee4 1374 Int_t nLabels = 0;
1375 Int_t padcol[3];
1376 padcol[0] = fFeeParam->GetPadColFromADC(fRobPos, fMcmPos, adcch);
1377 padcol[1] = fFeeParam->GetPadColFromADC(fRobPos, fMcmPos, adcch+1);
1378 padcol[2] = fFeeParam->GetPadColFromADC(fRobPos, fMcmPos, adcch+2);
1379 Int_t padrow = fFeeParam->GetPadRowFromMCM(fRobPos, fMcmPos);
1380 for (Int_t iDict = 0; iDict < 3; iDict++) {
ce4786b9 1381 if (!fDict[iDict])
40bd6ee4 1382 continue;
40bd6ee4 1383 for (Int_t iPad = 0; iPad < 3; iPad++) {
1384 if (padcol[iPad] < 0)
1385 continue;
25b41f6f 1386 Int_t currLabel = fDict[iDict]->GetData(padrow, padcol[iPad], timebin);
4ff7ed2b 1387 AliDebug(10, Form("Read label: %4i for det: %3i, row: %i, col: %i, tb: %i\n", currLabel, fDetector, padrow, padcol[iPad], timebin));
40bd6ee4 1388 for (Int_t iLabel = 0; iLabel < nLabels; iLabel++) {
1389 if (currLabel == label[iLabel]) {
1390 count[iLabel]++;
ce51199c 1391 currLabel = -1;
40bd6ee4 1392 break;
1393 }
1394 }
ce51199c 1395 if (currLabel >= 0) {
25b41f6f 1396 label[nLabels] = currLabel;
1397 count[nLabels] = 1;
1398 nLabels++;
40bd6ee4 1399 }
1400 }
1401 }
637666cd 1402 Int_t index[2*maxLabels];
25b41f6f 1403 TMath::Sort(maxLabels, count, index);
1404 for (Int_t i = 0; i < 3; i++) {
1405 if (count[index[i]] <= 0)
1406 break;
1407 mcLabel[i] = label[index[i]];
1408 }
40bd6ee4 1409 }
1410
1411 // add the hit to the fitregister
1412 AddHitToFitreg(adcch, timebin, qTotal[adcch], ypos, mcLabel);
1d93b218 1413 }
1d93b218 1414 }
1d93b218 1415 }
ce4786b9 1416
1417 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
1418 if (fFitReg[iAdc].fNhits != 0) {
1419 AliDebug(2, Form("fitreg[%i]: nHits = %i, sumX = %i, sumY = %i, sumX2 = %i, sumY2 = %i, sumXY = %i", iAdc,
1420 fFitReg[iAdc].fNhits,
1421 fFitReg[iAdc].fSumX,
1422 fFitReg[iAdc].fSumY,
1423 fFitReg[iAdc].fSumX2,
1424 fFitReg[iAdc].fSumY2,
1425 fFitReg[iAdc].fSumXY
1426 ));
1427 }
1428 }
b0a41e80 1429}
1d93b218 1430
b0a41e80 1431void AliTRDmcmSim::TrackletSelection()
1432{
1433 // Select up to 4 tracklet candidates from the fit registers
1434 // and assign them to the CPUs.
1435
ab9f7002 1436 UShort_t adcIdx, i, j, ntracks, tmp;
1437 UShort_t trackletCand[18][2]; // store the adcch[0] and number of hits[1] for all tracklet candidates
b0a41e80 1438
1439 ntracks = 0;
ab9f7002 1440 for (adcIdx = 0; adcIdx < 18; adcIdx++) // ADCs
1441 if ( (fFitReg[adcIdx].fNhits
b0a41e80 1442 >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPCL)) &&
ab9f7002 1443 (fFitReg[adcIdx].fNhits+fFitReg[adcIdx+1].fNhits
b0a41e80 1444 >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPCT)))
1445 {
ab9f7002 1446 trackletCand[ntracks][0] = adcIdx;
1447 trackletCand[ntracks][1] = fFitReg[adcIdx].fNhits+fFitReg[adcIdx+1].fNhits;
4ff7ed2b 1448 AliDebug(10,Form("%d %2d %4d\n", ntracks, trackletCand[ntracks][0], trackletCand[ntracks][1]));
b0a41e80 1449 ntracks++;
1450 };
1451
4ff7ed2b 1452 for (i=0; i<ntracks;i++)
1453 AliDebug(10,Form("%d %d %d\n",i,trackletCand[i][0], trackletCand[i][1]));
b0a41e80 1454
1455 if (ntracks > 4)
1456 {
1457 // primitive sorting according to the number of hits
1458 for (j = 0; j < (ntracks-1); j++)
1459 {
1460 for (i = j+1; i < ntracks; i++)
1461 {
ab9f7002 1462 if ( (trackletCand[j][1] < trackletCand[i][1]) ||
1463 ( (trackletCand[j][1] == trackletCand[i][1]) && (trackletCand[j][0] < trackletCand[i][0]) ) )
b0a41e80 1464 {
1465 // swap j & i
ab9f7002 1466 tmp = trackletCand[j][1];
1467 trackletCand[j][1] = trackletCand[i][1];
1468 trackletCand[i][1] = tmp;
1469 tmp = trackletCand[j][0];
1470 trackletCand[j][0] = trackletCand[i][0];
1471 trackletCand[i][0] = tmp;
b0a41e80 1472 }
1d93b218 1473 }
1d93b218 1474 }
b0a41e80 1475 ntracks = 4; // cut the rest, 4 is the max
1d93b218 1476 }
b0a41e80 1477 // else is not necessary to sort
1d93b218 1478
b0a41e80 1479 // now sort, so that the first tracklet going to CPU0 corresponds to the highest adc channel - as in the TRAP
1480 for (j = 0; j < (ntracks-1); j++)
1481 {
1482 for (i = j+1; i < ntracks; i++)
1483 {
ab9f7002 1484 if (trackletCand[j][0] < trackletCand[i][0])
b0a41e80 1485 {
1486 // swap j & i
ab9f7002 1487 tmp = trackletCand[j][1];
1488 trackletCand[j][1] = trackletCand[i][1];
1489 trackletCand[i][1] = tmp;
1490 tmp = trackletCand[j][0];
1491 trackletCand[j][0] = trackletCand[i][0];
1492 trackletCand[i][0] = tmp;
1d93b218 1493 }
1d93b218 1494 }
1495 }
b0a41e80 1496 for (i = 0; i < ntracks; i++) // CPUs with tracklets.
ab9f7002 1497 fFitPtr[i] = trackletCand[i][0]; // pointer to the left channel with tracklet for CPU[i]
b0a41e80 1498 for (i = ntracks; i < 4; i++) // CPUs without tracklets
1499 fFitPtr[i] = 31; // pointer to the left channel with tracklet for CPU[i] = 31 (invalid)
4ff7ed2b 1500 AliDebug(10,Form("found %i tracklet candidates\n", ntracks));
1501 for (i = 0; i < 4; i++)
1502 AliDebug(10,Form("fitPtr[%i]: %i\n", i, fFitPtr[i]));
b0a41e80 1503}
1d93b218 1504
b0a41e80 1505void AliTRDmcmSim::FitTracklet()
1506{
1507 // Perform the actual tracklet fit based on the fit sums
1508 // which have been filled in the fit registers.
1509
1510 // parameters in fitred.asm (fit program)
b0a41e80 1511 Int_t rndAdd = 0;
78c94f0b 1512 Int_t decPlaces = 5; // must be larger than 1 or change the following code
1513 // if (decPlaces > 1)
b0a41e80 1514 rndAdd = (1 << (decPlaces-1)) + 1;
78c94f0b 1515 // else if (decPlaces == 1)
1516 // rndAdd = 1;
1517
4ff7ed2b 1518 Int_t ndriftDp = 5; // decimal places for drift time
1519 Long64_t shift = ((Long64_t) 1 << 32);
1520
4ff7ed2b 1521 // calculated in fitred.asm
1522 Int_t padrow = ((fRobPos >> 1) << 2) | (fMcmPos >> 2);
1523 Int_t yoffs = (((((fRobPos & 0x1) << 2) + (fMcmPos & 0x3)) * 18) << 8) -
1524 ((18*4*2 - 18*2 - 1) << 7);
1525 yoffs = yoffs << decPlaces; // holds position of ADC channel 1
1526 Int_t layer = fDetector % 6;
1527 UInt_t scaleY = (UInt_t) ((0.635 + 0.03 * layer)/(256.0 * 160.0e-4) * shift);
1528 UInt_t scaleD = (UInt_t) ((0.635 + 0.03 * layer)/(256.0 * 140.0e-4) * shift);
4ff7ed2b 1529
8ea391e3 1530 Int_t deflCorr = (Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCorr, fDetector, fRobPos, fMcmPos);
1531 Int_t ndrift = (Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrNdrift, fDetector, fRobPos, fMcmPos);
b0a41e80 1532
1533 // local variables for calculation
1534 Long64_t mult, temp, denom; //???
8ea391e3 1535 UInt_t q0, q1, pid; // charges in the two windows and total charge
b0a41e80 1536 UShort_t nHits; // number of hits
1537 Int_t slope, offset; // slope and offset of the tracklet
1538 Int_t sumX, sumY, sumXY, sumX2; // fit sums from fit registers
ce51199c 1539 Int_t sumY2; // not used in the current TRAP program, now used for error calculation (simulation only)
1540 Float_t fitError, fitSlope, fitOffset;
b0a41e80 1541 FitReg_t *fit0, *fit1; // pointers to relevant fit registers
1542
1543// const uint32_t OneDivN[32] = { // 2**31/N : exactly like in the TRAP, the simple division here gives the same result!
1544// 0x00000000, 0x80000000, 0x40000000, 0x2AAAAAA0, 0x20000000, 0x19999990, 0x15555550, 0x12492490,
1545// 0x10000000, 0x0E38E380, 0x0CCCCCC0, 0x0BA2E8B0, 0x0AAAAAA0, 0x09D89D80, 0x09249240, 0x08888880,
1546// 0x08000000, 0x07878780, 0x071C71C0, 0x06BCA1A0, 0x06666660, 0x06186180, 0x05D17450, 0x0590B210,
1547// 0x05555550, 0x051EB850, 0x04EC4EC0, 0x04BDA120, 0x04924920, 0x0469EE50, 0x04444440, 0x04210840};
1548
1549 for (Int_t cpu = 0; cpu < 4; cpu++) {
1550 if (fFitPtr[cpu] == 31)
1551 {
1552 fMCMT[cpu] = 0x10001000; //??? AliTRDfeeParam::GetTrackletEndmarker();
1d93b218 1553 }
b0a41e80 1554 else
1555 {
1556 fit0 = &fFitReg[fFitPtr[cpu] ];
1557 fit1 = &fFitReg[fFitPtr[cpu]+1]; // next channel
1558
1559 mult = 1;
1560 mult = mult << (32 + decPlaces);
1561 mult = -mult;
1562
1563 // Merging
ab9f7002 1564 nHits = fit0->fNhits + fit1->fNhits; // number of hits
1565 sumX = fit0->fSumX + fit1->fSumX;
1566 sumX2 = fit0->fSumX2 + fit1->fSumX2;
54d34aac 1567 denom = ((Long64_t) nHits)*((Long64_t) sumX2) - ((Long64_t) sumX)*((Long64_t) sumX);
b0a41e80 1568
1569 mult = mult / denom; // exactly like in the TRAP program
ab9f7002 1570 q0 = fit0->fQ0 + fit1->fQ0;
1571 q1 = fit0->fQ1 + fit1->fQ1;
1572 sumY = fit0->fSumY + fit1->fSumY + 256*fit1->fNhits;
1573 sumXY = fit0->fSumXY + fit1->fSumXY + 256*fit1->fSumX;
ce51199c 1574 sumY2 = fit0->fSumY2 + fit1->fSumY2 + 512*fit1->fSumY + 256*256*fit1->fNhits;
b0a41e80 1575
1576 slope = nHits*sumXY - sumX * sumY;
1577 offset = sumX2*sumY - sumX * sumXY;
1578 temp = mult * slope;
1579 slope = temp >> 32; // take the upper 32 bits
4ff7ed2b 1580 slope = -slope;
b0a41e80 1581 temp = mult * offset;
1582 offset = temp >> 32; // take the upper 32 bits
1583
4ff7ed2b 1584 offset = offset + yoffs;
ce4786b9 1585 AliDebug(10, Form("slope = %i, slope * ndrift = %i, deflCorr: %i",
1586 slope, slope * ndrift, deflCorr));
1587 slope = ((slope * ndrift) >> ndriftDp) + deflCorr;
b0a41e80 1588 offset = offset - (fFitPtr[cpu] << (8 + decPlaces));
1d93b218 1589
4ff7ed2b 1590 temp = slope;
1591 temp = temp * scaleD;
1592 slope = (temp >> 32);
4ff7ed2b 1593 temp = offset;
1594 temp = temp * scaleY;
1595 offset = (temp >> 32);
1596
1597 // rounding, like in the TRAP
1598 slope = (slope + rndAdd) >> decPlaces;
4ff7ed2b 1599 offset = (offset + rndAdd) >> decPlaces;
1600
ce4786b9 1601 AliDebug(5, Form("Det: %3i, ROB: %i, MCM: %2i: deflection: %i, min: %i, max: %i",
1602 fDetector, fRobPos, fMcmPos, slope,
8ea391e3 1603 (Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 2*fFitPtr[cpu], fDetector, fRobPos, fMcmPos),
1604 (Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 1 + 2*fFitPtr[cpu], fDetector, fRobPos, fMcmPos)));
ce4786b9 1605
ce51199c 1606 AliDebug(5, Form("Fit sums: x = %i, X = %i, y = %i, Y = %i, Z = %i",
1607 sumX, sumX2, sumY, sumY2, sumXY));
1608
1609 fitSlope = (Float_t) (nHits * sumXY - sumX * sumY) / (nHits * sumX2 - sumX*sumX);
1610
1611 fitOffset = (Float_t) (sumX2 * sumY - sumX * sumXY) / (nHits * sumX2 - sumX*sumX);
1612
1613 Float_t sx = (Float_t) sumX;
1614 Float_t sx2 = (Float_t) sumX2;
1615 Float_t sy = (Float_t) sumY;
1616 Float_t sy2 = (Float_t) sumY2;
1617 Float_t sxy = (Float_t) sumXY;
1618 fitError = sy2 - (sx2 * sy*sy - 2 * sx * sxy * sy + nHits * sxy*sxy) / (nHits * sx2 - sx*sx);
1619 //fitError = (Float_t) sumY2 - (Float_t) (sumY*sumY) / nHits - fitSlope * ((Float_t) (sumXY - sumX*sumY) / nHits);
1620
40bd6ee4 1621 Bool_t rejected = kFALSE;
ce4786b9 1622 // deflection range table from DMEM
8ea391e3 1623 if ((slope < ((Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 2*fFitPtr[cpu], fDetector, fRobPos, fMcmPos))) ||
1624 (slope > ((Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 1 + 2*fFitPtr[cpu], fDetector, fRobPos, fMcmPos))))
40bd6ee4 1625 rejected = kTRUE;
4ff7ed2b 1626
1627 if (rejected && GetApplyCut())
b0a41e80 1628 {
1629 fMCMT[cpu] = 0x10001000; //??? AliTRDfeeParam::GetTrackletEndmarker();
1630 }
1631 else
1632 {
4ff7ed2b 1633 if (slope > 63 || slope < -64) { // wrapping in TRAP!
27a030ab 1634 AliDebug(1,Form("Overflow in slope: %i, tracklet discarded!", slope));
40bd6ee4 1635 fMCMT[cpu] = 0x10001000;
1636 continue;
1637 }
b0a41e80 1638
4ff7ed2b 1639 slope = slope & 0x7F; // 7 bit
1640
40bd6ee4 1641 if (offset > 0xfff || offset < -0xfff)
b0a41e80 1642 AliWarning("Overflow in offset");
1643 offset = offset & 0x1FFF; // 13 bit
1644
8ea391e3 1645 pid = GetPID(q0 >> fgkAddDigits, q1 >> fgkAddDigits); // divided by 4 because in simulation there are two additional decimal places
4ff7ed2b 1646
8ea391e3 1647 if (pid > 0xff)
1648 AliWarning("Overflow in PID");
1649 pid = pid & 0xFF; // 8 bit, exactly like in the TRAP program
4ff7ed2b 1650
b0a41e80 1651 // assemble and store the tracklet word
8ea391e3 1652 fMCMT[cpu] = (pid << 24) | (padrow << 20) | (slope << 13) | offset;
40bd6ee4 1653
1654 // calculate MC label
25b41f6f 1655 Int_t mcLabel[] = { -1, -1, -1};
4ff7ed2b 1656 Int_t nHits0 = 0;
1657 Int_t nHits1 = 0;
40bd6ee4 1658 if (fDigitsManager) {
25b41f6f 1659 const Int_t maxLabels = 30;
1660 Int_t label[maxLabels] = {0}; // up to 30 different labels possible
1661 Int_t count[maxLabels] = {0};
40bd6ee4 1662 Int_t nLabels = 0;
1663 for (Int_t iHit = 0; iHit < fNHits; iHit++) {
1664 if ((fHits[iHit].fChannel - fFitPtr[cpu] < 0) ||
1665 (fHits[iHit].fChannel - fFitPtr[cpu] > 1))
1666 continue;
4ff7ed2b 1667
1668 // counting contributing hits
1669 if (fHits[iHit].fTimebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS0) &&
1670 fHits[iHit].fTimebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE0))
1671 nHits0++;
1672 if (fHits[iHit].fTimebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS1) &&
1673 fHits[iHit].fTimebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE1))
1674 nHits1++;
1675
25b41f6f 1676 for (Int_t i = 0; i < 3; i++) {
1677 Int_t currLabel = fHits[iHit].fLabel[i];
1678 for (Int_t iLabel = 0; iLabel < nLabels; iLabel++) {
1679 if (currLabel == label[iLabel]) {
1680 count[iLabel]++;
1681 currLabel = -1;
1682 break;
1683 }
1684 }
1685 if (currLabel >= 0 && nLabels < maxLabels) {
1686 label[nLabels] = currLabel;
1687 count[nLabels]++;
1688 nLabels++;
1689 }
1690 }
1691 }
637666cd 1692 Int_t index[2*maxLabels];
25b41f6f 1693 TMath::Sort(maxLabels, count, index);
1694 for (Int_t i = 0; i < 3; i++) {
1695 if (count[index[i]] <= 0)
1696 break;
1697 mcLabel[i] = label[index[i]];
1698 }
40bd6ee4 1699 }
f793c83d 1700 new ((*fTrackletArray)[fTrackletArray->GetEntriesFast()]) AliTRDtrackletMCM((UInt_t) fMCMT[cpu], fDetector*2 + fRobPos%2, fRobPos, fMcmPos);
40bd6ee4 1701 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetLabel(mcLabel);
4ff7ed2b 1702
1703
1704 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetNHits(fit0->fNhits + fit1->fNhits);
1705 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetNHits0(nHits0);
1706 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetNHits1(nHits1);
27a030ab 1707 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetQ0(q0 >> fgkAddDigits);
1708 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetQ1(q1 >> fgkAddDigits);
ce51199c 1709 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetSlope(fitSlope);
1710 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetOffset(fitOffset);
1711 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetError(TMath::Sqrt(TMath::Abs(fitError)/nHits));
1712
1713// // cluster information
1714// Float_t *res = new Float_t[nHits];
1715// Float_t *qtot = new Float_t[nHits];
1716// Int_t nCls = 0;
1717// for (Int_t iHit = 0; iHit < fNHits; iHit++) {
1718// // check if hit contributes
1719// if (fHits[iHit].fChannel == fFitPtr[cpu]) {
1720// res[nCls] = fHits[iHit].fYpos - (fitSlope * fHits[iHit].fTimebin + fitOffset);
1721// qtot[nCls] = fHits[iHit].fQtot;
1722// nCls++;
1723// }
1724// else if (fHits[iHit].fChannel == fFitPtr[cpu] + 1) {
1725// res[nCls] = fHits[iHit].fYpos + 256 - (fitSlope * fHits[iHit].fTimebin + fitOffset);
1726// qtot[nCls] = fHits[iHit].fQtot;
1727// nCls++;
1728// }
1729// }
1730// ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetClusters(res, qtot, nCls);
1731// delete [] res;
1732// delete [] qtot;
1733
1734 if (fitError < 0)
1735 AliError(Form("Strange fit error: %f from Sx: %i, Sy: %i, Sxy: %i, Sx2: %i, Sy2: %i, nHits: %i",
1736 fitError, sumX, sumY, sumXY, sumX2, sumY2, nHits));
1737 AliDebug(3, Form("fit slope: %f, offset: %f, error: %f",
1738 fitSlope, fitOffset, TMath::Sqrt(TMath::Abs(fitError)/nHits)));
b0a41e80 1739 }
1d93b218 1740 }
1d93b218 1741 }
b0a41e80 1742}
1d93b218 1743
b0a41e80 1744void AliTRDmcmSim::Tracklet()
1745{
ab9f7002 1746 // Run the tracklet calculation by calling sequentially:
1747 // CalcFitreg(); TrackletSelection(); FitTracklet()
1748 // and store the tracklets
1749
b0a41e80 1750 if (!fInitialized) {
ab9f7002 1751 AliError("Called uninitialized! Nothing done!");
b0a41e80 1752 return;
1d93b218 1753 }
96e6312d 1754
b0a41e80 1755 fTrackletArray->Delete();
96e6312d 1756
b0a41e80 1757 CalcFitreg();
40bd6ee4 1758 if (fNHits == 0)
1759 return;
b0a41e80 1760 TrackletSelection();
1761 FitTracklet();
c8b1590d 1762}
1763
1764Bool_t AliTRDmcmSim::StoreTracklets()
1765{
36dc3337 1766 // store the found tracklets via the loader
1767
40bd6ee4 1768 if (fTrackletArray->GetEntriesFast() == 0)
c8b1590d 1769 return kTRUE;
1d93b218 1770
b0a41e80 1771 AliRunLoader *rl = AliRunLoader::Instance();
1772 AliDataLoader *dl = 0x0;
1773 if (rl)
1774 dl = rl->GetLoader("TRDLoader")->GetDataLoader("tracklets");
1775 if (!dl) {
1776 AliError("Could not get the tracklets data loader!");
c8b1590d 1777 return kFALSE;
1d93b218 1778 }
1d93b218 1779
c8b1590d 1780 TTree *trackletTree = dl->Tree();
1781 if (!trackletTree) {
1782 dl->MakeTree();
1783 trackletTree = dl->Tree();
1784 }
1785
1786 AliTRDtrackletMCM *trkl = 0x0;
6b094867 1787 TBranch *trkbranch = trackletTree->GetBranch(fTrklBranchName.Data());
c8b1590d 1788 if (!trkbranch)
6b094867 1789 trkbranch = trackletTree->Branch(fTrklBranchName.Data(), "AliTRDtrackletMCM", &trkl, 32000);
c8b1590d 1790
1791 for (Int_t iTracklet = 0; iTracklet < fTrackletArray->GetEntriesFast(); iTracklet++) {
1792 trkl = ((AliTRDtrackletMCM*) (*fTrackletArray)[iTracklet]);
1793 trkbranch->SetAddress(&trkl);
c8b1590d 1794 trkbranch->Fill();
1d93b218 1795 }
c8b1590d 1796
1797 return kTRUE;
b0a41e80 1798}
1d93b218 1799
b0a41e80 1800void AliTRDmcmSim::WriteData(AliTRDarrayADC *digits)
1801{
1802 // write back the processed data configured by EBSF
1803 // EBSF = 1: unfiltered data; EBSF = 0: filtered data
1804 // zero-suppressed valued are written as -1 to digits
1d93b218 1805
ce4786b9 1806 if( !CheckInitialized() )
b0a41e80 1807 return;
1d93b218 1808
ce4786b9 1809 Int_t offset = (fMcmPos % 4 + 1) * 21 + (fRobPos % 2) * 84 - 1;
1d93b218 1810
b0a41e80 1811 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBSF) != 0) // store unfiltered data
1812 {
ce4786b9 1813 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
1814 if (~fZSMap[iAdc] == 0) {
b0a41e80 1815 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
ce4786b9 1816 digits->SetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin, -1);
b0a41e80 1817 }
1d93b218 1818 }
ce51199c 1819 else if (iAdc < 2 || iAdc == 20) {
1820 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
1821 digits->SetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin, (fADCR[iAdc][iTimeBin] >> fgkAddDigits) - fgAddBaseline);
1822 }
1823 }
b0a41e80 1824 }
1d93b218 1825 }
52c19022 1826 else {
ce4786b9 1827 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
1828 if (~fZSMap[iAdc] != 0) {
b0a41e80 1829 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
ce4786b9 1830 digits->SetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin, (fADCF[iAdc][iTimeBin] >> fgkAddDigits) - fgAddBaseline);
b0a41e80 1831 }
1832 }
1833 else {
1834 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
ce4786b9 1835 digits->SetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin, -1);
b0a41e80 1836 }
1837 }
52c19022 1838 }
52c19022 1839 }
b0a41e80 1840}
23200400 1841
8ea391e3 1842
1843// ******************************
1844// PID section
1845//
1846// Memory area for the LUT: 0xC100 to 0xC3FF
1847//
1848// The addresses for the parameters (the order is optimized for maximum calculation speed in the MCMs):
1849// 0xC028: cor1
1850// 0xC029: nBins(sF)
1851// 0xC02A: cor0
1852// 0xC02B: TableLength
1853// Defined in AliTRDtrapConfig.h
1854//
1855// The algorithm implemented in the TRAP program of the MCMs (Venelin Angelov)
1856// 1) set the read pointer to the beginning of the Parameters in DMEM
1857// 2) shift right the FitReg with the Q0 + (Q1 << 16) to get Q1
1858// 3) read cor1 with rpointer++
1859// 4) start cor1*Q1
1860// 5) read nBins with rpointer++
1861// 6) start nBins*cor1*Q1
1862// 7) read cor0 with rpointer++
1863// 8) swap hi-low parts in FitReg, now is Q1 + (Q0 << 16)
1864// 9) shift right to get Q0
1865// 10) start cor0*Q0
1866// 11) read TableLength
1867// 12) compare cor0*Q0 with nBins
1868// 13) if >=, clip cor0*Q0 to nBins-1
1869// 14) add cor0*Q0 to nBins*cor1*Q1
1870// 15) compare the result with TableLength
1871// 16) if >=, clip to TableLength-1
1872// 17) read from the LUT 8 bits
1873
1874
1875Int_t AliTRDmcmSim::GetPID(Int_t q0, Int_t q1)
1876{
6b094867 1877 // return PID calculated from charges accumulated in two time windows
1878
8ea391e3 1879 ULong64_t addrQ0;
1880 ULong64_t addr;
1881
1882 UInt_t nBinsQ0 = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTnbins); // number of bins in q0 / 4 !!
1883 UInt_t pidTotalSize = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTLength);
27a030ab 1884 if(nBinsQ0==0 || pidTotalSize==0) // make sure we don't run into trouble if the value for Q0 is not configured
1885 return 0; // Q1 not configured is ok for 1D LUT
8ea391e3 1886
1887 ULong_t corrQ0 = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTcor0, fDetector, fRobPos, fMcmPos);
1888 ULong_t corrQ1 = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTcor1, fDetector, fRobPos, fMcmPos);
27a030ab 1889 if(corrQ0==0) // make sure we don't run into trouble if one of the values is not configured
5ac2e3b1 1890 return 0;
8ea391e3 1891
1892 addrQ0 = corrQ0;
1893 addrQ0 = (((addrQ0*q0)>>16)>>16); // because addrQ0 = (q0 * corrQ0) >> 32; does not work for unknown reasons
8ea391e3 1894
1895 if(addrQ0 >= nBinsQ0) { // check for overflow
e3bd81f7 1896 AliDebug(5,Form("Overflow in q0: %llu/4 is bigger then %u", addrQ0, nBinsQ0));
8ea391e3 1897 addrQ0 = nBinsQ0 -1;
1898 }
1899
1900 addr = corrQ1;
1901 addr = (((addr*q1)>>16)>>16);
1902 addr = addrQ0 + nBinsQ0*addr; // because addr = addrQ0 + nBinsQ0* (((corrQ1*q1)>>32); does not work
8ea391e3 1903
1904 if(addr >= pidTotalSize) {
e3bd81f7 1905 AliDebug(5,Form("Overflow in q1. Address %llu/4 is bigger then %u", addr, pidTotalSize));
8ea391e3 1906 addr = pidTotalSize -1;
1907 }
1908
1909 // For a LUT with 11 input and 8 output bits, the first memory address is set to LUT[0] | (LUT[1] << 8) | (LUT[2] << 16) | (LUT[3] << 24)
1910 // and so on
1911 UInt_t result = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTStart+(addr/4));
1912 return (result>>((addr%4)*8)) & 0xFF;
1913}
1914
1915
1916
b0a41e80 1917// help functions, to be cleaned up
1d93b218 1918
ab9f7002 1919UInt_t AliTRDmcmSim::AddUintClipping(UInt_t a, UInt_t b, UInt_t nbits) const
b0a41e80 1920{
1921 //
1922 // This function adds a and b (unsigned) and clips to
1923 // the specified number of bits.
1924 //
1925
1926 UInt_t sum = a + b;
1927 if (nbits < 32)
1928 {
1929 UInt_t maxv = (1 << nbits) - 1;;
1930 if (sum > maxv)
1931 sum = maxv;
1932 }
1933 else
1934 {
1935 if ((sum < a) || (sum < b))
1936 sum = 0xFFFFFFFF;
1937 }
1938 return sum;
1d93b218 1939}
b0a41e80 1940
982869bc 1941void AliTRDmcmSim::Sort2(UShort_t idx1i, UShort_t idx2i, \
1942 UShort_t val1i, UShort_t val2i, \
6b094867 1943 UShort_t * const idx1o, UShort_t * const idx2o, \
1944 UShort_t * const val1o, UShort_t * const val2o) const
b65e5048 1945{
ab9f7002 1946 // sorting for tracklet selection
b65e5048 1947
b0a41e80 1948 if (val1i > val2i)
b65e5048 1949 {
b0a41e80 1950 *idx1o = idx1i;
1951 *idx2o = idx2i;
1952 *val1o = val1i;
1953 *val2o = val2i;
b65e5048 1954 }
b0a41e80 1955 else
b65e5048 1956 {
b0a41e80 1957 *idx1o = idx2i;
1958 *idx2o = idx1i;
1959 *val1o = val2i;
1960 *val2o = val1i;
b65e5048 1961 }
1962}
b65e5048 1963
982869bc 1964void AliTRDmcmSim::Sort3(UShort_t idx1i, UShort_t idx2i, UShort_t idx3i, \
1965 UShort_t val1i, UShort_t val2i, UShort_t val3i, \
6b094867 1966 UShort_t * const idx1o, UShort_t * const idx2o, UShort_t * const idx3o, \
1967 UShort_t * const val1o, UShort_t * const val2o, UShort_t * const val3o)
b65e5048 1968{
ab9f7002 1969 // sorting for tracklet selection
1970
4ff7ed2b 1971 Int_t sel;
b65e5048 1972
b65e5048 1973
b0a41e80 1974 if (val1i > val2i) sel=4; else sel=0;
1975 if (val2i > val3i) sel=sel + 2;
1976 if (val3i > val1i) sel=sel + 1;
b0a41e80 1977 switch(sel)
b65e5048 1978 {
b0a41e80 1979 case 6 : // 1 > 2 > 3 => 1 2 3
1980 case 0 : // 1 = 2 = 3 => 1 2 3 : in this case doesn't matter, but so is in hardware!
1981 *idx1o = idx1i;
1982 *idx2o = idx2i;
1983 *idx3o = idx3i;
1984 *val1o = val1i;
1985 *val2o = val2i;
1986 *val3o = val3i;
1987 break;
1988
1989 case 4 : // 1 > 2, 2 <= 3, 3 <= 1 => 1 3 2
1990 *idx1o = idx1i;
1991 *idx2o = idx3i;
1992 *idx3o = idx2i;
1993 *val1o = val1i;
1994 *val2o = val3i;
1995 *val3o = val2i;
1996 break;
1997
1998 case 2 : // 1 <= 2, 2 > 3, 3 <= 1 => 2 1 3
1999 *idx1o = idx2i;
2000 *idx2o = idx1i;
2001 *idx3o = idx3i;
2002 *val1o = val2i;
2003 *val2o = val1i;
2004 *val3o = val3i;
2005 break;
2006
2007 case 3 : // 1 <= 2, 2 > 3, 3 > 1 => 2 3 1
2008 *idx1o = idx2i;
2009 *idx2o = idx3i;
2010 *idx3o = idx1i;
2011 *val1o = val2i;
2012 *val2o = val3i;
2013 *val3o = val1i;
2014 break;
2015
2016 case 1 : // 1 <= 2, 2 <= 3, 3 > 1 => 3 2 1
2017 *idx1o = idx3i;
2018 *idx2o = idx2i;
2019 *idx3o = idx1i;
2020 *val1o = val3i;
2021 *val2o = val2i;
2022 *val3o = val1i;
2023 break;
2024
2025 case 5 : // 1 > 2, 2 <= 3, 3 > 1 => 3 1 2
2026 *idx1o = idx3i;
2027 *idx2o = idx1i;
2028 *idx3o = idx2i;
2029 *val1o = val3i;
2030 *val2o = val1i;
2031 *val3o = val2i;
2032 break;
2033
2034 default: // the rest should NEVER happen!
40bd6ee4 2035 AliError("ERROR in Sort3!!!\n");
b0a41e80 2036 break;
2037 }
b65e5048 2038}
b0a41e80 2039
982869bc 2040void AliTRDmcmSim::Sort6To4(UShort_t idx1i, UShort_t idx2i, UShort_t idx3i, UShort_t idx4i, UShort_t idx5i, UShort_t idx6i, \
2041 UShort_t val1i, UShort_t val2i, UShort_t val3i, UShort_t val4i, UShort_t val5i, UShort_t val6i, \
6b094867 2042 UShort_t * const idx1o, UShort_t * const idx2o, UShort_t * const idx3o, UShort_t * const idx4o, \
2043 UShort_t * const val1o, UShort_t * const val2o, UShort_t * const val3o, UShort_t * const val4o)
b65e5048 2044{
ab9f7002 2045 // sorting for tracklet selection
b65e5048 2046
982869bc 2047 UShort_t idx21s, idx22s, idx23s, dummy;
2048 UShort_t val21s, val22s, val23s;
2049 UShort_t idx23as, idx23bs;
2050 UShort_t val23as, val23bs;
b0a41e80 2051
2052 Sort3(idx1i, idx2i, idx3i, val1i, val2i, val3i,
2053 idx1o, &idx21s, &idx23as,
2054 val1o, &val21s, &val23as);
2055
2056 Sort3(idx4i, idx5i, idx6i, val4i, val5i, val6i,
2057 idx2o, &idx22s, &idx23bs,
2058 val2o, &val22s, &val23bs);
2059
2060 Sort2(idx23as, idx23bs, val23as, val23bs, &idx23s, &dummy, &val23s, &dummy);
2061
2062 Sort3(idx21s, idx22s, idx23s, val21s, val22s, val23s,
2063 idx3o, idx4o, &dummy,
2064 val3o, val4o, &dummy);
2065
b65e5048 2066}
b0a41e80 2067
982869bc 2068void AliTRDmcmSim::Sort6To2Worst(UShort_t idx1i, UShort_t idx2i, UShort_t idx3i, UShort_t idx4i, UShort_t idx5i, UShort_t idx6i, \
2069 UShort_t val1i, UShort_t val2i, UShort_t val3i, UShort_t val4i, UShort_t val5i, UShort_t val6i, \
6b094867 2070 UShort_t * const idx5o, UShort_t * const idx6o)
0d64b05f 2071{
ab9f7002 2072 // sorting for tracklet selection
0d64b05f 2073
982869bc 2074 UShort_t idx21s, idx22s, idx23s, dummy1, dummy2, dummy3, dummy4, dummy5;
2075 UShort_t val21s, val22s, val23s;
2076 UShort_t idx23as, idx23bs;
2077 UShort_t val23as, val23bs;
b0a41e80 2078
2079 Sort3(idx1i, idx2i, idx3i, val1i, val2i, val3i,
2080 &dummy1, &idx21s, &idx23as,
2081 &dummy2, &val21s, &val23as);
2082
2083 Sort3(idx4i, idx5i, idx6i, val4i, val5i, val6i,
2084 &dummy1, &idx22s, &idx23bs,
2085 &dummy2, &val22s, &val23bs);
2086
2087 Sort2(idx23as, idx23bs, val23as, val23bs, &idx23s, idx5o, &val23s, &dummy1);
b65e5048 2088
b0a41e80 2089 Sort3(idx21s, idx22s, idx23s, val21s, val22s, val23s,
2090 &dummy1, &dummy2, idx6o,
2091 &dummy3, &dummy4, &dummy5);
0d64b05f 2092}
f793c83d 2093
2094
ce4786b9 2095// ----- I/O implementation -----
2096
59f78ad5 2097ostream& AliTRDmcmSim::Text(ostream& os)
ce4786b9 2098{
2099 // manipulator to activate output in text format (default)
2100
2101 os.iword(fgkFormatIndex) = 0;
2102 return os;
2103}
2104
59f78ad5 2105ostream& AliTRDmcmSim::Cfdat(ostream& os)
ce4786b9 2106{
2107 // manipulator to activate output in CFDAT format
2108 // to send to the FEE via SCSN
2109
2110 os.iword(fgkFormatIndex) = 1;
2111 return os;
2112}
2113
59f78ad5 2114ostream& AliTRDmcmSim::Raw(ostream& os)
ce4786b9 2115{
2116 // manipulator to activate output as raw data dump
2117
2118 os.iword(fgkFormatIndex) = 2;
2119 return os;
2120}
2121
2122ostream& operator<<(ostream& os, const AliTRDmcmSim& mcm)
2123{
2124 // output implementation
2125
2126 // no output for non-initialized MCM
2127 if (!mcm.CheckInitialized())
2128 return os;
2129
2130 // ----- human-readable output -----
2131 if (os.iword(AliTRDmcmSim::fgkFormatIndex) == 0) {
2132
2133 os << "MCM " << mcm.fMcmPos << " on ROB " << mcm.fRobPos <<
2134 " in detector " << mcm.fDetector << std::endl;
2135
2136 os << "----- Unfiltered ADC data (10 bit) -----" << std::endl;
2137 os << "ch ";
2138 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++)
2139 os << std::setw(5) << iChannel;
2140 os << std::endl;
2141 for (Int_t iTimeBin = 0; iTimeBin < mcm.fNTimeBin; iTimeBin++) {
2142 os << "tb " << std::setw(2) << iTimeBin << ":";
2143 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++) {
2144 os << std::setw(5) << (mcm.fADCR[iChannel][iTimeBin] >> mcm.fgkAddDigits);
2145 }
2146 os << std::endl;
2147 }
2148
2149 os << "----- Filtered ADC data (10+2 bit) -----" << std::endl;
2150 os << "ch ";
2151 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++)
2152 os << std::setw(4) << iChannel
2153 << ((~mcm.fZSMap[iChannel] != 0) ? "!" : " ");
2154 os << std::endl;
2155 for (Int_t iTimeBin = 0; iTimeBin < mcm.fNTimeBin; iTimeBin++) {
2156 os << "tb " << std::setw(2) << iTimeBin << ":";
2157 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++) {
2158 os << std::setw(4) << (mcm.fADCF[iChannel][iTimeBin])
2159 << (((mcm.fZSMap[iChannel] & (1 << iTimeBin)) == 0) ? "!" : " ");
2160 }
2161 os << std::endl;
2162 }
2163 }
2164
2165 // ----- CFDAT output -----
2166 else if(os.iword(AliTRDmcmSim::fgkFormatIndex) == 1) {
2167 Int_t dest = 127;
2168 Int_t addrOffset = 0x2000;
2169 Int_t addrStep = 0x80;
2170
2171 for (Int_t iTimeBin = 0; iTimeBin < mcm.fNTimeBin; iTimeBin++) {
2172 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++) {
2173 os << std::setw(5) << 10
2174 << std::setw(5) << addrOffset + iChannel * addrStep + iTimeBin
2175 << std::setw(5) << (mcm.fADCF[iChannel][iTimeBin])
2176 << std::setw(5) << dest << std::endl;
2177 }
2178 os << std::endl;
2179 }
2180 }
2181
2182 // ----- raw data ouptut -----
2183 else if (os.iword(AliTRDmcmSim::fgkFormatIndex) == 2) {
2184 Int_t bufSize = 300;
2185 UInt_t *buf = new UInt_t[bufSize];
2186
2187 Int_t bufLength = mcm.ProduceRawStream(&buf[0], bufSize);
2188
2189 for (Int_t i = 0; i < bufLength; i++)
78c94f0b 2190 std::cout << "0x" << std::hex << buf[i] << std::dec << std::endl;
ce4786b9 2191
2192 delete [] buf;
2193 }
2194
2195 else {
2196 os << "unknown format set" << std::endl;
2197 }
2198
2199 return os;
2200}
8ea391e3 2201
2202
2203void AliTRDmcmSim::PrintFitRegXml(ostream& os) const
2204{
6b094867 2205 // print fit registres in XML format
2206
8ea391e3 2207 bool tracklet=false;
2208
2209 for (Int_t cpu = 0; cpu < 4; cpu++) {
2210 if(fFitPtr[cpu] != 31)
2211 tracklet=true;
2212 }
2213
2214 if(tracklet==true) {
2215 os << "<nginject>" << std::endl;
2216 os << "<ack roc=\""<< fDetector << "\" cmndid=\"0\">" << std::endl;
2217 os << "<dmem-readout>" << std::endl;
2218 os << "<d det=\"" << fDetector << "\">" << std::endl;
2219 os << " <ro-board rob=\"" << fRobPos << "\">" << std::endl;
2220 os << " <m mcm=\"" << fMcmPos << "\">" << std::endl;
2221
2222 for(int cpu=0; cpu<4; cpu++) {
2223 os << " <c cpu=\"" << cpu << "\">" << std::endl;
2224 if(fFitPtr[cpu] != 31) {
2225 for(int adcch=fFitPtr[cpu]; adcch<fFitPtr[cpu]+2; adcch++) {
5ac2e3b1 2226 os << " <ch chnr=\"" << adcch << "\">"<< std::endl;
8ea391e3 2227 os << " <hits>" << fFitReg[adcch].fNhits << "</hits>"<< std::endl;
2228 os << " <q0>" << fFitReg[adcch].fQ0/4 << "</q0>"<< std::endl; // divided by 4 because in simulation we have 2 additional decimal places
2229 os << " <q1>" << fFitReg[adcch].fQ1/4 << "</q1>"<< std::endl; // in the output
2230 os << " <sumx>" << fFitReg[adcch].fSumX << "</sumx>"<< std::endl;
2231 os << " <sumxsq>" << fFitReg[adcch].fSumX2 << "</sumxsq>"<< std::endl;
2232 os << " <sumy>" << fFitReg[adcch].fSumY << "</sumy>"<< std::endl;
2233 os << " <sumysq>" << fFitReg[adcch].fSumY2 << "</sumysq>"<< std::endl;
2234 os << " <sumxy>" << fFitReg[adcch].fSumXY << "</sumxy>"<< std::endl;
2235 os << " </ch>" << std::endl;
2236 }
2237 }
2238 os << " </c>" << std::endl;
2239 }
2240 os << " </m>" << std::endl;
2241 os << " </ro-board>" << std::endl;
2242 os << "</d>" << std::endl;
2243 os << "</dmem-readout>" << std::endl;
2244 os << "</ack>" << std::endl;
2245 os << "</nginject>" << std::endl;
2246 }
2247}
2248
2249
2250void AliTRDmcmSim::PrintTrackletsXml(ostream& os) const
2251{
6b094867 2252 // print tracklets in XML format
2253
8ea391e3 2254 os << "<nginject>" << std::endl;
2255 os << "<ack roc=\""<< fDetector << "\" cmndid=\"0\">" << std::endl;
2256 os << "<dmem-readout>" << std::endl;
2257 os << "<d det=\"" << fDetector << "\">" << std::endl;
2258 os << " <ro-board rob=\"" << fRobPos << "\">" << std::endl;
2259 os << " <m mcm=\"" << fMcmPos << "\">" << std::endl;
2260
2261 Int_t pid, padrow, slope, offset;
2262 for(Int_t cpu=0; cpu<4; cpu++) {
2263 if(fMCMT[cpu] == 0x10001000) {
2264 pid=-1;
2265 padrow=-1;
2266 slope=-1;
2267 offset=-1;
2268 }
2269 else {
2270 pid = (fMCMT[cpu] & 0xFF000000) >> 24;
2271 padrow = (fMCMT[cpu] & 0xF00000 ) >> 20;
2272 slope = (fMCMT[cpu] & 0xFE000 ) >> 13;
2273 offset = (fMCMT[cpu] & 0x1FFF ) ;
2274
2275 }
2276 os << " <trk> <pid>" << pid << "</pid>" << " <padrow>" << padrow << "</padrow>"
5ac2e3b1 2277 << " <slope>" << slope << "</slope>" << " <offset>" << offset << "</offset>" << "</trk>" << std::endl;
8ea391e3 2278 }
2279
2280 os << " </m>" << std::endl;
2281 os << " </ro-board>" << std::endl;
2282 os << "</d>" << std::endl;
2283 os << "</dmem-readout>" << std::endl;
2284 os << "</ack>" << std::endl;
2285 os << "</nginject>" << std::endl;
2286}
2287
2288
2289void AliTRDmcmSim::PrintAdcDatHuman(ostream& os) const
2290{
6b094867 2291 // print ADC data in human-readable format
2292
8ea391e3 2293 os << "MCM " << fMcmPos << " on ROB " << fRobPos <<
2294 " in detector " << fDetector << std::endl;
2295
2296 os << "----- Unfiltered ADC data (10 bit) -----" << std::endl;
2297 os << "ch ";
2298 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++)
2299 os << std::setw(5) << iChannel;
2300 os << std::endl;
2301 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
2302 os << "tb " << std::setw(2) << iTimeBin << ":";
2303 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++) {
2304 os << std::setw(5) << (fADCR[iChannel][iTimeBin] >> fgkAddDigits);
2305 }
2306 os << std::endl;
2307 }
2308
2309 os << "----- Filtered ADC data (10+2 bit) -----" << std::endl;
2310 os << "ch ";
2311 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++)
2312 os << std::setw(4) << iChannel
2313 << ((~fZSMap[iChannel] != 0) ? "!" : " ");
2314 os << std::endl;
2315 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
2316 os << "tb " << std::setw(2) << iTimeBin << ":";
2317 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++) {
2318 os << std::setw(4) << (fADCF[iChannel][iTimeBin])
2319 << (((fZSMap[iChannel] & (1 << iTimeBin)) == 0) ? "!" : " ");
2320 }
2321 os << std::endl;
2322 }
2323}
2324
2325
2326void AliTRDmcmSim::PrintAdcDatXml(ostream& os) const
2327{
6b094867 2328 // print ADC data in XML format
2329
8ea391e3 2330 os << "<nginject>" << std::endl;
2331 os << "<ack roc=\""<< fDetector << "\" cmndid=\"0\">" << std::endl;
2332 os << "<dmem-readout>" << std::endl;
2333 os << "<d det=\"" << fDetector << "\">" << std::endl;
2334 os << " <ro-board rob=\"" << fRobPos << "\">" << std::endl;
2335 os << " <m mcm=\"" << fMcmPos << "\">" << std::endl;
2336
2337 for(Int_t iChannel = 0; iChannel < fgkNADC; iChannel++) {
2338 os << " <ch chnr=\"" << iChannel << "\">" << std::endl;
2339 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
2340 os << "<tb>" << fADCF[iChannel][iTimeBin]/4 << "</tb>";
2341 }
2342 os << " </ch>" << std::endl;
2343 }
2344
2345 os << " </m>" << std::endl;
2346 os << " </ro-board>" << std::endl;
2347 os << "</d>" << std::endl;
2348 os << "</dmem-readout>" << std::endl;
2349 os << "</ack>" << std::endl;
2350 os << "</nginject>" << std::endl;
2351}
2352
2353
2354
2355void AliTRDmcmSim::PrintAdcDatDatx(ostream& os, Bool_t broadcast) const
2356{
6b094867 2357 // print ADC data in datx format (to send to FEE)
2358
8ea391e3 2359 fTrapConfig->PrintDatx(os, 2602, 1, 0, 127); // command to enable the ADC clock - necessary to write ADC values to MCM
2360 os << std::endl;
2361
2362 Int_t addrOffset = 0x2000;
2363 Int_t addrStep = 0x80;
2364 Int_t addrOffsetEBSIA = 0x20;
2365
2366 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
2367 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++) {
2368 if(broadcast==kFALSE)
2369 fTrapConfig->PrintDatx(os, addrOffset+iChannel*addrStep+addrOffsetEBSIA+iTimeBin, (fADCF[iChannel][iTimeBin]/4), GetRobPos(), GetMcmPos());
2370 else
2371 fTrapConfig->PrintDatx(os, addrOffset+iChannel*addrStep+addrOffsetEBSIA+iTimeBin, (fADCF[iChannel][iTimeBin]/4), 0, 127);
2372 }
2373 os << std::endl;
2374 }
2375}
2376
2377
2378void AliTRDmcmSim::PrintPidLutHuman()
2379{
6b094867 2380 // print PID LUT in human readable format
2381
8ea391e3 2382 UInt_t result;
2383
2384 UInt_t addrEnd = AliTRDtrapConfig::fgkDmemAddrLUTStart + fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTLength)/4; // /4 because each addr contains 4 values
2385 UInt_t nBinsQ0 = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTnbins);
2386
2387 std::cout << "nBinsQ0: " << nBinsQ0 << std::endl;
5ac2e3b1 2388 std::cout << "LUT table length: " << fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTLength) << std::endl;
8ea391e3 2389
2390 for(UInt_t addr=AliTRDtrapConfig::fgkDmemAddrLUTStart; addr< addrEnd; addr++) {
2391 result = fTrapConfig->GetDmemUnsigned(addr);
5ac2e3b1 2392 std::cout << addr << " # x: " << ((addr-AliTRDtrapConfig::fgkDmemAddrLUTStart)%((nBinsQ0)/4))*4 << ", y: " <<(addr-AliTRDtrapConfig::fgkDmemAddrLUTStart)/(nBinsQ0/4)
2393 << " # " <<((result>>0)&0xFF)
2394 << " | " << ((result>>8)&0xFF)
2395 << " | " << ((result>>16)&0xFF)
2396 << " | " << ((result>>24)&0xFF) << std::endl;
8ea391e3 2397 }
2398}