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