Corrected sintax of the commands in ProcessLine (Yves)
[u/mrichter/AliRoot.git] / ANALYSIS / AliReaderAOD.cxx
index 2a4b92e..234f8dc 100644 (file)
@@ -1,20 +1,63 @@
-#include "AliReaderAOD.h"
+/**************************************************************************
+ * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ *                                                                        *
+ * Author: The ALICE Off-line Project.                                    *
+ * Contributors are mentioned in the code where appropriate.              *
+ *                                                                        *
+ * Permission to use, copy, modify and distribute this software and its   *
+ * documentation strictly for non-commercial purposes is hereby granted   *
+ * without fee, provided that the above copyright notice appears in all   *
+ * copies and that both the copyright notice and this permission notice   *
+ * appear in the supporting documentation. The authors make no claims     *
+ * about the suitability of this software for any purpose. It is          *
+ * provided "as is" without express or implied warranty.                  *
+ **************************************************************************/
+
+/* $Id$ */
+
+//______________________________________________________________________________
+////////////////////////////////////////////////////////////////////////////////
+//                                                                            //
+// class AliReaderAOD                                                         //
+//                                                                            //
+// Reader and Writer for AOD format.                                          //
+// AODs are stored in a tree named by the variable fgkTreeName.               //
+// There is stored 1 or 2 branches. Each of them stores AOD objects           //
+// First branch is named by the variable fgkReconstructedDataBranchName       //
+// ("reconstructed.") and keeps reconstructed data.                           //
+// Second branch is called by the variable fgkSimulatedDataBranchName         //
+// ("simulated.") and stores Monte carlo truth. If both branches are present  //
+// AODs are parallel, i.e. nth particle in one branch corresponds to the nth  //
+// particle in the other one.                                                 //
+//                                                                            //
+// Since we accept different formats of particles that are stored in AODs     //
+// reader must take care of that fact: clean buffer if the next file contains //
+// different particle type.                                                   //
+//                                                                            //
+// Piotr.Skowronski@cern.ch                                                   //
+//                                                                            //
+////////////////////////////////////////////////////////////////////////////////
 
-ClassImp(AliReaderAOD)
 
 #include <TError.h>
 #include <TFile.h>
 #include <TTree.h>
-#include "AliAOD.h"
+#include <TH1.h>
 
+#include "AliAOD.h"
+#include "AliLog.h"
+#include "AliReaderAOD.h"
 
 const TString AliReaderAOD::fgkTreeName("TAOD");
-const TString AliReaderAOD::fgkRecosntructedDataBranchName("reconstructed.");
+const TString AliReaderAOD::fgkReconstructedDataBranchName("reconstructed.");
 const TString AliReaderAOD::fgkSimulatedDataBranchName("simulated.");
 
+ClassImp(AliReaderAOD)
+
 AliReaderAOD::AliReaderAOD(const Char_t* aodfilename):
  fFileName(aodfilename),
  fReadSim(kFALSE),
+ fReadRec(kTRUE),
  fTree(0x0),
  fFile(0x0),
  fSimBuffer(0x0),
@@ -60,8 +103,8 @@ Int_t AliReaderAOD::ReadNext()
     {
       if (fFile == 0x0)
        {
-         Int_t opened = OpenFile(fCurrentDir);//rl is opened here
-         if (opened)
+         Int_t openfailed = OpenFile(fCurrentDir);//rl is opened here
+         if (openfailed)
            {
              //Error("ReadNext","Error Occured while opening directory number %d",fCurrentDir);
              fCurrentDir++;
@@ -69,33 +112,47 @@ Int_t AliReaderAOD::ReadNext()
            }
          fCurrentEvent = 0;
        }
+      //Tree must exist because OpenFile would reuturn error in the other case
+      if ( fCurrentEvent >= fTree->GetEntries() )
+       {
+         delete fTree;
+         fTree = 0x0;
+         delete fFile;
+         fFile = 0x0;
+         
+         delete fSimBuffer;
+         delete fRecBuffer;
+         
+         fSimBuffer = 0x0;
+         fRecBuffer = 0x0;
+         fCurrentDir++;
+         continue;
+       }
+      
       Info("ReadNext","Getting event %d",fCurrentEvent);
       fTree->GetEvent(fCurrentEvent);
       Info("ReadNext","Getting event %d Done",fCurrentEvent);
       
-      //Temporary testing sollution
-      fEventSim = fSimBuffer;
-      fEventRec = fRecBuffer;
-      
-      fCurrentEvent++;
-      fNEventsRead++;
-
-      if (fTree)
+      Int_t retval = 0;
+      if (fReadRec && fReadSim)
        {
-         if ( fCurrentEvent >= fTree->GetEntries() )
-          {
-            delete fTree;
-            fTree = 0x0;
-            delete fFile;
-            fFile = 0x0;
-            fSimBuffer = 0x0;
-            fRecBuffer = 0x0;
-            fCurrentDir++;
-          } 
+         retval = ReadRecAndSim();
        }
+      else
+       {
+         if (fReadRec) retval = ReadRec();
+         if (fReadSim) retval = ReadSim();
+       } 
 
+      fCurrentEvent++;
+      if (retval != 0) 
+        {
+          //something wrong has happend during reading this event, take next
+          continue;
+        }
 
-      return 0;//success -> read one event
+      fNEventsRead++;
+      return retval;//success -> read one event
       
     }while(fCurrentDir < GetNumberOfDirs());//end of loop over directories specified in fDirs Obj Array  
    
@@ -105,6 +162,167 @@ Int_t AliReaderAOD::ReadNext()
 }
 /********************************************************************/
 
+Int_t AliReaderAOD::ReadRecAndSim()
+{
+//Reads raconstructed and simulated data 
+
+ Info("ReadRecAndSim","Found %d reconstructed tracks and %d simulated particles",
+       fRecBuffer->GetNumberOfParticles(),fSimBuffer->GetNumberOfParticles());
+
+ if (fCuts->GetEntriesFast() == 0x0)
+  {//if there is no cuts we return pointer to the buffer
+    if (fEventRec != fRecBuffer)
+     {
+       delete fEventRec;
+       delete fEventSim;
+     }
+    fEventRec = fRecBuffer;//fEventRec is the pointer that the user gets when he asks about an event
+    fEventSim = fSimBuffer;
+  }
+ else
+  {//if there are cuts specified
+    if ( (fEventRec == 0x0) || (fEventRec == fRecBuffer) )
+     {//we need to create a new event, if it is not existing  or it is the same as branch buffer
+       fEventRec = new AliAOD();
+       fEventSim = new AliAOD();
+
+       fEventRec->SetParticleClass( fRecBuffer->GetParticleClass() );
+       fEventSim->SetParticleClass( fSimBuffer->GetParticleClass() );
+     } 
+    else
+     {//or simply reset it in case it already exists
+       fEventRec->Reset();
+       fEventSim->Reset();
+     }
+
+    Int_t npart = fRecBuffer->GetNumberOfParticles();
+    
+    if (npart != fSimBuffer->GetNumberOfParticles())
+     {
+       Error("ReadRecAndSim","There is different number of simulated and reconstructed particles!",
+                              fSimBuffer->GetNumberOfParticles(),npart);
+       return 1;
+     } 
+    for (Int_t i = 0; i < npart; i++)
+     {
+       AliVAODParticle* prec = fRecBuffer->GetParticle(i);
+       AliVAODParticle* psim = fSimBuffer->GetParticle(i);
+       
+       if (prec == 0x0)
+        {
+          Error("ReadRecAndSim","Reconstructed Particle is NULL !!!");
+          continue;
+        }
+       if (psim == 0x0)
+        {
+          Error("ReadRecAndSim","Simulated Particle is NULL !!!");
+          continue;
+        }
+       
+       if (Rejected(prec)) continue;//we make cuts only on reconstructed data
+       
+       fEventRec->AddParticle(prec);
+       fEventSim->AddParticle( fSimBuffer->GetParticle(i));
+     }
+  }
+
+ Info("ReadRecAndSim","Read %d reconstructed tracks and %d simulated particles",
+       fEventRec->GetNumberOfParticles(),fEventSim->GetNumberOfParticles());
+ fTrackCounter->Fill(fEventRec->GetNumberOfParticles());
+ return 0;
+}
+/********************************************************************/
+
+Int_t AliReaderAOD::ReadRec()
+{
+//Reads reconstructed data only
+
+ Info("ReadRec","Found %d reconstructed tracks",fRecBuffer->GetNumberOfParticles());
+
+ if (fCuts->GetEntriesFast() == 0x0)
+  {//if there is no cuts we return pointer to the buffer
+    if (fEventRec != fRecBuffer)
+     {
+       delete fEventRec;
+     }
+    fEventRec = fRecBuffer;//fEventRec is the pointer that the user gets when he asks about an event
+  }
+ else
+  {//if there are cuts specified
+    if ( (fEventRec == 0x0) || (fEventRec == fRecBuffer) )
+     {//we need to create a new event, if it is not existing  or it is the same as branch buffer
+       fEventRec = new AliAOD();
+
+       fEventRec->SetParticleClass( fRecBuffer->GetParticleClass() );
+     } 
+    else
+     {//or simply reset it in case it already exists
+       fEventRec->Reset();
+     }
+
+    Int_t npart = fRecBuffer->GetNumberOfParticles();
+    for (Int_t i = 0; i < npart; i++)
+     {
+       AliVAODParticle* prec = fRecBuffer->GetParticle(i);
+       if (Rejected(prec)) continue;//we make cuts only on simulated data
+
+       fEventRec->AddParticle(prec);
+     }
+  }
+
+ Info("ReadRec","Read %d reconstructed tracks",fEventRec->GetNumberOfParticles());
+ fTrackCounter->Fill(fEventRec->GetNumberOfParticles());
+
+ return 0;
+}
+/********************************************************************/
+
+Int_t AliReaderAOD::ReadSim()
+{
+//Reads simulated data only
+
+ Info("ReadSim","Found %d simulated particles",fSimBuffer->GetNumberOfParticles());
+
+ if (fCuts->GetEntriesFast() == 0x0)
+  {//if there is no cuts we return pointer to the buffer
+    if (fEventSim != fSimBuffer)
+     {
+       delete fEventSim;
+     }
+    fEventSim = fSimBuffer;
+  }
+ else
+  {//if there are cuts specified
+    if ( (fEventSim == 0x0) || (fEventSim == fSimBuffer) )
+     {//we need to create a new event, if it is not existing  or it is the same as branch buffer
+       fEventSim = new AliAOD();
+
+       fEventSim->SetParticleClass( fSimBuffer->GetParticleClass() );
+     } 
+    else
+     {//or simply reset it in case it already exists
+       fEventSim->Reset();
+     }
+
+    Int_t npart = fSimBuffer->GetNumberOfParticles();
+    for (Int_t i = 0; i < npart; i++)
+     {
+       AliVAODParticle* prec = fSimBuffer->GetParticle(i);
+       if (Rejected(prec)) continue;//we make cuts only on simulated data
+       fEventSim->AddParticle(prec);
+     }
+  }
+
+ Info("ReadSim","Read %d simulated particles",fEventSim->GetNumberOfParticles());
+ fTrackCounter->Fill(fEventSim->GetNumberOfParticles());
+
+
+ return 0;
+}
+/********************************************************************/
+
 Int_t AliReaderAOD::OpenFile(Int_t n)
 {
 //opens fFile with  tree
@@ -113,11 +331,8 @@ Int_t AliReaderAOD::OpenFile(Int_t n)
  const TString dirname = GetDirName(n);
  if (dirname == "")
   {
-    if (AliVAODParticle::GetDebug() > 2 )
-      {
-        Info("OpenFile","Got empty string as a directory name."); 
-      }
-   return 1;
+    AliDebug(3,"Got empty string as a directory name."); 
+    return 1;
   }
  
  TString filename = dirname +"/"+ fFileName;
@@ -135,33 +350,59 @@ Int_t AliReaderAOD::OpenFile(Int_t n)
     return 3;
   }
 
- Info("ReadNext","File Is Opened, Getting the TREE");
+ Info("ReadNext","File %s Is Opened, Getting the TREE",filename.Data());
  
  fTree = dynamic_cast<TTree*>(fFile->Get(fgkTreeName));
  if (fTree == 0x0)
   {
-    if (AliVAODParticle::GetDebug() > 2 )
-      {
-        Info("ReadNext","Can not find TTree object named %s",fgkTreeName.Data());
-      }
-    fCurrentDir++;
-    delete fFile;//we have to assume there is no more ESD objects in the fFile
+    AliDebug(3,Form("Can not find TTree object named %s",fgkTreeName.Data()));
+    delete fFile;
     fFile = 0x0;
     return 4;
   }
 
 //  Info("ReadNext","Got TREE, Setting branch addresses");
 
-  fTree->SetBranchAddress(fgkSimulatedDataBranchName,&fSimBuffer);
-  fTree->SetBranchAddress(fgkRecosntructedDataBranchName,&fRecBuffer);
+  if (fReadRec)
+   {
+     TBranch* branch = fTree->GetBranch(fgkReconstructedDataBranchName);
+     if (branch == 0x0)
+      {
+        Error("OpenFile","Can not find branch %s in file %s",
+              fgkReconstructedDataBranchName.Data(),filename.Data());
+        
+        delete fTree;
+        fTree = 0x0;
+        delete fFile;
+        fFile = 0x0;
+        return 5;
+      }
+     fTree->SetBranchAddress(fgkReconstructedDataBranchName,&fRecBuffer);
+   }
   
+
+  if (fReadSim)
+   {
+     TBranch* branch = fTree->GetBranch(fgkSimulatedDataBranchName);
+     if (branch == 0x0)
+      {
+        Error("OpenFile","Can not find branch %s in file %s",
+              fgkSimulatedDataBranchName.Data(),filename.Data());
+        
+        delete fTree;
+        fTree = 0x0;
+        delete fFile;
+        fFile = 0x0;
+        return 6;
+      }
+     fTree->SetBranchAddress(fgkSimulatedDataBranchName,&fSimBuffer);
+   }
 //  Info("ReadNext","Got TREE, Addresses are set.");
 //  Info("ReadNext","Quitting the method.");
   
   return 0;
  
 }
-
 /********************************************************************/
 
 Int_t AliReaderAOD::WriteAOD(AliReader* reader, const char* outfilename, const char* pclassname,  Bool_t /*multcheck*/)
@@ -196,7 +437,7 @@ Int_t AliReaderAOD::WriteAOD(AliReader* reader, const char* outfilename, const c
   AliAOD* recbuffer = eventrec;
   AliAOD* simbuffer = eventsim;
   
-  if (reader->ReadsRec()) recbranch = tree->Branch(fgkRecosntructedDataBranchName,"AliAOD",&recbuffer,32000,99);
+  if (reader->ReadsRec()) recbranch = tree->Branch(fgkReconstructedDataBranchName,"AliAOD",&recbuffer,32000,99);
   if (reader->ReadsSim()) simbranch = tree->Branch(fgkSimulatedDataBranchName,"AliAOD",&simbuffer,32000,99);
 
   reader->Rewind();
@@ -230,8 +471,6 @@ Int_t AliReaderAOD::WriteAOD(AliReader* reader, const char* outfilename, const c
            simbuffer = event;
          } 
       }
-     recbuffer->GetParticle(0)->Print();
-     simbuffer->GetParticle(0)->Print();
      tree->Fill();
    }