Coverity
[u/mrichter/AliRoot.git] / TRD / AliTRDReconstructor.cxx
index 26fe457..df5eb5c 100644 (file)
 #include <TObjArray.h>
 #include <TTreeStream.h>
 #include <TDirectory.h>
+#include <TRef.h>
 
 #include "AliRawReader.h"
+#include "AliLog.h"
 
 #include "AliTRDReconstructor.h"
 #include "AliTRDclusterizer.h"
 #include "AliTRDrawStream.h"
 #include "AliTRDdigitsManager.h"
 #include "AliTRDtrackerV1.h"
+#include "AliESDEvent.h"
+#include "AliESDTrdTrack.h"
+#include "AliESDTrdTracklet.h"
+#include "AliESDTrdTrigger.h"
+#include "AliTRDtrackletWord.h"
 
 #define SETFLG(n,f) ((n) |= f)
 #define CLRFLG(n,f) ((n) &= ~f)
 
 ClassImp(AliTRDReconstructor)
 
+AliESDTrdTrigger AliTRDReconstructor::fgTriggerFlags;
 TClonesArray *AliTRDReconstructor::fgClusters = NULL;
 TClonesArray *AliTRDReconstructor::fgTracklets = NULL;
+TClonesArray *AliTRDReconstructor::fgTracks = NULL;
 Char_t const * AliTRDReconstructor::fgSteerNames[kNsteer] = {
   "DigitsConversion       "
  ,"Write Clusters         "
@@ -118,6 +127,11 @@ AliTRDReconstructor::~AliTRDReconstructor()
     delete fgTracklets;
     fgTracklets = NULL;
   }
+  if(fgTracks) {
+    fgTracks->Delete();
+    delete fgTracks;
+    fgTracks = NULL;
+  }
   if(fSteerParam&kOwner){
     for(Int_t itask = 0; itask < AliTRDrecoParam::kTRDreconstructionTasks; itask++)
       if(fDebugStream[itask]) delete fDebugStream[itask];
@@ -171,6 +185,17 @@ void AliTRDReconstructor::ConvertDigits(AliRawReader *rawReader
   manager->WriteDigits();
   delete manager;
 
+  // take over ownership of online tracklets
+  fgTracklets = rawData.TrackletsArray();
+  rawData.SetTrackletsOwner(0x0);
+
+  // take over GTU tracks
+  fgTracks = rawData.TracksArray();
+  rawData.SetTracksOwner(0x0);
+
+  for (Int_t iSector = 0; iSector < 18; iSector++) {
+    fgTriggerFlags.SetFlags(iSector, rawData.GetTriggerFlags(iSector));
+  }
 }
 
 //_____________________________________________________________________________
@@ -200,15 +225,23 @@ void AliTRDReconstructor::Reconstruct(AliRawReader *rawReader
   
   fgNTimeBins = fClusterizer->GetNTimeBins();
   
+  // take over ownership of online tracklets
+  fgTracklets = fClusterizer->TrackletsArray();
+  fClusterizer->SetTrackletsOwner(kFALSE);
+
+  // take over GTU tracks
+  fgTracks = fClusterizer->TracksArray();
+  fClusterizer->SetTracksOwner(kFALSE);
+
+  for (Int_t iSector = 0; iSector < 18; iSector++) {
+    fgTriggerFlags.SetFlags(iSector, fClusterizer->GetTriggerFlags(iSector));
+  }
+
   if(IsWritingClusters()) return;
 
   // take over ownership of clusters
   fgClusters = fClusterizer->RecPoints();
   fClusterizer->SetClustersOwner(kFALSE);
-
-  // take over ownership of online tracklets
-  fgTracklets = fClusterizer->TrackletsArray();
-  fClusterizer->SetTrackletsOwner(kFALSE);
 }
 
 //_____________________________________________________________________________
@@ -227,6 +260,18 @@ void AliTRDReconstructor::Reconstruct(TTree *digitsTree
   clusterer.ReadDigits(digitsTree);
   clusterer.MakeClusters();
 
+  // read tracklets and tracks if not done during reading of raw data
+  if (!fgTracklets) {
+    clusterer.ReadTracklets();
+    fgTracklets = clusterer.TrackletsArray();
+    clusterer.SetTrackletsOwner(kFALSE);
+  }
+  if (!fgTracks) {
+    clusterer.ReadTracks();
+    fgTracks = clusterer.TracksArray();
+    clusterer.SetTracksOwner(kFALSE);
+  }
+
   fgNTimeBins = clusterer.GetNTimeBins();
 
   if(IsWritingClusters()) return;
@@ -234,11 +279,6 @@ void AliTRDReconstructor::Reconstruct(TTree *digitsTree
   // take over ownership of clusters
   fgClusters = clusterer.RecPoints();
   clusterer.SetClustersOwner(kFALSE);
-
-  // take over ownership of online tracklets
-  fgTracklets = clusterer.TrackletsArray();
-  clusterer.SetTrackletsOwner(kFALSE);
-
 }
 
 //_____________________________________________________________________________
@@ -258,12 +298,132 @@ AliTracker *AliTRDReconstructor::CreateTracker() const
 //_____________________________________________________________________________
 void AliTRDReconstructor::FillESD(TTree* /*digitsTree*/
         , TTree* /*clusterTree*/
-        , AliESDEvent* /*esd*/) const
+        , AliESDEvent* esd) const
 {
   //
   // Fill ESD
   //
 
+  // ----- filling tracklets -----
+  Int_t trackletIndex[1080] = { 0 };
+  AliDebug(1, Form("Filling tracklets from %p (%i)",
+                  fgTracklets, fgTracklets ? fgTracklets->GetEntriesFast() : 0));
+  if (fgTracklets) {
+    Int_t nTracklets = fgTracklets->GetEntriesFast();
+
+    Int_t lastHC = -1;
+    for (Int_t iTracklet = 0; iTracklet < nTracklets; iTracklet++) {
+      AliTRDtrackletBase *trkl = (AliTRDtrackletBase*) ((*fgTracklets)[iTracklet]);
+      Int_t hc = trkl->GetHCId();
+      //      hc += trkl->GetY() > 0 ? 1 : 0;
+      if ((hc < 0) || (hc >= 1080)) {
+       AliError(Form("HC for tracklet: 0x%08x out of range: %i", trkl->GetTrackletWord(), trkl->GetHCId()));
+       continue;
+      }
+      AliDebug(5, Form("hc: %4i : 0x%08x z: %2i", hc, trkl->GetTrackletWord(), trkl->GetZbin()));
+      if (hc != lastHC) {
+       AliDebug(2, Form("set tracklet index for HC %i to %i", hc, iTracklet));
+       trackletIndex[hc] = iTracklet + 1;
+       lastHC = hc;
+      }
+    }
+
+    for (Int_t iDet = 0; iDet < 540; iDet++) {
+      Int_t trklIndexA = trackletIndex[2*iDet + 0] - 1;
+      Int_t trklIndexB = trackletIndex[2*iDet + 1] - 1;
+      Int_t trklIndex  = esd->GetNumberOfTrdTracklets();
+      AliTRDtrackletBase *trklA = trklIndexA > -1 ? (AliTRDtrackletBase*) ((*fgTracklets)[trklIndexA]) : 0x0;
+      AliTRDtrackletBase *trklB = trklIndexB > -1 ? (AliTRDtrackletBase*) ((*fgTracklets)[trklIndexB]) : 0x0;
+      while (trklA != 0x0 || trklB != 0x0) {
+       AliDebug(5, Form("det %i - A: %i/%i -> %p, B: %i/%i -> %p",
+                        iDet, trklIndexA, nTracklets, trklA, trklIndexB, nTracklets, trklB));
+       if (trklA == 0x0) {
+         AliESDTrdTracklet esdTracklet(trklB->GetTrackletWord(), trklB->GetHCId());
+         esd->AddTrdTracklet(&esdTracklet);
+         trklIndexB++;
+         trklB = trklIndexB < nTracklets ? (AliTRDtrackletBase*) ((*fgTracklets)[trklIndexB]) : 0x0;
+         if (trklB && trklB->GetHCId() != 2*iDet + 1)
+           trklB = 0x0;
+       }
+       else if (trklB == 0x0) {
+         AliESDTrdTracklet esdTracklet(trklA->GetTrackletWord(), trklA->GetHCId());
+         esd->AddTrdTracklet(&esdTracklet);
+         trklIndexA++;
+         trklA = trklIndexA < nTracklets ? (AliTRDtrackletBase*) ((*fgTracklets)[trklIndexA]) : 0x0;
+         if (trklA && trklA->GetHCId() != 2*iDet)
+           trklA = 0x0;
+       }
+       else {
+         if ((trklA->GetZbin() < trklB->GetZbin()) ||
+             ((trklA->GetZbin() == trklB->GetZbin()) && (trklA->GetYbin() < trklB->GetYbin()))) {
+           AliESDTrdTracklet esdTracklet(trklA->GetTrackletWord(), trklA->GetHCId());
+           esd->AddTrdTracklet(&esdTracklet);
+           trklIndexA++;
+           trklA = trklIndexA < nTracklets ? (AliTRDtrackletBase*) ((*fgTracklets)[trklIndexA]) : 0x0;
+           if (trklA && trklA->GetHCId() != 2*iDet)
+             trklA = 0x0;
+         }
+         else {
+           AliESDTrdTracklet esdTracklet(trklB->GetTrackletWord(), trklB->GetHCId());
+           esd->AddTrdTracklet(&esdTracklet);
+           trklIndexB++;
+           trklB = trklIndexB < nTracklets ? (AliTRDtrackletBase*) ((*fgTracklets)[trklIndexB]) : 0x0;
+           if (trklB && trklB->GetHCId() != 2*iDet + 1)
+             trklB = 0x0;
+         }
+       }
+      }
+      // updating tracklet indices as in ESD
+      if (esd->GetNumberOfTrdTracklets() != trklIndex) {
+       trackletIndex[2*iDet + 0] = trackletIndex[2*iDet + 1] = trklIndex;
+      }
+      else
+       trackletIndex[2*iDet + 0] = trackletIndex[2*iDet + 1] = -1;
+    }
+  }
+
+  // ----- filling GTU tracks -----
+  AliDebug(1, Form("Now filling ESD with GTU tracks from %p (%i)",
+                  fgTracks, fgTracks ? fgTracks->GetEntriesFast() : 0));
+  if (fgTracks) {
+    for (Int_t iTrack = 0; iTrack < fgTracks->GetEntriesFast(); iTrack++) {
+      AliESDTrdTrack *trdTrack = (AliESDTrdTrack*) ((*fgTracks)[iTrack]);
+
+      UInt_t mask  = trdTrack->GetLayerMask();
+      UInt_t stack = trdTrack->GetStack();
+
+      for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
+       if (mask & (1 << iLayer)) {
+
+         Int_t det = trdTrack->GetSector()*30 + stack*6 + iLayer;
+         Int_t idx = trdTrack->GetTrackletIndex(iLayer);
+
+         if ((det < 0) || (det > 539)) {
+           AliError(Form("Invalid detector no. from track: %i", 2*det));
+           continue;
+         }
+         if (trackletIndex[2*det] >= 0) {
+           if ((trackletIndex[2*det] + idx > -1) &&
+               (trackletIndex[2*det] + idx < esd->GetNumberOfTrdTracklets())) {
+             AliESDTrdTracklet *trkl = esd->GetTrdTracklet(trackletIndex[2*det] + idx);
+             if (trkl) {
+               AliDebug(5, Form("adding tracklet: 0x%08x", trkl->GetTrackletWord()));
+               trdTrack->AddTrackletReference(trkl, iLayer);
+             }
+           }
+         }
+       }
+      }
+      // only add the track when it's complete (including tracklet references)
+      esd->AddTrdTrack(trdTrack);
+    }
+  }
+
+  esd->SetTrdTrigger(&fgTriggerFlags);
+
+  // clearing variables for next event
+  fgTracklets = 0x0;
+  fgTracks = 0x0;
 }
 
 //_____________________________________________________________________________