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 cluster finder //
22 ///////////////////////////////////////////////////////////////////////////////
24 #include <TClonesArray.h>
25 #include <TObjArray.h>
27 #include "AliRunLoader.h"
28 #include "AliLoader.h"
29 #include "AliTreeLoader.h"
30 #include "AliAlignObj.h"
32 #include "AliTRDclusterizer.h"
33 #include "AliTRDcluster.h"
34 #include "AliTRDReconstructor.h"
35 #include "AliTRDgeometry.h"
36 #include "AliTRDarrayDictionary.h"
37 #include "AliTRDarrayADC.h"
38 #include "AliTRDdigitsManager.h"
39 #include "AliTRDdigitsParam.h"
40 #include "AliTRDrawData.h"
41 #include "AliTRDcalibDB.h"
42 #include "AliTRDtransform.h"
43 #include "AliTRDSignalIndex.h"
44 #include "AliTRDrawStreamBase.h"
45 #include "AliTRDfeeParam.h"
46 #include "AliTRDtrackletWord.h"
48 #include "TTreeStream.h"
50 #include "Cal/AliTRDCalROC.h"
51 #include "Cal/AliTRDCalDet.h"
52 #include "Cal/AliTRDCalSingleChamberStatus.h"
54 ClassImp(AliTRDclusterizer)
56 //_____________________________________________________________________________
57 AliTRDclusterizer::AliTRDclusterizer(const AliTRDReconstructor *const rec)
65 ,fDigitsManager(new AliTRDdigitsManager())
66 ,fTrackletContainer(NULL)
68 ,fTransform(new AliTRDtransform(0))
75 ,fMinLeftRightCutSigma(0)
81 ,fCalGainFactorROC(NULL)
82 ,fCalGainFactorDetValue(0)
85 ,fCalPadStatusROC(NULL)
93 // AliTRDclusterizer default constructor
96 SetBit(kLabels, kTRUE);
97 SetBit(knewDM, kFALSE);
99 AliTRDcalibDB *trd = 0x0;
100 if (!(trd = AliTRDcalibDB::Instance())) {
101 AliFatal("Could not get calibration object");
104 fRawVersion = AliTRDfeeParam::Instance()->GetRAWversion();
106 // Initialize debug stream
108 if(fReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 1){
109 TDirectory *savedir = gDirectory;
110 //fgGetDebugStream = new TTreeSRedirector("TRD.ClusterizerDebug.root");
117 //_____________________________________________________________________________
118 AliTRDclusterizer::AliTRDclusterizer(const Text_t *name, const Text_t *title, const AliTRDReconstructor *const rec)
126 ,fDigitsManager(new AliTRDdigitsManager())
127 ,fTrackletContainer(NULL)
129 ,fTransform(new AliTRDtransform(0))
136 ,fMinLeftRightCutSigma(0)
142 ,fCalGainFactorROC(NULL)
143 ,fCalGainFactorDetValue(0)
145 ,fCalNoiseDetValue(0)
146 ,fCalPadStatusROC(NULL)
154 // AliTRDclusterizer constructor
157 SetBit(kLabels, kTRUE);
158 SetBit(knewDM, kFALSE);
160 AliTRDcalibDB *trd = 0x0;
161 if (!(trd = AliTRDcalibDB::Instance())) {
162 AliFatal("Could not get calibration object");
165 fDigitsManager->CreateArrays();
167 fRawVersion = AliTRDfeeParam::Instance()->GetRAWversion();
173 //_____________________________________________________________________________
174 AliTRDclusterizer::AliTRDclusterizer(const AliTRDclusterizer &c)
176 ,fReconstructor(c.fReconstructor)
182 ,fDigitsManager(NULL)
183 ,fTrackletContainer(NULL)
192 ,fMinLeftRightCutSigma(0)
198 ,fCalGainFactorROC(NULL)
199 ,fCalGainFactorDetValue(0)
201 ,fCalNoiseDetValue(0)
202 ,fCalPadStatusROC(NULL)
210 // AliTRDclusterizer copy constructor
213 SetBit(kLabels, kTRUE);
214 SetBit(knewDM, kFALSE);
220 //_____________________________________________________________________________
221 AliTRDclusterizer::~AliTRDclusterizer()
224 // AliTRDclusterizer destructor
227 if (fRecPoints/* && IsClustersOwner()*/){
228 fRecPoints->Delete();
233 fTracklets->Delete();
237 if (fDigitsManager) {
238 delete fDigitsManager;
239 fDigitsManager = NULL;
242 if (fTrackletContainer){
243 delete [] fTrackletContainer[0];
244 delete [] fTrackletContainer[1];
245 delete [] fTrackletContainer;
246 fTrackletContainer = NULL;
261 //_____________________________________________________________________________
262 AliTRDclusterizer &AliTRDclusterizer::operator=(const AliTRDclusterizer &c)
265 // Assignment operator
270 ((AliTRDclusterizer &) c).Copy(*this);
277 //_____________________________________________________________________________
278 void AliTRDclusterizer::Copy(TObject &c) const
284 ((AliTRDclusterizer &) c).fClusterTree = NULL;
285 ((AliTRDclusterizer &) c).fRecPoints = NULL;
286 ((AliTRDclusterizer &) c).fTrackletTree = NULL;
287 ((AliTRDclusterizer &) c).fDigitsManager = NULL;
288 ((AliTRDclusterizer &) c).fTrackletContainer = NULL;
289 ((AliTRDclusterizer &) c).fRawVersion = fRawVersion;
290 ((AliTRDclusterizer &) c).fTransform = NULL;
291 ((AliTRDclusterizer &) c).fDigits = NULL;
292 ((AliTRDclusterizer &) c).fIndexes = NULL;
293 ((AliTRDclusterizer &) c).fMaxThresh = 0;
294 ((AliTRDclusterizer &) c).fMaxThreshTest = 0;
295 ((AliTRDclusterizer &) c).fSigThresh = 0;
296 ((AliTRDclusterizer &) c).fMinMaxCutSigma= 0;
297 ((AliTRDclusterizer &) c).fMinLeftRightCutSigma = 0;
298 ((AliTRDclusterizer &) c).fLayer = 0;
299 ((AliTRDclusterizer &) c).fDet = 0;
300 ((AliTRDclusterizer &) c).fVolid = 0;
301 ((AliTRDclusterizer &) c).fColMax = 0;
302 ((AliTRDclusterizer &) c).fTimeTotal = 0;
303 ((AliTRDclusterizer &) c).fCalGainFactorROC = NULL;
304 ((AliTRDclusterizer &) c).fCalGainFactorDetValue = 0;
305 ((AliTRDclusterizer &) c).fCalNoiseROC = NULL;
306 ((AliTRDclusterizer &) c).fCalNoiseDetValue = 0;
307 ((AliTRDclusterizer &) c).fCalPadStatusROC = NULL;
308 ((AliTRDclusterizer &) c).fClusterROC = 0;
309 ((AliTRDclusterizer &) c).firstClusterROC= 0;
310 ((AliTRDclusterizer &) c).fNoOfClusters = 0;
311 ((AliTRDclusterizer &) c).fBaseline = 0;
312 ((AliTRDclusterizer &) c).fRawStream = NULL;
316 //_____________________________________________________________________________
317 Bool_t AliTRDclusterizer::Open(const Char_t *name, Int_t nEvent)
320 // Opens the AliROOT file. Output and input are in the same file
323 TString evfoldname = AliConfig::GetDefaultEventFolderName();
324 fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
327 fRunLoader = AliRunLoader::Open(name);
331 AliError(Form("Can not open session for file %s.",name));
342 //_____________________________________________________________________________
343 Bool_t AliTRDclusterizer::OpenOutput()
346 // Open the output file
349 if (!fReconstructor->IsWritingClusters()) return kTRUE;
351 TObjArray *ioArray = 0x0;
353 AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
354 loader->MakeTree("R");
356 fClusterTree = loader->TreeR();
357 fClusterTree->Branch("TRDcluster", "TObjArray", &ioArray, 32000, 0);
363 //_____________________________________________________________________________
364 Bool_t AliTRDclusterizer::OpenOutput(TTree *const clusterTree)
367 // Connect the output tree
371 if (fReconstructor->IsWritingClusters()){
372 TObjArray *ioArray = 0x0;
373 fClusterTree = clusterTree;
374 fClusterTree->Branch("TRDcluster", "TObjArray", &ioArray, 32000, 0);
379 //_____________________________________________________________________________
380 Bool_t AliTRDclusterizer::OpenInput(Int_t nEvent)
383 // Opens a ROOT-file with TRD-hits and reads in the digits-tree
386 // Import the Trees for the event nEvent in the file
387 fRunLoader->GetEvent(nEvent);
393 //_____________________________________________________________________________
394 Bool_t AliTRDclusterizer::WriteClusters(Int_t det)
397 // Fills TRDcluster branch in the tree with the clusters
398 // found in detector = det. For det=-1 writes the tree.
402 (det >= AliTRDgeometry::Ndet())) {
403 AliError(Form("Unexpected detector index %d.\n",det));
407 TObjArray *ioArray = new TObjArray(400);
408 TBranch *branch = fClusterTree->GetBranch("TRDcluster");
410 branch = fClusterTree->Branch("TRDcluster","TObjArray",&ioArray,32000,0);
411 } else branch->SetAddress(&ioArray);
413 Int_t nRecPoints = RecPoints()->GetEntriesFast();
415 for (Int_t i = 0; i < nRecPoints; i++) {
416 AliTRDcluster *c = (AliTRDcluster *) RecPoints()->UncheckedAt(i);
417 if(det != c->GetDetector()) continue;
420 fClusterTree->Fill();
423 Int_t detOld = -1, nw(0);
424 for (Int_t i = 0; i < nRecPoints; i++) {
425 AliTRDcluster *c = (AliTRDcluster *) RecPoints()->UncheckedAt(i);
426 if(c->GetDetector() != detOld){
427 nw += ioArray->GetEntriesFast();
428 fClusterTree->Fill();
430 detOld = c->GetDetector();
434 if(ioArray->GetEntriesFast()){
435 nw += ioArray->GetEntriesFast();
436 fClusterTree->Fill();
439 AliDebug(2, Form("Clusters FOUND[%d] WRITTEN[%d] STATUS[%s]", nRecPoints, nw, nw==nRecPoints?"OK":"FAILED"));
446 //_____________________________________________________________________________
447 Bool_t AliTRDclusterizer::WriteTracklets(Int_t det)
450 // Write the raw data tracklets into seperate file
453 UInt_t **leaves = new UInt_t *[2];
454 for (Int_t i=0; i<2 ;i++){
455 leaves[i] = new UInt_t[258];
456 leaves[i][0] = det; // det
457 leaves[i][1] = i; // side
458 memcpy(leaves[i]+2, fTrackletContainer[i], sizeof(UInt_t) * 256);
462 AliDataLoader *dl = fRunLoader->GetLoader("TRDLoader")->GetDataLoader("tracklets");
464 fTrackletTree = dl->Tree();
467 TBranch *trkbranch = fTrackletTree->GetBranch("trkbranch");
469 trkbranch = fTrackletTree->Branch("trkbranch",leaves[0],"det/i:side/i:tracklets[256]/i");
472 for (Int_t i=0; i<2; i++){
473 if (leaves[i][2]>0) {
474 trkbranch->SetAddress(leaves[i]);
475 fTrackletTree->Fill();
479 // AliDataLoader *dl = fRunLoader->GetLoader("TRDLoader")->GetDataLoader("tracklets"); //jkl: wrong here
480 // dl->WriteData("OVERWRITE"); //jkl: wrong here
488 //_____________________________________________________________________________
489 Bool_t AliTRDclusterizer::ReadDigits()
492 // Reads the digits arrays from the input aliroot file
496 AliError("No run loader available");
500 AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
501 if (!loader->TreeD()) {
502 loader->LoadDigits();
505 // Read in the digit arrays
506 return (fDigitsManager->ReadDigits(loader->TreeD()));
510 //_____________________________________________________________________________
511 Bool_t AliTRDclusterizer::ReadDigits(TTree *digitsTree)
514 // Reads the digits arrays from the input tree
517 // Read in the digit arrays
518 return (fDigitsManager->ReadDigits(digitsTree));
522 //_____________________________________________________________________________
523 Bool_t AliTRDclusterizer::ReadDigits(AliRawReader *rawReader)
526 // Reads the digits arrays from the ddl file
530 fDigitsManager = raw.Raw2Digits(rawReader);
536 //_____________________________________________________________________________
537 Bool_t AliTRDclusterizer::MakeClusters()
540 // Creates clusters from digits
543 // Propagate info from the digits manager
544 if (TestBit(kLabels)){
545 SetBit(kLabels, fDigitsManager->UsesDictionaries());
548 Bool_t fReturn = kTRUE;
549 for (Int_t i = 0; i < AliTRDgeometry::kNdet; i++){
551 AliTRDarrayADC *digitsIn = (AliTRDarrayADC*) fDigitsManager->GetDigits(i); //mod
552 // This is to take care of switched off super modules
553 if (!digitsIn->HasData()) continue;
555 digitsIn->DeleteNegatives(); // Restore digits array to >=0 values
556 AliTRDSignalIndex* indexes = fDigitsManager->GetIndexes(i);
557 if (indexes->IsAllocated() == kFALSE){
558 fDigitsManager->BuildIndexes(i);
562 if (indexes->HasEntry()){
563 if (TestBit(kLabels)){
564 for (Int_t iDict = 0; iDict < AliTRDdigitsManager::kNDict; iDict++){
565 AliTRDarrayDictionary *tracksIn = 0; //mod
566 tracksIn = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(i,iDict); //mod
570 fR = MakeClusters(i);
571 fReturn = fR && fReturn;
575 // if(IsWritingClusters()) WriteClusters(i);
579 // Clear arrays of this chamber, to prepare for next event
580 fDigitsManager->ClearArrays(i);
583 if(fReconstructor->IsWritingClusters()) WriteClusters(-1);
585 AliInfo(Form("Number of found clusters : %d", RecPoints()->GetEntriesFast()));
591 //_____________________________________________________________________________
592 Bool_t AliTRDclusterizer::Raw2Clusters(AliRawReader *rawReader)
595 // Creates clusters from raw data
598 return Raw2ClustersChamber(rawReader);
602 //_____________________________________________________________________________
603 Bool_t AliTRDclusterizer::Raw2ClustersChamber(AliRawReader *rawReader)
606 // Creates clusters from raw data
609 // Create the digits manager
610 if (!fDigitsManager){
611 SetBit(knewDM, kTRUE);
612 fDigitsManager = new AliTRDdigitsManager(kTRUE);
613 fDigitsManager->CreateArrays();
616 fDigitsManager->SetUseDictionaries(TestBit(kLabels));
618 // ----- preparing tracklet output -----
619 if (fReconstructor->IsWritingTracklets()) {
620 AliDataLoader *trklLoader = AliRunLoader::Instance()->GetLoader("TRDLoader")->GetDataLoader("tracklets");
622 //AliInfo("Could not get the tracklets data loader, adding it now!");
623 trklLoader = new AliDataLoader("TRD.Tracklets.root","tracklets", "tracklets");
624 AliRunLoader::Instance()->GetLoader("TRDLoader")->AddDataLoader(trklLoader);
626 AliTreeLoader *trklTreeLoader = dynamic_cast<AliTreeLoader*> (trklLoader->GetBaseLoader("tracklets-raw"));
627 if (!trklTreeLoader) {
628 trklTreeLoader = new AliTreeLoader("tracklets-raw", trklLoader);
629 trklLoader->AddBaseLoader(trklTreeLoader);
631 if (!trklTreeLoader->Tree())
632 trklTreeLoader->MakeTree();
635 // tracklet container for raw tracklet writing
636 if (!fTrackletContainer && ( fReconstructor->IsWritingTracklets() || fReconstructor->IsProcessingTracklets() )) {
637 // maximum tracklets for one HC
638 const Int_t kTrackletChmb=256;
639 fTrackletContainer = new UInt_t *[2];
640 fTrackletContainer[0] = new UInt_t[kTrackletChmb];
641 fTrackletContainer[1] = new UInt_t[kTrackletChmb];
642 memset(fTrackletContainer[0], 0, kTrackletChmb*sizeof(UInt_t)); //jkl
643 memset(fTrackletContainer[1], 0, kTrackletChmb*sizeof(UInt_t)); //jkl
647 fRawStream = AliTRDrawStreamBase::GetRawStream(rawReader);
649 fRawStream->SetReader(rawReader);
651 SetBit(kHLT, fReconstructor->IsHLT());
654 fRawStream->SetSharedPadReadout(kFALSE);
655 fRawStream->SetNoErrorWarning();
658 AliDebug(1,Form("Stream version: %s", fRawStream->IsA()->GetName()));
661 while ((det = fRawStream->NextChamber(fDigitsManager,fTrackletContainer)) >= 0){
662 if (fDigitsManager->GetIndexes(det)->HasEntry())
665 fDigitsManager->ClearArrays(det);
667 if (!fReconstructor->IsWritingTracklets()) continue;
668 if (*(fTrackletContainer[0]) > 0 || *(fTrackletContainer[1]) > 0) WriteTracklets(det);
671 if (fReconstructor->IsWritingTracklets()) {
672 if (AliDataLoader *trklLoader = AliRunLoader::Instance()->GetLoader("TRDLoader")->GetDataLoader("tracklets")) {
674 if (AliTreeLoader *trklTreeLoader = (AliTreeLoader*) trklLoader->GetBaseLoader("tracklets-raw"))
675 trklTreeLoader->WriteData("OVERWRITE");
676 trklLoader->UnloadAll();
681 if (fTrackletContainer){
682 delete [] fTrackletContainer[0];
683 delete [] fTrackletContainer[1];
684 delete [] fTrackletContainer;
685 fTrackletContainer = NULL;
688 if(fReconstructor->IsWritingClusters()) WriteClusters(-1);
690 if(!TestBit(knewDM)){
691 delete fDigitsManager;
692 fDigitsManager = NULL;
697 AliInfo(Form("Number of found clusters : %d", fNoOfClusters));
702 //_____________________________________________________________________________
703 UChar_t AliTRDclusterizer::GetStatus(Short_t &signal)
706 // Check if a pad is masked
711 if(signal>0 && TESTBIT(signal, 10)){
713 for(int ibit=0; ibit<4; ibit++){
714 if(TESTBIT(signal, 11+ibit)){
715 SETBIT(status, ibit);
716 CLRBIT(signal, 11+ibit);
723 //_____________________________________________________________________________
724 void AliTRDclusterizer::SetPadStatus(const UChar_t status, UChar_t &out) const {
726 // Set the pad status into out
727 // First three bits are needed for the position encoding
732 //_____________________________________________________________________________
733 UChar_t AliTRDclusterizer::GetPadStatus(UChar_t encoding) const {
735 // return the staus encoding of the corrupted pad
737 return static_cast<UChar_t>(encoding >> 3);
740 //_____________________________________________________________________________
741 Int_t AliTRDclusterizer::GetCorruption(UChar_t encoding) const {
743 // Return the position of the corruption
748 //_____________________________________________________________________________
749 Bool_t AliTRDclusterizer::MakeClusters(Int_t det)
752 // Generates the cluster.
756 fDigits = (AliTRDarrayADC *) fDigitsManager->GetDigits(det); //mod
757 fBaseline = fDigitsManager->GetDigitsParam()->GetADCbaseline(det);
759 // This is to take care of switched off super modules
760 if (!fDigits->HasData()) return kFALSE;
762 fIndexes = fDigitsManager->GetIndexes(det);
763 if (fIndexes->IsAllocated() == kFALSE) {
764 AliError("Indexes do not exist!");
768 AliTRDcalibDB* const calibration = AliTRDcalibDB::Instance();
770 AliFatal("No AliTRDcalibDB instance available\n");
774 if (!fReconstructor){
775 AliError("Reconstructor not set\n");
779 const AliTRDrecoParam *const recoParam = fReconstructor->GetRecoParam();
781 fMaxThresh = (Short_t)recoParam->GetClusMaxThresh();
782 fMaxThreshTest = (Short_t)(recoParam->GetClusMaxThresh()/2+fBaseline);
783 fSigThresh = (Short_t)recoParam->GetClusSigThresh();
784 fMinMaxCutSigma = recoParam->GetMinMaxCutSigma();
785 fMinLeftRightCutSigma = recoParam->GetMinLeftRightCutSigma();
786 const Int_t iEveryNTB = recoParam->GetRecEveryNTB();
788 Int_t istack = fIndexes->GetStack();
789 fLayer = fIndexes->GetLayer();
790 Int_t isector = fIndexes->GetSM();
792 // Start clustering in the chamber
794 fDet = AliTRDgeometry::GetDetector(fLayer,istack,isector);
796 AliError("Strange Detector number Missmatch!");
800 AliDebug(2, Form("Det[%d] @ Sec[%d] Stk[%d] Ly[%d]", fDet, isector, istack, fLayer));
802 // TRD space point transformation
803 fTransform->SetDetector(det);
805 Int_t iGeoLayer = AliGeomManager::kTRD1 + fLayer;
806 Int_t iGeoModule = istack + AliTRDgeometry::Nstack() * isector;
807 fVolid = AliGeomManager::LayerToVolUID(iGeoLayer,iGeoModule);
809 if(fReconstructor->IsProcessingTracklets() && fTrackletContainer)
810 AddTrackletsToArray();
812 fColMax = fDigits->GetNcol();
813 fTimeTotal = fDigitsManager->GetDigitsParam()->GetNTimeBins(det);
815 // Check consistency between OCDB and raw data
816 Int_t nTimeOCDB = calibration->GetNumberOfTimeBinsDCS();
818 if((nTimeOCDB > -1) && (fTimeTotal != nTimeOCDB)){
819 AliWarning(Form("Number of timebins does not match OCDB value (RAW[%d] OCDB[%d]), using raw value"
820 ,fTimeTotal,nTimeOCDB));
824 AliWarning("Undefined number of timebins in OCDB, using value from raw data.");
826 AliError("Number of timebins in raw data is negative, skipping chamber!");
829 }else if(nTimeOCDB == -2){
830 AliError("Mixed number of timebins in OCDB, no reconstruction of TRD data!");
832 }else if(fTimeTotal != nTimeOCDB){
833 AliError(Form("Number of timebins in raw data does not match OCDB value (RAW[%d] OCDB[%d]), skipping chamber!"
834 ,fTimeTotal,nTimeOCDB));
839 // Detector wise calibration object for the gain factors
840 const AliTRDCalDet *calGainFactorDet = calibration->GetGainFactorDet();
841 // Calibration object with pad wise values for the gain factors
842 fCalGainFactorROC = calibration->GetGainFactorROC(fDet);
843 // Calibration value for chamber wise gain factor
844 fCalGainFactorDetValue = calGainFactorDet->GetValue(fDet);
846 // Detector wise calibration object for the noise
847 const AliTRDCalDet *calNoiseDet = calibration->GetNoiseDet();
848 // Calibration object with pad wise values for the noise
849 fCalNoiseROC = calibration->GetNoiseROC(fDet);
850 // Calibration value for chamber wise noise
851 fCalNoiseDetValue = calNoiseDet->GetValue(fDet);
853 // Calibration object with the pad status
854 fCalPadStatusROC = calibration->GetPadStatusROC(fDet);
856 firstClusterROC = -1;
859 SetBit(kLUT, recoParam->UseLUT());
860 SetBit(kGAUS, recoParam->UseGAUS());
862 // Apply the gain and the tail cancelation via digital filter
863 if(recoParam->UseTailCancelation()) TailCancelation(recoParam);
865 MaxStruct curr, last;
866 Int_t nMaximas = 0, nCorrupted = 0;
868 // Here the clusterfining is happening
870 for(curr.time = 0; curr.time < fTimeTotal; curr.time+=iEveryNTB){
871 while(fIndexes->NextRCIndex(curr.row, curr.col)){
872 if(fDigits->GetData(curr.row, curr.col, curr.time) > fMaxThreshTest && IsMaximum(curr, curr.padStatus, &curr.signals[0])){
874 if(curr.time==last.time && curr.row==last.row && curr.col==last.col+2) FivePadCluster(last, curr);
877 last=curr; curr.fivePad=kFALSE;
881 if(last.row>-1) CreateCluster(last);
883 if(recoParam->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 2 && fReconstructor->IsDebugStreaming()){
884 TTreeSRedirector* fDebugStream = fReconstructor->GetDebugStream(AliTRDrecoParam::kClusterizer);
885 (*fDebugStream) << "MakeClusters"
886 << "Detector=" << det
887 << "NMaxima=" << nMaximas
888 << "NClusters=" << fClusterROC
889 << "NCorrupted=" << nCorrupted
892 if (TestBit(kLabels)) AddLabels();
898 //_____________________________________________________________________________
899 Bool_t AliTRDclusterizer::IsMaximum(const MaxStruct &Max, UChar_t &padStatus, Short_t *const Signals)
902 // Returns true if this row,col,time combination is a maximum.
903 // Gives back the padStatus and the signals of the center pad and the two neighbouring pads.
906 Float_t gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col,Max.row);
907 Signals[1] = (Short_t)((fDigits->GetData(Max.row, Max.col, Max.time) - fBaseline) / gain + 0.5f);
908 if(Signals[1] <= fMaxThresh) return kFALSE;
910 Short_t noiseMiddleThresh = (Short_t)(fMinMaxCutSigma*fCalNoiseDetValue*fCalNoiseROC->GetValue(Max.col, Max.row));
911 if (Signals[1] <= noiseMiddleThresh) return kFALSE;
913 if (Max.col + 1 >= fColMax || Max.col < 1) return kFALSE;
916 fCalPadStatusROC->GetStatus(Max.col-1, Max.row)
917 ,fCalPadStatusROC->GetStatus(Max.col, Max.row)
918 ,fCalPadStatusROC->GetStatus(Max.col+1, Max.row)
921 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col-1,Max.row);
922 Signals[0] = (Short_t)((fDigits->GetData(Max.row, Max.col-1, Max.time) - fBaseline) / gain + 0.5f);
923 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col+1,Max.row);
924 Signals[2] = (Short_t)((fDigits->GetData(Max.row, Max.col+1, Max.time) - fBaseline) / gain + 0.5f);
926 if(!(status[0] | status[1] | status[2])) {//all pads are good
927 if ((Signals[2] <= Signals[1]) && (Signals[0] < Signals[1])) {
928 if ((Signals[2] > fSigThresh) || (Signals[0] > fSigThresh)) {
929 if(Signals[0]<0)Signals[0]=0;
930 if(Signals[2]<0)Signals[2]=0;
931 Short_t noiseSumThresh = (Short_t)(fMinLeftRightCutSigma * fCalNoiseDetValue
932 * fCalNoiseROC->GetValue(Max.col, Max.row));
933 if ((Signals[2]+Signals[0]+Signals[1]) <= noiseSumThresh) return kFALSE;
938 } else { // at least one of the pads is bad, and reject candidates with more than 1 problematic pad
939 if(Signals[0]<0)Signals[0]=0;
940 if(Signals[2]<0)Signals[2]=0;
941 if (status[2] && (!(status[0] || status[1])) && Signals[1] > Signals[0] && Signals[0] > fSigThresh) {
943 SetPadStatus(status[2], padStatus);
946 else if (status[0] && (!(status[1] || status[2])) && Signals[1] >= Signals[2] && Signals[2] > fSigThresh) {
948 SetPadStatus(status[0], padStatus);
951 else if (status[1] && (!(status[0] || status[2])) && ((Signals[2] > fSigThresh) || (Signals[0] > fSigThresh))) {
952 Signals[1] = fMaxThresh;
953 SetPadStatus(status[1], padStatus);
960 //_____________________________________________________________________________
961 Bool_t AliTRDclusterizer::FivePadCluster(MaxStruct &ThisMax, MaxStruct &NeighbourMax)
964 // Look for 5 pad cluster with minimum in the middle
965 // Gives back the ratio
968 if (ThisMax.col >= fColMax - 3) return kFALSE;
970 if (ThisMax.col < fColMax - 5){
971 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(ThisMax.col+4,ThisMax.row);
972 if (fDigits->GetData(ThisMax.row, ThisMax.col+4, ThisMax.time) - fBaseline >= fSigThresh * gain)
975 if (ThisMax.col > 1) {
976 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(ThisMax.col-2,ThisMax.row);
977 if (fDigits->GetData(ThisMax.row, ThisMax.col-2, ThisMax.time) - fBaseline >= fSigThresh * gain)
981 const Float_t kEpsilon = 0.01;
982 Double_t padSignal[5] = {ThisMax.signals[0], ThisMax.signals[1], ThisMax.signals[2],
983 NeighbourMax.signals[1], NeighbourMax.signals[2]};
985 // Unfold the two maxima and set the signal on
986 // the overlapping pad to the ratio
987 Float_t ratio = Unfold(kEpsilon,fLayer,padSignal);
988 ThisMax.signals[2] = (Short_t)(ThisMax.signals[2]*ratio + 0.5f);
989 NeighbourMax.signals[0] = (Short_t)(NeighbourMax.signals[0]*(1-ratio) + 0.5f);
990 ThisMax.fivePad=kTRUE;
991 NeighbourMax.fivePad=kTRUE;
996 //_____________________________________________________________________________
997 void AliTRDclusterizer::CreateCluster(const MaxStruct &Max)
1000 // Creates a cluster at the given position and saves it in fRecPoints
1003 Int_t nPadCount = 1;
1004 Short_t signals[7] = { 0, 0, Max.signals[0], Max.signals[1], Max.signals[2], 0, 0 };
1005 if(!TestBit(kHLT)) CalcAdditionalInfo(Max, signals, nPadCount);
1007 AliTRDcluster cluster(fDet, ((UChar_t) Max.col), ((UChar_t) Max.row), ((UChar_t) Max.time), signals, fVolid);
1008 cluster.SetNPads(nPadCount);
1009 if(TestBit(kLUT)) cluster.SetRPhiMethod(AliTRDcluster::kLUT);
1010 else if(TestBit(kGAUS)) cluster.SetRPhiMethod(AliTRDcluster::kGAUS);
1011 else cluster.SetRPhiMethod(AliTRDcluster::kCOG);
1013 cluster.SetFivePad(Max.fivePad);
1014 // set pads status for the cluster
1015 UChar_t maskPosition = GetCorruption(Max.padStatus);
1017 cluster.SetPadMaskedPosition(maskPosition);
1018 cluster.SetPadMaskedStatus(GetPadStatus(Max.padStatus));
1021 // Transform the local cluster coordinates into calibrated
1022 // space point positions defined in the local tracking system.
1023 // Here the calibration for T0, Vdrift and ExB is applied as well.
1024 if(!fTransform->Transform(&cluster)) return;
1025 // Temporarily store the Max.Row, column and time bin of the center pad
1026 // Used to later on assign the track indices
1027 cluster.SetLabel(Max.row, 0);
1028 cluster.SetLabel(Max.col, 1);
1029 cluster.SetLabel(Max.time,2);
1031 //needed for HLT reconstruction
1032 AddClusterToArray(&cluster);
1034 // Store the index of the first cluster in the current ROC
1035 if (firstClusterROC < 0) firstClusterROC = fNoOfClusters;
1041 //_____________________________________________________________________________
1042 void AliTRDclusterizer::CalcAdditionalInfo(const MaxStruct &Max, Short_t *const signals, Int_t &nPadCount)
1044 // Look to the right
1046 while (fDigits->GetData(Max.row, Max.col-ii, Max.time) >= fSigThresh) {
1049 if (Max.col < ii) break;
1053 while (fDigits->GetData(Max.row, Max.col+ii, Max.time) >= fSigThresh) {
1056 if (Max.col+ii >= fColMax) break;
1059 // Store the amplitudes of the pads in the cluster for later analysis
1060 // and check whether one of these pads is masked in the database
1061 signals[2]=Max.signals[0];
1062 signals[3]=Max.signals[1];
1063 signals[4]=Max.signals[2];
1065 for(Int_t i = 0; i<2; i++)
1068 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col-3+i,Max.row);
1069 signals[i] = (Short_t)((fDigits->GetData(Max.row, Max.col-3+i, Max.time) - fBaseline) / gain + 0.5f);
1071 if(Max.col+3-i < fColMax){
1072 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col+3-i,Max.row);
1073 signals[6-i] = (Short_t)((fDigits->GetData(Max.row, Max.col+3-i, Max.time) - fBaseline) / gain + 0.5f);
1076 /*for (Int_t jPad = Max.Col-3; jPad <= Max.Col+3; jPad++) {
1077 if ((jPad >= 0) && (jPad < fColMax))
1078 signals[jPad-Max.Col+3] = TMath::Nint(fDigits->GetData(Max.Row,jPad,Max.Time));
1082 //_____________________________________________________________________________
1083 void AliTRDclusterizer::AddClusterToArray(AliTRDcluster* cluster)
1086 // Add a cluster to the array
1089 Int_t n = RecPoints()->GetEntriesFast();
1090 if(n!=fNoOfClusters)AliError(Form("fNoOfClusters != RecPoints()->GetEntriesFast %i != %i \n", fNoOfClusters, n));
1091 new((*RecPoints())[n]) AliTRDcluster(*cluster);
1094 //_____________________________________________________________________________
1095 void AliTRDclusterizer::AddTrackletsToArray()
1098 // Add the online tracklets of this chamber to the array
1101 UInt_t* trackletword;
1102 for(Int_t side=0; side<2; side++)
1105 trackletword=fTrackletContainer[side];
1106 while(trackletword[trkl]>0){
1107 Int_t n = TrackletsArray()->GetEntriesFast();
1108 AliTRDtrackletWord tmp(trackletword[trkl]);
1109 new((*TrackletsArray())[n]) AliTRDcluster(&tmp,fDet,fVolid);
1115 //_____________________________________________________________________________
1116 Bool_t AliTRDclusterizer::AddLabels()
1119 // Add the track indices to the found clusters
1122 const Int_t kNclus = 3;
1123 const Int_t kNdict = AliTRDdigitsManager::kNDict;
1124 const Int_t kNtrack = kNdict * kNclus;
1126 Int_t iClusterROC = 0;
1133 // Temporary array to collect the track indices
1134 Int_t *idxTracks = new Int_t[kNtrack*fClusterROC];
1136 // Loop through the dictionary arrays one-by-one
1137 // to keep memory consumption low
1138 AliTRDarrayDictionary *tracksIn = 0; //mod
1139 for (Int_t iDict = 0; iDict < kNdict; iDict++) {
1141 // tracksIn should be expanded beforehand!
1142 tracksIn = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(fDet,iDict);
1144 // Loop though the clusters found in this ROC
1145 for (iClusterROC = 0; iClusterROC < fClusterROC; iClusterROC++) {
1147 AliTRDcluster *cluster = (AliTRDcluster *)
1148 RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
1149 row = cluster->GetLabel(0);
1150 col = cluster->GetLabel(1);
1151 time = cluster->GetLabel(2);
1153 for (iPad = 0; iPad < kNclus; iPad++) {
1154 Int_t iPadCol = col - 1 + iPad;
1155 Int_t index = tracksIn->GetData(row,iPadCol,time); //Modification of -1 in Track
1156 idxTracks[3*iPad+iDict + iClusterROC*kNtrack] = index;
1163 // Copy the track indices into the cluster
1164 // Loop though the clusters found in this ROC
1165 for (iClusterROC = 0; iClusterROC < fClusterROC; iClusterROC++) {
1167 AliTRDcluster *cluster = (AliTRDcluster *)
1168 RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
1169 cluster->SetLabel(-9999,0);
1170 cluster->SetLabel(-9999,1);
1171 cluster->SetLabel(-9999,2);
1173 cluster->AddTrackIndex(&idxTracks[iClusterROC*kNtrack]);
1177 delete [] idxTracks;
1183 //_____________________________________________________________________________
1184 Float_t AliTRDclusterizer::Unfold(Double_t eps, Int_t layer, const Double_t *const padSignal) const
1187 // Method to unfold neighbouring maxima.
1188 // The charge ratio on the overlapping pad is calculated
1189 // until there is no more change within the range given by eps.
1190 // The resulting ratio is then returned to the calling method.
1193 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
1195 AliError("No AliTRDcalibDB instance available\n");
1200 Int_t itStep = 0; // Count iteration steps
1202 Double_t ratio = 0.5; // Start value for ratio
1203 Double_t prevRatio = 0.0; // Store previous ratio
1205 Double_t newLeftSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store left cluster signal
1206 Double_t newRightSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store right cluster signal
1207 Double_t newSignal[3] = { 0.0, 0.0, 0.0 };
1209 // Start the iteration
1210 while ((TMath::Abs(prevRatio - ratio) > eps) && (itStep < 10)) {
1215 // Cluster position according to charge ratio
1216 Double_t maxLeft = (ratio*padSignal[2] - padSignal[0])
1217 / (padSignal[0] + padSignal[1] + ratio * padSignal[2]);
1218 Double_t maxRight = (padSignal[4] - (1-ratio)*padSignal[2])
1219 / ((1.0 - ratio)*padSignal[2] + padSignal[3] + padSignal[4]);
1221 // Set cluster charge ratio
1222 irc = calibration->PadResponse(1.0, maxLeft, layer, newSignal);
1223 Double_t ampLeft = padSignal[1] / newSignal[1];
1224 irc = calibration->PadResponse(1.0, maxRight, layer, newSignal);
1225 Double_t ampRight = padSignal[3] / newSignal[1];
1227 // Apply pad response to parameters
1228 irc = calibration->PadResponse(ampLeft ,maxLeft ,layer,newLeftSignal );
1229 irc = calibration->PadResponse(ampRight,maxRight,layer,newRightSignal);
1231 // Calculate new overlapping ratio
1232 ratio = TMath::Min((Double_t) 1.0
1233 ,newLeftSignal[2] / (newLeftSignal[2] + newRightSignal[0]));
1241 //_____________________________________________________________________________
1242 void AliTRDclusterizer::TailCancelation(const AliTRDrecoParam* const recoParam)
1245 // Applies the tail cancelation
1252 TTreeSRedirector *fDebugStream = fReconstructor->GetDebugStream(AliTRDrecoParam::kClusterizer);
1253 Bool_t debugStreaming = recoParam->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 7 && fReconstructor->IsDebugStreaming();
1254 Int_t nexp = recoParam->GetTCnexp();
1255 while(fIndexes->NextRCIndex(iRow, iCol))
1257 // if corrupted then don't make the tail cancallation
1258 if (fCalPadStatusROC->GetStatus(iCol, iRow)) continue;
1261 for (iTime = 0; iTime < fTimeTotal; iTime++)
1262 (*fDebugStream) << "TailCancellation"
1268 // Apply the tail cancelation via the digital filter
1269 DeConvExp(fDigits->GetDataAddress(iRow,iCol),fTimeTotal,nexp);
1271 } // while irow icol
1277 //_____________________________________________________________________________
1278 void AliTRDclusterizer::DeConvExp(Short_t *const arr, const Int_t nTime, const Int_t nexp)
1281 // Tail cancellation by deconvolution for PASA v4 TRF
1285 Float_t coefficients[2];
1287 // Initialization (coefficient = alpha, rates = lambda)
1293 if (nexp == 1) { // 1 Exponentials
1299 if (nexp == 2) { // 2 Exponentials
1301 fReconstructor->GetRecoParam()->GetTCParams(par);
1302 r1 = par[0];//1.156;
1303 r2 = par[1];//0.130;
1304 c1 = par[2];//0.114;
1305 c2 = par[3];//0.624;
1308 coefficients[0] = c1;
1309 coefficients[1] = c2;
1313 rates[0] = TMath::Exp(-dt/(r1));
1314 rates[1] = TMath::Exp(-dt/(r2));
1319 Float_t reminder[2];
1320 Float_t correction = 0.0;
1321 Float_t result = 0.0;
1323 // Attention: computation order is important
1324 for (k = 0; k < nexp; k++) {
1328 for (i = 0; i < nTime; i++) {
1330 result = arr[i] - correction - fBaseline; // No rescaling
1331 arr[i] = (Short_t)(result + fBaseline + 0.5f);
1333 for (k = 0; k < nexp; k++) {
1334 reminder[k] = rates[k] * (reminder[k] + coefficients[k] * result);
1338 for (k = 0; k < nexp; k++) {
1339 correction += reminder[k];
1346 //_____________________________________________________________________________
1347 void AliTRDclusterizer::ResetRecPoints()
1350 // Resets the list of rec points
1354 fRecPoints->Clear();
1356 // delete fRecPoints;
1360 //_____________________________________________________________________________
1361 TClonesArray *AliTRDclusterizer::RecPoints()
1364 // Returns the list of rec points
1368 if(!(fRecPoints = AliTRDReconstructor::GetClusters())){
1369 // determine number of clusters which has to be allocated
1370 Float_t nclusters = fReconstructor->GetRecoParam()->GetNClusters();
1372 fRecPoints = new TClonesArray("AliTRDcluster", Int_t(nclusters));
1374 //SetClustersOwner(kTRUE);
1375 AliTRDReconstructor::SetClusters(0x0);
1381 //_____________________________________________________________________________
1382 TClonesArray *AliTRDclusterizer::TrackletsArray()
1385 // Returns the list of rec points
1388 if (!fTracklets && fReconstructor->IsProcessingTracklets()) {
1389 fTracklets = new TClonesArray("AliTRDcluster", 2*MAXTRACKLETSPERHC);
1390 //SetClustersOwner(kTRUE);
1391 //AliTRDReconstructor::SetTracklets(0x0);