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