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)
69 ,fDigitsManager(new AliTRDdigitsManager())
71 ,fTransform(new AliTRDtransform(0))
79 ,fMinLeftRightCutSigma(0)
85 ,fCalGainFactorROC(NULL)
86 ,fCalGainFactorDetValue(0)
89 ,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)
129 ,fDigitsManager(new AliTRDdigitsManager())
131 ,fTransform(new AliTRDtransform(0))
139 ,fMinLeftRightCutSigma(0)
145 ,fCalGainFactorROC(NULL)
146 ,fCalGainFactorDetValue(0)
148 ,fCalNoiseDetValue(0)
149 ,fCalPadStatusROC(NULL)
150 ,fCalOnlGainROC(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)
182 ,fDigitsManager(NULL)
192 ,fMinLeftRightCutSigma(0)
198 ,fCalGainFactorROC(NULL)
199 ,fCalGainFactorDetValue(0)
201 ,fCalNoiseDetValue(0)
202 ,fCalPadStatusROC(NULL)
203 ,fCalOnlGainROC(NULL)
212 // AliTRDclusterizer copy constructor
215 SetBit(kLabels, kTRUE);
216 SetBit(knewDM, kFALSE);
222 //_____________________________________________________________________________
223 AliTRDclusterizer::~AliTRDclusterizer()
226 // AliTRDclusterizer destructor
229 if (fDigitsManager) {
230 delete fDigitsManager;
231 fDigitsManager = NULL;
250 //_____________________________________________________________________________
251 AliTRDclusterizer &AliTRDclusterizer::operator=(const AliTRDclusterizer &c)
254 // Assignment operator
259 ((AliTRDclusterizer &) c).Copy(*this);
266 //_____________________________________________________________________________
267 void AliTRDclusterizer::Copy(TObject &c) const
273 ((AliTRDclusterizer &) c).fClusterTree = NULL;
274 ((AliTRDclusterizer &) c).fRecPoints = NULL;
275 ((AliTRDclusterizer &) c).fDigitsManager = NULL;
276 ((AliTRDclusterizer &) c).fRawVersion = fRawVersion;
277 ((AliTRDclusterizer &) c).fTransform = NULL;
278 ((AliTRDclusterizer &) c).fDigits = NULL;
279 ((AliTRDclusterizer &) c).fDigitsRaw = NULL;
280 ((AliTRDclusterizer &) c).fIndexes = NULL;
281 ((AliTRDclusterizer &) c).fMaxThresh = 0;
282 ((AliTRDclusterizer &) c).fMaxThreshTest = 0;
283 ((AliTRDclusterizer &) c).fSigThresh = 0;
284 ((AliTRDclusterizer &) c).fMinMaxCutSigma= 0;
285 ((AliTRDclusterizer &) c).fMinLeftRightCutSigma = 0;
286 ((AliTRDclusterizer &) c).fLayer = 0;
287 ((AliTRDclusterizer &) c).fDet = 0;
288 ((AliTRDclusterizer &) c).fVolid = 0;
289 ((AliTRDclusterizer &) c).fColMax = 0;
290 ((AliTRDclusterizer &) c).fTimeTotal = 0;
291 ((AliTRDclusterizer &) c).fCalGainFactorROC = NULL;
292 ((AliTRDclusterizer &) c).fCalGainFactorDetValue = 0;
293 ((AliTRDclusterizer &) c).fCalNoiseROC = NULL;
294 ((AliTRDclusterizer &) c).fCalNoiseDetValue = 0;
295 ((AliTRDclusterizer &) c).fCalPadStatusROC = NULL;
296 ((AliTRDclusterizer &) c).fClusterROC = 0;
297 ((AliTRDclusterizer &) c).firstClusterROC= 0;
298 ((AliTRDclusterizer &) c).fNoOfClusters = 0;
299 ((AliTRDclusterizer &) c).fBaseline = 0;
300 ((AliTRDclusterizer &) c).fRawStream = NULL;
304 //_____________________________________________________________________________
305 Bool_t AliTRDclusterizer::Open(const Char_t *name, Int_t nEvent)
308 // Opens the AliROOT file. Output and input are in the same file
311 TString evfoldname = AliConfig::GetDefaultEventFolderName();
312 fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
315 fRunLoader = AliRunLoader::Open(name);
319 AliError(Form("Can not open session for file %s.",name));
330 //_____________________________________________________________________________
331 Bool_t AliTRDclusterizer::OpenOutput()
334 // Open the output file
337 if (!fReconstructor->IsWritingClusters()) return kTRUE;
339 TObjArray *ioArray = 0x0;
341 AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
342 loader->MakeTree("R");
344 fClusterTree = loader->TreeR();
345 fClusterTree->Branch("TRDcluster", "TObjArray", &ioArray, 32000, 0);
351 //_____________________________________________________________________________
352 Bool_t AliTRDclusterizer::OpenOutput(TTree *const clusterTree)
355 // Connect the output tree
359 if (fReconstructor->IsWritingClusters()){
360 TObjArray *ioArray = 0x0;
361 fClusterTree = clusterTree;
362 fClusterTree->Branch("TRDcluster", "TObjArray", &ioArray, 32000, 0);
367 //_____________________________________________________________________________
368 Bool_t AliTRDclusterizer::OpenInput(Int_t nEvent)
371 // Opens a ROOT-file with TRD-hits and reads in the digits-tree
374 // Import the Trees for the event nEvent in the file
375 fRunLoader->GetEvent(nEvent);
381 //_____________________________________________________________________________
382 Bool_t AliTRDclusterizer::WriteClusters(Int_t det)
385 // Fills TRDcluster branch in the tree with the clusters
386 // found in detector = det. For det=-1 writes the tree.
390 (det >= AliTRDgeometry::Ndet())) {
391 AliError(Form("Unexpected detector index %d.\n",det));
394 Int_t nRecPoints = RecPoints()->GetEntriesFast();
395 if(!nRecPoints) return kTRUE;
397 TObjArray *ioArray = new TObjArray(400);
398 TBranch *branch = fClusterTree->GetBranch("TRDcluster");
400 fClusterTree->Branch("TRDcluster","TObjArray",&ioArray,32000,0);
401 } else branch->SetAddress(&ioArray);
403 AliTRDcluster *c(NULL);
405 for (Int_t i = 0; i < nRecPoints; i++) {
406 if(!(c = (AliTRDcluster *) RecPoints()->UncheckedAt(i))) continue;
407 if(det != c->GetDetector()) continue;
410 fClusterTree->Fill();
413 if(!(c = (AliTRDcluster*)RecPoints()->UncheckedAt(0))){
414 AliError("Missing first cluster.");
418 Int_t detOld(c->GetDetector()), nw(0);
420 for (Int_t i(1); i<nRecPoints; i++) {
421 if(!(c = (AliTRDcluster *) RecPoints()->UncheckedAt(i))) continue;
422 if(c->GetDetector() != detOld){
423 nw += ioArray->GetEntriesFast();
424 // fill & clear previously detector set of clusters
425 fClusterTree->Fill();
427 detOld = c->GetDetector();
431 if(ioArray->GetEntriesFast()){
432 nw += ioArray->GetEntriesFast();
433 // fill & clear last detector set of clusters (if any)
434 fClusterTree->Fill();
437 AliDebug(2, Form("Clusters FOUND[%d] WRITTEN[%d] STATUS[%s]", nRecPoints, nw, nw==nRecPoints?"OK":"FAILED"));
438 if(nw!=nRecPoints) AliWarning(Form("Clusters FOUND[%d] WRITTEN[%d]", nRecPoints, nw));
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) {
511 trackletLoader->Load();
513 Bool_t loaded = kFALSE;
514 // look for simulated tracklets
515 TTree *trackletTree = trackletLoader->Tree();
518 TBranch *trklbranch = trackletTree->GetBranch("mcmtrklbranch");
519 TClonesArray *trklArray = TrackletsArray("AliTRDtrackletMCM");
520 if (trklbranch && trklArray) {
521 AliTRDtrackletMCM *trkl = 0x0;
522 trklbranch->SetAddress(&trkl);
523 Int_t nTracklets = trklbranch->GetEntries();
524 for (Int_t iTracklet = 0; iTracklet < nTracklets; iTracklet++) {
525 trklbranch->GetEntry(iTracklet);
526 new ((*trklArray)[trklArray->GetEntries()]) AliTRDtrackletMCM(*trkl);
532 // if no simulated tracklets found, look for raw tracklets
533 AliTreeLoader *treeLoader = (AliTreeLoader*) trackletLoader->GetBaseLoader("tracklets-raw");
534 trackletTree = treeLoader ? treeLoader->Load(), treeLoader->Tree() : 0x0;
537 TClonesArray *trklArray = TrackletsArray("AliTRDtrackletWord");
540 TClonesArray *ar = 0x0;
541 trackletTree->SetBranchAddress("hc", &hc);
542 trackletTree->SetBranchAddress("trkl", &ar);
544 Int_t nEntries = trackletTree->GetEntries();
545 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
546 trackletTree->GetEntry(iEntry);
547 Int_t nTracklets = ar->GetEntriesFast();
548 AliDebug(2, Form("%i tracklets in HC %i", nTracklets, hc));
549 for (Int_t iTracklet = 0; iTracklet < nTracklets; iTracklet++) {
550 AliTRDtrackletWord *trklWord = (AliTRDtrackletWord*) (*ar)[iTracklet];
551 new ((*trklArray)[trklArray->GetEntries()]) AliTRDtrackletWord(trklWord->GetTrackletWord(), hc);
558 trackletLoader->UnloadAll();
559 trackletLoader->CloseFile();
564 Bool_t AliTRDclusterizer::ReadTracks()
567 // Reads simulated GTU tracks from the input aliroot file
570 AliRunLoader *runLoader = AliRunLoader::Instance();
573 AliError("No run loader available");
577 AliLoader* loader = runLoader->GetLoader("TRDLoader");
582 AliDataLoader *trackLoader = loader->GetDataLoader("gtutracks");
587 Bool_t loaded = kFALSE;
591 TTree *trackTree = trackLoader->Tree();
593 TClonesArray *trackArray = TracksArray();
594 AliTRDtrackGTU *trk = 0x0;
595 trackTree->SetBranchAddress("TRDtrackGTU", &trk);
596 for (Int_t iTrack = 0; iTrack < trackTree->GetEntries(); iTrack++) {
597 trackTree->GetEntry(iTrack);
598 new ((*trackArray)[trackArray->GetEntries()]) AliESDTrdTrack(*(trk->CreateTrdTrack()));
603 trackLoader->UnloadAll();
604 trackLoader->CloseFile();
609 //_____________________________________________________________________________
610 Bool_t AliTRDclusterizer::MakeClusters()
613 // Creates clusters from digits
616 // Propagate info from the digits manager
617 if (TestBit(kLabels)){
618 SetBit(kLabels, fDigitsManager->UsesDictionaries());
621 Bool_t fReturn = kTRUE;
622 for (Int_t i = 0; i < AliTRDgeometry::kNdet; i++){
624 AliTRDarrayADC *digitsIn = (AliTRDarrayADC*) fDigitsManager->GetDigits(i); //mod
625 // This is to take care of switched off super modules
626 if (!digitsIn->HasData()) continue;
628 digitsIn->DeleteNegatives(); // Restore digits array to >=0 values
629 AliTRDSignalIndex* indexes = fDigitsManager->GetIndexes(i);
630 // if (indexes->IsAllocated() == kFALSE){ // A.B.
631 fDigitsManager->BuildIndexes(i);
635 if (indexes->HasEntry()){
636 if (TestBit(kLabels)){
638 for (Int_t iDict = 0; iDict < AliTRDdigitsManager::kNDict; iDict++){
639 AliTRDarrayDictionary *tracksIn(NULL); //mod
640 tracksIn = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(i,iDict); //mod
641 // This is to take care of data reconstruction
642 if (!tracksIn->GetDim()) continue;
643 tracksIn->Expand(); nDict++;
646 AliDebug(1, "MC labels not available. Switch them off.");
647 SetUseLabels(kFALSE);
650 fR = MakeClusters(i);
651 fReturn = fR && fReturn;
655 // if(IsWritingClusters()) WriteClusters(i);
659 // Clear arrays of this chamber, to prepare for next event
660 fDigitsManager->ClearArrays(i);
663 if(fReconstructor->IsWritingClusters()) WriteClusters(-1);
665 AliInfo(Form("Found :: clusters[%d] tracklets[%d] tracks[%d]",
666 RecPoints()?RecPoints()->GetEntriesFast():0,
667 TrackletsArray()?TrackletsArray()->GetEntriesFast():0,
668 TracksArray()?TracksArray()->GetEntriesFast():0));
674 //_____________________________________________________________________________
675 Bool_t AliTRDclusterizer::Raw2Clusters(AliRawReader *rawReader)
678 // Creates clusters from raw data
681 return Raw2ClustersChamber(rawReader);
685 //_____________________________________________________________________________
686 Bool_t AliTRDclusterizer::Raw2ClustersChamber(AliRawReader *rawReader)
689 // Creates clusters from raw data
692 // Create the digits manager
693 if (!fDigitsManager){
694 SetBit(knewDM, kTRUE);
695 fDigitsManager = new AliTRDdigitsManager(kTRUE);
696 fDigitsManager->CreateArrays();
699 fDigitsManager->SetUseDictionaries(TestBit(kLabels));
702 fRawStream = new AliTRDrawStream(rawReader);
704 fRawStream->SetReader(rawReader);
706 //if(fReconstructor->IsHLT()){
707 fRawStream->DisableErrorStorage();
710 // register tracklet array for output
711 fRawStream->SetTrackletArray(TrackletsArray("AliTRDtrackletWord"));
712 fRawStream->SetTrackArray(TracksArray());
715 while ((det = fRawStream->NextChamber(fDigitsManager)) < AliTRDgeometry::kNdet){
716 if (fDigitsManager->GetIndexes(det)->HasEntry()){
718 fDigitsManager->ClearArrays(det);
722 for (Int_t iSector = 0; iSector < AliTRDgeometry::kNsector; iSector++) {
723 fTrgFlags[iSector] = fRawStream->GetTriggerFlags(iSector);
726 if(fReconstructor->IsWritingClusters()) WriteClusters(-1);
728 if(!TestBit(knewDM)){
729 delete fDigitsManager;
730 fDigitsManager = NULL;
735 AliInfo(Form("Found :: clusters[%d] tracklets[%d] tracks[%d]",
736 RecPoints()?RecPoints()->GetEntriesFast():0,
737 TrackletsArray()?TrackletsArray()->GetEntriesFast():0,
738 TracksArray()?TracksArray()->GetEntriesFast():0));
743 //_____________________________________________________________________________
744 UChar_t AliTRDclusterizer::GetStatus(Short_t &signal)
747 // Check if a pad is masked
752 if(signal>0 && TESTBIT(signal, 10)){
754 for(int ibit=0; ibit<4; ibit++){
755 if(TESTBIT(signal, 11+ibit)){
756 SETBIT(status, ibit);
757 CLRBIT(signal, 11+ibit);
764 //_____________________________________________________________________________
765 void AliTRDclusterizer::SetPadStatus(const UChar_t status, UChar_t &out) const {
767 // Set the pad status into out
768 // First three bits are needed for the position encoding
773 //_____________________________________________________________________________
774 UChar_t AliTRDclusterizer::GetPadStatus(UChar_t encoding) const {
776 // return the staus encoding of the corrupted pad
778 return static_cast<UChar_t>(encoding >> 3);
781 //_____________________________________________________________________________
782 Int_t AliTRDclusterizer::GetCorruption(UChar_t encoding) const {
784 // Return the position of the corruption
789 //_____________________________________________________________________________
790 Bool_t AliTRDclusterizer::MakeClusters(Int_t det)
793 // Generates the cluster.
797 fDigits = (AliTRDarrayADC *) fDigitsManager->GetDigits(det); //mod
798 fBaseline = fDigitsManager->GetDigitsParam()->GetADCbaseline(det);
800 // This is to take care of switched off super modules
801 if (!fDigits->HasData()) return kFALSE;
803 fIndexes = fDigitsManager->GetIndexes(det);
804 if (fIndexes->IsAllocated() == kFALSE) {
805 AliError("Indexes do not exist!");
809 AliTRDcalibDB* const calibration = AliTRDcalibDB::Instance();
811 AliFatal("No AliTRDcalibDB instance available\n");
815 if (!fReconstructor){
816 AliError("Reconstructor not set\n");
820 const AliTRDrecoParam *const recoParam = fReconstructor->GetRecoParam();
822 fMaxThresh = recoParam->GetClusMaxThresh();
823 fMaxThreshTest = (recoParam->GetClusMaxThresh()/2+fBaseline);
824 fSigThresh = recoParam->GetClusSigThresh();
825 fMinMaxCutSigma = recoParam->GetMinMaxCutSigma();
826 fMinLeftRightCutSigma = recoParam->GetMinLeftRightCutSigma();
827 const Int_t iEveryNTB = recoParam->GetRecEveryNTB();
829 Int_t istack = fIndexes->GetStack();
830 fLayer = fIndexes->GetLayer();
831 Int_t isector = fIndexes->GetSM();
833 // Start clustering in the chamber
835 fDet = AliTRDgeometry::GetDetector(fLayer,istack,isector);
837 AliError(Form("Detector number missmatch! Request[%03d] RAW[%03d]", det, fDet));
841 AliDebug(2, Form("Det[%d] @ Sec[%d] Stk[%d] Ly[%d]", fDet, isector, istack, fLayer));
843 // TRD space point transformation
844 fTransform->SetDetector(det);
846 Int_t iGeoLayer = AliGeomManager::kTRD1 + fLayer;
847 Int_t iGeoModule = istack + AliTRDgeometry::Nstack() * isector;
848 fVolid = AliGeomManager::LayerToVolUID(iGeoLayer,iGeoModule);
850 fColMax = fDigits->GetNcol();
851 fTimeTotal = fDigitsManager->GetDigitsParam()->GetNTimeBins(det);
853 // Check consistency between Geometry and raw data
854 AliTRDpadPlane *pp(fTransform->GetPadPlane());
855 Int_t ncols(pp->GetNcols()), nrows(pp->GetNrows());
856 if(ncols != fColMax) AliDebug(1, Form("N cols missmatch in Digits for Det[%3d] :: Geom[%3d] RAW[%3d]", fDet, ncols, fColMax));
857 if(nrows != fDigits->GetNrow()) AliDebug(1, Form("N rows missmatch in Digits for Det[%3d] :: Geom[%3d] RAW[%3d]", fDet, nrows, fDigits->GetNrow()));
858 if(ncols != fIndexes->GetNcol()) AliDebug(1, Form("N cols missmatch in Digits for Det[%3d] :: Geom[%3d] RAW[%3d]", fDet, ncols, fIndexes->GetNcol()));
859 if(nrows != fIndexes->GetNrow()) AliDebug(1, Form("N rows missmatch in Digits for Det[%3d] :: Geom[%3d] RAW[%3d]", fDet, nrows, fIndexes->GetNrow()));
861 // Check consistency between OCDB and raw data
862 Int_t nTimeOCDB = calibration->GetNumberOfTimeBinsDCS();
863 if(fReconstructor->IsHLT()){
864 if((nTimeOCDB > -1) && (fTimeTotal != nTimeOCDB)){
865 AliWarning(Form("Number of timebins does not match OCDB value (RAW[%d] OCDB[%d]), using raw value"
866 ,fTimeTotal,nTimeOCDB));
870 AliDebug(1, "Undefined number of timebins in OCDB, using value from raw data.");
872 AliError(Form("Number of timebins in raw data is negative, skipping chamber[%3d]!", fDet));
875 }else if(nTimeOCDB == -2){
876 AliError("Mixed number of timebins in OCDB, no reconstruction of TRD data!");
878 }else if(fTimeTotal != nTimeOCDB){
879 AliError(Form("Number of timebins in raw data does not match OCDB value (RAW[%d] OCDB[%d]), skipping chamber[%3d]!"
880 ,fTimeTotal,nTimeOCDB, fDet));
884 AliDebug(1, Form("Using %2d number of timebins for Det[%03d].", fTimeTotal, fDet));
886 // Detector wise calibration object for the gain factors
887 const AliTRDCalDet *calGainFactorDet = calibration->GetGainFactorDet();
888 // Calibration object with pad wise values for the gain factors
889 fCalGainFactorROC = calibration->GetGainFactorROC(fDet);
890 // Calibration value for chamber wise gain factor
891 fCalGainFactorDetValue = calGainFactorDet->GetValue(fDet);
893 // Detector wise calibration object for the noise
894 const AliTRDCalDet *calNoiseDet = calibration->GetNoiseDet();
895 // Calibration object with pad wise values for the noise
896 fCalNoiseROC = calibration->GetNoiseROC(fDet);
897 // Calibration value for chamber wise noise
898 fCalNoiseDetValue = calNoiseDet->GetValue(fDet);
900 // Calibration object with the pad status
901 fCalPadStatusROC = calibration->GetPadStatusROC(fDet);
902 // Calibration object of the online gain
903 fCalOnlGainROC = 0x0;
904 if (calibration->HasOnlineFilterGain()) {
905 fCalOnlGainROC = calibration->GetOnlineGainTableROC(fDet);
908 firstClusterROC = -1;
911 SetBit(kLUT, recoParam->UseLUT());
912 SetBit(kGAUS, recoParam->UseGAUS());
914 // Apply the gain and the tail cancelation via digital filter
915 // Use the configuration from the DCS to find out whether online
916 // tail cancellation was applied
917 if ((!calibration->HasOnlineTailCancellation()) &&
918 (recoParam->UseTailCancelation())) {
919 // save a copy of raw data
920 if(TestBit(kRawSignal)){
922 fDigitsRaw->~AliTRDarrayADC();
923 new(fDigitsRaw) AliTRDarrayADC(*fDigits);
924 } else fDigitsRaw = new AliTRDarrayADC(*fDigits);
926 TailCancelation(recoParam);
929 MaxStruct curr, last;
930 Int_t nMaximas = 0, nCorrupted = 0;
932 // Here the clusterfining is happening
934 for(curr.time = 0; curr.time < fTimeTotal; curr.time+=iEveryNTB){
935 while(fIndexes->NextRCIndex(curr.row, curr.col)){
936 if(fDigits->GetData(curr.row, curr.col, curr.time) > fMaxThreshTest && IsMaximum(curr, curr.padStatus, &curr.signals[0])){
938 if(curr.col==last.col+2 && curr.row==last.row && curr.time==last.time) FivePadCluster(last, curr);
941 last=curr; curr.fivePad=kFALSE;
945 if(last.row>-1) CreateCluster(last);
947 if(recoParam->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 2 && fReconstructor->IsDebugStreaming()){
948 TTreeSRedirector* fDebugStream = fReconstructor->GetDebugStream(AliTRDrecoParam::kClusterizer);
949 (*fDebugStream) << "MakeClusters"
950 << "Detector=" << det
951 << "NMaxima=" << nMaximas
952 << "NClusters=" << fClusterROC
953 << "NCorrupted=" << nCorrupted
956 if (TestBit(kLabels)) AddLabels();
962 //_____________________________________________________________________________
963 Bool_t AliTRDclusterizer::IsMaximum(const MaxStruct &Max, UChar_t &padStatus, Float_t *const Signals)
966 // Returns true if this row,col,time combination is a maximum.
967 // Gives back the padStatus and the signals of the center pad and the two neighbouring pads.
970 Float_t gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col,Max.row);
971 Float_t onlcf = fCalOnlGainROC ? fCalOnlGainROC->GetGainCorrectionFactor(Max.row,Max.col) : 1;
972 Signals[1] = (fDigits->GetData(Max.row, Max.col, Max.time) - fBaseline) /(onlcf * gain) + 0.5f;
973 if(Signals[1] <= fMaxThresh) return kFALSE;
975 if(Max.col < 1 || Max.col + 1 >= fColMax) return kFALSE;
977 Float_t noiseMiddleThresh = fMinMaxCutSigma*fCalNoiseDetValue*fCalNoiseROC->GetValue(Max.col, Max.row);
978 if (Signals[1] <= noiseMiddleThresh) return kFALSE;
981 fCalPadStatusROC->GetStatus(Max.col-1, Max.row)
982 ,fCalPadStatusROC->GetStatus(Max.col, Max.row)
983 ,fCalPadStatusROC->GetStatus(Max.col+1, Max.row)
987 if((signal = fDigits->GetData(Max.row, Max.col-1, Max.time))){
988 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col-1,Max.row);
989 onlcf = fCalOnlGainROC ? fCalOnlGainROC->GetGainCorrectionFactor(Max.row,Max.col-1) : 1;
990 Signals[0] = (signal - fBaseline) /( onlcf * gain) + 0.5f;
991 } else Signals[0] = 0.;
992 if((signal = fDigits->GetData(Max.row, Max.col+1, Max.time))){
993 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(Max.col+1,Max.row);
994 onlcf = fCalOnlGainROC ? fCalOnlGainROC->GetGainCorrectionFactor(Max.row,Max.col+1) : 1;
995 Signals[2] = (signal - fBaseline) /( onlcf * gain) + 0.5f;
996 } else Signals[2] = 0.;
998 if(!(status[0] | status[1] | status[2])) {//all pads are good
999 if ((Signals[2] <= Signals[1]) && (Signals[0] < Signals[1])) {
1000 if ((Signals[2] > fSigThresh) || (Signals[0] > fSigThresh)) {
1001 if(Signals[0]<0) Signals[0]=0.;
1002 if(Signals[2]<0) Signals[2]=0.;
1003 Float_t noiseSumThresh = fMinLeftRightCutSigma * fCalNoiseDetValue
1004 * fCalNoiseROC->GetValue(Max.col, Max.row);
1005 if ((Signals[2]+Signals[0]+Signals[1]) <= noiseSumThresh) return kFALSE;
1010 } else { // at least one of the pads is bad, and reject candidates with more than 1 problematic pad
1011 if(Signals[0]<0)Signals[0]=0;
1012 if(Signals[2]<0)Signals[2]=0;
1013 if (status[2] && (!(status[0] || status[1])) && Signals[1] > Signals[0] && Signals[0] > fSigThresh) {
1015 SetPadStatus(status[2], padStatus);
1018 else if (status[0] && (!(status[1] || status[2])) && Signals[1] >= Signals[2] && Signals[2] > fSigThresh) {
1020 SetPadStatus(status[0], padStatus);
1023 else if (status[1] && (!(status[0] || status[2])) && ((Signals[2] > fSigThresh) || (Signals[0] > fSigThresh))) {
1024 Signals[1] = fMaxThresh;
1025 SetPadStatus(status[1], padStatus);
1032 //_____________________________________________________________________________
1033 Bool_t AliTRDclusterizer::FivePadCluster(MaxStruct &ThisMax, MaxStruct &NeighbourMax)
1036 // Look for 5 pad cluster with minimum in the middle
1037 // Gives back the ratio
1040 if (ThisMax.col >= fColMax - 3) return kFALSE;
1043 if (ThisMax.col < fColMax - 5){
1044 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(ThisMax.col+4,ThisMax.row);
1045 onlcf = fCalOnlGainROC ? fCalOnlGainROC->GetGainCorrectionFactor(ThisMax.row,ThisMax.col+4) : 1;
1046 if (fDigits->GetData(ThisMax.row, ThisMax.col+4, ThisMax.time) - fBaseline >= fSigThresh * gain * onlcf)
1049 if (ThisMax.col > 1) {
1050 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(ThisMax.col-2,ThisMax.row);
1051 onlcf = fCalOnlGainROC ? fCalOnlGainROC->GetGainCorrectionFactor(ThisMax.row,ThisMax.col-2) : 1;
1052 if (fDigits->GetData(ThisMax.row, ThisMax.col-2, ThisMax.time) - fBaseline >= fSigThresh * gain * onlcf)
1056 const Float_t kEpsilon = 0.01;
1057 Double_t padSignal[5] = {ThisMax.signals[0], ThisMax.signals[1], ThisMax.signals[2],
1058 NeighbourMax.signals[1], NeighbourMax.signals[2]};
1060 // Unfold the two maxima and set the signal on
1061 // the overlapping pad to the ratio
1062 Float_t ratio = Unfold(kEpsilon,fLayer,padSignal);
1063 ThisMax.signals[2] = ThisMax.signals[2]*ratio + 0.5f;
1064 NeighbourMax.signals[0] = NeighbourMax.signals[0]*(1-ratio) + 0.5f;
1065 ThisMax.fivePad=kTRUE;
1066 NeighbourMax.fivePad=kTRUE;
1071 //_____________________________________________________________________________
1072 void AliTRDclusterizer::CreateCluster(const MaxStruct &Max)
1075 // Creates a cluster at the given position and saves it in RecPoints
1078 Int_t nPadCount = 1;
1079 Short_t signals[7] = { 0, 0, (Short_t)Max.signals[0], (Short_t)Max.signals[1], (Short_t)Max.signals[2], 0, 0 };
1080 if(!fReconstructor->IsHLT()) CalcAdditionalInfo(Max, signals, nPadCount);
1082 AliTRDcluster cluster(fDet, ((UChar_t) Max.col), ((UChar_t) Max.row), ((UChar_t) Max.time), signals, fVolid);
1083 cluster.SetNPads(nPadCount);
1084 cluster.SetQ(Max.signals[0]+Max.signals[1]+Max.signals[2]);
1085 if(TestBit(kLUT)) cluster.SetRPhiMethod(AliTRDcluster::kLUT);
1086 else if(TestBit(kGAUS)) cluster.SetRPhiMethod(AliTRDcluster::kGAUS);
1087 else cluster.SetRPhiMethod(AliTRDcluster::kCOG);
1089 cluster.SetFivePad(Max.fivePad);
1090 // set pads status for the cluster
1091 UChar_t maskPosition = GetCorruption(Max.padStatus);
1093 cluster.SetPadMaskedPosition(maskPosition);
1094 cluster.SetPadMaskedStatus(GetPadStatus(Max.padStatus));
1096 cluster.SetXcorr(fReconstructor->UseClusterRadialCorrection());
1098 // Transform the local cluster coordinates into calibrated
1099 // space point positions defined in the local tracking system.
1100 // Here the calibration for T0, Vdrift and ExB is applied as well.
1101 if(!TestBit(kSkipTrafo)) if(!fTransform->Transform(&cluster)) return;
1102 // Store raw signals in cluster. This MUST be called after position reconstruction !
1103 // Xianguo Lu and Alex Bercuci 19.03.2012
1104 if(TestBit(kRawSignal) && fDigitsRaw){
1105 Float_t tmp(0.), kMaxShortVal(32767.); // protect against data overflow due to wrong gain calibration
1106 Short_t rawSignal[7] = {0};
1107 for(Int_t ipad(Max.col-3), iRawId(0); ipad<=Max.col+3; ipad++, iRawId++){
1108 if(ipad<0 || ipad>=fColMax) continue;
1109 if(!fCalOnlGainROC){
1110 rawSignal[iRawId] = fDigitsRaw->GetData(Max.row, ipad, Max.time);
1113 // Deconvolute online gain calibration when available
1114 // Alex Bercuci 27.04.2012
1115 tmp = (fDigitsRaw->GetData(Max.row, ipad, Max.time) - fBaseline)/fCalOnlGainROC->GetGainCorrectionFactor(Max.row, ipad) + 0.5f;
1116 rawSignal[iRawId] = (Short_t)TMath::Min(tmp, kMaxShortVal);
1118 cluster.SetSignals(rawSignal, kTRUE);
1120 // Temporarily store the Max.Row, column and time bin of the center pad
1121 // Used to later on assign the track indices
1122 cluster.SetLabel(Max.row, 0);
1123 cluster.SetLabel(Max.col, 1);
1124 cluster.SetLabel(Max.time,2);
1126 //needed for HLT reconstruction
1127 AddClusterToArray(&cluster);
1129 // Store the index of the first cluster in the current ROC
1130 if (firstClusterROC < 0) firstClusterROC = fNoOfClusters;
1136 //_____________________________________________________________________________
1137 void AliTRDclusterizer::CalcAdditionalInfo(const MaxStruct &Max, Short_t *const signals, Int_t &nPadCount)
1139 // Calculate number of pads/cluster and
1140 // ADC signals at position 0, 1, 5 and 6
1142 Float_t tmp(0.), kMaxShortVal(32767.); // protect against data overflow due to wrong gain calibration
1143 Float_t gain(1.); Float_t onlcf(1.); Short_t signal(0);
1144 // Store the amplitudes of the pads in the cluster for later analysis
1145 // and check whether one of these pads is masked in the database
1146 signals[3]=Max.signals[1];
1147 Int_t ipad(1), jpad(0);
1148 // Look to the right
1149 while((jpad = Max.col-ipad)){
1150 if(!(signal = fDigits->GetData(Max.row, jpad, Max.time))) break; // empty digit !
1151 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(jpad, Max.row);
1152 onlcf = fCalOnlGainROC ? fCalOnlGainROC->GetGainCorrectionFactor(Max.row,jpad) : 1;
1153 tmp = (signal - fBaseline) / (onlcf * gain) + 0.5f;
1154 signal = (Short_t)TMath::Min(tmp, kMaxShortVal);
1155 if(signal<fSigThresh) break; // signal under threshold
1157 if(ipad<=3) signals[3 - ipad] = signal;
1162 while((jpad = Max.col+ipad)<fColMax){
1163 if(!(signal = fDigits->GetData(Max.row, jpad, Max.time))) break; // empty digit !
1164 gain = fCalGainFactorDetValue * fCalGainFactorROC->GetValue(jpad, Max.row);
1165 onlcf = fCalOnlGainROC ? fCalOnlGainROC->GetGainCorrectionFactor(Max.row,jpad) : 1;
1166 tmp = (signal - fBaseline) / (onlcf * gain) + 0.5f;
1167 signal = (Short_t)TMath::Min(tmp, kMaxShortVal);
1168 if(signal<fSigThresh) break; // signal under threshold
1170 if(ipad<=3) signals[3 + ipad] = signal;
1174 AliDebug(4, Form("Signals[%3d %3d %3d %3d %3d %3d %3d] Npads[%d]."
1175 , signals[0], signals[1], signals[2], signals[3], signals[4], signals[5], signals[6], nPadCount));
1178 //_____________________________________________________________________________
1179 void AliTRDclusterizer::AddClusterToArray(AliTRDcluster* cluster)
1182 // Add a cluster to the array
1185 Int_t n = RecPoints()->GetEntriesFast();
1186 if(n!=fNoOfClusters)AliError(Form("fNoOfClusters != RecPoints()->GetEntriesFast %i != %i \n", fNoOfClusters, n));
1187 new((*RecPoints())[n]) AliTRDcluster(*cluster);
1190 //_____________________________________________________________________________
1191 Bool_t AliTRDclusterizer::AddLabels()
1194 // Add the track indices to the found clusters
1197 const Int_t kNclus = 3;
1198 const Int_t kNdict = AliTRDdigitsManager::kNDict;
1199 const Int_t kNtrack = kNdict * kNclus;
1201 Int_t iClusterROC = 0;
1208 // Temporary array to collect the track indices
1209 Int_t *idxTracks = new Int_t[kNtrack*fClusterROC];
1211 // Loop through the dictionary arrays one-by-one
1212 // to keep memory consumption low
1213 AliTRDarrayDictionary *tracksIn(NULL); //mod
1214 for (Int_t iDict = 0; iDict < kNdict; iDict++) {
1216 // tracksIn should be expanded beforehand!
1217 tracksIn = (AliTRDarrayDictionary *) fDigitsManager->GetDictionary(fDet,iDict);
1219 // Loop though the clusters found in this ROC
1220 for (iClusterROC = 0; iClusterROC < fClusterROC; iClusterROC++) {
1222 AliTRDcluster *cluster = (AliTRDcluster *)
1223 RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
1224 row = cluster->GetLabel(0);
1225 col = cluster->GetLabel(1);
1226 time = cluster->GetLabel(2);
1228 for (iPad = 0; iPad < kNclus; iPad++) {
1229 Int_t iPadCol = col - 1 + iPad;
1230 Int_t index = tracksIn->GetData(row,iPadCol,time); //Modification of -1 in Track
1231 idxTracks[3*iPad+iDict + iClusterROC*kNtrack] = index;
1238 // Copy the track indices into the cluster
1239 // Loop though the clusters found in this ROC
1240 for (iClusterROC = 0; iClusterROC < fClusterROC; iClusterROC++) {
1242 AliTRDcluster *cluster = (AliTRDcluster *)
1243 RecPoints()->UncheckedAt(firstClusterROC+iClusterROC);
1244 cluster->SetLabel(-9999,0);
1245 cluster->SetLabel(-9999,1);
1246 cluster->SetLabel(-9999,2);
1248 cluster->AddTrackIndex(&idxTracks[iClusterROC*kNtrack]);
1252 delete [] idxTracks;
1258 //_____________________________________________________________________________
1259 Float_t AliTRDclusterizer::Unfold(Double_t eps, Int_t layer, const Double_t *const padSignal) const
1262 // Method to unfold neighbouring maxima.
1263 // The charge ratio on the overlapping pad is calculated
1264 // until there is no more change within the range given by eps.
1265 // The resulting ratio is then returned to the calling method.
1268 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
1270 AliError("No AliTRDcalibDB instance available\n");
1275 Int_t itStep = 0; // Count iteration steps
1277 Double_t ratio = 0.5; // Start value for ratio
1278 Double_t prevRatio = 0.0; // Store previous ratio
1280 Double_t newLeftSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store left cluster signal
1281 Double_t newRightSignal[3] = { 0.0, 0.0, 0.0 }; // Array to store right cluster signal
1282 Double_t newSignal[3] = { 0.0, 0.0, 0.0 };
1284 // Start the iteration
1285 while ((TMath::Abs(prevRatio - ratio) > eps) && (itStep < 10)) {
1290 // Cluster position according to charge ratio
1291 Double_t maxLeft = (ratio*padSignal[2] - padSignal[0])
1292 / (padSignal[0] + padSignal[1] + ratio * padSignal[2]);
1293 Double_t maxRight = (padSignal[4] - (1-ratio)*padSignal[2])
1294 / ((1.0 - ratio)*padSignal[2] + padSignal[3] + padSignal[4]);
1296 // Set cluster charge ratio
1297 irc = calibration->PadResponse(1.0, maxLeft, layer, newSignal);
1298 Double_t ampLeft = padSignal[1] / newSignal[1];
1299 irc = calibration->PadResponse(1.0, maxRight, layer, newSignal);
1300 Double_t ampRight = padSignal[3] / newSignal[1];
1302 // Apply pad response to parameters
1303 irc = calibration->PadResponse(ampLeft ,maxLeft ,layer,newLeftSignal );
1304 irc = calibration->PadResponse(ampRight,maxRight,layer,newRightSignal);
1306 // Calculate new overlapping ratio
1309 ratio = TMath::Min((Double_t) 1.0
1310 ,newLeftSignal[2] / (newLeftSignal[2] + newRightSignal[0]));
1319 //_____________________________________________________________________________
1320 void AliTRDclusterizer::TailCancelation(const AliTRDrecoParam* const recoParam)
1323 // Applies the tail cancelation
1326 Int_t nexp = recoParam->GetTCnexp();
1333 TTreeSRedirector *fDebugStream = fReconstructor->GetDebugStream(AliTRDrecoParam::kClusterizer);
1334 Bool_t debugStreaming = recoParam->GetStreamLevel(AliTRDrecoParam::kClusterizer) > 7 && fReconstructor->IsDebugStreaming();
1335 while(fIndexes->NextRCIndex(iRow, iCol))
1337 // if corrupted then don't make the tail cancallation
1338 if (fCalPadStatusROC->GetStatus(iCol, iRow)) continue;
1341 for (iTime = 0; iTime < fTimeTotal; iTime++)
1342 (*fDebugStream) << "TailCancellation"
1348 // Apply the tail cancelation via the digital filter
1349 //DeConvExp(fDigits->GetDataAddress(iRow,iCol),fTimeTotal,nexp);
1350 ApplyTCTM(fDigits->GetDataAddress(iRow,iCol),fTimeTotal,nexp);
1351 } // while irow icol
1358 //_____________________________________________________________________________
1359 void AliTRDclusterizer::ApplyTCTM(Short_t *const arr, const Int_t nTime, const Int_t nexp)
1362 // Steer tail cancellation
1369 DeConvExp(arr,nTime,nexp);
1375 DeConvExp(arr,nTime,1);
1384 //_____________________________________________________________________________
1385 void AliTRDclusterizer::ConvExp(Short_t *const arr, const Int_t nTime)
1391 // Initialization (coefficient = alpha, rates = lambda)
1392 Float_t slope = 1.0;
1393 Float_t coeff = 0.5;
1397 fReconstructor->GetRecoParam()->GetTCParams(par);
1403 rate = TMath::Exp(-dt/(slope));
1405 Float_t reminder = .0;
1406 Float_t correction = 0.0;
1407 Float_t result = 0.0;
1409 for (int i = nTime-1; i >= 0; i--) {
1411 result = arr[i] + correction - fBaseline; // No rescaling
1412 arr[i] = (Short_t)(result + fBaseline + 0.5f);
1416 correction += reminder = rate * (reminder + coeff * result);
1421 //_____________________________________________________________________________
1422 void AliTRDclusterizer::DeConvExp(Short_t *const arr, const Int_t nTime, const Int_t nexp)
1425 // Tail cancellation by deconvolution for PASA v4 TRF
1429 Float_t coefficients[2];
1431 // Initialization (coefficient = alpha, rates = lambda)
1437 if (nexp == 1) { // 1 Exponentials
1443 if (nexp == 2) { // 2 Exponentials
1445 fReconstructor->GetRecoParam()->GetTCParams(par);
1446 r1 = par[0];//1.156;
1447 r2 = par[1];//0.130;
1448 c1 = par[2];//0.114;
1449 c2 = par[3];//0.624;
1452 coefficients[0] = c1;
1453 coefficients[1] = c2;
1457 rates[0] = TMath::Exp(-dt/(r1));
1458 rates[1] = (nexp == 1) ? .0 : TMath::Exp(-dt/(r2));
1460 Float_t reminder[2] = { .0, .0 };
1461 Float_t correction = 0.0;
1462 Float_t result = 0.0;
1464 for (int i = 0; i < nTime; i++) {
1466 result = arr[i] - correction - fBaseline; // No rescaling
1467 arr[i] = (Short_t)(result + fBaseline + 0.5f);
1470 for (int k = 0; k < 2; k++) {
1471 correction += reminder[k] = rates[k] * (reminder[k] + coefficients[k] * result);
1476 //_____________________________________________________________________________
1477 TClonesArray *AliTRDclusterizer::RecPoints()
1480 // Returns the list of rec points
1483 fRecPoints = AliTRDReconstructor::GetClusters();
1484 if (!fRecPoints) AliError("Missing cluster array");
1488 //_____________________________________________________________________________
1489 TClonesArray *AliTRDclusterizer::TrackletsArray(const TString &trkltype)
1492 // Returns the array of on-line tracklets
1494 fTracklets = AliTRDReconstructor::GetTracklets(trkltype.Data());
1495 if (!fTracklets) AliError("Missing online tracklets array");
1499 //_____________________________________________________________________________
1500 TClonesArray* AliTRDclusterizer::TracksArray()
1502 // return array of GTU tracks (create TClonesArray if necessary)
1504 fTracks = AliTRDReconstructor::GetTracks();
1505 if (!fTracks) AliError("Missing online tracks array");