1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // TRD MCM (Multi Chip Module) simulator //
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. //
25 ///////////////////////////////////////////////////////////////////////////////
36 #include "TClonesArray.h"
40 #include "AliRunLoader.h"
41 #include "AliLoader.h"
43 #include "AliTRDfeeParam.h"
44 #include "AliTRDtrapConfig.h"
45 #include "AliTRDdigitsManager.h"
46 #include "AliTRDarrayADC.h"
47 #include "AliTRDarrayDictionary.h"
48 #include "AliTRDtrackletMCM.h"
49 #include "AliTRDmcmSim.h"
51 ClassImp(AliTRDmcmSim)
53 Bool_t AliTRDmcmSim::fgApplyCut = kTRUE;
54 Int_t AliTRDmcmSim::fgAddBaseline = 0;
56 const Int_t AliTRDmcmSim::fgkFormatIndex = std::ios_base::xalloc();
58 const Int_t AliTRDmcmSim::fgkNADC = AliTRDfeeParam::GetNadcMcm();
59 const UShort_t AliTRDmcmSim::fgkFPshifts[4] = {11, 14, 17, 21};
62 AliTRDmcmSim::AliTRDmcmSim() :
75 fTrklBranchName("mcmtrklbranch"),
88 // AliTRDmcmSim default constructor
89 // By default, nothing is initialized.
90 // It is necessary to issue Init before use.
93 AliTRDmcmSim::~AliTRDmcmSim()
96 // AliTRDmcmSim destructor
100 for( Int_t iAdc = 0 ; iAdc < fgkNADC; iAdc++ ) {
101 delete [] fADCR[iAdc];
102 delete [] fADCF[iAdc];
110 delete [] fGainCounterA;
111 delete [] fGainCounterB;
112 delete [] fTailAmplLong;
113 delete [] fTailAmplShort;
116 fTrackletArray->Delete();
117 delete fTrackletArray;
121 void AliTRDmcmSim::Init( Int_t det, Int_t robPos, Int_t mcmPos, Bool_t /* newEvent */ )
124 // Initialize the class with new MCM position information
125 // memory is allocated in the first initialization
129 fFeeParam = AliTRDfeeParam::Instance();
130 fTrapConfig = AliTRDtrapConfig::Instance();
136 fNTimeBin = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kC13CPUA);
137 fRow = fFeeParam->GetPadRowFromMCM( fRobPos, fMcmPos );
140 fADCR = new Int_t *[fgkNADC];
141 fADCF = new Int_t *[fgkNADC];
142 fZSMap = new Int_t [fgkNADC];
143 fGainCounterA = new UInt_t[fgkNADC];
144 fGainCounterB = new UInt_t[fgkNADC];
145 for( Int_t iAdc = 0 ; iAdc < fgkNADC; iAdc++ ) {
146 fADCR[iAdc] = new Int_t[fNTimeBin];
147 fADCF[iAdc] = new Int_t[fNTimeBin];
151 fPedAcc = new UInt_t[fgkNADC]; // accumulator for pedestal filter
152 fTailAmplLong = new UShort_t[fgkNADC];
153 fTailAmplShort = new UShort_t[fgkNADC];
155 // tracklet calculation
156 fFitReg = new FitReg_t[fgkNADC];
157 fTrackletArray = new TClonesArray("AliTRDtrackletMCM", fgkMaxTracklets);
159 fMCMT = new UInt_t[fgkMaxTracklets];
162 fInitialized = kTRUE;
167 void AliTRDmcmSim::Reset()
169 // Resets the data values and internal filter registers
170 // by re-initialising them
172 if( !CheckInitialized() )
175 for( Int_t iAdc = 0 ; iAdc < fgkNADC; iAdc++ ) {
176 for( Int_t it = 0 ; it < fNTimeBin ; it++ ) {
180 fZSMap[iAdc] = -1; // Default unread, low active bit mask
181 fGainCounterA[iAdc] = 0;
182 fGainCounterB[iAdc] = 0;
185 for(Int_t i = 0; i < fgkMaxTracklets; i++) {
189 for (Int_t iDict = 0; iDict < 3; iDict++)
192 FilterPedestalInit();
197 void AliTRDmcmSim::SetNTimebins(Int_t ntimebins)
199 // Reallocate memory if a change in the number of timebins
200 // is needed (should not be the case for real data)
202 if( !CheckInitialized() )
205 fNTimeBin = ntimebins;
206 for( Int_t iAdc = 0 ; iAdc < fgkNADC; iAdc++ ) {
209 fADCR[iAdc] = new Int_t[fNTimeBin];
210 fADCF[iAdc] = new Int_t[fNTimeBin];
214 Bool_t AliTRDmcmSim::LoadMCM(AliRunLoader* const runloader, Int_t det, Int_t rob, Int_t mcm)
216 // loads the ADC data as obtained from the digitsManager for the specified MCM.
217 // This method is meant for rare execution, e.g. in the visualization. When called
218 // frequently use SetData(...) instead.
223 AliError("No Runloader given");
227 AliLoader *trdLoader = runloader->GetLoader("TRDLoader");
229 AliError("Could not get TRDLoader");
233 Bool_t retval = kTRUE;
234 trdLoader->LoadDigits();
235 fDigitsManager = 0x0;
236 AliTRDdigitsManager *digMgr = new AliTRDdigitsManager();
237 digMgr->SetSDigits(0);
238 digMgr->CreateArrays();
239 digMgr->ReadDigits(trdLoader->TreeD());
240 AliTRDarrayADC *digits = (AliTRDarrayADC*) digMgr->GetDigits(det);
241 if (digits->HasData()) {
244 if (fNTimeBin != digits->GetNtime()) {
245 AliWarning(Form("Changing no. of timebins from %i to %i", fNTimeBin, digits->GetNtime()));
246 SetNTimebins(digits->GetNtime());
259 void AliTRDmcmSim::NoiseTest(Int_t nsamples, Int_t mean, Int_t sigma, Int_t inputGain, Int_t inputTail)
261 // This function can be used to test the filters.
262 // It feeds nsamples of ADC values with a gaussian distribution specified by mean and sigma.
263 // The filter chain implemented here consists of:
264 // Pedestal -> Gain -> Tail
265 // With inputGain and inputTail the input to the gain and tail filter, respectively,
266 // can be chosen where
268 // 1: pedestal output
270 // The input has to be chosen from a stage before.
271 // The filter behaviour is controlled by the TRAP parameters from AliTRDtrapConfig in the
272 // same way as in normal simulation.
273 // The functions produces four histograms with the values at the different stages.
275 if( !CheckInitialized() )
278 TString nameInputGain;
279 TString nameInputTail;
283 nameInputGain = "Noise";
287 nameInputGain = "Pedestal";
291 AliError("Undefined input to tail cancellation filter");
297 nameInputTail = "Noise";
301 nameInputTail = "Pedestal";
305 nameInputTail = "Gain";
309 AliError("Undefined input to tail cancellation filter");
313 TH1F *h = new TH1F("noise", "Gaussian Noise;sample;ADC count",
314 nsamples, 0, nsamples);
315 TH1F *hfp = new TH1F("ped", "Noise #rightarrow Pedestal filter;sample;ADC count", nsamples, 0, nsamples);
316 TH1F *hfg = new TH1F("gain",
317 (nameInputGain + "#rightarrow Gain;sample;ADC count").Data(),
318 nsamples, 0, nsamples);
319 TH1F *hft = new TH1F("tail",
320 (nameInputTail + "#rightarrow Tail;sample;ADC count").Data(),
321 nsamples, 0, nsamples);
323 hfp->SetStats(kFALSE);
324 hfg->SetStats(kFALSE);
325 hft->SetStats(kFALSE);
327 Int_t value; // ADC count with noise (10 bit)
328 Int_t valuep; // pedestal filter output (12 bit)
329 Int_t valueg; // gain filter output (12 bit)
330 Int_t valuet; // tail filter value (12 bit)
332 for (Int_t i = 0; i < nsamples; i++) {
333 value = (Int_t) gRandom->Gaus(mean, sigma); // generate noise with gaussian distribution
334 h->SetBinContent(i, value);
336 valuep = FilterPedestalNextSample(1, 0, ((Int_t) value) << 2);
339 valueg = FilterGainNextSample(1, ((Int_t) value) << 2);
341 valueg = FilterGainNextSample(1, valuep);
344 valuet = FilterTailNextSample(1, ((Int_t) value) << 2);
345 else if (inputTail == 1)
346 valuet = FilterTailNextSample(1, valuep);
348 valuet = FilterTailNextSample(1, valueg);
350 hfp->SetBinContent(i, valuep >> 2);
351 hfg->SetBinContent(i, valueg >> 2);
352 hft->SetBinContent(i, valuet >> 2);
355 TCanvas *c = new TCanvas;
367 Bool_t AliTRDmcmSim::CheckInitialized() const
370 // Check whether object is initialized
374 AliError(Form ("AliTRDmcmSim is not initialized but function other than Init() is called."));
379 void AliTRDmcmSim::Print(Option_t* const option) const
381 // Prints the data stored and/or calculated for this MCM.
382 // The output is controlled by option which can be a sequence of any of
383 // the following characters:
384 // R - prints raw ADC data
385 // F - prints filtered data
386 // H - prints detected hits
387 // T - prints found tracklets
388 // The later stages are only meaningful after the corresponding calculations
389 // have been performed.
391 if ( !CheckInitialized() )
394 printf("MCM %i on ROB %i in detector %i\n", fMcmPos, fRobPos, fDetector);
396 TString opt = option;
397 if (opt.Contains("R") || opt.Contains("F")) {
401 if (opt.Contains("H")) {
402 printf("Found %i hits:\n", fNHits);
403 for (Int_t iHit = 0; iHit < fNHits; iHit++) {
404 printf("Hit %3i in timebin %2i, ADC %2i has charge %3i and position %3i\n",
405 iHit, fHits[iHit].fTimebin, fHits[iHit].fChannel, fHits[iHit].fQtot, fHits[iHit].fYpos);
409 if (opt.Contains("T")) {
410 printf("Tracklets:\n");
411 for (Int_t iTrkl = 0; iTrkl < fTrackletArray->GetEntriesFast(); iTrkl++) {
412 printf("tracklet %i: 0x%08x\n", iTrkl, ((AliTRDtrackletMCM*) (*fTrackletArray)[iTrkl])->GetTrackletWord());
417 void AliTRDmcmSim::Draw(Option_t* const option)
419 // Plots the data stored in a 2-dim. timebin vs. ADC channel plot.
420 // The option selects what data is plotted and can be a sequence of
421 // the following characters:
422 // R - plot raw data (default)
423 // F - plot filtered data (meaningless if R is specified)
424 // In addition to the ADC values:
426 // T - plot tracklets
428 if( !CheckInitialized() )
431 TString opt = option;
433 TH2F *hist = new TH2F("mcmdata", Form("Data of MCM %i on ROB %i in detector %i", \
434 fMcmPos, fRobPos, fDetector), \
435 fgkNADC, -0.5, fgkNADC-.5, fNTimeBin, -.5, fNTimeBin-.5);
436 hist->GetXaxis()->SetTitle("ADC Channel");
437 hist->GetYaxis()->SetTitle("Timebin");
438 hist->SetStats(kFALSE);
440 if (opt.Contains("R")) {
441 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
442 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
443 hist->SetBinContent(iAdc+1, iTimeBin+1, fADCR[iAdc][iTimeBin] >> fgkAddDigits);
448 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
449 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
450 hist->SetBinContent(iAdc+1, iTimeBin+1, fADCF[iAdc][iTimeBin] >> fgkAddDigits);
456 if (opt.Contains("H")) {
457 TGraph *grHits = new TGraph();
458 for (Int_t iHit = 0; iHit < fNHits; iHit++) {
459 grHits->SetPoint(iHit,
460 fHits[iHit].fChannel + 1 + fHits[iHit].fYpos/256.,
461 fHits[iHit].fTimebin);
466 if (opt.Contains("T")) {
467 TLine *trklLines = new TLine[4];
468 for (Int_t iTrkl = 0; iTrkl < fTrackletArray->GetEntries(); iTrkl++) {
469 AliTRDtrackletMCM *trkl = (AliTRDtrackletMCM*) (*fTrackletArray)[iTrkl];
470 Float_t padWidth = 0.635 + 0.03 * (fDetector % 6);
471 Float_t offset = padWidth/256. * ((((((fRobPos & 0x1) << 2) + (fMcmPos & 0x3)) * 18) << 8) - ((18*4*2 - 18*2 - 3) << 7)); // revert adding offset in FitTracklet
472 Int_t ndrift = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrNdrift, fDetector, fRobPos, fMcmPos) >> 5;
473 Float_t slope = trkl->GetdY() * 140e-4 / ndrift;
475 Int_t t0 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFS);
476 Int_t t1 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFE);
478 trklLines[iTrkl].SetX1((offset - (trkl->GetY() - slope * t0)) / padWidth); // ??? sign?
479 trklLines[iTrkl].SetY1(t0);
480 trklLines[iTrkl].SetX2((offset - (trkl->GetY() - slope * t1)) / padWidth); // ??? sign?
481 trklLines[iTrkl].SetY2(t1);
482 trklLines[iTrkl].SetLineColor(2);
483 trklLines[iTrkl].SetLineWidth(2);
484 printf("Tracklet %i: y = %f, dy = %f, offset = %f\n", iTrkl, trkl->GetY(), (trkl->GetdY() * 140e-4), offset);
485 trklLines[iTrkl].Draw();
490 void AliTRDmcmSim::SetData( Int_t adc, Int_t* const data )
493 // Store ADC data into array of raw data
496 if( !CheckInitialized() ) return;
498 if( adc < 0 || adc >= fgkNADC ) {
499 AliError(Form ("Error: ADC %i is out of range (0 .. %d).", adc, fgkNADC-1));
503 for( Int_t it = 0 ; it < fNTimeBin ; it++ ) {
504 fADCR[adc][it] = (Int_t) (data[it]) << fgkAddDigits;
505 fADCF[adc][it] = (Int_t) (data[it]) << fgkAddDigits;
509 void AliTRDmcmSim::SetData( Int_t adc, Int_t it, Int_t data )
512 // Store ADC data into array of raw data
515 if( !CheckInitialized() ) return;
517 if( adc < 0 || adc >= fgkNADC ) {
518 AliError(Form ("Error: ADC %i is out of range (0 .. %d).", adc, fgkNADC-1));
522 fADCR[adc][it] = data << fgkAddDigits;
523 fADCF[adc][it] = data << fgkAddDigits;
526 void AliTRDmcmSim::SetData(AliTRDarrayADC* const adcArray, AliTRDdigitsManager * const digitsManager)
528 // Set the ADC data from an AliTRDarrayADC
530 if( !CheckInitialized() )
533 fDigitsManager = digitsManager;
534 if (fDigitsManager) {
535 for (Int_t iDict = 0; iDict < 3; iDict++) {
536 AliTRDarrayDictionary *newDict = (AliTRDarrayDictionary*) fDigitsManager->GetDictionary(fDetector, iDict);
537 if (fDict[iDict] != 0x0 && newDict != 0x0) {
539 if (fDict[iDict] == newDict)
542 fDict[iDict] = newDict;
544 if (fDict[iDict]->GetDim() == 0) {
545 AliError(Form("Dictionary %i of det. %i has dim. 0", fDetector, iDict));
548 fDict[iDict]->Expand();
551 fDict[iDict] = newDict;
553 fDict[iDict]->Expand();
558 if (fNTimeBin != adcArray->GetNtime())
559 SetNTimebins(adcArray->GetNtime());
561 Int_t offset = (fMcmPos % 4 + 1) * 21 + (fRobPos % 2) * 84 - 1;
563 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
564 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
565 Int_t value = adcArray->GetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin);
566 if (value < 0 || (offset - iAdc < 1) || (offset - iAdc > 165)) {
567 fADCR[iAdc][iTimeBin] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP) + (fgAddBaseline << fgkAddDigits);
568 fADCF[iAdc][iTimeBin] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFP) + (fgAddBaseline << fgkAddDigits);
572 fADCR[iAdc][iTimeBin] = (value << fgkAddDigits) + (fgAddBaseline << fgkAddDigits);
573 fADCF[iAdc][iTimeBin] = (value << fgkAddDigits) + (fgAddBaseline << fgkAddDigits);
579 void AliTRDmcmSim::SetDataByPad(AliTRDarrayADC* const adcArray, AliTRDdigitsManager * const digitsManager)
581 // Set the ADC data from an AliTRDarrayADC
582 // (by pad, to be used during initial reading in simulation)
584 if( !CheckInitialized() )
587 fDigitsManager = digitsManager;
588 if (fDigitsManager) {
589 for (Int_t iDict = 0; iDict < 3; iDict++) {
590 AliTRDarrayDictionary *newDict = (AliTRDarrayDictionary*) fDigitsManager->GetDictionary(fDetector, iDict);
591 if (fDict[iDict] != 0x0 && newDict != 0x0) {
593 if (fDict[iDict] == newDict)
596 fDict[iDict] = newDict;
598 if (fDict[iDict]->GetDim() == 0) {
599 AliError(Form("Dictionary %i of det. %i has dim. 0", fDetector, iDict));
602 fDict[iDict]->Expand();
605 fDict[iDict] = newDict;
607 fDict[iDict]->Expand();
612 if (fNTimeBin != adcArray->GetNtime())
613 SetNTimebins(adcArray->GetNtime());
615 Int_t offset = (fMcmPos % 4 + 1) * 18 + (fRobPos % 2) * 72 + 1;
617 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
618 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
620 Int_t pad = offset - iAdc;
621 if (pad > -1 && pad < 144)
622 value = adcArray->GetData(GetRow(), offset - iAdc, iTimeBin);
623 // Int_t value = adcArray->GetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin);
624 if (value < 0 || (offset - iAdc < 1) || (offset - iAdc > 165)) {
625 fADCR[iAdc][iTimeBin] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP) + (fgAddBaseline << fgkAddDigits);
626 fADCF[iAdc][iTimeBin] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFP) + (fgAddBaseline << fgkAddDigits);
630 fADCR[iAdc][iTimeBin] = (value << fgkAddDigits) + (fgAddBaseline << fgkAddDigits);
631 fADCF[iAdc][iTimeBin] = (value << fgkAddDigits) + (fgAddBaseline << fgkAddDigits);
637 void AliTRDmcmSim::SetDataPedestal( Int_t adc )
640 // Store ADC data into array of raw data
643 if( !CheckInitialized() )
646 if( adc < 0 || adc >= fgkNADC ) {
650 for( Int_t it = 0 ; it < fNTimeBin ; it++ ) {
651 fADCR[adc][it] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP) + (fgAddBaseline << fgkAddDigits);
652 fADCF[adc][it] = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFP) + (fgAddBaseline << fgkAddDigits);
656 Bool_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
658 // retrieve the MC hit information (not available in TRAP hardware)
660 if (index < 0 || index >= fNHits)
663 channel = fHits[index].fChannel;
664 timebin = fHits[index].fTimebin;
665 qtot = fHits[index].fQtot;
666 ypos = fHits[index].fYpos;
667 y = (Float_t) ((((((fRobPos & 0x1) << 2) + (fMcmPos & 0x3)) * 18) << 8) - ((18*4*2 - 18*2 - 1) << 7) -
668 (channel << 8) - ypos)
669 * (0.635 + 0.03 * (fDetector % 6))
671 label = fHits[index].fLabel;
676 Int_t AliTRDmcmSim::GetCol( Int_t adc )
679 // Return column id of the pad for the given ADC channel
682 if( !CheckInitialized() )
685 Int_t col = fFeeParam->GetPadColFromADC(fRobPos, fMcmPos, adc);
686 if (col < 0 || col >= fFeeParam->GetNcol())
692 Int_t AliTRDmcmSim::ProduceRawStream( UInt_t *buf, Int_t bufSize, UInt_t iEv) const
695 // Produce raw data stream from this MCM and put in buf
696 // Returns number of words filled, or negative value
697 // with -1 * number of overflowed words
700 if( !CheckInitialized() )
704 UInt_t mcmHeader = 0;
706 Int_t nw = 0; // Number of written words
707 Int_t of = 0; // Number of overflowed words
708 Int_t rawVer = fFeeParam->GetRAWversion();
710 Int_t nActiveADC = 0; // number of activated ADC bits in a word
712 if( !CheckInitialized() )
715 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBSF) != 0) // store unfiltered data
720 // Produce ADC mask : nncc cccm mmmm mmmm mmmm mmmm mmmm 1100
721 // n : unused , c : ADC count, m : selected ADCs
723 (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kC15CPUA) & (1 << 13))) { // check for zs flag in TRAP configuration
724 for( Int_t iAdc = 0 ; iAdc < fgkNADC ; iAdc++ ) {
725 if( ~fZSMap[iAdc] != 0 ) { // 0 means not suppressed
726 adcMask |= (1 << (iAdc+4) ); // last 4 digit reserved for 1100=0xc
727 nActiveADC++; // number of 1 in mmm....m
731 if ((nActiveADC == 0) &&
732 (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kC15CPUA) & (1 << 8))) // check for DEH flag in TRAP configuration
735 // assemble adc mask word
736 adcMask |= (1 << 30) | ( ( 0x3FFFFFFC ) & (~(nActiveADC) << 25) ) | 0xC; // nn = 01, ccccc are inverted, 0xc=1100
740 mcmHeader = (1<<31) | (fRobPos << 28) | (fMcmPos << 24) | ((iEv % 0x100000) << 4) | 0xC;
742 buf[nw++] = mcmHeader;
754 // Produce ADC data. 3 timebins are packed into one 32 bits word
755 // In this version, different ADC channel will NOT share the same word
757 UInt_t aa=0, a1=0, a2=0, a3=0;
759 for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
760 if( rawVer>= 3 && ~fZSMap[iAdc] == 0 ) continue; // Zero Suppression, 0 means not suppressed
761 aa = !(iAdc & 1) + 2;
762 for (Int_t iT = 0; iT < fNTimeBin; iT+=3 ) {
763 a1 = ((iT ) < fNTimeBin ) ? adc[iAdc][iT ] >> fgkAddDigits : 0;
764 a2 = ((iT + 1) < fNTimeBin ) ? adc[iAdc][iT+1] >> fgkAddDigits : 0;
765 a3 = ((iT + 2) < fNTimeBin ) ? adc[iAdc][iT+2] >> fgkAddDigits : 0;
766 x = (a3 << 22) | (a2 << 12) | (a1 << 2) | aa;
776 if( of != 0 ) return -of; else return nw;
779 Int_t AliTRDmcmSim::ProduceTrackletStream( UInt_t *buf, Int_t bufSize )
782 // Produce tracklet data stream from this MCM and put in buf
783 // Returns number of words filled, or negative value
784 // with -1 * number of overflowed words
787 if( !CheckInitialized() )
790 Int_t nw = 0; // Number of written words
791 Int_t of = 0; // Number of overflowed words
793 // Produce tracklet data. A maximum of four 32 Bit words will be written per MCM
794 // fMCMT is filled continuously until no more tracklet words available
796 for (Int_t iTracklet = 0; iTracklet < fTrackletArray->GetEntriesFast(); iTracklet++) {
798 buf[nw++] = ((AliTRDtrackletMCM*) (*fTrackletArray)[iTracklet])->GetTrackletWord();
803 if( of != 0 ) return -of; else return nw;
806 void AliTRDmcmSim::Filter()
809 // Filter the raw ADC values. The active filter stages and their
810 // parameters are taken from AliTRDtrapConfig.
811 // The raw data is stored separate from the filtered data. Thus,
812 // it is possible to run the filters on a set of raw values
813 // sequentially for parameter tuning.
816 if( !CheckInitialized() )
819 // Apply filters sequentially. Bypass is handled by filters
820 // since counters and internal registers may be updated even
821 // if the filter is bypassed.
822 // The first filter takes the data from fADCR and
825 // Non-linearity filter not implemented.
829 // Crosstalk filter not implemented.
832 void AliTRDmcmSim::FilterPedestalInit(Int_t baseline)
834 // Initializes the pedestal filter assuming that the input has
835 // been constant for a long time (compared to the time constant).
837 UShort_t fptc = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPTC); // 0..3, 0 - fastest, 3 - slowest
839 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++)
840 fPedAcc[iAdc] = (baseline << 2) * (1 << fgkFPshifts[fptc]);
843 UShort_t AliTRDmcmSim::FilterPedestalNextSample(Int_t adc, Int_t timebin, UShort_t value)
845 // Returns the output of the pedestal filter given the input value.
846 // The output depends on the internal registers and, thus, the
847 // history of the filter.
849 UShort_t fpnp = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP); // 0..511 -> 0..127.75, pedestal at the output
850 UShort_t fptc = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPTC); // 0..3, 0 - fastest, 3 - slowest
851 UShort_t fpby = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPBY); // 0..1 bypass, active low
853 UShort_t accumulatorShifted;
857 inpAdd = value + fpnp;
859 accumulatorShifted = (fPedAcc[adc] >> fgkFPshifts[fptc]) & 0x3FF; // 10 bits
860 if (timebin == 0) // the accumulator is disabled in the drift time
862 correction = (value & 0x3FF) - accumulatorShifted;
863 fPedAcc[adc] = (fPedAcc[adc] + correction) & 0x7FFFFFFF; // 31 bits
869 if (inpAdd <= accumulatorShifted)
873 inpAdd = inpAdd - accumulatorShifted;
881 void AliTRDmcmSim::FilterPedestal()
884 // Apply pedestal filter
886 // As the first filter in the chain it reads data from fADCR
887 // and outputs to fADCF.
888 // It has only an effect if previous samples have been fed to
889 // find the pedestal. Currently, the simulation assumes that
890 // the input has been stable for a sufficiently long time.
892 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
893 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
894 fADCF[iAdc][iTimeBin] = FilterPedestalNextSample(iAdc, iTimeBin, fADCR[iAdc][iTimeBin]);
899 void AliTRDmcmSim::FilterGainInit()
901 // Initializes the gain filter. In this case, only threshold
902 // counters are reset.
904 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
905 // these are counters which in hardware continue
906 // until maximum or reset
907 fGainCounterA[iAdc] = 0;
908 fGainCounterB[iAdc] = 0;
912 UShort_t AliTRDmcmSim::FilterGainNextSample(Int_t adc, UShort_t value)
914 // Apply the gain filter to the given value.
915 // BEGIN_LATEX O_{i}(t) = #gamma_{i} * I_{i}(t) + a_{i} END_LATEX
916 // The output depends on the internal registers and, thus, the
917 // history of the filter.
919 UShort_t fgby = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFGBY); // bypass, active low
920 UShort_t fgf = fTrapConfig->GetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGF0 + adc)); // 0x700 + (0 & 0x1ff);
921 UShort_t fga = fTrapConfig->GetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGA0 + adc)); // 40;
922 UShort_t fgta = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFGTA); // 20;
923 UShort_t fgtb = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFGTB); // 2060;
925 UInt_t corr; // corrected value
928 corr = (value * fgf) >> 11;
929 corr = corr > 0xfff ? 0xfff : corr;
930 corr = AddUintClipping(corr, fga, 12);
932 // Update threshold counters
933 // not really useful as they are cleared with every new event
934 if (!((fGainCounterA[adc] == 0x3FFFFFF) || (fGainCounterB[adc] == 0x3FFFFFF)))
938 fGainCounterB[adc]++;
939 else if (corr >= fgta)
940 fGainCounterA[adc]++;
949 void AliTRDmcmSim::FilterGain()
951 // Read data from fADCF and apply gain filter.
953 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
954 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
955 fADCF[iAdc][iTimeBin] = FilterGainNextSample(iAdc, fADCF[iAdc][iTimeBin]);
960 void AliTRDmcmSim::FilterTailInit(Int_t baseline)
962 // Initializes the tail filter assuming that the input has
963 // been at the baseline value (configured by FTFP) for a
964 // sufficiently long time.
966 // exponents and weight calculated from configuration
967 UShort_t alphaLong = 0x3ff & fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTAL); // the weight of the long component
968 UShort_t lambdaLong = (1 << 10) | (1 << 9) | (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTLL) & 0x1FF); // the multiplier
969 UShort_t lambdaShort = (0 << 10) | (1 << 9) | (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTLS) & 0x1FF); // the multiplier
971 Float_t lambdaL = lambdaLong * 1.0 / (1 << 11);
972 Float_t lambdaS = lambdaShort * 1.0 / (1 << 11);
973 Float_t alphaL = alphaLong * 1.0 / (1 << 11);
975 qup = (1 - lambdaL) * (1 - lambdaS);
976 qdn = 1 - lambdaS * alphaL - lambdaL * (1 - alphaL);
977 Float_t kdc = qup/qdn;
983 baseline = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFPNP);
985 ql = lambdaL * (1 - lambdaS) * alphaL;
986 qs = lambdaS * (1 - lambdaL) * (1 - alphaL);
988 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
989 Int_t value = baseline & 0xFFF;
990 Int_t corr = (value * fTrapConfig->GetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGF0 + iAdc))) >> 11;
991 corr = corr > 0xfff ? 0xfff : corr;
992 corr = AddUintClipping(corr, fTrapConfig->GetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGA0 + iAdc)), 12);
995 aout = baseline - (UShort_t) kt;
997 fTailAmplLong[iAdc] = (UShort_t) (aout * ql / (ql + qs));
998 fTailAmplShort[iAdc] = (UShort_t) (aout * qs / (ql + qs));
1002 UShort_t AliTRDmcmSim::FilterTailNextSample(Int_t adc, UShort_t value)
1004 // Returns the output of the tail filter for the given input value.
1005 // The output depends on the internal registers and, thus, the
1006 // history of the filter.
1008 // exponents and weight calculated from configuration
1009 UShort_t alphaLong = 0x3ff & fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTAL); // the weight of the long component
1010 UShort_t lambdaLong = (1 << 10) | (1 << 9) | (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTLL) & 0x1FF); // the multiplier of the long component
1011 UShort_t lambdaShort = (0 << 10) | (1 << 9) | (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTLS) & 0x1FF); // the multiplier of the short component
1013 // intermediate signals
1019 UShort_t inpVolt = value & 0xFFF; // 12 bits
1021 // add the present generator outputs
1022 aQ = AddUintClipping(fTailAmplLong[adc], fTailAmplShort[adc], 12);
1024 // calculate the difference between the input and the generated signal
1026 aDiff = inpVolt - aQ;
1030 // the inputs to the two generators, weighted
1031 alInpv = (aDiff * alphaLong) >> 11;
1033 // the new values of the registers, used next time
1035 tmp = AddUintClipping(fTailAmplLong[adc], alInpv, 12);
1036 tmp = (tmp * lambdaLong) >> 11;
1037 fTailAmplLong[adc] = tmp & 0xFFF;
1039 tmp = AddUintClipping(fTailAmplShort[adc], aDiff - alInpv, 12);
1040 tmp = (tmp * lambdaShort) >> 11;
1041 fTailAmplShort[adc] = tmp & 0xFFF;
1043 // the output of the filter
1044 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kFTBY) == 0) // bypass mode, active low
1050 void AliTRDmcmSim::FilterTail()
1052 // Apply tail cancellation filter to all data.
1054 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
1055 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
1056 fADCF[iAdc][iTimeBin] = FilterTailNextSample(iAdc, fADCF[iAdc][iTimeBin]);
1061 void AliTRDmcmSim::ZSMapping()
1064 // Zero Suppression Mapping implemented in TRAP chip
1065 // only implemented for up to 30 timebins
1067 // See detail TRAP manual "Data Indication" section:
1068 // http://www.kip.uni-heidelberg.de/ti/TRD/doc/trap/TRAP-UserManual.pdf
1071 if( !CheckInitialized() )
1074 Int_t eBIS = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBIS);
1075 Int_t eBIT = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBIT);
1076 Int_t eBIL = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBIL);
1077 Int_t eBIN = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBIN);
1079 Int_t **adc = fADCF;
1081 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++)
1084 for( Int_t it = 0 ; it < fNTimeBin ; it++ ) {
1085 Int_t iAdc; // current ADC channel
1090 Int_t supp; // suppression of the current channel (low active)
1092 // ----- first channel -----
1096 ac = adc[iAdc ][it]; // current
1097 an = adc[iAdc+1][it]; // next
1099 mask = ( ac >= ap && ac >= an ) ? 0 : 0x1; // peak center detection
1100 mask += ( ap + ac + an > eBIT ) ? 0 : 0x2; // cluster
1101 mask += ( ac > eBIS ) ? 0 : 0x4; // absolute large peak
1103 supp = (eBIL >> mask) & 1;
1105 fZSMap[iAdc] &= ~((1-supp) << it);
1106 if( eBIN == 0 ) { // neighbour sensitivity
1107 fZSMap[iAdc+1] &= ~((1-supp) << it);
1110 // ----- last channel -----
1113 ap = adc[iAdc-1][it]; // previous
1114 ac = adc[iAdc ][it]; // current
1117 mask = ( ac >= ap && ac >= an ) ? 0 : 0x1; // peak center detection
1118 mask += ( ap + ac + an > eBIT ) ? 0 : 0x2; // cluster
1119 mask += ( ac > eBIS ) ? 0 : 0x4; // absolute large peak
1121 supp = (eBIL >> mask) & 1;
1123 fZSMap[iAdc] &= ~((1-supp) << it);
1124 if( eBIN == 0 ) { // neighbour sensitivity
1125 fZSMap[iAdc-1] &= ~((1-supp) << it);
1128 // ----- middle channels -----
1129 for( iAdc = 1 ; iAdc < fgkNADC-1; iAdc++ ) {
1130 ap = adc[iAdc-1][it]; // previous
1131 ac = adc[iAdc ][it]; // current
1132 an = adc[iAdc+1][it]; // next
1134 mask = ( ac >= ap && ac >= an ) ? 0 : 0x1; // peak center detection
1135 mask += ( ap + ac + an > eBIT ) ? 0 : 0x2; // cluster
1136 mask += ( ac > eBIS ) ? 0 : 0x4; // absolute large peak
1138 supp = (eBIL >> mask) & 1;
1140 fZSMap[iAdc] &= ~((1-supp) << it);
1141 if( eBIN == 0 ) { // neighbour sensitivity
1142 fZSMap[iAdc-1] &= ~((1-supp) << it);
1143 fZSMap[iAdc+1] &= ~((1-supp) << it);
1150 void AliTRDmcmSim::AddHitToFitreg(Int_t adc, UShort_t timebin, UShort_t qtot, Short_t ypos, Int_t label)
1152 // Add the given hit to the fit register which is lateron used for
1153 // the tracklet calculation.
1154 // In addition to the fit sums in the fit register MC information
1157 if ((timebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS0)) &&
1158 (timebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE0)))
1159 fFitReg[adc].fQ0 += qtot;
1161 if ((timebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS1)) &&
1162 (timebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE1)))
1163 fFitReg[adc].fQ1 += qtot;
1165 if ((timebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFS) ) &&
1166 (timebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFE)))
1168 fFitReg[adc].fSumX += timebin;
1169 fFitReg[adc].fSumX2 += timebin*timebin;
1170 fFitReg[adc].fNhits++;
1171 fFitReg[adc].fSumY += ypos;
1172 fFitReg[adc].fSumY2 += ypos*ypos;
1173 fFitReg[adc].fSumXY += timebin*ypos;
1176 // register hits (MC info)
1177 fHits[fNHits].fChannel = adc;
1178 fHits[fNHits].fQtot = qtot;
1179 fHits[fNHits].fYpos = ypos;
1180 fHits[fNHits].fTimebin = timebin;
1181 fHits[fNHits].fLabel = label;
1185 void AliTRDmcmSim::CalcFitreg()
1188 // Detect the hits and fill the fit registers.
1189 // Requires 12-bit data from fADCF which means Filter()
1190 // has to be called before even if all filters are bypassed.
1192 //??? to be clarified:
1193 UInt_t adcMask = 0xffffffff;
1195 UShort_t timebin, adcch, adcLeft, adcCentral, adcRight, hitQual, timebin1, timebin2, qtotTemp;
1196 Short_t ypos, fromLeft, fromRight, found;
1197 UShort_t qTotal[19+1]; // the last is dummy
1198 UShort_t marked[6], qMarked[6], worse1, worse2;
1200 timebin1 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFS);
1201 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS0)
1203 timebin1 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS0);
1204 timebin2 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFE);
1205 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE1)
1207 timebin2 = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE1);
1209 // reset the fit registers
1211 for (adcch = 0; adcch < fgkNADC-2; adcch++) // due to border channels
1213 fFitReg[adcch].fNhits = 0;
1214 fFitReg[adcch].fQ0 = 0;
1215 fFitReg[adcch].fQ1 = 0;
1216 fFitReg[adcch].fSumX = 0;
1217 fFitReg[adcch].fSumY = 0;
1218 fFitReg[adcch].fSumX2 = 0;
1219 fFitReg[adcch].fSumY2 = 0;
1220 fFitReg[adcch].fSumXY = 0;
1223 for (timebin = timebin1; timebin < timebin2; timebin++)
1225 // first find the hit candidates and store the total cluster charge in qTotal array
1226 // in case of not hit store 0 there.
1227 for (adcch = 0; adcch < fgkNADC-2; adcch++) {
1228 if ( ( (adcMask >> adcch) & 7) == 7) //??? all 3 channels are present in case of ZS
1230 adcLeft = fADCF[adcch ][timebin];
1231 adcCentral = fADCF[adcch+1][timebin];
1232 adcRight = fADCF[adcch+2][timebin];
1233 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPVBY) == 1)
1234 hitQual = ( (adcLeft * adcRight) <
1235 (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPVT) * adcCentral) );
1238 // The accumulated charge is with the pedestal!!!
1239 qtotTemp = adcLeft + adcCentral + adcRight;
1241 (qtotTemp >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPHT)) &&
1242 (adcLeft <= adcCentral) &&
1243 (adcCentral > adcRight) )
1244 qTotal[adcch] = qtotTemp;
1249 qTotal[adcch] = 0; //jkl
1250 if (qTotal[adcch] != 0)
1251 AliDebug(10,Form("ch %2d qTotal %5d",adcch, qTotal[adcch]));
1257 marked[4] = 19; // invalid channel
1258 marked[5] = 19; // invalid channel
1260 while ((adcch < 16) && (found < 3))
1262 if (qTotal[adcch] > 0)
1265 marked[2*found+1]=adcch;
1274 while ((adcch > 2) && (found < 3))
1276 if (qTotal[adcch] > 0)
1278 marked[2*found]=adcch;
1285 AliDebug(10,Form("Fromleft=%d, Fromright=%d",fromLeft, fromRight));
1286 // here mask the hit candidates in the middle, if any
1287 if ((fromLeft >= 0) && (fromRight >= 0) && (fromLeft < fromRight))
1288 for (adcch = fromLeft+1; adcch < fromRight; adcch++)
1292 for (adcch = 0; adcch < 19; adcch++)
1293 if (qTotal[adcch] > 0) found++;
1296 if (found > 4) // sorting like in the TRAP in case of 5 or 6 candidates!
1298 if (marked[4] == marked[5]) marked[5] = 19;
1299 for (found=0; found<6; found++)
1301 qMarked[found] = qTotal[marked[found]] >> 4;
1302 AliDebug(10,Form("ch_%d qTotal %d qTotals %d",marked[found],qTotal[marked[found]],qMarked[found]));
1305 Sort6To2Worst(marked[0], marked[3], marked[4], marked[1], marked[2], marked[5],
1313 // Now mask the two channels with the smallest charge
1317 AliDebug(10,Form("Kill ch %d\n",worse1));
1322 AliDebug(10,Form("Kill ch %d\n",worse2));
1326 for (adcch = 0; adcch < 19; adcch++) {
1327 if (qTotal[adcch] > 0) // the channel is marked for processing
1329 adcLeft = fADCF[adcch ][timebin];
1330 adcCentral = fADCF[adcch+1][timebin];
1331 adcRight = fADCF[adcch+2][timebin];
1332 // hit detected, in TRAP we have 4 units and a hit-selection, here we proceed all channels!
1333 // subtract the pedestal TPFP, clipping instead of wrapping
1335 Int_t regTPFP = fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPFP);
1336 AliDebug(10, Form("Hit found, time=%d, adcch=%d/%d/%d, adc values=%d/%d/%d, regTPFP=%d, TPHT=%d\n",
1337 timebin, adcch, adcch+1, adcch+2, adcLeft, adcCentral, adcRight, regTPFP,
1338 fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPHT)));
1340 if (adcLeft < regTPFP) adcLeft = 0; else adcLeft -= regTPFP;
1341 if (adcCentral < regTPFP) adcCentral = 0; else adcCentral -= regTPFP;
1342 if (adcRight < regTPFP) adcRight = 0; else adcRight -= regTPFP;
1344 // Calculate the center of gravity
1345 // checking for adcCentral != 0 (in case of "bad" configuration)
1346 if (adcCentral == 0)
1348 ypos = 128*(adcLeft - adcRight) / adcCentral;
1349 if (ypos < 0) ypos = -ypos;
1350 // make the correction using the position LUT
1351 ypos = ypos + fTrapConfig->GetTrapReg((AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kTPL00 + (ypos & 0x7F)));
1352 if (adcLeft > adcRight) ypos = -ypos;
1354 // label calculation
1356 if (fDigitsManager) {
1357 Int_t label[9] = { 0 }; // up to 9 different labels possible
1358 Int_t count[9] = { 0 };
1363 padcol[0] = fFeeParam->GetPadColFromADC(fRobPos, fMcmPos, adcch);
1364 padcol[1] = fFeeParam->GetPadColFromADC(fRobPos, fMcmPos, adcch+1);
1365 padcol[2] = fFeeParam->GetPadColFromADC(fRobPos, fMcmPos, adcch+2);
1366 Int_t padrow = fFeeParam->GetPadRowFromMCM(fRobPos, fMcmPos);
1367 for (Int_t iDict = 0; iDict < 3; iDict++) {
1370 for (Int_t iPad = 0; iPad < 3; iPad++) {
1371 if (padcol[iPad] < 0)
1373 Int_t currLabel = fDict[iDict]->GetData(padrow, padcol[iPad], timebin); //fDigitsManager->GetTrack(iDict, padrow, padcol, timebin, fDetector);
1374 AliDebug(10, Form("Read label: %4i for det: %3i, row: %i, col: %i, tb: %i\n", currLabel, fDetector, padrow, padcol[iPad], timebin));
1375 for (Int_t iLabel = 0; iLabel < nLabels; iLabel++) {
1376 if (currLabel == label[iLabel]) {
1378 if (count[iLabel] > maxCount) {
1379 maxCount = count[iLabel];
1386 if (currLabel >= 0) {
1387 label[nLabels++] = currLabel;
1392 mcLabel = label[maxIdx];
1395 // add the hit to the fitregister
1396 AddHitToFitreg(adcch, timebin, qTotal[adcch], ypos, mcLabel);
1401 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
1402 if (fFitReg[iAdc].fNhits != 0) {
1403 AliDebug(2, Form("fitreg[%i]: nHits = %i, sumX = %i, sumY = %i, sumX2 = %i, sumY2 = %i, sumXY = %i", iAdc,
1404 fFitReg[iAdc].fNhits,
1405 fFitReg[iAdc].fSumX,
1406 fFitReg[iAdc].fSumY,
1407 fFitReg[iAdc].fSumX2,
1408 fFitReg[iAdc].fSumY2,
1409 fFitReg[iAdc].fSumXY
1415 void AliTRDmcmSim::TrackletSelection()
1417 // Select up to 4 tracklet candidates from the fit registers
1418 // and assign them to the CPUs.
1420 UShort_t adcIdx, i, j, ntracks, tmp;
1421 UShort_t trackletCand[18][2]; // store the adcch[0] and number of hits[1] for all tracklet candidates
1424 for (adcIdx = 0; adcIdx < 18; adcIdx++) // ADCs
1425 if ( (fFitReg[adcIdx].fNhits
1426 >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPCL)) &&
1427 (fFitReg[adcIdx].fNhits+fFitReg[adcIdx+1].fNhits
1428 >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPCT)))
1430 trackletCand[ntracks][0] = adcIdx;
1431 trackletCand[ntracks][1] = fFitReg[adcIdx].fNhits+fFitReg[adcIdx+1].fNhits;
1432 AliDebug(10,Form("%d %2d %4d\n", ntracks, trackletCand[ntracks][0], trackletCand[ntracks][1]));
1436 for (i=0; i<ntracks;i++)
1437 AliDebug(10,Form("%d %d %d\n",i,trackletCand[i][0], trackletCand[i][1]));
1441 // primitive sorting according to the number of hits
1442 for (j = 0; j < (ntracks-1); j++)
1444 for (i = j+1; i < ntracks; i++)
1446 if ( (trackletCand[j][1] < trackletCand[i][1]) ||
1447 ( (trackletCand[j][1] == trackletCand[i][1]) && (trackletCand[j][0] < trackletCand[i][0]) ) )
1450 tmp = trackletCand[j][1];
1451 trackletCand[j][1] = trackletCand[i][1];
1452 trackletCand[i][1] = tmp;
1453 tmp = trackletCand[j][0];
1454 trackletCand[j][0] = trackletCand[i][0];
1455 trackletCand[i][0] = tmp;
1459 ntracks = 4; // cut the rest, 4 is the max
1461 // else is not necessary to sort
1463 // now sort, so that the first tracklet going to CPU0 corresponds to the highest adc channel - as in the TRAP
1464 for (j = 0; j < (ntracks-1); j++)
1466 for (i = j+1; i < ntracks; i++)
1468 if (trackletCand[j][0] < trackletCand[i][0])
1471 tmp = trackletCand[j][1];
1472 trackletCand[j][1] = trackletCand[i][1];
1473 trackletCand[i][1] = tmp;
1474 tmp = trackletCand[j][0];
1475 trackletCand[j][0] = trackletCand[i][0];
1476 trackletCand[i][0] = tmp;
1480 for (i = 0; i < ntracks; i++) // CPUs with tracklets.
1481 fFitPtr[i] = trackletCand[i][0]; // pointer to the left channel with tracklet for CPU[i]
1482 for (i = ntracks; i < 4; i++) // CPUs without tracklets
1483 fFitPtr[i] = 31; // pointer to the left channel with tracklet for CPU[i] = 31 (invalid)
1484 AliDebug(10,Form("found %i tracklet candidates\n", ntracks));
1485 for (i = 0; i < 4; i++)
1486 AliDebug(10,Form("fitPtr[%i]: %i\n", i, fFitPtr[i]));
1489 void AliTRDmcmSim::FitTracklet()
1491 // Perform the actual tracklet fit based on the fit sums
1492 // which have been filled in the fit registers.
1494 // parameters in fitred.asm (fit program)
1495 Int_t decPlaces = 5;
1498 rndAdd = (1 << (decPlaces-1)) + 1;
1499 else if (decPlaces == 1)
1501 Int_t ndriftDp = 5; // decimal places for drift time
1502 Long64_t shift = ((Long64_t) 1 << 32);
1504 // calculated in fitred.asm
1505 Int_t padrow = ((fRobPos >> 1) << 2) | (fMcmPos >> 2);
1506 Int_t yoffs = (((((fRobPos & 0x1) << 2) + (fMcmPos & 0x3)) * 18) << 8) -
1507 ((18*4*2 - 18*2 - 1) << 7);
1508 yoffs = yoffs << decPlaces; // holds position of ADC channel 1
1509 Int_t layer = fDetector % 6;
1510 UInt_t scaleY = (UInt_t) ((0.635 + 0.03 * layer)/(256.0 * 160.0e-4) * shift);
1511 UInt_t scaleD = (UInt_t) ((0.635 + 0.03 * layer)/(256.0 * 140.0e-4) * shift);
1513 Int_t deflCorr = (Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCorr, fDetector, fRobPos, fMcmPos);
1514 Int_t ndrift = (Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrNdrift, fDetector, fRobPos, fMcmPos);
1516 // local variables for calculation
1517 Long64_t mult, temp, denom; //???
1518 UInt_t q0, q1, pid; // charges in the two windows and total charge
1519 UShort_t nHits; // number of hits
1520 Int_t slope, offset; // slope and offset of the tracklet
1521 Int_t sumX, sumY, sumXY, sumX2; // fit sums from fit registers
1522 Int_t sumY2; // not used in the current TRAP program, now used for error calculation (simulation only)
1523 Float_t fitError, fitSlope, fitOffset;
1524 FitReg_t *fit0, *fit1; // pointers to relevant fit registers
1526 // const uint32_t OneDivN[32] = { // 2**31/N : exactly like in the TRAP, the simple division here gives the same result!
1527 // 0x00000000, 0x80000000, 0x40000000, 0x2AAAAAA0, 0x20000000, 0x19999990, 0x15555550, 0x12492490,
1528 // 0x10000000, 0x0E38E380, 0x0CCCCCC0, 0x0BA2E8B0, 0x0AAAAAA0, 0x09D89D80, 0x09249240, 0x08888880,
1529 // 0x08000000, 0x07878780, 0x071C71C0, 0x06BCA1A0, 0x06666660, 0x06186180, 0x05D17450, 0x0590B210,
1530 // 0x05555550, 0x051EB850, 0x04EC4EC0, 0x04BDA120, 0x04924920, 0x0469EE50, 0x04444440, 0x04210840};
1532 for (Int_t cpu = 0; cpu < 4; cpu++) {
1533 if (fFitPtr[cpu] == 31)
1535 fMCMT[cpu] = 0x10001000; //??? AliTRDfeeParam::GetTrackletEndmarker();
1539 fit0 = &fFitReg[fFitPtr[cpu] ];
1540 fit1 = &fFitReg[fFitPtr[cpu]+1]; // next channel
1543 mult = mult << (32 + decPlaces);
1547 nHits = fit0->fNhits + fit1->fNhits; // number of hits
1548 sumX = fit0->fSumX + fit1->fSumX;
1549 sumX2 = fit0->fSumX2 + fit1->fSumX2;
1550 denom = nHits*sumX2 - sumX*sumX;
1552 mult = mult / denom; // exactly like in the TRAP program
1553 q0 = fit0->fQ0 + fit1->fQ0;
1554 q1 = fit0->fQ1 + fit1->fQ1;
1555 sumY = fit0->fSumY + fit1->fSumY + 256*fit1->fNhits;
1556 sumXY = fit0->fSumXY + fit1->fSumXY + 256*fit1->fSumX;
1557 sumY2 = fit0->fSumY2 + fit1->fSumY2 + 512*fit1->fSumY + 256*256*fit1->fNhits;
1559 slope = nHits*sumXY - sumX * sumY;
1560 offset = sumX2*sumY - sumX * sumXY;
1561 temp = mult * slope;
1562 slope = temp >> 32; // take the upper 32 bits
1564 temp = mult * offset;
1565 offset = temp >> 32; // take the upper 32 bits
1567 offset = offset + yoffs;
1568 AliDebug(10, Form("slope = %i, slope * ndrift = %i, deflCorr: %i",
1569 slope, slope * ndrift, deflCorr));
1570 slope = ((slope * ndrift) >> ndriftDp) + deflCorr;
1571 offset = offset - (fFitPtr[cpu] << (8 + decPlaces));
1574 temp = temp * scaleD;
1575 slope = (temp >> 32);
1577 temp = temp * scaleY;
1578 offset = (temp >> 32);
1580 // rounding, like in the TRAP
1581 slope = (slope + rndAdd) >> decPlaces;
1582 offset = (offset + rndAdd) >> decPlaces;
1584 AliDebug(5, Form("Det: %3i, ROB: %i, MCM: %2i: deflection: %i, min: %i, max: %i",
1585 fDetector, fRobPos, fMcmPos, slope,
1586 (Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 2*fFitPtr[cpu], fDetector, fRobPos, fMcmPos),
1587 (Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 1 + 2*fFitPtr[cpu], fDetector, fRobPos, fMcmPos)));
1589 AliDebug(5, Form("Fit sums: x = %i, X = %i, y = %i, Y = %i, Z = %i",
1590 sumX, sumX2, sumY, sumY2, sumXY));
1592 fitSlope = (Float_t) (nHits * sumXY - sumX * sumY) / (nHits * sumX2 - sumX*sumX);
1594 fitOffset = (Float_t) (sumX2 * sumY - sumX * sumXY) / (nHits * sumX2 - sumX*sumX);
1596 Float_t sx = (Float_t) sumX;
1597 Float_t sx2 = (Float_t) sumX2;
1598 Float_t sy = (Float_t) sumY;
1599 Float_t sy2 = (Float_t) sumY2;
1600 Float_t sxy = (Float_t) sumXY;
1601 fitError = sy2 - (sx2 * sy*sy - 2 * sx * sxy * sy + nHits * sxy*sxy) / (nHits * sx2 - sx*sx);
1602 //fitError = (Float_t) sumY2 - (Float_t) (sumY*sumY) / nHits - fitSlope * ((Float_t) (sumXY - sumX*sumY) / nHits);
1604 Bool_t rejected = kFALSE;
1605 // deflection range table from DMEM
1606 if ((slope < ((Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 2*fFitPtr[cpu], fDetector, fRobPos, fMcmPos))) ||
1607 (slope > ((Int_t) fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 1 + 2*fFitPtr[cpu], fDetector, fRobPos, fMcmPos))))
1610 if (rejected && GetApplyCut())
1612 fMCMT[cpu] = 0x10001000; //??? AliTRDfeeParam::GetTrackletEndmarker();
1616 if (slope > 63 || slope < -64) { // wrapping in TRAP!
1617 AliError(Form("Overflow in slope: %i, tracklet discarded!", slope));
1618 fMCMT[cpu] = 0x10001000;
1622 slope = slope & 0x7F; // 7 bit
1624 if (offset > 0xfff || offset < -0xfff)
1625 AliWarning("Overflow in offset");
1626 offset = offset & 0x1FFF; // 13 bit
1628 pid = GetPID(q0 >> fgkAddDigits, q1 >> fgkAddDigits); // divided by 4 because in simulation there are two additional decimal places
1631 AliWarning("Overflow in PID");
1632 pid = pid & 0xFF; // 8 bit, exactly like in the TRAP program
1634 // assemble and store the tracklet word
1635 fMCMT[cpu] = (pid << 24) | (padrow << 20) | (slope << 13) | offset;
1637 // calculate MC label
1641 if (fDigitsManager) {
1642 Int_t label[30] = {0}; // up to 30 different labels possible
1643 Int_t count[30] = {0};
1647 for (Int_t iHit = 0; iHit < fNHits; iHit++) {
1648 if ((fHits[iHit].fChannel - fFitPtr[cpu] < 0) ||
1649 (fHits[iHit].fChannel - fFitPtr[cpu] > 1))
1652 // counting contributing hits
1653 if (fHits[iHit].fTimebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS0) &&
1654 fHits[iHit].fTimebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE0))
1656 if (fHits[iHit].fTimebin >= fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQS1) &&
1657 fHits[iHit].fTimebin < fTrapConfig->GetTrapReg(AliTRDtrapConfig::kTPQE1))
1660 Int_t currLabel = fHits[iHit].fLabel;
1661 for (Int_t iLabel = 0; iLabel < nLabels; iLabel++) {
1662 if (currLabel == label[iLabel]) {
1664 if (count[iLabel] > maxCount) {
1665 maxCount = count[iLabel];
1672 if (currLabel >= 0) {
1673 label[nLabels++] = currLabel;
1677 mcLabel = label[maxIdx];
1679 new ((*fTrackletArray)[fTrackletArray->GetEntriesFast()]) AliTRDtrackletMCM((UInt_t) fMCMT[cpu], fDetector*2 + fRobPos%2, fRobPos, fMcmPos);
1680 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetLabel(mcLabel);
1683 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetNHits(fit0->fNhits + fit1->fNhits);
1684 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetNHits0(nHits0);
1685 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetNHits1(nHits1);
1686 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetQ0(q0);
1687 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetQ1(q1);
1688 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetSlope(fitSlope);
1689 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetOffset(fitOffset);
1690 ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetError(TMath::Sqrt(TMath::Abs(fitError)/nHits));
1692 // // cluster information
1693 // Float_t *res = new Float_t[nHits];
1694 // Float_t *qtot = new Float_t[nHits];
1696 // for (Int_t iHit = 0; iHit < fNHits; iHit++) {
1697 // // check if hit contributes
1698 // if (fHits[iHit].fChannel == fFitPtr[cpu]) {
1699 // res[nCls] = fHits[iHit].fYpos - (fitSlope * fHits[iHit].fTimebin + fitOffset);
1700 // qtot[nCls] = fHits[iHit].fQtot;
1703 // else if (fHits[iHit].fChannel == fFitPtr[cpu] + 1) {
1704 // res[nCls] = fHits[iHit].fYpos + 256 - (fitSlope * fHits[iHit].fTimebin + fitOffset);
1705 // qtot[nCls] = fHits[iHit].fQtot;
1709 // ((AliTRDtrackletMCM*) (*fTrackletArray)[fTrackletArray->GetEntriesFast()-1])->SetClusters(res, qtot, nCls);
1714 AliError(Form("Strange fit error: %f from Sx: %i, Sy: %i, Sxy: %i, Sx2: %i, Sy2: %i, nHits: %i",
1715 fitError, sumX, sumY, sumXY, sumX2, sumY2, nHits));
1716 AliDebug(3, Form("fit slope: %f, offset: %f, error: %f",
1717 fitSlope, fitOffset, TMath::Sqrt(TMath::Abs(fitError)/nHits)));
1723 void AliTRDmcmSim::Tracklet()
1725 // Run the tracklet calculation by calling sequentially:
1726 // CalcFitreg(); TrackletSelection(); FitTracklet()
1727 // and store the tracklets
1729 if (!fInitialized) {
1730 AliError("Called uninitialized! Nothing done!");
1734 fTrackletArray->Delete();
1739 TrackletSelection();
1743 Bool_t AliTRDmcmSim::StoreTracklets()
1745 // store the found tracklets via the loader
1747 if (fTrackletArray->GetEntriesFast() == 0)
1750 AliRunLoader *rl = AliRunLoader::Instance();
1751 AliDataLoader *dl = 0x0;
1753 dl = rl->GetLoader("TRDLoader")->GetDataLoader("tracklets");
1755 AliError("Could not get the tracklets data loader!");
1759 TTree *trackletTree = dl->Tree();
1760 if (!trackletTree) {
1762 trackletTree = dl->Tree();
1765 AliTRDtrackletMCM *trkl = 0x0;
1766 TBranch *trkbranch = trackletTree->GetBranch(fTrklBranchName.Data());
1768 trkbranch = trackletTree->Branch(fTrklBranchName.Data(), "AliTRDtrackletMCM", &trkl, 32000);
1770 for (Int_t iTracklet = 0; iTracklet < fTrackletArray->GetEntriesFast(); iTracklet++) {
1771 trkl = ((AliTRDtrackletMCM*) (*fTrackletArray)[iTracklet]);
1772 trkbranch->SetAddress(&trkl);
1779 void AliTRDmcmSim::WriteData(AliTRDarrayADC *digits)
1781 // write back the processed data configured by EBSF
1782 // EBSF = 1: unfiltered data; EBSF = 0: filtered data
1783 // zero-suppressed valued are written as -1 to digits
1785 if( !CheckInitialized() )
1788 Int_t offset = (fMcmPos % 4 + 1) * 21 + (fRobPos % 2) * 84 - 1;
1790 if (fTrapConfig->GetTrapReg(AliTRDtrapConfig::kEBSF) != 0) // store unfiltered data
1792 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
1793 if (~fZSMap[iAdc] == 0) {
1794 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
1795 digits->SetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin, -1);
1798 else if (iAdc < 2 || iAdc == 20) {
1799 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
1800 digits->SetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin, (fADCR[iAdc][iTimeBin] >> fgkAddDigits) - fgAddBaseline);
1806 for (Int_t iAdc = 0; iAdc < fgkNADC; iAdc++) {
1807 if (~fZSMap[iAdc] != 0) {
1808 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
1809 digits->SetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin, (fADCF[iAdc][iTimeBin] >> fgkAddDigits) - fgAddBaseline);
1813 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
1814 digits->SetDataByAdcCol(GetRow(), offset - iAdc, iTimeBin, -1);
1822 // ******************************
1825 // Memory area for the LUT: 0xC100 to 0xC3FF
1827 // The addresses for the parameters (the order is optimized for maximum calculation speed in the MCMs):
1829 // 0xC029: nBins(sF)
1831 // 0xC02B: TableLength
1832 // Defined in AliTRDtrapConfig.h
1834 // The algorithm implemented in the TRAP program of the MCMs (Venelin Angelov)
1835 // 1) set the read pointer to the beginning of the Parameters in DMEM
1836 // 2) shift right the FitReg with the Q0 + (Q1 << 16) to get Q1
1837 // 3) read cor1 with rpointer++
1839 // 5) read nBins with rpointer++
1840 // 6) start nBins*cor1*Q1
1841 // 7) read cor0 with rpointer++
1842 // 8) swap hi-low parts in FitReg, now is Q1 + (Q0 << 16)
1843 // 9) shift right to get Q0
1844 // 10) start cor0*Q0
1845 // 11) read TableLength
1846 // 12) compare cor0*Q0 with nBins
1847 // 13) if >=, clip cor0*Q0 to nBins-1
1848 // 14) add cor0*Q0 to nBins*cor1*Q1
1849 // 15) compare the result with TableLength
1850 // 16) if >=, clip to TableLength-1
1851 // 17) read from the LUT 8 bits
1854 Int_t AliTRDmcmSim::GetPID(Int_t q0, Int_t q1)
1856 // return PID calculated from charges accumulated in two time windows
1861 UInt_t nBinsQ0 = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTnbins); // number of bins in q0 / 4 !!
1862 UInt_t pidTotalSize = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTLength);
1863 if(nBinsQ0==0 || pidTotalSize==0) // make sure we don't run into trouble if one of the values is not configured
1866 ULong_t corrQ0 = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTcor0, fDetector, fRobPos, fMcmPos);
1867 ULong_t corrQ1 = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTcor1, fDetector, fRobPos, fMcmPos);
1868 if(corrQ0==0 || corrQ1==0) // make sure we don't run into trouble if one of the values is not configured
1872 addrQ0 = (((addrQ0*q0)>>16)>>16); // because addrQ0 = (q0 * corrQ0) >> 32; does not work for unknown reasons
1874 if(addrQ0 >= nBinsQ0) { // check for overflow
1875 AliDebug(5,Form("Overflow in q0: %i/4 is bigger then %i", addrQ0, nBinsQ0));
1876 addrQ0 = nBinsQ0 -1;
1880 addr = (((addr*q1)>>16)>>16);
1881 addr = addrQ0 + nBinsQ0*addr; // because addr = addrQ0 + nBinsQ0* (((corrQ1*q1)>>32); does not work
1883 if(addr >= pidTotalSize) {
1884 AliDebug(5,Form("Overflow in q1. Address %i/4 is bigger then %i", addr, pidTotalSize));
1885 addr = pidTotalSize -1;
1888 // 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)
1890 UInt_t result = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTStart+(addr/4));
1891 return (result>>((addr%4)*8)) & 0xFF;
1896 // help functions, to be cleaned up
1898 UInt_t AliTRDmcmSim::AddUintClipping(UInt_t a, UInt_t b, UInt_t nbits) const
1901 // This function adds a and b (unsigned) and clips to
1902 // the specified number of bits.
1908 UInt_t maxv = (1 << nbits) - 1;;
1914 if ((sum < a) || (sum < b))
1920 void AliTRDmcmSim::Sort2(UShort_t idx1i, UShort_t idx2i, \
1921 UShort_t val1i, UShort_t val2i, \
1922 UShort_t * const idx1o, UShort_t * const idx2o, \
1923 UShort_t * const val1o, UShort_t * const val2o) const
1925 // sorting for tracklet selection
1943 void AliTRDmcmSim::Sort3(UShort_t idx1i, UShort_t idx2i, UShort_t idx3i, \
1944 UShort_t val1i, UShort_t val2i, UShort_t val3i, \
1945 UShort_t * const idx1o, UShort_t * const idx2o, UShort_t * const idx3o, \
1946 UShort_t * const val1o, UShort_t * const val2o, UShort_t * const val3o)
1948 // sorting for tracklet selection
1953 if (val1i > val2i) sel=4; else sel=0;
1954 if (val2i > val3i) sel=sel + 2;
1955 if (val3i > val1i) sel=sel + 1;
1958 case 6 : // 1 > 2 > 3 => 1 2 3
1959 case 0 : // 1 = 2 = 3 => 1 2 3 : in this case doesn't matter, but so is in hardware!
1968 case 4 : // 1 > 2, 2 <= 3, 3 <= 1 => 1 3 2
1977 case 2 : // 1 <= 2, 2 > 3, 3 <= 1 => 2 1 3
1986 case 3 : // 1 <= 2, 2 > 3, 3 > 1 => 2 3 1
1995 case 1 : // 1 <= 2, 2 <= 3, 3 > 1 => 3 2 1
2004 case 5 : // 1 > 2, 2 <= 3, 3 > 1 => 3 1 2
2013 default: // the rest should NEVER happen!
2014 AliError("ERROR in Sort3!!!\n");
2019 void AliTRDmcmSim::Sort6To4(UShort_t idx1i, UShort_t idx2i, UShort_t idx3i, UShort_t idx4i, UShort_t idx5i, UShort_t idx6i, \
2020 UShort_t val1i, UShort_t val2i, UShort_t val3i, UShort_t val4i, UShort_t val5i, UShort_t val6i, \
2021 UShort_t * const idx1o, UShort_t * const idx2o, UShort_t * const idx3o, UShort_t * const idx4o, \
2022 UShort_t * const val1o, UShort_t * const val2o, UShort_t * const val3o, UShort_t * const val4o)
2024 // sorting for tracklet selection
2026 UShort_t idx21s, idx22s, idx23s, dummy;
2027 UShort_t val21s, val22s, val23s;
2028 UShort_t idx23as, idx23bs;
2029 UShort_t val23as, val23bs;
2031 Sort3(idx1i, idx2i, idx3i, val1i, val2i, val3i,
2032 idx1o, &idx21s, &idx23as,
2033 val1o, &val21s, &val23as);
2035 Sort3(idx4i, idx5i, idx6i, val4i, val5i, val6i,
2036 idx2o, &idx22s, &idx23bs,
2037 val2o, &val22s, &val23bs);
2039 Sort2(idx23as, idx23bs, val23as, val23bs, &idx23s, &dummy, &val23s, &dummy);
2041 Sort3(idx21s, idx22s, idx23s, val21s, val22s, val23s,
2042 idx3o, idx4o, &dummy,
2043 val3o, val4o, &dummy);
2047 void AliTRDmcmSim::Sort6To2Worst(UShort_t idx1i, UShort_t idx2i, UShort_t idx3i, UShort_t idx4i, UShort_t idx5i, UShort_t idx6i, \
2048 UShort_t val1i, UShort_t val2i, UShort_t val3i, UShort_t val4i, UShort_t val5i, UShort_t val6i, \
2049 UShort_t * const idx5o, UShort_t * const idx6o)
2051 // sorting for tracklet selection
2053 UShort_t idx21s, idx22s, idx23s, dummy1, dummy2, dummy3, dummy4, dummy5;
2054 UShort_t val21s, val22s, val23s;
2055 UShort_t idx23as, idx23bs;
2056 UShort_t val23as, val23bs;
2058 Sort3(idx1i, idx2i, idx3i, val1i, val2i, val3i,
2059 &dummy1, &idx21s, &idx23as,
2060 &dummy2, &val21s, &val23as);
2062 Sort3(idx4i, idx5i, idx6i, val4i, val5i, val6i,
2063 &dummy1, &idx22s, &idx23bs,
2064 &dummy2, &val22s, &val23bs);
2066 Sort2(idx23as, idx23bs, val23as, val23bs, &idx23s, idx5o, &val23s, &dummy1);
2068 Sort3(idx21s, idx22s, idx23s, val21s, val22s, val23s,
2069 &dummy1, &dummy2, idx6o,
2070 &dummy3, &dummy4, &dummy5);
2074 // ----- I/O implementation -----
2076 ostream& AliTRDmcmSim::Text(ostream& os)
2078 // manipulator to activate output in text format (default)
2080 os.iword(fgkFormatIndex) = 0;
2084 ostream& AliTRDmcmSim::Cfdat(ostream& os)
2086 // manipulator to activate output in CFDAT format
2087 // to send to the FEE via SCSN
2089 os.iword(fgkFormatIndex) = 1;
2093 ostream& AliTRDmcmSim::Raw(ostream& os)
2095 // manipulator to activate output as raw data dump
2097 os.iword(fgkFormatIndex) = 2;
2101 ostream& operator<<(ostream& os, const AliTRDmcmSim& mcm)
2103 // output implementation
2105 // no output for non-initialized MCM
2106 if (!mcm.CheckInitialized())
2109 // ----- human-readable output -----
2110 if (os.iword(AliTRDmcmSim::fgkFormatIndex) == 0) {
2112 os << "MCM " << mcm.fMcmPos << " on ROB " << mcm.fRobPos <<
2113 " in detector " << mcm.fDetector << std::endl;
2115 os << "----- Unfiltered ADC data (10 bit) -----" << std::endl;
2117 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++)
2118 os << std::setw(5) << iChannel;
2120 for (Int_t iTimeBin = 0; iTimeBin < mcm.fNTimeBin; iTimeBin++) {
2121 os << "tb " << std::setw(2) << iTimeBin << ":";
2122 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++) {
2123 os << std::setw(5) << (mcm.fADCR[iChannel][iTimeBin] >> mcm.fgkAddDigits);
2128 os << "----- Filtered ADC data (10+2 bit) -----" << std::endl;
2130 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++)
2131 os << std::setw(4) << iChannel
2132 << ((~mcm.fZSMap[iChannel] != 0) ? "!" : " ");
2134 for (Int_t iTimeBin = 0; iTimeBin < mcm.fNTimeBin; iTimeBin++) {
2135 os << "tb " << std::setw(2) << iTimeBin << ":";
2136 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++) {
2137 os << std::setw(4) << (mcm.fADCF[iChannel][iTimeBin])
2138 << (((mcm.fZSMap[iChannel] & (1 << iTimeBin)) == 0) ? "!" : " ");
2144 // ----- CFDAT output -----
2145 else if(os.iword(AliTRDmcmSim::fgkFormatIndex) == 1) {
2147 Int_t addrOffset = 0x2000;
2148 Int_t addrStep = 0x80;
2150 for (Int_t iTimeBin = 0; iTimeBin < mcm.fNTimeBin; iTimeBin++) {
2151 for (Int_t iChannel = 0; iChannel < mcm.fgkNADC; iChannel++) {
2152 os << std::setw(5) << 10
2153 << std::setw(5) << addrOffset + iChannel * addrStep + iTimeBin
2154 << std::setw(5) << (mcm.fADCF[iChannel][iTimeBin])
2155 << std::setw(5) << dest << std::endl;
2161 // ----- raw data ouptut -----
2162 else if (os.iword(AliTRDmcmSim::fgkFormatIndex) == 2) {
2163 Int_t bufSize = 300;
2164 UInt_t *buf = new UInt_t[bufSize];
2166 Int_t bufLength = mcm.ProduceRawStream(&buf[0], bufSize);
2168 for (Int_t i = 0; i < bufLength; i++)
2169 std::cout << "0x" << std::hex << buf[i] << std::endl;
2175 os << "unknown format set" << std::endl;
2182 void AliTRDmcmSim::PrintFitRegXml(ostream& os) const
2184 // print fit registres in XML format
2186 bool tracklet=false;
2188 for (Int_t cpu = 0; cpu < 4; cpu++) {
2189 if(fFitPtr[cpu] != 31)
2193 if(tracklet==true) {
2194 os << "<nginject>" << std::endl;
2195 os << "<ack roc=\""<< fDetector << "\" cmndid=\"0\">" << std::endl;
2196 os << "<dmem-readout>" << std::endl;
2197 os << "<d det=\"" << fDetector << "\">" << std::endl;
2198 os << " <ro-board rob=\"" << fRobPos << "\">" << std::endl;
2199 os << " <m mcm=\"" << fMcmPos << "\">" << std::endl;
2201 for(int cpu=0; cpu<4; cpu++) {
2202 os << " <c cpu=\"" << cpu << "\">" << std::endl;
2203 if(fFitPtr[cpu] != 31) {
2204 for(int adcch=fFitPtr[cpu]; adcch<fFitPtr[cpu]+2; adcch++) {
2205 os << " <ch chnr=\"" << adcch << "\">"<< std::endl;
2206 os << " <hits>" << fFitReg[adcch].fNhits << "</hits>"<< std::endl;
2207 os << " <q0>" << fFitReg[adcch].fQ0/4 << "</q0>"<< std::endl; // divided by 4 because in simulation we have 2 additional decimal places
2208 os << " <q1>" << fFitReg[adcch].fQ1/4 << "</q1>"<< std::endl; // in the output
2209 os << " <sumx>" << fFitReg[adcch].fSumX << "</sumx>"<< std::endl;
2210 os << " <sumxsq>" << fFitReg[adcch].fSumX2 << "</sumxsq>"<< std::endl;
2211 os << " <sumy>" << fFitReg[adcch].fSumY << "</sumy>"<< std::endl;
2212 os << " <sumysq>" << fFitReg[adcch].fSumY2 << "</sumysq>"<< std::endl;
2213 os << " <sumxy>" << fFitReg[adcch].fSumXY << "</sumxy>"<< std::endl;
2214 os << " </ch>" << std::endl;
2217 os << " </c>" << std::endl;
2219 os << " </m>" << std::endl;
2220 os << " </ro-board>" << std::endl;
2221 os << "</d>" << std::endl;
2222 os << "</dmem-readout>" << std::endl;
2223 os << "</ack>" << std::endl;
2224 os << "</nginject>" << std::endl;
2229 void AliTRDmcmSim::PrintTrackletsXml(ostream& os) const
2231 // print tracklets in XML format
2233 os << "<nginject>" << std::endl;
2234 os << "<ack roc=\""<< fDetector << "\" cmndid=\"0\">" << std::endl;
2235 os << "<dmem-readout>" << std::endl;
2236 os << "<d det=\"" << fDetector << "\">" << std::endl;
2237 os << " <ro-board rob=\"" << fRobPos << "\">" << std::endl;
2238 os << " <m mcm=\"" << fMcmPos << "\">" << std::endl;
2240 Int_t pid, padrow, slope, offset;
2241 for(Int_t cpu=0; cpu<4; cpu++) {
2242 if(fMCMT[cpu] == 0x10001000) {
2249 pid = (fMCMT[cpu] & 0xFF000000) >> 24;
2250 padrow = (fMCMT[cpu] & 0xF00000 ) >> 20;
2251 slope = (fMCMT[cpu] & 0xFE000 ) >> 13;
2252 offset = (fMCMT[cpu] & 0x1FFF ) ;
2255 os << " <trk> <pid>" << pid << "</pid>" << " <padrow>" << padrow << "</padrow>"
2256 << " <slope>" << slope << "</slope>" << " <offset>" << offset << "</offset>" << "</trk>" << std::endl;
2259 os << " </m>" << std::endl;
2260 os << " </ro-board>" << std::endl;
2261 os << "</d>" << std::endl;
2262 os << "</dmem-readout>" << std::endl;
2263 os << "</ack>" << std::endl;
2264 os << "</nginject>" << std::endl;
2268 void AliTRDmcmSim::PrintAdcDatHuman(ostream& os) const
2270 // print ADC data in human-readable format
2272 os << "MCM " << fMcmPos << " on ROB " << fRobPos <<
2273 " in detector " << fDetector << std::endl;
2275 os << "----- Unfiltered ADC data (10 bit) -----" << std::endl;
2277 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++)
2278 os << std::setw(5) << iChannel;
2280 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
2281 os << "tb " << std::setw(2) << iTimeBin << ":";
2282 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++) {
2283 os << std::setw(5) << (fADCR[iChannel][iTimeBin] >> fgkAddDigits);
2288 os << "----- Filtered ADC data (10+2 bit) -----" << std::endl;
2290 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++)
2291 os << std::setw(4) << iChannel
2292 << ((~fZSMap[iChannel] != 0) ? "!" : " ");
2294 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
2295 os << "tb " << std::setw(2) << iTimeBin << ":";
2296 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++) {
2297 os << std::setw(4) << (fADCF[iChannel][iTimeBin])
2298 << (((fZSMap[iChannel] & (1 << iTimeBin)) == 0) ? "!" : " ");
2305 void AliTRDmcmSim::PrintAdcDatXml(ostream& os) const
2307 // print ADC data in XML format
2309 os << "<nginject>" << std::endl;
2310 os << "<ack roc=\""<< fDetector << "\" cmndid=\"0\">" << std::endl;
2311 os << "<dmem-readout>" << std::endl;
2312 os << "<d det=\"" << fDetector << "\">" << std::endl;
2313 os << " <ro-board rob=\"" << fRobPos << "\">" << std::endl;
2314 os << " <m mcm=\"" << fMcmPos << "\">" << std::endl;
2316 for(Int_t iChannel = 0; iChannel < fgkNADC; iChannel++) {
2317 os << " <ch chnr=\"" << iChannel << "\">" << std::endl;
2318 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
2319 os << "<tb>" << fADCF[iChannel][iTimeBin]/4 << "</tb>";
2321 os << " </ch>" << std::endl;
2324 os << " </m>" << std::endl;
2325 os << " </ro-board>" << std::endl;
2326 os << "</d>" << std::endl;
2327 os << "</dmem-readout>" << std::endl;
2328 os << "</ack>" << std::endl;
2329 os << "</nginject>" << std::endl;
2334 void AliTRDmcmSim::PrintAdcDatDatx(ostream& os, Bool_t broadcast) const
2336 // print ADC data in datx format (to send to FEE)
2338 fTrapConfig->PrintDatx(os, 2602, 1, 0, 127); // command to enable the ADC clock - necessary to write ADC values to MCM
2341 Int_t addrOffset = 0x2000;
2342 Int_t addrStep = 0x80;
2343 Int_t addrOffsetEBSIA = 0x20;
2345 for (Int_t iTimeBin = 0; iTimeBin < fNTimeBin; iTimeBin++) {
2346 for (Int_t iChannel = 0; iChannel < fgkNADC; iChannel++) {
2347 if(broadcast==kFALSE)
2348 fTrapConfig->PrintDatx(os, addrOffset+iChannel*addrStep+addrOffsetEBSIA+iTimeBin, (fADCF[iChannel][iTimeBin]/4), GetRobPos(), GetMcmPos());
2350 fTrapConfig->PrintDatx(os, addrOffset+iChannel*addrStep+addrOffsetEBSIA+iTimeBin, (fADCF[iChannel][iTimeBin]/4), 0, 127);
2357 void AliTRDmcmSim::PrintPidLutHuman()
2359 // print PID LUT in human readable format
2363 UInt_t addrEnd = AliTRDtrapConfig::fgkDmemAddrLUTStart + fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTLength)/4; // /4 because each addr contains 4 values
2364 UInt_t nBinsQ0 = fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTnbins);
2366 std::cout << "nBinsQ0: " << nBinsQ0 << std::endl;
2367 std::cout << "LUT table length: " << fTrapConfig->GetDmemUnsigned(AliTRDtrapConfig::fgkDmemAddrLUTLength) << std::endl;
2369 for(UInt_t addr=AliTRDtrapConfig::fgkDmemAddrLUTStart; addr< addrEnd; addr++) {
2370 result = fTrapConfig->GetDmemUnsigned(addr);
2371 std::cout << addr << " # x: " << ((addr-AliTRDtrapConfig::fgkDmemAddrLUTStart)%((nBinsQ0)/4))*4 << ", y: " <<(addr-AliTRDtrapConfig::fgkDmemAddrLUTStart)/(nBinsQ0/4)
2372 << " # " <<((result>>0)&0xFF)
2373 << " | " << ((result>>8)&0xFF)
2374 << " | " << ((result>>16)&0xFF)
2375 << " | " << ((result>>24)&0xFF) << std::endl;