Issue AliError instead of AliFatal in case of inconsistent number of tracks
[u/mrichter/AliRoot.git] / STEER / AliDetector.cxx
index 14c7a07..71d748b 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-Revision 1.18  2002/08/26 13:51:17  hristov
-Remaping of track references at the end of each primary particle (M.Ivanov)
-
-Revision 1.17  2002/05/24 13:29:58  hristov
-AliTrackReference added, AliDisplay modified
-
-Revision 1.16  2001/10/04 15:30:56  hristov
-Changes to accommodate the set of PHOS folders and tasks (Y.Schutz)
-
-Revision 1.15  2001/07/27 13:03:13  hristov
-Default Branch split level set to 99
-
-Revision 1.14  2001/05/21 17:22:51  buncic
-Fixed problem with missing AliConfig while reading galice.root
-
-Revision 1.13  2001/05/16 14:57:22  alibrary
-New files for folders and Stack
-
-Revision 1.12  2001/03/12 17:47:03  hristov
-Changes needed on Sun with CC 5.0
-
-Revision 1.11  2001/01/26 19:58:46  hristov
-Major upgrade of AliRoot code
-
-Revision 1.10  2001/01/17 10:50:50  hristov
-Corrections to destructors
-
-Revision 1.9  2000/12/12 18:19:06  alibrary
-Introduce consistency check when loading points
-
-Revision 1.8  2000/11/30 07:12:48  alibrary
-Introducing new Rndm and QA classes
-
-Revision 1.7  2000/10/02 21:28:14  fca
-Removal of useless dependecies via forward declarations
-
-Revision 1.6  2000/07/12 08:56:25  fca
-Coding convention correction and warning removal
-
-Revision 1.5  1999/09/29 09:24:29  fca
-Introduction of the Copyright and cvs Log
-
-*/
+/* $Id$ */
 
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
@@ -77,49 +33,73 @@ Introduction of the Copyright and cvs Log
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
-#include <assert.h>
-#include <iostream.h>
-
-#include <TTree.h>
 #include <TBrowser.h>
-#include <TFile.h>
-#include <TROOT.h>
-#include <TFolder.h>
+#include <TTree.h>
 
+#include "AliLog.h"
 #include "AliConfig.h"
 #include "AliDetector.h"
-#include "AliRun.h"
 #include "AliHit.h"
 #include "AliPoints.h"
-#include "AliTrackReference.h"
-
-
-// Static variables for the hit iterator routines
-static Int_t sMaxIterHit=0;
-static Int_t sCurIterHit=0;
+#include "AliLoader.h"
+#include "AliRun.h"
+#include "AliMC.h"
 
 
 ClassImp(AliDetector)
  
-//_____________________________________________________________________________
-AliDetector::AliDetector()
+//_______________________________________________________________________
+AliDetector::AliDetector():
+  AliModule(),
+  fTimeGate(200.e-9),
+  fIshunt(0),
+  fNhits(0),
+  fNdigits(0),
+  fBufferSize(1600),
+  fMaxIterHit(0),
+  fCurIterHit(0),
+  fHits(0),
+  fDigits(0),
+  fPoints(0),
+  fLoader(0x0)
 {
   //
   // Default constructor for the AliDetector class
   //
-  fNhits      = 0;
-  fNdigits    = 0;
-  fPoints     = 0;
-  fHits       = 0;
-  fTrackReferences =0;
-  fDigits     = 0;
-  fTimeGate   = 200.e-9;
-  fBufferSize = 16000;
-  fDigitsFile = 0;
 }
  
+//_______________________________________________________________________
+AliDetector::AliDetector(const AliDetector &det):
+  AliModule(det),
+  fTimeGate(200.e-9),
+  fIshunt(0),
+  fNhits(0),
+  fNdigits(0),
+  fBufferSize(1600),
+  fMaxIterHit(0),
+  fCurIterHit(0),
+  fHits(0),
+  fDigits(0),
+  fPoints(0),
+  fLoader(0x0)
+{
+  det.Copy(*this);
+}
+
 //_____________________________________________________________________________
-AliDetector::AliDetector(const char* name,const char *title):AliModule(name,title)
+AliDetector::AliDetector(const char* name,const char *title):
+  AliModule(name,title),
+  fTimeGate(200.e-9),
+  fIshunt(0),
+  fNhits(0),
+  fNdigits(0),
+  fBufferSize(1600),
+  fMaxIterHit(0),
+  fCurIterHit(0),
+  fHits(0),
+  fDigits(0),
+  fPoints(0),
+  fLoader(0x0)
 {
   //
   // Normal constructor invoked by all Detectors.
@@ -127,33 +107,18 @@ AliDetector::AliDetector(const char* name,const char *title):AliModule(name,titl
   // Add this Detector to the global list of Detectors in Run.
   //
 
-  fTimeGate   = 200.e-9;
   fActive     = kTRUE;
-  fNhits      = 0;
-  fHits       = 0;
-  fTrackReferences =0;
-  fDigits     = 0;
-  fNdigits    = 0;
-  fPoints     = 0;
-  fBufferSize = 16000;
-  fDigitsFile = 0;
-
   AliConfig::Instance()->Add(this);
 
-  fTrackReferences        = new TClonesArray("AliTrackReference", 100);
-  //if detector to want to create another track reference - than let's be free 
-  
 }
  
-//_____________________________________________________________________________
+//_______________________________________________________________________
 AliDetector::~AliDetector()
 {
   //
   // Destructor
   //
-  fNhits      = 0;
-  fNdigits    = 0;
-  //
+
   // Delete space point structure
   if (fPoints) {
     fPoints->Delete();
@@ -166,111 +131,69 @@ AliDetector::~AliDetector()
     delete fDigits;
     fDigits     = 0;
   }
-  if (fDigitsFile) delete [] fDigitsFile;
+
+  if (fLoader)
+   {
+    fLoader->GetModulesFolder()->Remove(this);
+   }
+
 }
 
-//_____________________________________________________________________________
-void AliDetector::Publish(const char *dir, void *address, const char *name)
+//_______________________________________________________________________
+void AliDetector::Publish(const char */*dir*/, void */*address*/, const char */*name*/) const
 {
-  //
-  // Register pointer to detector objects. 
-  // 
-  TFolder *topFolder = (TFolder *)gROOT->FindObjectAny("/Folders");
-  if  (topFolder) { 
-    TFolder *folder = (TFolder *)topFolder->FindObjectAny(dir);
-    // TFolder *folder = (TFolder *)gROOT->FindObjectAny(dir);
-    if (!folder)  {
-      cerr << "Cannot register: Missing folder: " << dir << endl;
-    } else {
-      TFolder *subfolder = (TFolder *) folder->FindObjectAny(this->GetName()); 
-
-      if(!subfolder)
-         subfolder = folder->AddFolder(this->GetName(),this->GetTitle());
-      if (address) {
-        TObject **obj = (TObject **) address;
-        if ((*obj)->InheritsFrom(TCollection::Class())) {
-           TCollection *collection = (TCollection *) (*obj); 
-           if (name)
-             collection->SetName(name);
-        } 
-        subfolder->Add(*obj);
-      } 
-    }  
-  }
+//
+// Register pointer to detector objects. 
+// 
+  MayNotUse("Publish");
 }
 
-//_____________________________________________________________________________
-TBranch* AliDetector::MakeBranchInTree(TTree *tree, const char* name, void* address, Int_t size,const char *file)
+//_______________________________________________________________________
+TBranch* AliDetector::MakeBranchInTree(TTree *tree, const char* name, 
+                                       void* address, Int_t size,
+                                       const char *file)
 { 
     return(MakeBranchInTree(tree,name,0,address,size,99,file));
 }
 
-//_____________________________________________________________________________
-TBranch* AliDetector::MakeBranchInTree(TTree *tree, const char* name, const char *classname, void* address,Int_t size, Int_t splitlevel, const char *file)
+//_______________________________________________________________________
+TBranch* AliDetector::MakeBranchInTree(TTree *tree, const char* name, 
+                                       const char *classname, 
+                                       void* address,Int_t size, 
+                                       Int_t splitlevel, const char */*file*/)
 { 
-    //
-    // Makes branch in given tree and diverts them to a separate file
-    //  
-    if (GetDebug()>1)
-      printf("* MakeBranch * Making Branch %s \n",name);
-      
-    TDirectory *cwd = gDirectory;
-    TBranch *branch = 0;
+//
+// Makes branch in given tree and diverts them to a separate file
+// 
+//
+//
     
-    if (classname) {
-      branch = tree->Branch(name,classname,address,size,splitlevel);
-    } else {
-      branch = tree->Branch(name,address,size);
-    }
-       
-    if (file) {
-        char * outFile = new char[strlen(gAlice->GetBaseFile())+strlen(file)+2];
-        sprintf(outFile,"%s/%s",gAlice->GetBaseFile(),file);
-        branch->SetFile(outFile);
-        TIter next( branch->GetListOfBranches());
-        while ((branch=(TBranch*)next())) {
-           branch->SetFile(outFile);
-        } 
-       delete outFile;
-        
-       cwd->cd();
-        
-       if (GetDebug()>1)
-           printf("* MakeBranch * Diverting Branch %s to file %s\n",name,file);
-    }
-    char *folder = 0;
-    TString folderName(name);  
-    
-    if (!strncmp(tree->GetName(),"TreeE",5)) folder = "RunMC/Event/Data";
-    if (!strncmp(tree->GetName(),"TreeK",5)) folder = "RunMC/Event/Data";
-    if (!strncmp(tree->GetName(),"TreeH",5)) {
-      folder     = "RunMC/Event/Data/Hits";
-      folderName = "Hits" ; 
-    }
-    if (!strncmp(tree->GetName(),"TreeTrackReferences",5)) {
-      folder     = "RunMC/Event/Data/TrackReferences";
-      folderName = "TrackReferences" ; 
-    }
-
-    if (!strncmp(tree->GetName(),"TreeD",5)) {
-      folder     = "Run/Event/Data";
-      folderName = "Digits" ; 
-    }
-    if (!strncmp(tree->GetName(),"TreeS",5)) {
-      folder     = "RunMC/Event/Data/SDigits";
-      folderName = "SDigits" ; 
-    }
-    if (!strncmp(tree->GetName(),"TreeR",5)) folder = "Run/Event/RecData";
-
-    if (folder) {
-      if (GetDebug())
-          printf("%15s: Publishing %s to %s\n",ClassName(),name,folder);
-      Publish(folder,address, folderName.Data());
-    }  
+ AliDebug(2,Form("Making Branch %s",name));
+ if (tree == 0x0) 
+  {
+   AliError(Form("Making Branch %s Tree is NULL",name));
+   return 0x0;
+  }
+ TBranch *branch = tree->GetBranch(name);
+ if (branch) 
+  {  
+    AliDebug(2,Form("Branch %s is already in tree.",name));
     return branch;
+  }
+    
+ if (classname) 
+  {
+    branch = tree->Branch(name,classname,address,size,splitlevel);
+  } 
+ else 
+  {
+    branch = tree->Bronch(name, "TClonesArray", address, size, splitlevel);
+  }
+ AliDebug(2,Form("Branch %s returning branch %#x",name,branch));
+ return branch;
 }
 
-//_____________________________________________________________________________
+//_______________________________________________________________________
 void AliDetector::Browse(TBrowser *b)
 {
   //
@@ -289,16 +212,16 @@ void AliDetector::Browse(TBrowser *b)
   }
 }
 
-//_____________________________________________________________________________
-void AliDetector::Copy(AliDetector &det) const
+//_______________________________________________________________________
+void AliDetector::Copy(TObject &) const
 {
   //
   // Copy *this onto det -- not implemented
   //
-  Fatal("Copy","Not implemented~\n");
+  AliFatal("Not implemented");
 }
 
-//_____________________________________________________________________________
+//_______________________________________________________________________
 void AliDetector::FinishRun()
 {
   //
@@ -306,26 +229,7 @@ void AliDetector::FinishRun()
   //
 }
 
-
-
-void AliDetector::RemapTrackReferencesIDs(Int_t *map)
-{
-  // 
-  //remaping track reference
-  //called at finish primary
-  if (!fTrackReferences) return;
-  for (Int_t i=0;i<fTrackReferences->GetEntries();i++){
-    AliTrackReference * ref = (AliTrackReference*) fTrackReferences->UncheckedAt(i);
-    if (ref) {
-      Int_t newID = map[ref->GetTrack()];
-      if (newID>=0) ref->SetTrack(newID);
-      else ref->SetTrack(-1);
-      
-    }
-  }
-}
-
-//_____________________________________________________________________________
+//_______________________________________________________________________
 AliHit* AliDetector::FirstHit(Int_t track)
 {
   //
@@ -336,84 +240,52 @@ AliHit* AliDetector::FirstHit(Int_t track)
   // track is returned
   // 
   if(track>=0) {
-    gAlice->ResetHits();
-    gAlice->TreeH()->GetEvent(track);
-  }
-  //
-  sMaxIterHit=fHits->GetEntriesFast();
-  sCurIterHit=0;
-  if(sMaxIterHit) return (AliHit*) fHits->UncheckedAt(0);
-  else            return 0;
-}
-
-
-//_____________________________________________________________________________
-AliTrackReference* AliDetector::FirstTrackReference(Int_t track)
-{
-  //
-  // Initialise the hit iterator
-  // Return the address of the first hit for track
-  // If track>=0 the track is read from disk
-  // while if track<0 the first hit of the current
-  // track is returned
-  // 
-  if(track>=0) {
-    gAlice->ResetTrackReferences();
-    gAlice->TreeTR()->GetEvent(track);
+    gAlice->ResetHits(); //stupid = if N detector this method is called N times
+    TreeH()->GetEvent(track); //skowron
   }
   //
-  fMaxIterTrackRef     = fTrackReferences->GetEntriesFast();
-  fCurrentIterTrackRef = 0;
-  if(fMaxIterTrackRef) return (AliTrackReference*) fTrackReferences->UncheckedAt(0);
+  fMaxIterHit=fHits->GetEntriesFast();
+  fCurIterHit=0;
+  if(fMaxIterHit) return dynamic_cast<AliHit*>(fHits->UncheckedAt(0));
   else            return 0;
 }
 
-
-
-//_____________________________________________________________________________
+//_______________________________________________________________________
 AliHit* AliDetector::NextHit()
 {
   //
   // Return the next hit for the current track
   //
-  if(sMaxIterHit) {
-    if(++sCurIterHit<sMaxIterHit) 
-      return (AliHit*) fHits->UncheckedAt(sCurIterHit);
-    else        
-      return 0;
-  } else {
-    printf("* AliDetector::NextHit * Hit Iterator called without calling FistHit before\n");
-    return 0;
-  }
-}
-//_____________________________________________________________________________
-AliTrackReference* AliDetector::NextTrackReference()
-{
-  //
-  // Return the next hit for the current track
-  //
-  if(fMaxIterTrackRef) {
-    if(++fCurrentIterTrackRef<fMaxIterTrackRef) 
-      return (AliTrackReference*) fTrackReferences->UncheckedAt(fCurrentIterTrackRef);
+  if(fMaxIterHit) {
+    if(++fCurIterHit<fMaxIterHit) 
+      return dynamic_cast<AliHit*>(fHits->UncheckedAt(fCurIterHit));
     else        
       return 0;
   } else {
-    printf("* AliDetector::NextTrackReference * TrackReference  Iterator called without calling FistTrackReference before\n");
+    AliWarning("Hit Iterator called without calling FistHit before");
     return 0;
   }
 }
 
-//_____________________________________________________________________________
+//_______________________________________________________________________
 void AliDetector::LoadPoints(Int_t)
 {
   //
   // Store x, y, z of all hits in memory
   //
-  if (fHits == 0) return;
+  if (fHits == 0) 
+   {
+    AliError(Form("fHits == 0. Name is %s",GetName()));
+    return;
+   }
   //
   Int_t nhits = fHits->GetEntriesFast();
-  if (nhits == 0) return;
-  Int_t tracks = gAlice->GetNtrack();
+  if (nhits == 0) 
+   {
+//    Error("LoadPoints","nhits == 0. Name is %s",GetName());
+    return;
+   }
+  Int_t tracks = gAlice->GetMCApp()->GetNtrack();
   if (fPoints == 0) fPoints = new TObjArray(tracks);
   AliHit *ahit;
   //
@@ -433,22 +305,29 @@ void AliDetector::LoadPoints(Int_t)
   //
   // Loop over all the hits and store their position
   for (Int_t hit=0;hit<nhits;hit++) {
-    ahit = (AliHit*)fHits->UncheckedAt(hit);
+    ahit = dynamic_cast<AliHit*>(fHits->UncheckedAt(hit));
     trk=ahit->GetTrack();
-    assert(trk<=tracks);
-    if(ntrk[trk]==limi[trk]) {
+    if(trk>tracks) {
+      AliError(Form("Found track number %d, max track %d",trk, tracks));
+      continue;
+    }
+    if(ntrk[trk]==limi[trk])
+     {
       //
       // Initialise a new track
       fp=new Float_t[3*(limi[trk]+chunk)];
-      if(coor[trk]) {
-       memcpy(fp,coor[trk],sizeof(Float_t)*3*limi[trk]);
-       delete [] coor[trk];
-      }
+      if(coor[trk]) 
+       {
+          memcpy(fp,coor[trk],sizeof(Float_t)*3*limi[trk]);
+          delete [] coor[trk];
+       }
       limi[trk]+=chunk;
       coor[trk] = fp;
-    } else {
+     } 
+    else 
+     {
       fp = coor[trk];
-    }
+     }
     fp[3*ntrk[trk]  ] = ahit->X();
     fp[3*ntrk[trk]+1] = ahit->Y();
     fp[3*ntrk[trk]+2] = ahit->Z();
@@ -473,89 +352,43 @@ void AliDetector::LoadPoints(Int_t)
   delete [] limi;
 }
 
-//_____________________________________________________________________________
-void AliDetector::MakeBranch(Option_t *option, const char *file)
+//_______________________________________________________________________
+void AliDetector::MakeBranch(Option_t *option)
 {
   //
-  // Create a new branch in the current Root Tree
-  // The branch of fHits is automatically split
-  //
-  char branchname[10];
-  sprintf(branchname,"%s",GetName());
+  // Create a new branch for this detector in its treeH
   //
-  // Get the pointer to the header
+
+  AliDebug(2,Form(" for %s",GetName()));
   const char *cH = strstr(option,"H");
-  //
-  if (fHits && gAlice->TreeH() && cH) {
-    MakeBranchInTree(gAlice->TreeH(), 
-                     branchname, &fHits, fBufferSize, file) ;              
-  }    
-  
-  const char *cD = strstr(option,"D");
 
-  if (cD) {
-    if (file) {
-       fDigitsFile = new char[strlen (file)];
-       strcpy(fDigitsFile,file);
-    }
-  }
-}
-//_____________________________________________________________________________
-void AliDetector::MakeBranchTR(Option_t *option, const char *file)
-{
-  //
-  // Create a new branch in the current Root Tree
-  // The branch of fHits is automatically split
-  //
-  char branchname[10];
-  sprintf(branchname,"%s",GetName());
-  //
-  // Get the pointer to the header
-  const char *cTR = strstr(option,"T");
-  //
-  if (fTrackReferences && gAlice->TreeTR() && cTR) {
-    MakeBranchInTree(gAlice->TreeTR(), 
-                     branchname, &fTrackReferences, fBufferSize, file) ;              
-  }      
+  if (fHits && TreeH() && cH) 
+   {
+     MakeBranchInTree(TreeH(), GetName(), &fHits, fBufferSize, 0);
+   }   
 }
 
-//_____________________________________________________________________________
+//_______________________________________________________________________
 void AliDetector::ResetDigits()
 {
   //
   // Reset number of digits and the digits array
   //
   fNdigits   = 0;
-  if (fDigits)   fDigits->Clear();
+  if (fDigits) fDigits->Clear();
 }
 
-//_____________________________________________________________________________
+//_______________________________________________________________________
 void AliDetector::ResetHits()
 {
   //
   // Reset number of hits and the hits array
   //
   fNhits   = 0;
-  if (fHits)   fHits->Clear();
-}
-
-
-
-//_____________________________________________________________________________
-void AliDetector::ResetTrackReferences()
-{
-  //
-  // Reset number of hits and the hits array
-  //
-  fMaxIterTrackRef   = 0;
-  if (fTrackReferences)   fTrackReferences->Clear();
+  if (fHits) fHits->Clear();
 }
 
-
-
-//_____________________________________________________________________________
+//_______________________________________________________________________
 void AliDetector::ResetPoints()
 {
   //
@@ -568,38 +401,84 @@ void AliDetector::ResetPoints()
   }
 }
 
-//_____________________________________________________________________________
+//_______________________________________________________________________
 void AliDetector::SetTreeAddress()
 {
   //
   // Set branch address for the Hits and Digits Trees
   //
   TBranch *branch;
-  char branchname[20];
-  sprintf(branchname,"%s",GetName());
   //
   // Branch address for hit tree
-  TTree *treeH = gAlice->TreeH();
-  if (treeH && fHits) {
-    branch = treeH->GetBranch(branchname);
-    if (branch) branch->SetAddress(&fHits);
+  
+  TTree *tree = TreeH();
+  if (tree && fHits) {
+    branch = tree->GetBranch(GetName());
+    if (branch) 
+     {
+       AliDebug(2,Form("(%s) Setting for Hits",GetName()));
+       branch->SetAddress(&fHits);
+     }
+    else
+     { //can be invoked before branch creation
+       AliDebug(2,Form("(%s) Failed for Hits. Can not find branch in tree.",GetName()));
+     }
   }
+  
   //
   // Branch address for digit tree
-  TTree *treeD = gAlice->TreeD();
+  TTree *treeD = fLoader->TreeD();
   if (treeD && fDigits) {
-    branch = treeD->GetBranch(branchname);
+    branch = treeD->GetBranch(GetName());
     if (branch) branch->SetAddress(&fDigits);
   }
+  
+  AliModule::SetTreeAddress();
+}
 
-  // Branch address for tr  tree
-  TTree *treeTR = gAlice->TreeTR();
-  if (treeTR && fTrackReferences) {
-    branch = treeTR->GetBranch(branchname);
-    if (branch) branch->SetAddress(&fTrackReferences);
-  }
-
-
+//_______________________________________________________________________
+void AliDetector::MakeTree(Option_t *option)
+ {
+ //makes a tree (container) for the data defined in option
+ //"H" - hits
+ //"D" - digits
+ //"S" - summable digits
+ //"R" - recontructed points and tracks
+    AliLoader* loader = GetLoader();
+    if (loader == 0x0)
+     {
+       AliError(Form("Can not get loader for %s",GetName()));
+       return;
+     }
+    loader->MakeTree(option); //delegate this job to getter
+ }
+
+//_______________________________________________________________________
+AliLoader* AliDetector::MakeLoader(const char* topfoldername)
+{ 
+//builds standard getter (AliLoader type)
+//if detector wants to use castomized getter, it must overload this method
+
+ AliDebug(1,Form("Creating standard getter for detector %s. Top folder is %s.",
+         GetName(),topfoldername));
+     
+ fLoader = new AliLoader(GetName(),topfoldername);
+ return fLoader;
 }
 
+//_______________________________________________________________________
+TTree* AliDetector::TreeH() const
+{
+//Get the hits container from the folder
+  if (GetLoader() == 0x0) 
+    {
+    //sunstitude this with make getter when we can obtain the event folder name 
+     AliError("Can not get the getter");
+     return 0x0;
+    }
  
+  TTree* tree = (TTree*)GetLoader()->TreeH();
+  return tree;
+}
+