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