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