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 "AliTRDrawStream.h"
45 #include "AliTRDfeeParam.h"
46 #include "AliTRDtrackletWord.h"
47 #include "AliTRDtrackletMCM.h"
48 #include "AliTRDtrackGTU.h"
49 #include "AliESDTrdTrack.h"
51 #include "TTreeStream.h"
53 #include "Cal/AliTRDCalROC.h"
54 #include "Cal/AliTRDCalDet.h"
55 #include "Cal/AliTRDCalSingleChamberStatus.h"
56 #include "Cal/AliTRDCalOnlineGainTableROC.h"
58 ClassImp(AliTRDclusterizer)
60 //_____________________________________________________________________________
61 AliTRDclusterizer::AliTRDclusterizer(const AliTRDReconstructor *const rec)
70 ,fDigitsManager(new AliTRDdigitsManager())
71 ,fTrackletContainer(NULL)
73 ,fTransform(new AliTRDtransform(0))
80 ,fMinLeftRightCutSigma(0)
86 ,fCalGainFactorROC(NULL)
87 ,fCalGainFactorDetValue(0)
90 ,fCalPadStatusROC(NULL)
99 // AliTRDclusterizer default constructor
102 SetBit(kLabels, kTRUE);
103 SetBit(knewDM, kFALSE);
105 fRawVersion = AliTRDfeeParam::Instance()->GetRAWversion();
107 // Initialize debug stream
109 if(fReconstructor->GetRecoParam()->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 1){
110 TDirectory *savedir = gDirectory;
111 //fgGetDebugStream = new TTreeSRedirector("TRD.ClusterizerDebug.root");
118 //_____________________________________________________________________________
119 AliTRDclusterizer::AliTRDclusterizer(const Text_t *name
120 , const Text_t *title
121 , const AliTRDReconstructor *const rec)
130 ,fDigitsManager(new AliTRDdigitsManager())
131 ,fTrackletContainer(NULL)
133 ,fTransform(new AliTRDtransform(0))
140 ,fMinLeftRightCutSigma(0)
146 ,fCalGainFactorROC(NULL)
147 ,fCalGainFactorDetValue(0)
149 ,fCalNoiseDetValue(0)
150 ,fCalPadStatusROC(NULL)
159 // AliTRDclusterizer constructor
162 SetBit(kLabels, kTRUE);
163 SetBit(knewDM, kFALSE);
165 fDigitsManager->CreateArrays();
167 fRawVersion = AliTRDfeeParam::Instance()->GetRAWversion();
173 //_____________________________________________________________________________
174 AliTRDclusterizer::AliTRDclusterizer(const AliTRDclusterizer &c)
176 ,fReconstructor(c.fReconstructor)
183 ,fDigitsManager(NULL)
184 ,fTrackletContainer(NULL)
193 ,fMinLeftRightCutSigma(0)
199 ,fCalGainFactorROC(NULL)
200 ,fCalGainFactorDetValue(0)
202 ,fCalNoiseDetValue(0)
203 ,fCalPadStatusROC(NULL)
212 // AliTRDclusterizer copy constructor
215 SetBit(kLabels, kTRUE);
216 SetBit(knewDM, kFALSE);
222 //_____________________________________________________________________________
223 AliTRDclusterizer::~AliTRDclusterizer()
226 // AliTRDclusterizer destructor
229 if (fRecPoints/* && IsClustersOwner()*/){
230 fRecPoints->Delete();
235 fTracklets->Delete();
244 if (fDigitsManager) {
245 delete fDigitsManager;
246 fDigitsManager = 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).fRawVersion = fRawVersion;
289 ((AliTRDclusterizer &) c).fTransform = NULL;
290 ((AliTRDclusterizer &) c).fDigits = NULL;
291 ((AliTRDclusterizer &) c).fIndexes = NULL;
292 ((AliTRDclusterizer &) c).fMaxThresh = 0;
293 ((AliTRDclusterizer &) c).fMaxThreshTest = 0;
294 ((AliTRDclusterizer &) c).fSigThresh = 0;
295 ((AliTRDclusterizer &) c).fMinMaxCutSigma= 0;
296 ((AliTRDclusterizer &) c).fMinLeftRightCutSigma = 0;
297 ((AliTRDclusterizer &) c).fLayer = 0;
298 ((AliTRDclusterizer &) c).fDet = 0;
299 ((AliTRDclusterizer &) c).fVolid = 0;
300 ((AliTRDclusterizer &) c).fColMax = 0;
301 ((AliTRDclusterizer &) c).fTimeTotal = 0;
302 ((AliTRDclusterizer &) c).fCalGainFactorROC = NULL;
303 ((AliTRDclusterizer &) c).fCalGainFactorDetValue = 0;
304 ((AliTRDclusterizer &) c).fCalNoiseROC = NULL;
305 ((AliTRDclusterizer &) c).fCalNoiseDetValue = 0;
306 ((AliTRDclusterizer &) c).fCalPadStatusROC = NULL;
307 ((AliTRDclusterizer &) c).fClusterROC = 0;
308 ((AliTRDclusterizer &) c).firstClusterROC= 0;
309 ((AliTRDclusterizer &) c).fNoOfClusters = 0;
310 ((AliTRDclusterizer &) c).fBaseline = 0;
311 ((AliTRDclusterizer &) c).fRawStream = NULL;
315 //_____________________________________________________________________________
316 Bool_t AliTRDclusterizer::Open(const Char_t *name, Int_t nEvent)
319 // Opens the AliROOT file. Output and input are in the same file
322 TString evfoldname = AliConfig::GetDefaultEventFolderName();
323 fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
326 fRunLoader = AliRunLoader::Open(name);
330 AliError(Form("Can not open session for file %s.",name));
341 //_____________________________________________________________________________
342 Bool_t AliTRDclusterizer::OpenOutput()
345 // Open the output file
348 if (!fReconstructor->IsWritingClusters()) return kTRUE;
350 TObjArray *ioArray = 0x0;
352 AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
353 loader->MakeTree("R");
355 fClusterTree = loader->TreeR();
356 fClusterTree->Branch("TRDcluster", "TObjArray", &ioArray, 32000, 0);
362 //_____________________________________________________________________________
363 Bool_t AliTRDclusterizer::OpenOutput(TTree *const clusterTree)
366 // Connect the output tree
370 if (fReconstructor->IsWritingClusters()){
371 TObjArray *ioArray = 0x0;
372 fClusterTree = clusterTree;
373 fClusterTree->Branch("TRDcluster", "TObjArray", &ioArray, 32000, 0);
378 //_____________________________________________________________________________
379 Bool_t AliTRDclusterizer::OpenInput(Int_t nEvent)
382 // Opens a ROOT-file with TRD-hits and reads in the digits-tree
385 // Import the Trees for the event nEvent in the file
386 fRunLoader->GetEvent(nEvent);
392 //_____________________________________________________________________________
393 Bool_t AliTRDclusterizer::WriteClusters(Int_t det)
396 // Fills TRDcluster branch in the tree with the clusters
397 // found in detector = det. For det=-1 writes the tree.
401 (det >= AliTRDgeometry::Ndet())) {
402 AliError(Form("Unexpected detector index %d.\n",det));
406 TObjArray *ioArray = new TObjArray(400);
407 TBranch *branch = fClusterTree->GetBranch("TRDcluster");
409 fClusterTree->Branch("TRDcluster","TObjArray",&ioArray,32000,0);
410 } else branch->SetAddress(&ioArray);
412 Int_t nRecPoints = RecPoints()->GetEntriesFast();
414 for (Int_t i = 0; i < nRecPoints; i++) {
415 AliTRDcluster *c = (AliTRDcluster *) RecPoints()->UncheckedAt(i);
416 if(det != c->GetDetector()) continue;
419 fClusterTree->Fill();
422 Int_t detOld = -1, nw(0);
423 for (Int_t i = 0; i < nRecPoints; i++) {
424 AliTRDcluster *c = (AliTRDcluster *) RecPoints()->UncheckedAt(i);
425 if(c->GetDetector() != detOld){
426 nw += ioArray->GetEntriesFast();
427 fClusterTree->Fill();
429 detOld = c->GetDetector();
433 if(ioArray->GetEntriesFast()){
434 nw += ioArray->GetEntriesFast();
435 fClusterTree->Fill();
438 AliDebug(2, Form("Clusters FOUND[%d] WRITTEN[%d] STATUS[%s]", nRecPoints, nw, nw==nRecPoints?"OK":"FAILED"));
445 //_____________________________________________________________________________
446 Bool_t AliTRDclusterizer::ReadDigits()
449 // Reads the digits arrays from the input aliroot file
453 AliError("No run loader available");
457 AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
458 if (!loader->TreeD()) {
459 loader->LoadDigits();
462 // Read in the digit arrays
463 return (fDigitsManager->ReadDigits(loader->TreeD()));
467 //_____________________________________________________________________________
468 Bool_t AliTRDclusterizer::ReadDigits(TTree *digitsTree)
471 // Reads the digits arrays from the input tree
474 // Read in the digit arrays
475 return (fDigitsManager->ReadDigits(digitsTree));
479 //_____________________________________________________________________________
480 Bool_t AliTRDclusterizer::ReadDigits(AliRawReader *rawReader)
483 // Reads the digits arrays from the ddl file
487 fDigitsManager = raw.Raw2Digits(rawReader);
493 Bool_t AliTRDclusterizer::ReadTracklets()
496 // Reads simulated tracklets from the input aliroot file
499 AliRunLoader *runLoader = AliRunLoader::Instance();
501 AliError("No run loader available");
505 AliLoader* loader = runLoader->GetLoader("TRDLoader");
507 AliDataLoader *trackletLoader = loader->GetDataLoader("tracklets");
508 if (!trackletLoader) {
512 // simulated tracklets
513 trackletLoader->Load();
514 TTree *trackletTree = trackletLoader->Tree();
517 TBranch *trklbranch = trackletTree->GetBranch("mcmtrklbranch");
518 TClonesArray *trklArray = TrackletsArray("AliTRDtrackletMCM");
519 if (trklbranch && trklArray) {
520 AliTRDtrackletMCM *trkl = 0x0;
521 trklbranch->SetAddress(&trkl);
522 for (Int_t iTracklet = 0; iTracklet < trklbranch->GetEntries(); iTracklet++) {
523 trklbranch->GetEntry(iTracklet);
524 new ((*trklArray)[trklArray->GetEntries()]) AliTRDtrackletMCM(*trkl);
532 Bool_t AliTRDclusterizer::ReadTracks()
535 // Reads simulated GTU tracks from the input aliroot file
538 AliRunLoader *runLoader = AliRunLoader::Instance();
541 AliError("No run loader available");
545 AliLoader* loader = runLoader->GetLoader("TRDLoader");
550 AliDataLoader *trackLoader = loader->GetDataLoader("gtutracks");
557 TTree *trackTree = trackLoader->Tree();
562 TClonesArray *trackArray = TracksArray();
563 AliTRDtrackGTU *trk = 0x0;
564 trackTree->SetBranchAddress("TRDtrackGTU", &trk);
565 for (Int_t iTrack = 0; iTrack < trackTree->GetEntries(); iTrack++) {
566 trackTree->GetEntry(iTrack);
567 new ((*trackArray)[trackArray->GetEntries()]) AliESDTrdTrack(*(trk->CreateTrdTrack()));
573 //_____________________________________________________________________________
574 Bool_t AliTRDclusterizer::MakeClusters()
577 // Creates clusters from digits
580 // Propagate info from the digits manager
581 if (TestBit(kLabels)){
582 SetBit(kLabels, fDigitsManager->UsesDictionaries());
585 Bool_t fReturn = kTRUE;
586 for (Int_t i = 0; i < AliTRDgeometry::kNdet; i++){
588 AliTRDarrayADC *digitsIn = (AliTRDarrayADC*) fDigitsManager->GetDigits(i); //mod
589 // This is to take care of switched off super modules
590 if (!digitsIn->HasData()) continue;
592 digitsIn->DeleteNegatives(); // Restore digits array to >=0 values
593 AliTRDSignalIndex* indexes = fDigitsManager->GetIndexes(i);
594 if (indexes->IsAllocated() == kFALSE){
595 fDigitsManager->BuildIndexes(i);
599 if (indexes->HasEntry()){
600 if (TestBit(kLabels)){
601 for (Int_t iDict = 0; iDict < AliTRDdigitsManager::kNDict; iDict++){
602 AliTRDarrayDictionary *tracksIn = 0; //mod
603 tracksIn = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(i,iDict); //mod
607 fR = MakeClusters(i);
608 fReturn = fR && fReturn;
612 // if(IsWritingClusters()) WriteClusters(i);
616 // Clear arrays of this chamber, to prepare for next event
617 fDigitsManager->ClearArrays(i);
620 if(fReconstructor->IsWritingClusters()) WriteClusters(-1);
622 AliInfo(Form("Number of found clusters : %d", RecPoints()->GetEntriesFast()));
628 //_____________________________________________________________________________
629 Bool_t AliTRDclusterizer::Raw2Clusters(AliRawReader *rawReader)
632 // Creates clusters from raw data
635 return Raw2ClustersChamber(rawReader);
639 //_____________________________________________________________________________
640 Bool_t AliTRDclusterizer::Raw2ClustersChamber(AliRawReader *rawReader)
643 // Creates clusters from raw data
646 // Create the digits manager
647 if (!fDigitsManager){
648 SetBit(knewDM, kTRUE);
649 fDigitsManager = new AliTRDdigitsManager(kTRUE);
650 fDigitsManager->CreateArrays();
653 fDigitsManager->SetUseDictionaries(TestBit(kLabels));
655 // ----- preparing tracklet output -----
656 if (fReconstructor->IsWritingTracklets()) {
657 AliDataLoader *trklLoader = AliRunLoader::Instance()->GetLoader("TRDLoader")->GetDataLoader("tracklets");
659 //AliInfo("Could not get the tracklets data loader, adding it now!");
660 trklLoader = new AliDataLoader("TRD.Tracklets.root","tracklets", "tracklets");
661 AliRunLoader::Instance()->GetLoader("TRDLoader")->AddDataLoader(trklLoader);
663 AliTreeLoader *trklTreeLoader = dynamic_cast<AliTreeLoader*> (trklLoader->GetBaseLoader("tracklets-raw"));
664 if (!trklTreeLoader) {
665 trklTreeLoader = new AliTreeLoader("tracklets-raw", trklLoader);
666 trklLoader->AddBaseLoader(trklTreeLoader);
668 if (!trklTreeLoader->Tree())
669 trklTreeLoader->MakeTree();
673 fRawStream = new AliTRDrawStream(rawReader);
675 fRawStream->SetReader(rawReader);
677 //if(fReconstructor->IsHLT()){
678 fRawStream->DisableErrorStorage();
681 // register tracklet array for output
682 fRawStream->SetTrackletArray(TrackletsArray("AliTRDtrackletMCM"));
683 fRawStream->SetTrackArray(TracksArray());
686 while ((det = fRawStream->NextChamber(fDigitsManager)) < AliTRDgeometry::kNdet){
687 if (fDigitsManager->GetIndexes(det)->HasEntry()){
689 fDigitsManager->ClearArrays(det);
693 if (fReconstructor->IsWritingTracklets()) {
694 if (AliDataLoader *trklLoader = AliRunLoader::Instance()->GetLoader("TRDLoader")->GetDataLoader("tracklets")) {
696 if (AliTreeLoader *trklTreeLoader = (AliTreeLoader*) trklLoader->GetBaseLoader("tracklets-raw"))
697 trklTreeLoader->WriteData("OVERWRITE");
698 trklLoader->UnloadAll();
703 if(fReconstructor->IsWritingClusters()) WriteClusters(-1);
705 if(!TestBit(knewDM)){
706 delete fDigitsManager;
707 fDigitsManager = NULL;
712 AliInfo(Form("Number of found clusters : %d", fNoOfClusters));
717 //_____________________________________________________________________________
718 UChar_t AliTRDclusterizer::GetStatus(Short_t &signal)
721 // Check if a pad is masked
726 if(signal>0 && TESTBIT(signal, 10)){
728 for(int ibit=0; ibit<4; ibit++){
729 if(TESTBIT(signal, 11+ibit)){
730 SETBIT(status, ibit);
731 CLRBIT(signal, 11+ibit);
738 //_____________________________________________________________________________
739 void AliTRDclusterizer::SetPadStatus(const UChar_t status, UChar_t &out) const {
741 // Set the pad status into out
742 // First three bits are needed for the position encoding
747 //_____________________________________________________________________________
748 UChar_t AliTRDclusterizer::GetPadStatus(UChar_t encoding) const {
750 // return the staus encoding of the corrupted pad
752 return static_cast<UChar_t>(encoding >> 3);
755 //_____________________________________________________________________________
756 Int_t AliTRDclusterizer::GetCorruption(UChar_t encoding) const {
758 // Return the position of the corruption
763 //_____________________________________________________________________________
764 Bool_t AliTRDclusterizer::MakeClusters(Int_t det)
767 // Generates the cluster.
771 fDigits = (AliTRDarrayADC *) fDigitsManager->GetDigits(det); //mod
772 fBaseline = fDigitsManager->GetDigitsParam()->GetADCbaseline(det);
774 // This is to take care of switched off super modules
775 if (!fDigits->HasData()) return kFALSE;
777 fIndexes = fDigitsManager->GetIndexes(det);
778 if (fIndexes->IsAllocated() == kFALSE) {
779 AliError("Indexes do not exist!");
783 AliTRDcalibDB* const calibration = AliTRDcalibDB::Instance();
785 AliFatal("No AliTRDcalibDB instance available\n");
789 if (!fReconstructor){
790 AliError("Reconstructor not set\n");
794 const AliTRDrecoParam *const recoParam = fReconstructor->GetRecoParam();
796 fMaxThresh = (Short_t)recoParam->GetClusMaxThresh();
797 fMaxThreshTest = (Short_t)(recoParam->GetClusMaxThresh()/2+fBaseline);
798 fSigThresh = (Short_t)recoParam->GetClusSigThresh();
799 fMinMaxCutSigma = recoParam->GetMinMaxCutSigma();
800 fMinLeftRightCutSigma = recoParam->GetMinLeftRightCutSigma();
801 const Int_t iEveryNTB = recoParam->GetRecEveryNTB();
803 Int_t istack = fIndexes->GetStack();
804 fLayer = fIndexes->GetLayer();
805 Int_t isector = fIndexes->GetSM();
807 // Start clustering in the chamber
809 fDet = AliTRDgeometry::GetDetector(fLayer,istack,isector);
811 AliError(Form("Detector number missmatch! Request[%03d] RAW[%03d]", det, fDet));
815 AliDebug(2, Form("Det[%d] @ Sec[%d] Stk[%d] Ly[%d]", fDet, isector, istack, fLayer));
817 // TRD space point transformation
818 fTransform->SetDetector(det);
820 Int_t iGeoLayer = AliGeomManager::kTRD1 + fLayer;
821 Int_t iGeoModule = istack + AliTRDgeometry::Nstack() * isector;
822 fVolid = AliGeomManager::LayerToVolUID(iGeoLayer,iGeoModule);
824 fColMax = fDigits->GetNcol();
825 fTimeTotal = fDigitsManager->GetDigitsParam()->GetNTimeBins(det);
827 // Check consistency between Geometry and raw data
828 AliTRDpadPlane *pp(fTransform->GetPadPlane());
829 Int_t ncols(pp->GetNcols()), nrows(pp->GetNrows());
830 if(ncols != fColMax) AliDebug(1, Form("N cols missmatch in Digits for Det[%3d] :: Geom[%3d] RAW[%3d]", fDet, ncols, fColMax));
831 if(nrows != fDigits->GetNrow()) AliDebug(1, Form("N rows missmatch in Digits for Det[%3d] :: Geom[%3d] RAW[%3d]", fDet, nrows, fDigits->GetNrow()));
832 if(ncols != fIndexes->GetNcol()) AliDebug(1, Form("N cols missmatch in Digits for Det[%3d] :: Geom[%3d] RAW[%3d]", fDet, ncols, fIndexes->GetNcol()));
833 if(nrows != fIndexes->GetNrow()) AliDebug(1, Form("N rows missmatch in Digits for Det[%3d] :: Geom[%3d] RAW[%3d]", fDet, nrows, fIndexes->GetNrow()));
835 // Check consistency between OCDB and raw data
836 Int_t nTimeOCDB = calibration->GetNumberOfTimeBinsDCS();
837 if(fReconstructor->IsHLT()){
838 if((nTimeOCDB > -1) && (fTimeTotal != nTimeOCDB)){
839 AliWarning(Form("Number of timebins does not match OCDB value (RAW[%d] OCDB[%d]), using raw value"
840 ,fTimeTotal,nTimeOCDB));
844 AliWarning("Undefined number of timebins in OCDB, using value from raw data.");
846 AliError(Form("Number of timebins in raw data is negative, skipping chamber[%3d]!", fDet));
849 }else if(nTimeOCDB == -2){
850 AliError("Mixed number of timebins in OCDB, no reconstruction of TRD data!");
852 }else if(fTimeTotal != nTimeOCDB){
853 AliError(Form("Number of timebins in raw data does not match OCDB value (RAW[%d] OCDB[%d]), skipping chamber[%3d]!"
854 ,fTimeTotal,nTimeOCDB, fDet));
859 // Detector wise calibration object for the gain factors
860 const AliTRDCalDet *calGainFactorDet = calibration->GetGainFactorDet();
861 // Calibration object with pad wise values for the gain factors
862 fCalGainFactorROC = calibration->GetGainFactorROC(fDet);
863 // Calibration value for chamber wise gain factor
864 fCalGainFactorDetValue = calGainFactorDet->GetValue(fDet);
866 // Detector wise calibration object for the noise
867 const AliTRDCalDet *calNoiseDet = calibration->GetNoiseDet();
868 // Calibration object with pad wise values for the noise
869 fCalNoiseROC = calibration->GetNoiseROC(fDet);
870 // Calibration value for chamber wise noise
871 fCalNoiseDetValue = calNoiseDet->GetValue(fDet);
873 // Calibration object with the pad status
874 fCalPadStatusROC = calibration->GetPadStatusROC(fDet);
875 // Calibration object of the online gain
876 fCalOnGainROC = calibration->GetOnlineGainTableROC(fDet);
878 firstClusterROC = -1;
881 SetBit(kLUT, recoParam->UseLUT());
882 SetBit(kGAUS, recoParam->UseGAUS());
884 // Apply the gain and the tail cancelation via digital filter
885 // Use the configuration from the DCS to find out whether online
886 // tail cancellation was applied
887 if(!calibration->HasOnlineTailCancellation()) TailCancelation(recoParam);
889 MaxStruct curr, last;
890 Int_t nMaximas = 0, nCorrupted = 0;
892 // Here the clusterfining is happening
894 for(curr.time = 0; curr.time < fTimeTotal; curr.time+=iEveryNTB){
895 while(fIndexes->NextRCIndex(curr.row, curr.col)){
896 if(fDigits->GetData(curr.row, curr.col, curr.time) > fMaxThreshTest && IsMaximum(curr, curr.padStatus, &curr.signals[0])){
898 if(curr.col==last.col+2 && curr.row==last.row && curr.time==last.time) FivePadCluster(last, curr);
901 last=curr; curr.fivePad=kFALSE;
905 if(last.row>-1) CreateCluster(last);
907 if(recoParam->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 2 && fReconstructor->IsDebugStreaming()){
908 TTreeSRedirector* fDebugStream = fReconstructor->GetDebugStream(AliTRDrecoParam::kClusterizer);
909 (*fDebugStream) << "MakeClusters"
910 << "Detector=" << det
911 << "NMaxima=" << nMaximas
912 << "NClusters=" << fClusterROC
913 << "NCorrupted=" << nCorrupted
916 // if (TestBit(kLabels)) AddLabels();
922 //_____________________________________________________________________________
923 Bool_t AliTRDclusterizer::IsMaximum(const MaxStruct &Max, UChar_t &padStatus, Short_t *const Signals)
926 // Returns true if this row,col,time combination is a maximum.
927 // Gives back the padStatus and the signals of the center pad and the two neighbouring pads.
930 Float_t gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col,Max.row);
931 Float_t ongain = fCalOnGainROC ? fCalOnGainROC->GetGainCorrectionFactor(Max.row,Max.col) : 1;
932 Signals[1] = (Short_t)((fDigits->GetData(Max.row, Max.col, Max.time) - fBaseline) * ongain / gain + 0.5f);
933 if(Signals[1] <= fMaxThresh) return kFALSE;
935 if(Max.col < 1 || Max.col + 1 >= fColMax) return kFALSE;
937 Short_t noiseMiddleThresh = (Short_t)(fMinMaxCutSigma*fCalNoiseDetValue*fCalNoiseROC->GetValue(Max.col, Max.row));
938 if (Signals[1] <= noiseMiddleThresh) return kFALSE;
941 fCalPadStatusROC->GetStatus(Max.col-1, Max.row)
942 ,fCalPadStatusROC->GetStatus(Max.col, Max.row)
943 ,fCalPadStatusROC->GetStatus(Max.col+1, Max.row)
947 if((signal = fDigits->GetData(Max.row, Max.col-1, Max.time))){
948 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col-1,Max.row);
949 ongain = fCalOnGainROC ? fCalOnGainROC->GetGainCorrectionFactor(Max.row,Max.col-1) : 1;
950 Signals[0] = (Short_t)((signal - fBaseline) * ongain / gain + 0.5f);
951 } else Signals[0] = 0;
952 if((signal = fDigits->GetData(Max.row, Max.col+1, Max.time))){
953 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col+1,Max.row);
954 ongain = fCalOnGainROC ? fCalOnGainROC->GetGainCorrectionFactor(Max.row,Max.col+1) : 1;
955 Signals[2] = (Short_t)((signal - fBaseline) * ongain / gain + 0.5f);
956 } else Signals[2] = 0;
958 if(!(status[0] | status[1] | status[2])) {//all pads are good
959 if ((Signals[2] <= Signals[1]) && (Signals[0] < Signals[1])) {
960 if ((Signals[2] > fSigThresh) || (Signals[0] > fSigThresh)) {
961 if(Signals[0]<0)Signals[0]=0;
962 if(Signals[2]<0)Signals[2]=0;
963 Short_t noiseSumThresh = (Short_t)(fMinLeftRightCutSigma * fCalNoiseDetValue
964 * fCalNoiseROC->GetValue(Max.col, Max.row));
965 if ((Signals[2]+Signals[0]+Signals[1]) <= noiseSumThresh) return kFALSE;
970 } else { // at least one of the pads is bad, and reject candidates with more than 1 problematic pad
971 if(Signals[0]<0)Signals[0]=0;
972 if(Signals[2]<0)Signals[2]=0;
973 if (status[2] && (!(status[0] || status[1])) && Signals[1] > Signals[0] && Signals[0] > fSigThresh) {
975 SetPadStatus(status[2], padStatus);
978 else if (status[0] && (!(status[1] || status[2])) && Signals[1] >= Signals[2] && Signals[2] > fSigThresh) {
980 SetPadStatus(status[0], padStatus);
983 else if (status[1] && (!(status[0] || status[2])) && ((Signals[2] > fSigThresh) || (Signals[0] > fSigThresh))) {
984 Signals[1] = fMaxThresh;
985 SetPadStatus(status[1], padStatus);
992 //_____________________________________________________________________________
993 Bool_t AliTRDclusterizer::FivePadCluster(MaxStruct &ThisMax, MaxStruct &NeighbourMax)
996 // Look for 5 pad cluster with minimum in the middle
997 // Gives back the ratio
1000 if (ThisMax.col >= fColMax - 3) return kFALSE;
1002 if (ThisMax.col < fColMax - 5){
1003 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(ThisMax.col+4,ThisMax.row);
1004 if (fDigits->GetData(ThisMax.row, ThisMax.col+4, ThisMax.time) - fBaseline >= fSigThresh * gain)
1007 if (ThisMax.col > 1) {
1008 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(ThisMax.col-2,ThisMax.row);
1009 if (fDigits->GetData(ThisMax.row, ThisMax.col-2, ThisMax.time) - fBaseline >= fSigThresh * gain)
1013 const Float_t kEpsilon = 0.01;
1014 Double_t padSignal[5] = {ThisMax.signals[0], ThisMax.signals[1], ThisMax.signals[2],
1015 NeighbourMax.signals[1], NeighbourMax.signals[2]};
1017 // Unfold the two maxima and set the signal on
1018 // the overlapping pad to the ratio
1019 Float_t ratio = Unfold(kEpsilon,fLayer,padSignal);
1020 ThisMax.signals[2] = (Short_t)(ThisMax.signals[2]*ratio + 0.5f);
1021 NeighbourMax.signals[0] = (Short_t)(NeighbourMax.signals[0]*(1-ratio) + 0.5f);
1022 ThisMax.fivePad=kTRUE;
1023 NeighbourMax.fivePad=kTRUE;
1028 //_____________________________________________________________________________
1029 void AliTRDclusterizer::CreateCluster(const MaxStruct &Max)
1032 // Creates a cluster at the given position and saves it in fRecPoints
1035 Int_t nPadCount = 1;
1036 Short_t signals[7] = { 0, 0, Max.signals[0], Max.signals[1], Max.signals[2], 0, 0 };
1037 if(!fReconstructor->IsHLT()) CalcAdditionalInfo(Max, signals, nPadCount);
1039 AliTRDcluster cluster(fDet, ((UChar_t) Max.col), ((UChar_t) Max.row), ((UChar_t) Max.time), signals, fVolid);
1040 cluster.SetNPads(nPadCount);
1041 if(TestBit(kLUT)) cluster.SetRPhiMethod(AliTRDcluster::kLUT);
1042 else if(TestBit(kGAUS)) cluster.SetRPhiMethod(AliTRDcluster::kGAUS);
1043 else cluster.SetRPhiMethod(AliTRDcluster::kCOG);
1045 cluster.SetFivePad(Max.fivePad);
1046 // set pads status for the cluster
1047 UChar_t maskPosition = GetCorruption(Max.padStatus);
1049 cluster.SetPadMaskedPosition(maskPosition);
1050 cluster.SetPadMaskedStatus(GetPadStatus(Max.padStatus));
1052 cluster.SetXcorr(fReconstructor->UseClusterRadialCorrection());
1054 // Transform the local cluster coordinates into calibrated
1055 // space point positions defined in the local tracking system.
1056 // Here the calibration for T0, Vdrift and ExB is applied as well.
1057 if(!TestBit(kSkipTrafo)) if(!fTransform->Transform(&cluster)) return;
1059 // Temporarily store the Max.Row, column and time bin of the center pad
1060 // Used to later on assign the track indices
1061 cluster.SetLabel(Max.row, 0);
1062 cluster.SetLabel(Max.col, 1);
1063 cluster.SetLabel(Max.time,2);
1065 //needed for HLT reconstruction
1066 AddClusterToArray(&cluster);
1068 // Store the index of the first cluster in the current ROC
1069 if (firstClusterROC < 0) firstClusterROC = fNoOfClusters;
1075 //_____________________________________________________________________________
1076 void AliTRDclusterizer::CalcAdditionalInfo(const MaxStruct &Max, Short_t *const signals, Int_t &nPadCount)
1078 // Calculate number of pads/cluster and
1079 // ADC signals at position 0, 1, 5 and 6
1081 Float_t tmp(0.), kMaxShortVal(32767.); // protect against data overflow due to wrong gain calibration
1082 Float_t gain(1.); Short_t signal(0);
1083 // Store the amplitudes of the pads in the cluster for later analysis
1084 // and check whether one of these pads is masked in the database
1085 signals[3]=Max.signals[1];
1086 Int_t ipad(1), jpad(0);
1087 // Look to the right
1088 while((jpad = Max.col-ipad)){
1089 if(!(signal = fDigits->GetData(Max.row, jpad, Max.time))) break; // empty digit !
1090 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(jpad, Max.row);
1091 tmp = (signal - fBaseline) / gain + 0.5f;
1092 signal = (Short_t)TMath::Min(tmp, kMaxShortVal);
1093 if(signal<fSigThresh) break; // signal under threshold
1095 if(ipad<=3) signals[3 - ipad] = signal;
1100 while((jpad = Max.col+ipad)<fColMax){
1101 if(!(signal = fDigits->GetData(Max.row, jpad, Max.time))) break; // empty digit !
1102 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(jpad, Max.row);
1103 tmp = (signal - fBaseline) / gain + 0.5f;
1104 signal = (Short_t)TMath::Min(tmp, kMaxShortVal);
1105 if(signal<fSigThresh) break; // signal under threshold
1107 if(ipad<=3) signals[3 + ipad] = signal;
1111 AliDebug(4, Form("Signals[%3d %3d %3d %3d %3d %3d %3d] Npads[%d]."
1112 , signals[0], signals[1], signals[2], signals[3], signals[4], signals[5], signals[6], nPadCount));
1115 //_____________________________________________________________________________
1116 void AliTRDclusterizer::AddClusterToArray(AliTRDcluster* cluster)
1119 // Add a cluster to the array
1122 Int_t n = RecPoints()->GetEntriesFast();
1123 if(n!=fNoOfClusters)AliError(Form("fNoOfClusters != RecPoints()->GetEntriesFast %i != %i \n", fNoOfClusters, n));
1124 new((*RecPoints())[n]) AliTRDcluster(*cluster);
1127 //_____________________________________________________________________________
1128 Bool_t AliTRDclusterizer::AddLabels()
1131 // Add the track indices to the found clusters
1134 const Int_t kNclus = 3;
1135 const Int_t kNdict = AliTRDdigitsManager::kNDict;
1136 const Int_t kNtrack = kNdict * kNclus;
1138 Int_t iClusterROC = 0;
1145 // Temporary array to collect the track indices
1146 Int_t *idxTracks = new Int_t[kNtrack*fClusterROC];
1148 // Loop through the dictionary arrays one-by-one
1149 // to keep memory consumption low
1150 AliTRDarrayDictionary *tracksIn = 0; //mod
1151 for (Int_t iDict = 0; iDict < kNdict; iDict++) {
1153 // tracksIn should be expanded beforehand!
1154 tracksIn = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(fDet,iDict);
1156 // Loop though the clusters found in this ROC
1157 for (iClusterROC = 0; iClusterROC < fClusterROC; iClusterROC++) {
1159 AliTRDcluster *cluster = (AliTRDcluster *)
1160 RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
1161 row = cluster->GetLabel(0);
1162 col = cluster->GetLabel(1);
1163 time = cluster->GetLabel(2);
1165 for (iPad = 0; iPad < kNclus; iPad++) {
1166 Int_t iPadCol = col - 1 + iPad;
1167 Int_t index = tracksIn->GetData(row,iPadCol,time); //Modification of -1 in Track
1168 idxTracks[3*iPad+iDict + iClusterROC*kNtrack] = index;
1175 // Copy the track indices into the cluster
1176 // Loop though the clusters found in this ROC
1177 for (iClusterROC = 0; iClusterROC < fClusterROC; iClusterROC++) {
1179 AliTRDcluster *cluster = (AliTRDcluster *)
1180 RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
1181 cluster->SetLabel(-9999,0);
1182 cluster->SetLabel(-9999,1);
1183 cluster->SetLabel(-9999,2);
1185 cluster->AddTrackIndex(&idxTracks[iClusterROC*kNtrack]);
1189 delete [] idxTracks;
1195 //_____________________________________________________________________________
1196 Float_t AliTRDclusterizer::Unfold(Double_t eps, Int_t layer, const Double_t *const padSignal) const
1199 // Method to unfold neighbouring maxima.
1200 // The charge ratio on the overlapping pad is calculated
1201 // until there is no more change within the range given by eps.
1202 // The resulting ratio is then returned to the calling method.
1205 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
1207 AliError("No AliTRDcalibDB instance available\n");
1212 Int_t itStep = 0; // Count iteration steps
1214 Double_t ratio = 0.5; // Start value for ratio
1215 Double_t prevRatio = 0.0; // Store previous ratio
1217 Double_t newLeftSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store left cluster signal
1218 Double_t newRightSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store right cluster signal
1219 Double_t newSignal[3] = { 0.0, 0.0, 0.0 };
1221 // Start the iteration
1222 while ((TMath::Abs(prevRatio - ratio) > eps) && (itStep < 10)) {
1227 // Cluster position according to charge ratio
1228 Double_t maxLeft = (ratio*padSignal[2] - padSignal[0])
1229 / (padSignal[0] + padSignal[1] + ratio * padSignal[2]);
1230 Double_t maxRight = (padSignal[4] - (1-ratio)*padSignal[2])
1231 / ((1.0 - ratio)*padSignal[2] + padSignal[3] + padSignal[4]);
1233 // Set cluster charge ratio
1234 irc = calibration->PadResponse(1.0, maxLeft, layer, newSignal);
1235 Double_t ampLeft = padSignal[1] / newSignal[1];
1236 irc = calibration->PadResponse(1.0, maxRight, layer, newSignal);
1237 Double_t ampRight = padSignal[3] / newSignal[1];
1239 // Apply pad response to parameters
1240 irc = calibration->PadResponse(ampLeft ,maxLeft ,layer,newLeftSignal );
1241 irc = calibration->PadResponse(ampRight,maxRight,layer,newRightSignal);
1243 // Calculate new overlapping ratio
1244 ratio = TMath::Min((Double_t) 1.0
1245 ,newLeftSignal[2] / (newLeftSignal[2] + newRightSignal[0]));
1253 //_____________________________________________________________________________
1254 void AliTRDclusterizer::TailCancelation(const AliTRDrecoParam* const recoParam)
1257 // Applies the tail cancelation
1260 Int_t nexp = recoParam->GetTCnexp();
1267 TTreeSRedirector *fDebugStream = fReconstructor->GetDebugStream(AliTRDrecoParam::kClusterizer);
1268 Bool_t debugStreaming = recoParam->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 7 && fReconstructor->IsDebugStreaming();
1269 while(fIndexes->NextRCIndex(iRow, iCol))
1271 // if corrupted then don't make the tail cancallation
1272 if (fCalPadStatusROC->GetStatus(iCol, iRow)) continue;
1275 for (iTime = 0; iTime < fTimeTotal; iTime++)
1276 (*fDebugStream) << "TailCancellation"
1282 // Apply the tail cancelation via the digital filter
1283 //DeConvExp(fDigits->GetDataAddress(iRow,iCol),fTimeTotal,nexp);
1284 ApplyTCTM(fDigits->GetDataAddress(iRow,iCol),fTimeTotal,nexp);
1285 } // while irow icol
1292 //_____________________________________________________________________________
1293 void AliTRDclusterizer::ApplyTCTM(Short_t *const arr, const Int_t nTime, const Int_t nexp)
1296 // Steer tail cancellation
1303 DeConvExp(arr,nTime,nexp);
1309 DeConvExp(arr,nTime,1);
1318 //_____________________________________________________________________________
1319 void AliTRDclusterizer::ConvExp(Short_t *const arr, const Int_t nTime)
1325 // Initialization (coefficient = alpha, rates = lambda)
1326 Float_t slope = 1.0;
1327 Float_t coeff = 0.5;
1331 fReconstructor->GetRecoParam()->GetTCParams(par);
1337 rate = TMath::Exp(-dt/(slope));
1339 Float_t reminder = .0;
1340 Float_t correction = 0.0;
1341 Float_t result = 0.0;
1343 for (int i = nTime-1; i >= 0; i--) {
1345 result = arr[i] + correction - fBaseline; // No rescaling
1346 arr[i] = (Short_t)(result + fBaseline + 0.5f);
1350 correction += reminder = rate * (reminder + coeff * result);
1355 //_____________________________________________________________________________
1356 void AliTRDclusterizer::DeConvExp(Short_t *const arr, const Int_t nTime, const Int_t nexp)
1359 // Tail cancellation by deconvolution for PASA v4 TRF
1363 Float_t coefficients[2];
1365 // Initialization (coefficient = alpha, rates = lambda)
1371 if (nexp == 1) { // 1 Exponentials
1377 if (nexp == 2) { // 2 Exponentials
1379 fReconstructor->GetRecoParam()->GetTCParams(par);
1380 r1 = par[0];//1.156;
1381 r2 = par[1];//0.130;
1382 c1 = par[2];//0.114;
1383 c2 = par[3];//0.624;
1386 coefficients[0] = c1;
1387 coefficients[1] = c2;
1391 rates[0] = TMath::Exp(-dt/(r1));
1392 rates[1] = (nexp == 1) ? .0 : TMath::Exp(-dt/(r2));
1394 Float_t reminder[2] = { .0, .0 };
1395 Float_t correction = 0.0;
1396 Float_t result = 0.0;
1398 for (int i = 0; i < nTime; i++) {
1400 result = arr[i] - correction - fBaseline; // No rescaling
1401 arr[i] = (Short_t)(result + fBaseline + 0.5f);
1404 for (int k = 0; k < 2; k++) {
1405 correction += reminder[k] = rates[k] * (reminder[k] + coefficients[k] * result);
1412 //_____________________________________________________________________________
1413 void AliTRDclusterizer::ResetRecPoints()
1416 // Resets the list of rec points
1420 fRecPoints->Clear();
1422 // delete fRecPoints;
1426 //_____________________________________________________________________________
1427 TClonesArray *AliTRDclusterizer::RecPoints()
1430 // Returns the list of rec points
1434 if(!(fRecPoints = AliTRDReconstructor::GetClusters())){
1435 // determine number of clusters which has to be allocated
1436 Float_t nclusters = fReconstructor->GetRecoParam()->GetNClusters();
1438 fRecPoints = new TClonesArray("AliTRDcluster", Int_t(nclusters));
1440 //SetClustersOwner(kTRUE);
1441 AliTRDReconstructor::SetClusters(0x0);
1447 //_____________________________________________________________________________
1448 TClonesArray *AliTRDclusterizer::TrackletsArray(const TString &trkltype)
1451 // Returns the array of on-line tracklets
1454 if (trkltype.Length() != 0) {
1456 fTracklets = new TClonesArray(trkltype, 200);
1458 else if (TClass::GetClass(trkltype.Data()) != fTracklets->GetClass()){
1459 fTracklets->Delete();
1461 fTracklets = new TClonesArray(trkltype, 200);
1467 //_____________________________________________________________________________
1468 TClonesArray* AliTRDclusterizer::TracksArray()
1470 // return array of GTU tracks (create TClonesArray if necessary)
1473 fTracks = new TClonesArray("AliESDTrdTrack",100);