]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PHOS/AliPHOSAnalyze.cxx
Compilation warnings fixed
[u/mrichter/AliRoot.git] / PHOS / AliPHOSAnalyze.cxx
index 5226cb4c2588d3a9f996268c0e8f241c2b599b69..100d5119cb3977256e1f7f2aceeb570c85aabf56 100644 (file)
 /* $Id$ */
 
 //_________________________________________________________________________
-// Algorythm class to analyze PHOSv1 events:
-// Construct histograms and displays them.
-// Use the macro EditorBar.C for best access to the functionnalities
-//
-//*-- Author: Y. Schutz (SUBATECH) & Gines Martinez (SUBATECH)
+// Algorythm class to analyze PHOS events. In this class we demostrate, 
+// how to handle reconstructed objects with AliPHSOIndexToObject.
+// As an example we propose sulotions for four most frequently used tasks:
+//    DrawRecon(...) - to draw reconstructed objects in the PHOS plane,
+//                     very usefull in the debuging
+//    InvarianMass(...) - to calculate "REAL" and "MIXED" photon pairs 
+//                        invariant mass distributions
+//    EnergyResoluition(...) -\ Energy and position resolutions of the 
+//    PositionResolution(...)-/ reconstructed photons
+//    Contamination(...) - calculates contamination of the photon spectrum and 
+//                         pobability of reconstruction of several primaries as 
+//                         kGAMMA,kELECTRON etc.
+////    User Case:
+//    root [0] AliPHOSAnalyze * a = new AliPHOSAnalyze("galice.root")
+//                    // set the file you want to analyse
+//    root [1] a->DrawRecon(1,3)
+//                    // plot RecObjects, made in event 1, PHOS module 3 
+//    root [2] a->DrawRecon(1,3,"PHOSRP","another PID")
+//                    // plot RecObjets made in the event 1, PHOS module 3,
+//                    // produced in the another reconstruction pass,
+//                    // which produced PHOS RecParticles ("PHOSRP") with 
+//                    // title "another PID".
+//    root [3] a->InvariantMass()
+//                    // Calculates "REAL" and "MIXED" invariant mass 
+//                    // distributions of kGAMMA and (kGAMMA+kNEUTRALEM)
+//                    // and APPENDS this to the file "invmass.root"
+//    root [4] a->PositionResolution()
+//                    // calculates two dimentional histos: energy of the primary
+//                    // photon vs distance betwin incedence point and reconstructed 
+//                    // poisition. One can analyse the produced file position.root 
+//                    // with macro PhotonPosition.C
+//    root [5] a->EnergyResolution()
+//                    // calculates two dimentional histos: energy of the primary
+//                    // photon vs energy of the reconstructed particle. One can 
+//                    // analyse the produced file energy.root 
+//                    // with macro PhotonEnergy.C
+//    root [6] a->Contamination()
+//                    // fills spectra of primary photons and several kinds of 
+//                    // reconstructed particles, so that analyzing them one can 
+//                    // estimate conatmination, efficiency of registration etc.
+//*--
+//*-- Author: Dmitri Peressounko (SUBATECH & RRC Kurchatov Institute)
 //////////////////////////////////////////////////////////////////////////////
 
+
 // --- ROOT system ---
 
 #include "TFile.h"
 #include "TH1.h"
-#include "TPad.h"
+#include "TH2.h"
 #include "TH2.h"
 #include "TParticle.h"
+#include "TDatabasePDG.h"
 #include "TClonesArray.h"
-#include "TTree.h"
 #include "TMath.h"
-#include "TCanvas.h" 
+#include "TROOT.h"
 
 // --- Standard library ---
 
-#include <iostream.h>
-#include <stdio.h>
-
 // --- AliRoot header files ---
 
-#include "AliRun.h"
+#include "AliLog.h"
+#include "AliStack.h"
+#include "AliPHOSGeometry.h"
 #include "AliPHOSAnalyze.h"
-#include "AliPHOSClusterizerv1.h"
-#include "AliPHOSTrackSegmentMakerv1.h"
-#include "AliPHOSPIDv1.h"
-#include "AliPHOSReconstructioner.h"
 #include "AliPHOSDigit.h"
+#include "AliPHOSSDigitizer.h"
+#include "AliPHOSEmcRecPoint.h"
+#include "AliPHOSCpvRecPoint.h"
 #include "AliPHOSTrackSegment.h"
 #include "AliPHOSRecParticle.h"
-#include "AliPHOSIndexToObject.h"
+#include "AliPHOSLoader.h"
 
-ClassImp(AliPHOSAnalyze)
 
+ClassImp(AliPHOSAnalyze)
 
 //____________________________________________________________________________
-  AliPHOSAnalyze::AliPHOSAnalyze()
+AliPHOSAnalyze::AliPHOSAnalyze():
+  fCorrection(1.2),  //Value calculated for default parameters of reconstruction
+  fEvt(0),
+  ffileName(),
+  fRunLoader(0)
 {
   // default ctor (useless)
-  
-  fRootFile = 0 ; 
 }
 
 //____________________________________________________________________________
-AliPHOSAnalyze::AliPHOSAnalyze(Text_t * name)
+AliPHOSAnalyze::AliPHOSAnalyze(Text_t * fileName):
+  fCorrection(1.05),  //Value calculated for default parameters of reconstruction   
+  fEvt(0),
+  ffileName(fileName),
+  fRunLoader(0)
 {
   // ctor: analyze events from root file "name"
-  
-  Bool_t ok = OpenRootFile(name)  ; 
-  if ( !ok ) {
-    cout << " AliPHOSAnalyze > Error opening " << name << endl ; 
-  }
-  else { 
-      //========== Get AliRun object from file 
-      gAlice = (AliRun*) fRootFile->Get("gAlice") ;
-
-      //=========== Get the PHOS object and associated geometry from the file      
-      fPHOS  = (AliPHOSv1 *)gAlice->GetDetector("PHOS") ;
-      fGeom  = AliPHOSGeometry::GetInstance( fPHOS->GetGeometry()->GetName(), fPHOS->GetGeometry()->GetTitle() );
-      //========== Initializes the Index to Object converter
-      fObjGetter = AliPHOSIndexToObject::GetInstance(fPHOS) ; 
-      //========== Current event number 
-      fEvt = -999 ; 
-
-  }
-  ResetHistograms() ;
+  fRunLoader = AliRunLoader::Open(fileName,"AliPHOSAnalyze");
+  if (fRunLoader == 0x0)
+   {
+     AliError(Form("Error Loading session"));
+   }
 }
 
 //____________________________________________________________________________
-AliPHOSAnalyze::AliPHOSAnalyze(const AliPHOSAnalyze & ana)
+AliPHOSAnalyze::AliPHOSAnalyze(const AliPHOSAnalyze & ana): 
+  TObject(ana),
+  fCorrection(0.),
+  fEvt(0),
+  ffileName(),
+  fRunLoader(0)
 {
   // copy ctor
   ( (AliPHOSAnalyze &)ana ).Copy(*this) ;
 }
 
-//____________________________________________________________________________
-void AliPHOSAnalyze::Copy(TObject & obj)
-{
-  // copy an analysis into an other one
-  TObject::Copy(obj) ;
-  // I do nothing more because the copy is silly but the Code checkers requires one
-}
-
 //____________________________________________________________________________
 AliPHOSAnalyze::~AliPHOSAnalyze()
 {
   // dtor
 
-  if (fRootFile->IsOpen() ) 
-    fRootFile->Close() ; 
-  if(fRootFile)
-    delete fRootFile ; 
-
-  if(fPHOS)
-    delete fPHOS ; 
-
-  if(fClu)
-    delete fClu ; 
-
-  if(fPID)
-    delete fPID ; 
-
-  if(fRec)
-    delete fRec ; 
-
-  if(fTrs)
-    delete fTrs ; 
-
 }
-
 //____________________________________________________________________________
-void AliPHOSAnalyze::ActivePPSD(Int_t Nevents=1){
+void AliPHOSAnalyze::DrawRecon(Int_t Nevent,Int_t Nmod){
+  //Draws pimary particles and reconstructed 
+  //digits, RecPoints, RecPartices etc 
+  //for event Nevent in the module Nmod.
+
+  //========== Create ObjectLoader
+  if (fRunLoader == 0x0)
+   {
+     AliError(Form("Error Loading session"));
+     return;
+   }
   
-  fhEnergyCorrelations  = new TH2F("hEnergyCorrelations","hEnergyCorrelations",40,  0., 0.15, 30, 0., 3.e-5);
-  //========== Create the Clusterizer
-  fClu = new AliPHOSClusterizerv1() ; 
-  fClu->SetEmcEnergyThreshold(0.05) ; 
-  fClu->SetEmcClusteringThreshold(0.20) ; 
-  fClu->SetPpsdEnergyThreshold    (0.0000002) ; 
-  fClu->SetPpsdClusteringThreshold(0.0000001) ; 
-  fClu->SetLocalMaxCut(0.03) ;
-  fClu->SetCalibrationParameters(0., 0.00000001) ; 
-
-  Int_t ievent;
+  AliPHOSLoader* gime = dynamic_cast<AliPHOSLoader*>(fRunLoader->GetLoader("PHOSLoader"));
+  if ( gime == 0 ) 
+   {
+     AliError(Form("Could not obtain the Loader object !")); 
+     return ;
+   } 
   
-  for ( ievent=0; ievent<Nevents; ievent++)
-    {  
-      
-      //========== Event Number>         
-      if ( ( log10((Float_t)(ievent+1)) - (Int_t)(log10((Float_t)(ievent+1))) ) == 0. ) 
-       cout <<  "AnalyzeResolutions > " << "Event is " << ievent << endl ;  
-      
-      //=========== Connects the various Tree's for evt
-      gAlice->GetEvent(ievent);
-
-      //=========== Gets the Kine TTree
-      gAlice->TreeK()->GetEvent(0) ;
-            
-      //=========== Get the Digit Tree
-      gAlice->TreeD()->GetEvent(0) ;
-      
-      //========== Creating branches ===================================       
-      AliPHOSRecPoint::RecPointsList ** EmcRecPoints =  fPHOS->EmcRecPoints() ;
-      gAlice->TreeR()->SetBranchAddress( "PHOSEmcRP", EmcRecPoints ) ;
-      
-      AliPHOSRecPoint::RecPointsList ** PpsdRecPoints = fPHOS->PpsdRecPoints() ;
-      gAlice->TreeR()->SetBranchAddress( "PHOSPpsdRP", PpsdRecPoints ) ;
-      
-      AliPHOSTrackSegment::TrackSegmentsList **  TrackSegmentsList = fPHOS->TrackSegments() ;
-      if( (*TrackSegmentsList) )
-       (*TrackSegmentsList)->Clear() ;
-      gAlice->TreeR()->SetBranchAddress( "PHOSTS", TrackSegmentsList ) ;
-      
-      AliPHOSRecParticle::RecParticlesList ** RecParticleList  = fPHOS->RecParticles() ;
-      if( (*RecParticleList) )
-       (*RecParticleList)->Clear() ;
-      gAlice->TreeR()->SetBranchAddress( "PHOSRP", RecParticleList ) ;
-      
-      
-      //=========== Gets the Reconstraction TTree
-      gAlice->TreeR()->GetEvent(0) ;
-            
-      AliPHOSPpsdRecPoint * RecPoint ;
-      Int_t relid[4] ; 
-      TIter nextRP(*fPHOS->PpsdRecPoints() ) ;
-      while( ( RecPoint = (AliPHOSPpsdRecPoint *)nextRP() ) )
-       {
-         if(!(RecPoint->GetUp()) ) {
-           AliPHOSDigit *digit ;
-           Int_t iDigit ;
-           for(iDigit = 0; iDigit < fPHOS->Digits()->GetEntries(); iDigit++) 
-             {
-               digit = (AliPHOSDigit *) fPHOS->Digits()->At(iDigit) ;
-               fGeom->AbsToRelNumbering(digit->GetId(), relid) ;    
-                if((relid[2]==1)&&(relid[3]==1)&&(relid[0]==RecPoint->GetPHOSMod())){
-                 Float_t ConvertorEnergy = fClu->Calibrate(digit->GetAmp()) ;
-                 fhEnergyCorrelations->Fill(ConvertorEnergy,RecPoint->GetTotalEnergy() );  
-                 break ; 
-               } 
-             }
-           break ;
-         }
-       }
-    }
-  SaveHistograms() ;
-  fhEnergyCorrelations->Draw("BOX") ;
-}
+  
+  if(Nevent >= fRunLoader->GetNumberOfEvents() ) {
+    AliError(Form("There is no event %d only %d events available", Nevent, fRunLoader->GetNumberOfEvents() )) ;
+    return ;
+  }
+  AliPHOSGeometry * phosgeom =  AliPHOSGeometry::GetInstance() ;
+  fRunLoader->GetEvent(Nevent);
+
+  Int_t nx = phosgeom->GetNPhi() ;
+  Int_t nz = phosgeom->GetNZ() ;
+  const Float_t * cri= phosgeom->GetEMCAGeometry()->GetCrystalHalfSize() ;
+  Float_t x = nx*cri[0] ;
+  Float_t z = nz*cri[2] ;
+  Int_t nxCPV = (Int_t) (nx*phosgeom->GetPadSizePhi()/(2.*cri[0])) ;
+  Int_t nzCPV = (Int_t) (nz*phosgeom->GetPadSizeZ()/(2.*cri[2])) ;
+  
+  TH2F * emcDigits = (TH2F*) gROOT->FindObject("emcDigits") ;
+  if(emcDigits)
+    emcDigits->Delete() ;
+  emcDigits = new TH2F("emcDigits","EMC digits",  nx,-x,x,nz,-z,z);
+  TH2F * emcSdigits =(TH2F*) gROOT->FindObject("emcSdigits") ;  
+  if(emcSdigits)
+    emcSdigits->Delete() ;
+  emcSdigits = new TH2F("emcSdigits","EMC sdigits", nx,-x,x,nz,-z,z);
+  TH2F * emcRecPoints = (TH2F*)gROOT->FindObject("emcRecPoints") ; 
+  if(emcRecPoints)
+    emcRecPoints->Delete() ;
+  emcRecPoints = new TH2F("emcRecPoints","EMC RecPoints",nx,-x,x,nz,-z,z);
+  TH2F * cpvSdigits =(TH2F*) gROOT->FindObject("cpvSdigits") ;
+  if(cpvSdigits)
+    cpvSdigits->Delete() ;
+  cpvSdigits = new TH2F("cpvSdigits","CPV sdigits", nx,-x,x,nz,-z,z);
+  TH2F * cpvDigits = (TH2F*)gROOT->FindObject("cpvDigits") ;
+  if(cpvDigits)
+    cpvDigits->Delete() ;
+  cpvDigits = new TH2F("cpvDigits","CPV digits",   nxCPV,-x,x,nzCPV,-z,z) ;
+  TH2F * cpvRecPoints= (TH2F*)gROOT->FindObject("cpvRecPoints") ; 
+  if(cpvRecPoints)
+    cpvRecPoints->Delete() ;
+  cpvRecPoints = new TH2F("cpvRecPoints","CPV RecPoints",    nxCPV,-x,x,nzCPV,-z,z) ;
+
+  TH2F * phot = (TH2F*)gROOT->FindObject("phot") ;
+  if(phot)
+    phot->Delete() ;
+  phot = new TH2F("phot","Primary Photon",  nx,-x,x,nz,-z,z);
+  TH2F * recPhot = (TH2F*)gROOT->FindObject("recPhot") ; 
+  if(recPhot)
+    recPhot->Delete() ;
+  recPhot = new TH2F("recPhot","RecParticles with primary Photon",nx,-x,x,nz,-z,z);
+  
+  //Get Vertex
+  Double_t vtx[3]={0.,0.,0.} ;  
+//DP: extract vertex either from Generator or from data
 
-//____________________________________________________________________________
- void AliPHOSAnalyze::AnalyzeManyEvents(Int_t Nevents, Int_t module)    
-{
-  // analyzes Nevents events in a single PHOS module  
-  // Events should be reconstructed by Reconstruct()
+  
+  //Plot Primary Particles
+  
+  if (fRunLoader->Stack() == 0x0) fRunLoader->LoadKinematics("READ");
+  
 
-  if ( fRootFile == 0 ) 
-    cout << "AnalyzeManyEvents > " << "Root File not openned" << endl ;  
-  else
+  const TParticle * primary ;
+  Int_t iPrimary ;
+  for ( iPrimary = 0 ; iPrimary < fRunLoader->Stack()->GetNprimary() ; iPrimary++)
     {
-      //========== Booking Histograms
-      cout << "AnalyzeManyEvents > " << "Booking Histograms" << endl ; 
-      BookingHistograms();
-
-      Int_t ievent;
-      Int_t relid[4] ; 
-      AliPHOSDigit * digit ;
-      AliPHOSEmcRecPoint * emc ;
-      AliPHOSPpsdRecPoint * ppsd ;
-      //      AliPHOSTrackSegment * tracksegment ;
-      AliPHOSRecParticle * recparticle;
-
-      for ( ievent=0; ievent<Nevents; ievent++)
-       {  
-         //========== Event Number>         
-         if ( ( log10((Float_t)(ievent+1)) - (Int_t)(log10((Float_t)(ievent+1))) ) == 0. ) 
-           cout <<  "AnalyzeManyEvents > " << "Event is " << ievent << endl ;  
-
-         //=========== Connects the various Tree's for evt
-         gAlice->GetEvent(ievent);
-
-         //=========== Gets the Digit TTree
-         gAlice->TreeD()->GetEvent(0) ;
-
-         //=========== Gets the number of entries in the Digits array 
-         TIter nextdigit(fPHOS->Digits()) ;
-         while( ( digit = (AliPHOSDigit *)nextdigit() ) )
-           {
-             fGeom->AbsToRelNumbering(digit->GetId(), relid) ;         
-             if (fClu->IsInEmc(digit)) fhEmcDigit->Fill(fClu->Calibrate(digit->GetAmp())) ; 
-             else    
-             {  
-               if (relid[1]<17) fhVetoDigit->Fill(fClu->Calibrate(digit->GetAmp())); 
-               if (relid[1]>16) fhConvertorDigit->Fill(fClu->Calibrate(digit->GetAmp()));
-             }
-           }
-         
-
-         //=========== Cluster in module
-         TIter nextEmc(*fPHOS->EmcRecPoints()  ) ;
-         while((emc = (AliPHOSEmcRecPoint *)nextEmc())) 
-           {
-             if ( emc->GetPHOSMod() == module )
-               {  
-                 fhEmcCluster->Fill(  emc->GetTotalEnergy()  ); 
-                 TIter nextPpsd( *fPHOS->PpsdRecPoints()) ;
-                 while((ppsd = (AliPHOSPpsdRecPoint *)nextPpsd())) 
-                   {
-                     if ( ppsd->GetPHOSMod() == module )
-                       {                         
-                         if (!ppsd->GetUp()) fhConvertorEmc->Fill(emc->GetTotalEnergy(),ppsd->GetTotalEnergy()) ;
-                       }
-                   } 
-               }
-           }
-
-         //=========== Cluster in module PPSD Down
-         TIter nextPpsd(*fPHOS->PpsdRecPoints() ) ;
-         while((ppsd = (AliPHOSPpsdRecPoint *)nextPpsd())) 
-           {
-             if ( ppsd->GetPHOSMod() == module )
-               { 
-                 if (!ppsd->GetUp()) fhConvertorCluster->Fill(ppsd->GetTotalEnergy()) ;
-                 if (ppsd->GetUp())  fhVetoCluster     ->Fill(ppsd->GetTotalEnergy()) ;
-               }
-           }
-
-         //========== TRackSegments in the event
-         TIter nextRecParticle(*fPHOS->RecParticles() ) ; 
-         while((recparticle = (AliPHOSRecParticle *)nextRecParticle())) 
-           {
-             if ( recparticle->GetPHOSTrackSegment()->GetPHOSMod() == module )
-               { 
-                 cout << "Particle type is " << recparticle->GetType() << endl ; 
-                 Int_t numberofprimaries = 0 ;
-                 Int_t * listofprimaries = recparticle->GetPrimaries(numberofprimaries) ;
-                 cout << "Number of primaries = " << numberofprimaries << endl ; 
-                 Int_t index ;
-                 for ( index = 0 ; index < numberofprimaries ; index++)
-                   cout << "    primary # " << index << " =  " << listofprimaries[index] << endl ;  
-               }
-           }
-       }   // endfor
-      SaveHistograms();
-    }       // endif
-}           // endfunction
-
-//____________________________________________________________________________
- void AliPHOSAnalyze::Reconstruct(Int_t Nevents )    
-{     
-  Int_t ievent ;   
-  for ( ievent=0; ievent<Nevents; ievent++)
-    {  
-      if (ievent==0) 
-       {
-         cout << "Analyze > Starting Reconstructing " << endl ; 
-         //========== Create the Clusterizer
-         fClu = new AliPHOSClusterizerv1() ; 
-         fClu->SetEmcEnergyThreshold(0.05) ; 
-         fClu->SetEmcClusteringThreshold(0.20) ; 
-         fClu->SetPpsdEnergyThreshold    (0.0000002) ; 
-         fClu->SetPpsdClusteringThreshold(0.0000001) ; 
-         fClu->SetLocalMaxCut(0.03) ;
-         fClu->SetCalibrationParameters(0., 0.00000001) ; 
-         
-         //========== Creates the track segment maker
-         fTrs = new AliPHOSTrackSegmentMakerv1()  ;
-         //      fTrs->UnsetUnfoldFlag() ; 
-         
-         //========== Creates the particle identifier
-         fPID = new AliPHOSPIDv1() ;
-         fPID->SetShowerProfileCuts(0.3, 1.8, 0.3, 1.8 ) ; 
-         
-         //========== Creates the Reconstructioner  
-         fRec = new AliPHOSReconstructioner(fClu, fTrs, fPID) ; 
-         //  fRec -> SetDebugReconstruction(kTRUE);     
-
-       }
-      
-      //========== Event Number>         
-      if ( ( log10((Float_t)(ievent+1)) - (Int_t)(log10((Float_t)(ievent+1))) ) == 0. ) 
-       cout <<  "Analyze > Event is " << ievent << endl ;  
+      primary = fRunLoader->Stack()->Particle(iPrimary);
       
-      //=========== Connects the various Tree's for evt
-      gAlice->GetEvent(ievent);
-
-      //=========== Gets the Digit TTree
-      gAlice->TreeD()->GetEvent(0) ;
+      Int_t primaryType = primary->GetPdgCode();
+//       if( (primaryType == 211)||(primaryType == -211)||(primaryType == 2212)||(primaryType == -2212)
+//          ||(primaryType == 11)||(primaryType == -11) ) {
+//         Int_t moduleNumber ;
+//         Double_t primX, primZ ;
+//         phosgeom->ImpactOnEmc(primary->Theta(), primary->Phi(), moduleNumber, primX, primZ) ;
+//         if(moduleNumber==Nmod)
+//           charg->Fill(primZ,primX,primary->Energy()) ;
+//       }
+      if( primaryType == 22 ) {
+        Int_t moduleNumber ;
+        Double_t primX, primZ ;
+        phosgeom->ImpactOnEmc(vtx,primary->Theta(), primary->Phi(), moduleNumber, primX, primZ) ;
+        if(moduleNumber==Nmod) 
+          phot->Fill(primZ,primX,primary->Energy()) ;
+      }
+//       else{
+//         if( primaryType == -2112 ) {
+//           Int_t moduleNumber ;
+//           Double_t primX, primZ ;
+//           phosgeom->ImpactOnEmc(primary->Theta(), primary->Phi(), moduleNumber, primX, primZ) ;
+//           if(moduleNumber==Nmod)
+//             nbar->Fill(primZ,primX,primary->Energy()) ;
+//         }
+//       }
+    }  
 
-      //=========== Do the reconstruction
-      fPHOS->Reconstruction(fRec);
+  
+  Int_t iSDigit ;
+  AliPHOSDigit * sdigit ;
+  const TClonesArray * sdigits = gime->SDigits() ;
+  Int_t nsdig[5] = {0,0,0,0,0} ;
+  if(sdigits){
+    for(iSDigit = 0; iSDigit < sdigits->GetEntriesFast() ; iSDigit++)
+      {
+       sdigit = (AliPHOSDigit *) sdigits->At(iSDigit) ;
+       Int_t relid[4];
+       phosgeom->AbsToRelNumbering(sdigit->GetId(), relid) ;
+       Float_t xd,zd ;
+       phosgeom->RelPosInModule(relid,xd,zd);
+       Float_t e = sdigit->GetEnergy() ;
+       nsdig[relid[0]-1]++ ;
+       if(relid[0]==Nmod){
+         if(relid[1]==0)  //EMC
+           emcSdigits->Fill(xd,zd,e) ;
+         if( relid[1]!=0 )
+           cpvSdigits->Fill(xd,zd,e) ;
+       }
+      }
+  }
+  TString message ; 
+  message  = "Number of EMC + CPV SDigits per module: \n" ;
+  message += "%d %d %d %d %d\n"; 
+  AliInfo(Form(message.Data(), nsdig[0], nsdig[1], nsdig[2], nsdig[3], nsdig[4] )) ;
+
+  //Plot digits
+  Int_t iDigit ;
+  AliPHOSDigit * digit ;
+  const TClonesArray * digits = gime->Digits(); 
+  if(digits) {
+    for(iDigit = 0; iDigit < digits->GetEntriesFast(); iDigit++)
+      {
+       digit = (AliPHOSDigit *) digits->At(iDigit) ;
+       Int_t relid[4];
+       phosgeom->AbsToRelNumbering(digit->GetId(), relid) ;
+       Float_t xd,zd ;
+       phosgeom->RelPosInModule(relid,xd,zd) ;
+       Float_t e = digit->GetEnergy() ;
+       if(relid[0]==Nmod){
+         if(relid[1]==0)  //EMC
+           emcDigits->Fill(xd,zd,e) ;
+         if( relid[1]!=0 )
+           cpvDigits->Fill(xd,zd,e) ;
+       }
+      }
+  }
+  
+  
+  //Plot RecPoints
+  Int_t irecp ;
+  TVector3 pos ;
+  TObjArray * emcrp = gime->EmcRecPoints() ;
+  if(emcrp) {
+    for(irecp = 0; irecp < emcrp->GetEntriesFast() ; irecp ++){
+      AliPHOSEmcRecPoint * emc = (AliPHOSEmcRecPoint *) emcrp->At(irecp) ;
+      if(emc->GetPHOSMod()==Nmod){
+       emc->GetLocalPosition(pos) ;
+       emcRecPoints->Fill(pos.X(),pos.Z(),emc->GetEnergy());
+      }
     }
+  }
+  
+  TObjArray * cpvrp = gime->CpvRecPoints() ;
+  if(cpvrp) {
+    for(irecp = 0; irecp < cpvrp->GetEntriesFast() ; irecp ++){
+      AliPHOSRecPoint * cpv = (AliPHOSCpvRecPoint *) cpvrp->At(irecp) ;
+      if(cpv->GetPHOSMod()==Nmod){
+       cpv->GetLocalPosition(pos) ;
+       cpvRecPoints->Fill(pos.X(),pos.Z(),cpv->GetEnergy());
+      }
+    }
+  }
+    
+  //Plot RecParticles
+  AliPHOSRecParticle * recParticle ;
+  Int_t iRecParticle ;
+  TClonesArray * rp = gime->RecParticles() ;
+  TClonesArray * ts = gime->TrackSegments() ;
+  if(rp && ts && emcrp) {
+    for(iRecParticle = 0; iRecParticle < rp->GetEntriesFast() ; iRecParticle++ )
+      {
+       recParticle = (AliPHOSRecParticle *) rp->At(iRecParticle) ;
+       Int_t moduleNumberRec ;
+       Double_t recX, recZ ;
+       phosgeom->ImpactOnEmc(vtx,recParticle->Theta(), recParticle->Phi(), moduleNumberRec, recX, recZ) ;
+       if(moduleNumberRec == Nmod){
+         
+         Double_t minDistance = 5. ;
+         Int_t closestPrimary = -1 ;       
+
+         //extract list of primaries: it is stored at EMC RecPoints
+         Int_t emcIndex = ((AliPHOSTrackSegment *) ts->At(recParticle->GetPHOSTSIndex()))->GetEmcIndex() ;
+         Int_t numberofprimaries ;
+         Int_t * listofprimaries  = ((AliPHOSRecPoint*) emcrp->At(emcIndex))->GetPrimaries(numberofprimaries)  ;
+         Int_t index ;
+         const TParticle * primPart ;
+         Double_t distance = minDistance ;
+         
+         for ( index = 0 ; index < numberofprimaries ; index++){
+           primPart = fRunLoader->Stack()->Particle(listofprimaries[index]) ;
+           Int_t moduleNumber ;
+           Double_t primX, primZ ;
+           phosgeom->ImpactOnEmc(vtx,primPart->Theta(), primPart->Phi(), moduleNumber, primX, primZ) ;
+           if(moduleNumberRec == moduleNumber)
+             distance = TMath::Sqrt((recX-primX)*(recX-primX)+(recZ-primZ)*(recZ-primZ) ) ;
+           if(minDistance > distance)
+             {
+              minDistance = distance ;
+              closestPrimary = listofprimaries[index] ;
+             }
+         }
+         
+         if(closestPrimary >=0 ){
+           
+           Int_t primaryType = fRunLoader->Stack()->Particle(closestPrimary)->GetPdgCode() ;
+           
+           if(primaryType==22)
+             recPhot->Fill(recZ,recX,recParticle->Energy()) ;
+//            else
+//              if(primaryType==-2112)
+//               recNbar->Fill(recZ,recX,recParticle->Energy()) ; 
+         }
+       }
+      }
 
-    fClu->Delete();
-    fTrs->Delete();
-    fPID->Delete();
-    fRec->Delete();
-
+  }
+  
+  //Plot made histograms
+  emcSdigits->Draw("box") ;
+  emcDigits->SetLineColor(5) ;
+  emcDigits->Draw("boxsame") ;
+  emcRecPoints->SetLineColor(2) ;
+  emcRecPoints->Draw("boxsame") ;
+  cpvSdigits->SetLineColor(1) ;
+  cpvSdigits->Draw("boxsame") ;
+  
 }
-//-------------------------------------------------------------------------------------
-
-//   TClonesArray AllDigitArray = TClonesArray("AliPHOSDigit",1000) ;
-//   TClonesArray * PhotonsList ;
-//   TClonesArray * FalsDigitsList ;
-//   TClonesArray AllPrimary = TClonesArray("TParticle",5000) ;
-//   TFile * file2 = new TFile("ph100.root") ; // file with added photons
-//   gAlice = (AliRun*) file2->Get("gAlice") ;
-//   Int_t ievent;
-//   Int_t NDigits[Nevents+1] ;
-//   NDigits[0]=0 ;
-//   Int_t NAllDigits = 0;
-//   Int_t NprimPerEvent = 20 ;
-//   for (ievent=0; ievent <Nevents; ievent++)
-//     {
-//       PhotonsList  = gAlice->Particles();  //Primary
-//       FalsDigitsList  = ((AliPHOSv1 *)gAlice->GetDetector("PHOS"))->Digits();  //Digits
-//       gAlice->GetEvent(ievent) ;
-//       gAlice->TreeD()->GetEvent(0) ;
-//       gAlice->TreeK()->GetEvent(0) ;
-//       //Copy Primary
-//       Int_t Nprim ;
-//       for(Nprim = 0 ;Nprim < NprimPerEvent ; Nprim++)
-//     new (AllPrimary[Nprim+ievent*NprimPerEvent])  TParticle(*((TParticle *) PhotonsList->At(Nprim))) ;
-
-//       //Copy Digits
-//       TIter nextDigit(FalsDigitsList) ;
-//       AliPHOSDigit * FalseDigit ;
-//       NDigits[ievent+1] = NDigits[ievent]+ FalsDigitsList->GetEntriesFast() ; 
-//       while( (FalseDigit = (AliPHOSDigit *) nextDigit()))
-//     {        
-//       new (AllDigitArray[NAllDigits])  AliPHOSDigit(FalseDigit->GetPrimary(1),FalseDigit->GetId(),FalseDigit->GetAmp()) ;
-//       NAllDigits++ ;
-//     }         
-//     }
-//   file2->Close() ;
-
-
-
-//       //Add primary particles
-//       cout << "# of Primaries before add " << PrimaryList->GetEntriesFast() << endl;
-//      Int_t NTruePrimary = 0 ;  //PrimaryList->GetEntriesFast() ;
-//       Int_t Nprim ;
-//       for(Nprim = 0; Nprim < NprimPerEvent; Nprim++)
-//     new ((*PrimaryList)[NTruePrimary+Nprim])  TParticle(*((TParticle *) AllPrimary.At(Nprim+ievent*NprimPerEvent))) ;
-      
-//       cout << "# of Primaries after add " << PrimaryList->GetEntriesFast() <<endl;
-
-//       cout << "Digits before add " << DigitsList->GetEntries() << endl ;
-//       cout << "Digits to add " <<  NDigits[ievent+1]-  NDigits[ievent]<< endl ;
-      
-      //=========== Add fals digits ==============================
-//       TIter nextDigit(DigitsList) ;
-//       AliPHOSDigit * FalseDigit ;
-//       AliPHOSDigit * RealDigit ;
-//       Int_t NTrueDigits = DigitsList->GetEntriesFast() ; 
-//       Int_t Ndigit ;
-//       for(Ndigit=NDigits[ievent];Ndigit<NDigits[ievent+1];Ndigit++)
-//     {        
-//       FalseDigit = (AliPHOSDigit*) AllDigitArray.At(Ndigit) ;
-//       Bool_t Add = kTRUE ; 
-//       AliPHOSDigit tmpDigit=AliPHOSDigit(FalseDigit->GetPrimary(1)+NTruePrimary,FalseDigit->GetId(),FalseDigit->GetAmp()) ;
-         
-//       while( (RealDigit = (AliPHOSDigit *) nextDigit()) && Add)
-//         {
-//           if((*RealDigit) == (tmpDigit)) 
-//             {
-//               *RealDigit=*RealDigit+tmpDigit ;
-//               Add = kFALSE ;
-//             }
-//         }
-//       if(Add)
-//         {
-//           new ((*DigitsList)[NTrueDigits])  AliPHOSDigit(FalseDigit->GetPrimary(1)+NTruePrimary,FalseDigit->GetId(),FalseDigit->GetAmp()) ;
-//           ((AliPHOSDigit *)DigitsList->At(NTrueDigits))->SetIndexInList(NTrueDigits) ;
-//           NTrueDigits++ ;
-//         }     
-//     }
-//       cout << "Digits after add " << DigitsList->GetEntries() << endl ;
-
-
 //____________________________________________________________________________
- void AliPHOSAnalyze::AnalyzeResolutions(Int_t Nevents )    
-{
-  // analyzes Nevents events and calculate Energy and Position resolution as well as
-  // probaility of correct indentifiing of the incident particle
-
-  //========== Booking Histograms
-  cout << "AnalyzeResolutions > " << "Booking Histograms" << endl ; 
-  BookResolutionHistograms();
-
-  Int_t Counter[9][5] ;     
-  Int_t i1,i2,TotalInd = 0 ;
-  for(i1 = 0; i1<9; i1++)
-    for(i2 = 0; i2<5; i2++)
-      Counter[i1][i2] = 0 ;
+void AliPHOSAnalyze::Ls(){
+  //lists branches and titles of PHOS-related branches of TreeR, TreeD, TreeS
   
-  Int_t TotalPrimary = 0 ;
-  Int_t TotalRecPart = 0 ;
-  Int_t TotalRPwithPrim = 0 ;
-  Int_t ievent;
-
-  cout << "Start Analysing"<< endl ;
-  for ( ievent=0; ievent<Nevents; ievent++)
-    {  
-      
-      //========== Event Number>         
-      if ( ( log10((Float_t)(ievent+1)) - (Int_t)(log10((Float_t)(ievent+1))) ) == 0. ) 
-       cout <<  "AnalyzeResolutions > " << "Event is " << ievent << endl ;  
-      
-      //=========== Connects the various Tree's for evt
-      gAlice->GetEvent(ievent);
-
-      
-
-            //=========== Gets the Kine TTree
-      gAlice->TreeK()->GetEvent(0) ;
-      
-      //=========== Gets the list of Primari Particles
-      TClonesArray * PrimaryList  = gAlice->Particles();     
-
-      TParticle * Primary ;
-      Int_t iPrimary ;
-      for ( iPrimary = 0 ; iPrimary < PrimaryList->GetEntries() ; iPrimary++)
-           {
-             Primary = (TParticle*)PrimaryList->UncheckedAt(iPrimary) ;
-             Int_t PrimaryType = Primary->GetPdgCode() ;
-             if( PrimaryType == 22 ) 
-               fhPrimary->Fill(Primary->Energy()) ;
-           } 
-      
-      //=========== Get the Digit Tree
-      gAlice->TreeD()->GetEvent(0) ;
-
-      //========== Creating branches ===================================       
-      AliPHOSRecPoint::RecPointsList ** EmcRecPoints =  fPHOS->EmcRecPoints() ;
-      gAlice->TreeR()->SetBranchAddress( "PHOSEmcRP", EmcRecPoints ) ;
+  if (fRunLoader == 0x0)
+   {
+     AliError(Form("Error Loading session"));
+     return;
+   }
+  
+  AliPHOSLoader* gime = dynamic_cast<AliPHOSLoader*>(fRunLoader->GetLoader("PHOSLoader"));
+  if ( gime == 0 ) 
+   {
+     AliError(Form("Could not obtain the Loader object !")); 
+     return ;
+   } 
 
-      AliPHOSRecPoint::RecPointsList ** PpsdRecPoints = fPHOS->PpsdRecPoints() ;
-      gAlice->TreeR()->SetBranchAddress( "PHOSPpsdRP", PpsdRecPoints ) ;
 
-      AliPHOSTrackSegment::TrackSegmentsList **  TrackSegmentsList = fPHOS->TrackSegments() ;
-      if( (*TrackSegmentsList) )
-       (*TrackSegmentsList)->Clear() ;
-      gAlice->TreeR()->SetBranchAddress( "PHOSTS", TrackSegmentsList ) ;
-      
-      AliPHOSRecParticle::RecParticlesList ** RecParticleList  = fPHOS->RecParticles() ;
-      if( (*RecParticleList) )
-       (*RecParticleList)->Clear() ;
-      gAlice->TreeR()->SetBranchAddress( "PHOSRP", RecParticleList ) ;
-           
-
-      //=========== Gets the Reconstraction TTree
-      gAlice->TreeR()->GetEvent(0) ;
+  Int_t ibranch;
+  TObjArray * branches; 
+  
+  if (gime->TreeS() == 0x0) 
+   {
+     if (gime->LoadSDigits("READ"))
+      {
+        AliError(Form("Problems with loading summable digits"));
+        return;
+      }
+   }
+  branches = gime->TreeS()->GetListOfBranches() ;
  
-      cout << ievent << " " << (*EmcRecPoints) << " " <<(*PpsdRecPoints) <<fPHOS->Digits()<< endl ; 
-      cout << "    " << " " << (*EmcRecPoints)->GetEntries() << " " <<(*PpsdRecPoints)->GetEntries() <<fPHOS->Digits()->GetEntries()<< endl ; 
-
-      AliPHOSRecParticle * RecParticle ;
-      Int_t iRecParticle ;
-      for(iRecParticle = 0; iRecParticle < (*RecParticleList)->GetEntries() ;iRecParticle++ )
-       {
-         RecParticle = (AliPHOSRecParticle *) (*RecParticleList)->At(iRecParticle) ;
-         
-         Int_t ModuleNumberRec ;
-         Double_t RecX, RecZ ;
-         fGeom->ImpactOnEmc(RecParticle->Theta(), RecParticle->Phi(), ModuleNumberRec, RecX, RecZ) ;
-         
-         Double_t MinDistance = 10000 ;
-         Int_t ClosestPrimary = -1 ;
-         
-         Int_t numberofprimaries ;
-         Int_t * listofprimaries  = RecParticle->GetPrimaries(numberofprimaries)  ;
-         Int_t index ;
-         TParticle * Primary ;
-         Double_t Distance = MinDistance ;
-         for ( index = 0 ; index < numberofprimaries ; index++)
-           {
-             Primary = (TParticle*)PrimaryList->UncheckedAt(listofprimaries[index]) ;
-             Int_t ModuleNumber ;
-             Double_t PrimX, PrimZ ;
-             fGeom->ImpactOnEmc(Primary->Theta(), Primary->Phi(), ModuleNumber, PrimX, PrimZ) ;
-             if(ModuleNumberRec == ModuleNumber)
-               Distance = TMath::Sqrt((RecX-PrimX)*(RecX-PrimX)+(RecZ-PrimZ)*(RecZ-PrimZ) ) ;
-             if(MinDistance > Distance)
-               {
-                 MinDistance = Distance ;
-                 ClosestPrimary = listofprimaries[index] ;
-               }
-           }
-         TotalRecPart++ ;
-         if(ClosestPrimary >=0 )
-           {
-             fhPhotonAllEnergy->Fill(((TParticle *) PrimaryList->At(ClosestPrimary))->Energy(), RecParticle->Energy() ) ; 
-             fhPhotonAllPosition->Fill(((TParticle *) PrimaryList->At(ClosestPrimary))->Energy(),Distance) ;
-             TotalRPwithPrim++;
-             Int_t PrimaryType = ((TParticle *)PrimaryList->At(ClosestPrimary))->GetPdgCode() ;
-             TParticlePDG* PDGparticle = ((TParticle *)PrimaryList->At(ClosestPrimary))->GetPDG();
-             Double_t charge =  PDGparticle->Charge() ;
-             Int_t PrimaryCode ;
-             switch(PrimaryType)
-               {
-               case 22:
-                 PrimaryCode = 0;  //Photon
-                 break;
-               case 11 :
-                 PrimaryCode = 1;  //Electron
-                 break;
-               case -11 :
-                 PrimaryCode = 1;  //positron
-                 break;
-               case 321 :
-                 PrimaryCode = 4;  //K+
-                 break;
-               case -321 :
-                 PrimaryCode = 4;  //K-
-                 break;
-               case 310 :
-                 PrimaryCode = 4;  //K0s
-                 break;
-               case 130 :
-                 PrimaryCode = 4;  //K0l
-                 break;
-               default:
-                 if(charge)
-                   PrimaryCode = 2; //Charged hadron
-                 else
-                   PrimaryCode = 3; //Neutral hadron
-                 break;
-               }
-
-             switch(RecParticle->GetType())
-               {
-               case AliPHOSFastRecParticle::kGAMMA:
-                 if(PrimaryType == 22){
-                   fhPhotonEnergy->Fill(((TParticle *) PrimaryList->At(ClosestPrimary))->Energy(), RecParticle->Energy() ) ; 
-                   fhPhotonPosition->Fill(((TParticle *) PrimaryList->At(ClosestPrimary))->Energy(),Distance) ;
-                   fhPhotonReg->Fill(RecParticle->Energy() ) ;
-                   fhPhotonEM->Fill(RecParticle->Energy() ) ;
-                   fhPhotPhot->Fill(RecParticle->Energy() ) ;
-                 }
-                 if(PrimaryType == 2112){ //neutron
-                   fhNReg->Fill(RecParticle->Energy() ) ;
-                   fhNEM->Fill(RecParticle->Energy() ) ;
-                 }
-                 
-                 if(PrimaryType == -2112){ //neutron ~
-                   fhNBarReg->Fill(RecParticle->Energy() ) ;
-                   fhNBarEM->Fill(RecParticle->Energy() ) ;
-
-                 }
-                 if(PrimaryCode == 2){
-                   fhChargedReg->Fill(RecParticle->Energy() ) ;
-                   fhChargedEM->Fill(RecParticle->Energy() ) ;
-                 }
-
-                 fhAllReg->Fill(RecParticle->Energy() ) ;
-                 fhAllEM->Fill(RecParticle->Energy() ) ;
-                 Counter[0][PrimaryCode]++;
-                 break;
-               case  AliPHOSFastRecParticle::kELECTRON:
-                 if(PrimaryType == 11 || PrimaryType == -11){
-                   fhElectronEnergy->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(), RecParticle->Energy() ) ; 
-                   fhElectronPosition->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(),Distance ) ;
-                 }
-                 if(PrimaryType == 22) 
-                   fhPhotElec->Fill(RecParticle->Energy() ) ;
-
-                 Counter[1][PrimaryCode]++;
-                 break;
-               case  AliPHOSFastRecParticle::kNEUTRALHA:
-                 if(PrimaryType == 22) 
-                   fhPhotNeuH->Fill(RecParticle->Energy() ) ;
-
-                 fhNeutralHadronEnergy->Fill( ((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(), RecParticle->Energy()) ; 
-                 fhNeutralHadronPosition->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy() ,Distance  ) ;
-                 Counter[2][PrimaryCode]++;
-                 break ;
-               case  AliPHOSFastRecParticle::kNEUTRALEM:
-                 if(PrimaryType == 22 || PrimaryType == 11 || PrimaryType == -11){
-                   fhNeutralEMEnergy->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(),RecParticle->Energy() ) ; 
-                   fhNeutralEMPosition->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(),Distance ) ;
-                 }
-
-                 if(PrimaryType == 22){ //photon
-                   fhPhotNuEM->Fill(RecParticle->Energy() ) ;
-                   fhPhotonEM->Fill(RecParticle->Energy() ) ;
-                 }
-                 if(PrimaryType == 2112) //neutron
-                   fhNEM->Fill(RecParticle->Energy() ) ;
-                 
-                 if(PrimaryType == -2112) //neutron ~
-                   fhNBarEM->Fill(RecParticle->Energy() ) ;
-
-                 if(PrimaryCode == 2)
-                   fhChargedEM->Fill(RecParticle->Energy() ) ;
-
-                 fhAllEM->Fill(RecParticle->Energy() ) ;
-
-                 Counter[3][PrimaryCode]++;
-                 break ;
-               case  AliPHOSFastRecParticle::kCHARGEDHA:
-                 if(PrimaryType == 22) //photon
-                   fhPhotChHa->Fill(RecParticle->Energy() ) ;
-                 
-                 fhChargedHadronEnergy->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(),RecParticle->Energy() ) ; 
-                 fhChargedHadronPosition->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(),Distance ) ;
-                 Counter[4][PrimaryCode]++ ;
-                 break ;
-               case  AliPHOSFastRecParticle::kGAMMAHA:
-                 if(PrimaryType == 22) //photon
-                   fhPhotGaHa->Fill(RecParticle->Energy() ) ;
-                 fhPhotonHadronEnergy->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(), RecParticle->Energy()) ; 
-                 fhPhotonHadronPosition->Fill(((TParticle *)PrimaryList->At(ClosestPrimary))->Energy(),Distance ) ;
-                 Counter[5][PrimaryCode]++ ;
-                 break ;       
-               case  AliPHOSFastRecParticle::kABSURDEM:              
-                 Counter[6][PrimaryCode]++ ;
-                 break;
-               case  AliPHOSFastRecParticle::kABSURDHA:
-                 Counter[7][PrimaryCode]++ ;
-                 break;
-               default:
-                 Counter[8][PrimaryCode]++ ;
-                 break;
-               }
-           }
-       }  
-    }   // endfor
-  SaveHistograms();
-  cout << "Resolutions: Analyzed " << Nevents << " event(s)" << endl ;
-  cout << "Resolutions: Total primary       " << TotalPrimary << endl ;
-  cout << "Resoluitons: Total reconstracted " << TotalRecPart << endl ;
-  cout << "TotalReconstructed with Primarie " << TotalRPwithPrim << endl ;
-  cout << "                        Primary:   Photon   Electron   Ch. Hadr.  Neutr. Hadr  Kaons" << endl ; 
-  cout << "             Detected as photon       " << Counter[0][0] << "          " << Counter[0][1] << "          " << Counter[0][2] << "          " <<Counter[0][3] << "          " << Counter[0][4] << endl ;
-  cout << "           Detected as electron       " << Counter[1][0] << "          " << Counter[1][1] << "          " << Counter[1][2] << "          " <<Counter[1][3] << "          " << Counter[1][4] << endl ; 
-  cout << "     Detected as neutral hadron       " << Counter[2][0] << "          " << Counter[2][1] << "          " << Counter[2][2] << "          " <<Counter[2][3] << "          " << Counter[2][4] << endl ;
-  cout << "         Detected as neutral EM       " << Counter[3][0] << "          " << Counter[3][1] << "          " << Counter[3][2] << "          " <<Counter[3][3] << "          " << Counter[3][4] << endl ;
-  cout << "     Detected as charged hadron       " << Counter[4][0] << "          " << Counter[4][1] << "          " << Counter[4][2] << "          " <<Counter[4][3] << "          " << Counter[4][4] << endl ;
-  cout << "       Detected as gamma-hadron       " << Counter[5][0] << "          " << Counter[5][1] << "          " << Counter[5][2] << "          " <<Counter[5][3] << "          " << Counter[5][4] << endl ;
-  cout << "          Detected as Absurd EM       " << Counter[6][0] << "          " << Counter[6][1] << "          " << Counter[6][2] << "          " <<Counter[6][3] << "          " << Counter[6][4] << endl ;
-  cout << "      Detected as absurd hadron       " << Counter[7][0] << "          " << Counter[7][1] << "          " << Counter[7][2] << "          " <<Counter[7][3] << "          " << Counter[7][4] << endl ;
-  cout << "          Detected as undefined       " << Counter[8][0] << "          " << Counter[8][1] << "          " << Counter[8][2] << "          " <<Counter[8][3] << "          " << Counter[8][4] << endl ;
-      
-      for(i1 = 0; i1<9; i1++)
-       for(i2 = 0; i2<5; i2++)
-         TotalInd+=Counter[i1][i2] ;
-      cout << "Indentified particles            " << TotalInd << endl ;
-      
-}           // endfunction
-
+  TString message ; 
+  message  = "TreeS:\n" ;
+  for(ibranch = 0;ibranch <branches->GetEntries();ibranch++){
+    TBranch * branch=(TBranch *) branches->At(ibranch) ;
+    if(strstr(branch->GetName(),"PHOS") ){
+      message += "       " ; 
+      message += branch->GetName() ; 
+      message += "     " ; 
+      message += branch->GetTitle() ;
+      message += "\n" ; 
+    }
+  }
+  if (gime->TreeD() == 0x0) 
+   {
+     if (gime->LoadDigits("READ"))
+      {
+        AliError(Form("Problems with loading digits"));
+        return;
+      }
+   }
 
-//____________________________________________________________________________
-void  AliPHOSAnalyze::BookingHistograms()
-{
-  // Books the histograms where the results of the analysis are stored (to be changed)
-
-  delete fhEmcDigit  ;
-  delete fhVetoDigit  ;
-  delete fhConvertorDigit   ;
-  delete  fhEmcCluster   ;
-  delete fhVetoCluster   ;
-  delete fhConvertorCluster  ;
-  delete fhConvertorEmc  ;
+  branches = gime->TreeD()->GetListOfBranches() ;
+  
+  message += "TreeD:\n" ;
+  for(ibranch = 0;ibranch <branches->GetEntries();ibranch++){
+    TBranch * branch=(TBranch *) branches->At(ibranch) ;
+    if(strstr(branch->GetName(),"PHOS") ) {
+      message += "       "; 
+      message += branch->GetName() ; 
+      message += "     " ; 
+      message += branch->GetTitle() ; 
+      message +="\n" ;
+    }
+  }
   
-  fhEmcDigit                = new TH1F("hEmcDigit",      "hEmcDigit",         1000,  0. ,  25.);
-  fhVetoDigit               = new TH1F("hVetoDigit",     "hVetoDigit",         500,  0. ,  3.e-5);
-  fhConvertorDigit          = new TH1F("hConvertorDigit","hConvertorDigit",    500,  0. ,  3.e-5);
-  fhEmcCluster              = new TH1F("hEmcCluster",    "hEmcCluster",       1000,  0. ,  30.);
-  fhVetoCluster             = new TH1F("hVetoCluster",   "hVetoCluster",       500,  0. ,  3.e-5);
-  fhConvertorCluster        = new TH1F("hConvertorCluster","hConvertorCluster",500,  0. ,  3.e-5);
-  fhConvertorEmc            = new TH2F("hConvertorEmc",  "hConvertorEmc",      200,  1. ,  3., 200, 0., 3.e-5);
+  if (gime->TreeR() == 0x0) 
+   {
+     if (gime->LoadRecPoints("READ"))
+      {
+        AliError(Form("Problems with loading rec points"));
+        return;
+      }
+   }
 
+  branches = gime->TreeR()->GetListOfBranches() ;
+  
+  message += "TreeR: \n" ;
+  for(ibranch = 0;ibranch <branches->GetEntries();ibranch++){
+    TBranch * branch=(TBranch *) branches->At(ibranch) ;
+    if(strstr(branch->GetName(),"PHOS") ) {
+      message += "       " ; 
+      message += branch->GetName() ; 
+      message += "     " ;
+      message += branch->GetTitle() ; 
+      message += "\n" ;
+    }
+  }
+  AliInfo(Form(message.Data())) ;  
 }
 //____________________________________________________________________________
-void  AliPHOSAnalyze::BookResolutionHistograms()
+ void AliPHOSAnalyze::InvariantMass()
 {
-  // Books the histograms where the results of the Resolution analysis are stored
-
-  if(fhPhotonEnergy)
-    delete fhPhotonEnergy ;
-  if(fhPhotonAllEnergy)
-    delete fhPhotonAllEnergy ;
-  if(fhElectronEnergy)
-    delete fhElectronEnergy ;
-  if(fhElectronAllEnergy)
-    delete fhElectronAllEnergy ;
-  if(fhNeutralHadronEnergy)
-    delete fhNeutralHadronEnergy ;
-  if(fhNeutralEMEnergy)
-    delete fhNeutralEMEnergy ;
-  if(fhNeutralEMAllEnergy)
-    delete fhNeutralEMAllEnergy ;
-  if(fhChargedHadronEnergy)
-    delete fhChargedHadronEnergy ;
-  if(fhPhotonHadronEnergy)
-    delete fhPhotonHadronEnergy ;
-  if(fhPhotonPosition)
-    delete fhPhotonPosition ;
-  if(fhPhotonAllPosition)
-    delete fhPhotonAllPosition ;
-  if(fhElectronPosition)
-    delete fhElectronPosition ;
-  if(fhElectronAllPosition)
-    delete fhElectronAllPosition ;
-  if(fhNeutralHadronPosition)
-    delete fhNeutralHadronPosition ;
-  if(fhNeutralEMPosition)
-    delete fhNeutralEMPosition ;
-  if(fhNeutralEMAllPosition)
-    delete fhNeutralEMAllPosition ;
-  if(fhChargedHadronPosition)
-    delete fhChargedHadronPosition ;
-  if(fhPhotonHadronPosition)
-    delete fhPhotonHadronPosition ;
-
-  fhPhotonEnergy            = new TH2F("hPhotonEnergy",  "hPhotonEnergy",              100, 0., 5., 100, 0., 5.);
-  fhPhotonAllEnergy         = new TH2F("hPhotonAllEnergy",  "hPhotonAllEnergy",        100, 0., 5., 100, 0., 5.);
-  fhElectronEnergy          = new TH2F("hElectronEnergy","hElectronEnergy",            100, 0., 5., 100, 0., 5.);
-  fhElectronAllEnergy       = new TH2F("hElectronAllEnergy","hElectronAllEnergy",      100, 0., 5., 100, 0., 5.);
-  fhNeutralHadronEnergy     = new TH2F("hNeutralHadronEnergy", "hNeutralHadronEnergy", 100, 0., 5., 100, 0., 5.);
-  fhNeutralEMEnergy         = new TH2F("hNeutralEMEnergy", "hNeutralEMEnergy",         100, 0., 5., 100, 0., 5.);
-  fhNeutralEMAllEnergy      = new TH2F("hNeutralEMAllEnergy", "hNeutralEMAllEnergy",   100, 0., 5., 100, 0., 5.);
-  fhChargedHadronEnergy     = new TH2F("hChargedHadronEnergy", "hChargedHadronEnergy", 100, 0., 5., 100, 0., 5.);
-  fhPhotonHadronEnergy      = new TH2F("hPhotonHadronEnergy","hPhotonHadronEnergy",    100, 0., 5., 100, 0., 5.);
-  fhPhotonPosition          = new TH2F("hPhotonPosition","hPhotonPosition",                20, 0., 5., 100, 0., 5.);
-  fhPhotonAllPosition       = new TH2F("hPhotonAllPosition","hPhotonAllPosition",          20, 0., 5., 100, 0., 5.);
-  fhElectronPosition        = new TH2F("hElectronPosition","hElectronPosition",            20, 0., 5., 100, 0., 5.);
-  fhElectronAllPosition     = new TH2F("hElectronAllPosition","hElectronAllPosition",      20, 0., 5., 100, 0., 5.);
-  fhNeutralHadronPosition   = new TH2F("hNeutralHadronPosition","hNeutralHadronPosition",  20, 0., 5., 100, 0., 5.);
-  fhNeutralEMPosition       = new TH2F("hNeutralEMPosition","hNeutralEMPosition",          20, 0., 5., 100, 0., 5.);
-  fhNeutralEMAllPosition    = new TH2F("hNeutralEMAllPosition","hNeutralEMAllPosition",    20, 0., 5., 100, 0., 5.);
-  fhChargedHadronPosition   = new TH2F("hChargedHadronPosition","hChargedHadronPosition",  20, 0., 5., 100, 0., 5.);
-  fhPhotonHadronPosition    = new TH2F("hPhotonHadronPosition","hPhotonHadronPosition",    20, 0., 5., 100, 0., 5.);
-
-  if(fhPhotonReg)
-    delete fhPhotonReg ;
-  if(fhAllReg)
-    delete fhAllReg ;
-  if(fhNReg)
-    delete fhNReg ;
-  if(fhNReg)
-    delete fhNReg ;
-  if(fhNReg)
-    delete fhNReg ;
+  // Calculates Real and Mixed invariant mass distributions
+  if (fRunLoader == 0x0)
+   {
+     AliError(Form("Error Loading session"));
+     return;
+   }
   
-  fhPhotonReg = new TH1F("hPhotonReg","hPhotonReg", 20, 0., 5.);
-  fhAllReg    = new TH1F("hAllReg", "hAllReg",  20, 0., 5.);
-  fhNReg      = new TH1F("hNReg", "hNReg",  20, 0., 5.);
-  fhNBarReg   = new TH1F("hNBarReg", "hNBarReg",  20, 0., 5.);
-  fhChargedReg= new TH1F("hChargedReg", "hChargedReg",  20, 0., 5.);
+  AliPHOSLoader* gime = dynamic_cast<AliPHOSLoader*>(fRunLoader->GetLoader("PHOSLoader"));
+  if ( gime == 0 ) 
+   {
+     AliError(Form("Could not obtain the Loader object !")); 
+     return ;
+   } 
   
-  if(fhPhotonEM)
-    delete fhPhotonEM ;
-  if(fhAllEM)
-    delete fhAllEM ;
-  if(fhNEM)
-    delete fhNEM ;
-  if(fhNBarEM)
-    delete fhNBarEM ;
-  if(fhChargedEM)
-    delete fhChargedEM ;
+  gime->LoadRecParticles("READ");
   
-  fhPhotonEM = new TH1F("hPhotonEM","hPhotonEM", 20, 0., 5.);
-  fhAllEM    = new TH1F("hAllEM", "hAllEM",  20, 0., 5.);
-  fhNEM      = new TH1F("hNEM", "hNEM",  20, 0., 5.);
-  fhNBarEM   = new TH1F("hNBarEM", "hNBarEM",  20, 0., 5.);
-  fhChargedEM= new TH1F("hChargedEM", "hChargedEM",  20, 0., 5.);
+  Int_t nMixedEvents = 4 ; //# of events used for calculation of 'mixed' distribution 
+  
+  
+  //opening file
+  TFile * mfile = new TFile("invmass.root","update");
+  
+  //========== Reading /Booking Histograms
+  TH2D * hRealEM = 0 ;
+  hRealEM = (TH2D*) mfile->Get("hRealEM") ;
+  if(hRealEM == 0) 
+    hRealEM = new TH2D("hRealEM",   "Real for EM particles",      250,0.,1.,40,0.,4.) ;
+  TH2D * hRealPhot = 0 ;
+
+  hRealPhot = (TH2D*)mfile->Get("hRealPhot");
+  if(hRealPhot == 0)
+    hRealPhot = new TH2D("hRealPhot", "Real for kPhoton particles", 250,0.,1.,40,0.,4.) ;
+
+  TH2D * hMixedEM = 0 ;
+  hMixedEM = (TH2D*) mfile->Get("hMixedEM") ;
+  if(hMixedEM == 0)
+    hMixedEM = new TH2D("hMixedEM",  "Mixed for EM particles",     250,0.,1.,40,0.,4.) ;
+
+  TH2D * hMixedPhot = 0 ;
+  hMixedPhot = (TH2D*) mfile->Get("hMixedPhot") ;
+  if(hMixedPhot == 0)
+    hMixedPhot = new TH2D("hMixedPhot","Mixed for kPhoton particles",250,0.,1.,40,0.,4.) ;
   
-  if(fhPrimary)
-    delete fhPrimary ;
-  fhPrimary= new TH1F("hPrimary", "hPrimary",  20, 0., 5.);
-
-  if(fhPhotPhot)
-    delete fhPhotPhot ;
-  if(fhPhotElec)
-    delete fhPhotElec ;
-  if(fhPhotNeuH)
-    delete fhPhotNeuH ;
-  if(fhPhotNuEM)
-    delete fhPhotNuEM ;
-  if(fhPhotChHa)
-    delete fhPhotChHa ;
-  if(fhPhotGaHa)
-    delete fhPhotGaHa ;
-
-  fhPhotPhot = new TH1F("hPhotPhot","hPhotPhot", 20, 0., 5.);   //Photon registered as photon
-  fhPhotElec = new TH1F("hPhotElec","hPhotElec", 20, 0., 5.);   //Photon registered as Electron
-  fhPhotNeuH = new TH1F("hPhotNeuH","hPhotNeuH", 20, 0., 5.);   //Photon registered as Neutral Hadron
-  fhPhotNuEM = new TH1F("hPhotNuEM","hPhotNuEM", 20, 0., 5.);   //Photon registered as Neutral EM
-  fhPhotChHa = new TH1F("hPhotChHa","hPhotChHa", 20, 0., 5.);   //Photon registered as Charged Hadron
-  fhPhotGaHa = new TH1F("hPhotGaHa","hPhotGaHa", 20, 0., 5.);   //Photon registered as Gamma-Hadron
 
+  //reading event and copyng it to TConesArray of all photons
 
-}
-//____________________________________________________________________________
-Bool_t AliPHOSAnalyze::Init(Int_t evt)
-{
-  // Do a few initializations: open the root file
-  //                           get the AliRun object
-  //                           defines the clusterizer, tracksegment maker and particle identifier
-  //                           sets the associated parameters
-
-  Bool_t ok = kTRUE ; 
+  TClonesArray * allRecParticleList  = new TClonesArray("AliPHOSRecParticle", 1000) ;
+  Int_t * nRecParticles = new Int_t[nMixedEvents] ; // to mark boundaries of each event in the total list
+  for(Int_t index = 0; index < nMixedEvents; index ++)
+    nRecParticles[index] = 0 ;
+  Int_t iRecPhot = 0 ;                // number of EM particles in total list
+  
+  //scan over all events
+  Int_t event ;
   
-   //========== Open galice root file  
-
-  if ( fRootFile == 0 ) {
-    Text_t * name  = new Text_t[80] ; 
-    cout << "AnalyzeOneEvent > Enter file root file name : " ;  
-    cin >> name ; 
-    Bool_t ok = OpenRootFile(name) ; 
-    if ( !ok )
-      cout << " AliPHOSAnalyze > Error opening " << name << endl ; 
-    else { 
-      //========== Get AliRun object from file 
-      
-      gAlice = (AliRun*) fRootFile->Get("gAlice") ;
-      
-      //=========== Get the PHOS object and associated geometry from the file 
-      
-      fPHOS  = (AliPHOSv1 *)gAlice->GetDetector("PHOS") ;
-      fGeom = fPHOS->GetGeometry();
-      //      fGeom  = AliPHOSGeometry::GetInstance( fPHOS->GetGeometry()->GetName(), fPHOS->GetGeometry()->GetTitle() );
 
-    } // else !ok
-  } // if fRootFile
+  if (fRunLoader->TreeE() == 0x0) fRunLoader->LoadHeader();
   
-  if ( ok ) {
-    
-    //========== Create the Clusterizer
-
-    fClu =  new AliPHOSClusterizerv1() ; 
-    fClu->SetEmcEnergyThreshold(0.030) ; 
-    fClu->SetEmcClusteringThreshold(0.20) ; 
-    fClu->SetPpsdEnergyThreshold    (0.0000002) ; 
-    fClu->SetPpsdClusteringThreshold(0.0000001) ; 
-    fClu->SetLocalMaxCut(0.03) ;
-    fClu->SetCalibrationParameters(0., 0.00000001) ;  
-    cout <<  "AnalyzeOneEvent > using clusterizer " << fClu->GetName() << endl ; 
-    fClu->PrintParameters() ; 
-    
-    //========== Creates the track segment maker
-    
-    fTrs = new AliPHOSTrackSegmentMakerv1() ;
-    cout <<  "AnalyzeOneEvent > using tack segment maker " << fTrs->GetName() << endl ; 
-    //   fTrs->UnsetUnfoldFlag() ;
-    
-    //========== Creates the particle identifier
+  
+  Int_t maxevent = (Int_t)fRunLoader->TreeE()->GetEntries();
+  //  for(event = 0; event < gime->MaxEvent(); event++  ){
+  
+  
+  
+  for(event = 0; event < maxevent; event++  ){
+    fRunLoader->GetEvent(event);  //will read only TreeR 
     
-    fPID = new AliPHOSPIDv1() ;
-    cout <<  "AnalyzeOneEvent > using particle identifier " << fPID->GetName() << endl ; 
-    //fPID->SetShowerProfileCuts(Float_t l1m, Float_t l1M, Float_t l2m, Float_t l2M) ; 
-    fPID->SetShowerProfileCuts(0.7, 2.0 , 0.6 , 1.5) ; 
+    //copy EM RecParticles to the "total" list        
+    const AliPHOSRecParticle * recParticle ;
+    Int_t iRecParticle ;
+    TClonesArray * rp = gime->RecParticles() ;
+    if(!rp){
+      AliError(Form("Can't find RecParticles")) ; 
+      return ;
+    }
 
-    //========== Creates the Reconstructioner  
-    
-    fRec = new AliPHOSReconstructioner(fClu, fTrs, fPID) ;
-    fRec -> SetDebugReconstruction(kFALSE);     
+    for(iRecParticle = 0; iRecParticle < rp->GetEntriesFast(); iRecParticle++ )
+      {
+       recParticle = (AliPHOSRecParticle *) rp->At(iRecParticle) ;
+       if((recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST)||
+          (recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMSLOW))
+         new( (*allRecParticleList)[iRecPhot++] ) AliPHOSRecParticle(*recParticle) ;
+      }
     
-    //=========== Connect the various Tree's for evt
+    Int_t mevent = event%nMixedEvents ; //event number in the "mixed" cicle
+    nRecParticles[mevent] = iRecPhot-1 ;  
     
-    if ( evt == -999 ) {
-      cout <<  "AnalyzeOneEvent > Enter event number : " ; 
-      cin >> evt ;  
-      cout << evt << endl ; 
+    //check, if it is time to calculate invariant mass?
+    if((mevent == 0) && (event +1 == maxevent)){
+      
+      //   if((mevent == 0) && (event +1 == gime->MaxEvent())){
+      
+      //calculate invariant mass:
+      Int_t irp1,irp2 ;
+      Int_t nCurEvent = 0 ;
+      
+      for(irp1 = 0; irp1 < allRecParticleList->GetEntries()-1; irp1++){
+       AliPHOSRecParticle * rp1 = (AliPHOSRecParticle *)allRecParticleList->At(irp1) ;
+       
+       for(irp2 = irp1+1; irp2 < allRecParticleList->GetEntries(); irp2++){
+         AliPHOSRecParticle * rp2 = (AliPHOSRecParticle *)allRecParticleList->At(irp2) ;
+         
+         Double_t invMass ;
+         invMass = (rp1->Energy()+rp2->Energy())*(rp1->Energy()+rp2->Energy())-
+           (rp1->Px()+rp2->Px())*(rp1->Px()+rp2->Px())-
+           (rp1->Py()+rp2->Py())*(rp1->Py()+rp2->Py())-
+           (rp1->Pz()+rp2->Pz())*(rp1->Pz()+rp2->Pz()) ;
+         
+         if(invMass> 0)
+           invMass = TMath::Sqrt(invMass);
+         
+         Double_t pt ; 
+         pt = TMath::Sqrt((rp1->Px()+rp2->Px() )*( rp1->Px()+rp2->Px() ) + 
+                        (rp1->Py()+rp2->Py() )*( rp1->Py()+rp2->Py() ) );
+         
+         if(irp1 > nRecParticles[nCurEvent])
+           nCurEvent++;
+         
+         if(irp2 <= nRecParticles[nCurEvent]){ //'Real' event
+           hRealEM->Fill(invMass,pt);
+           if((rp1->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST)&&
+              (rp2->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST) )
+             hRealPhot->Fill(invMass,pt);
+         }
+         else{
+           hMixedEM->Fill(invMass,pt);
+           if((rp1->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST)&&
+              (rp2->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST) )
+             hMixedPhot->Fill(invMass,pt);
+         } //real-mixed
+         
+      } //loop over second rp
+      }//loop over first rp
+      
+      //Make some cleanings 
+      for(Int_t index = 0; index < nMixedEvents; index ++)
+       nRecParticles[index] = 0 ;
+      iRecPhot = 0 ;              
+      allRecParticleList->Clear() ;
+      
     }
-    fEvt = evt ; 
-    gAlice->GetEvent(evt);
-    
-    //=========== Get the Digit TTree
-    
-    gAlice->TreeD()->GetEvent(0) ;     
-    
-  } // ok
+  }
+  delete allRecParticleList ;
   
-  return ok ; 
-}
+  //writing output
+  mfile->cd();
+  
+  hRealEM->Write(0,kOverwrite) ;
+  hRealPhot->Write(0,kOverwrite) ;
+  hMixedEM->Write(0,kOverwrite) ;
+  hMixedPhot->Write(0,kOverwrite) ;
+  
+  mfile->Write();
+  mfile->Close();
+  delete mfile ;
+  delete nRecParticles;
 
+}
 
 //____________________________________________________________________________
-void AliPHOSAnalyze::DisplayKineEvent(Int_t evt)
+ void AliPHOSAnalyze::EnergyResolution()
 {
-  // Display particles from the Kine Tree in global Alice (theta, phi) coordinates. 
-  // One PHOS module at the time.
-  // The particle type can be selected.
-  
-  if (evt == -999) 
-    evt = fEvt ;
+  //fills two dimentional histo: energy of primary vs. energy of reconstructed
 
-  Int_t module ; 
-  cout <<  "DisplayKineEvent > which module (1-5,  -1: all) ? " ; 
-  cin >> module ; cout << module << endl ; 
+  TH2F * hAllEnergy = 0 ;  //all reconstructed with primary photon   
+  TH2F * hPhotEnergy= 0 ;  //kGamma  with primary photon   
+  TH2F * hEMEnergy  = 0 ;  //electromagnetic with primary photon   
 
-  Int_t testparticle ; 
-  cout << " 22      : PHOTON " << endl 
-       << " (-)11   : (POSITRON)ELECTRON " << endl 
-       << " (-)2112 : (ANTI)NEUTRON " << endl  
-       << " -999    : Everything else " << endl ; 
-  cout  <<  "DisplayKineEvent > enter PDG particle code to display " ; 
-  cin >> testparticle ; cout << testparticle << endl ; 
+  //opening file and reading histograms if any
+  TFile * efile = new TFile("energy.root","update");
 
-  Text_t histoname[80] ;
-  sprintf(histoname,"Event %d: Incident particles in module %d", evt, module) ; 
+  hAllEnergy = (TH2F*)efile->Get("hAllEnergy") ;
+  if(hAllEnergy == 0)
+    hAllEnergy  = new TH2F("hAllEnergy",  "Energy of any RP with primary photon",100, 0., 5., 100, 0., 5.);
 
-  Double_t tm, tM, pm, pM ; // min and Max theta and phi covered by module   
-  fGeom->EmcModuleCoverage(module, tm, tM, pm, pM, AliPHOSGeometry::Degre() ) ;
+  hPhotEnergy =(TH2F*) efile->Get("hPhotEnergy") ;
+  if(hPhotEnergy == 0)
+    hPhotEnergy = new TH2F("hPhotEnergy", "Energy of kGAMMA with primary photon",100, 0., 5., 100, 0., 5.);
 
-  Double_t theta, phi ; 
-  fGeom->EmcXtalCoverage(theta, phi, AliPHOSGeometry::Degre() ) ;
+  hEMEnergy =(TH2F*) efile->Get("hEMEnergy");
+  if(hEMEnergy == 0)
+    hEMEnergy   = new TH2F("hEMEnergy",   "Energy of EM with primary photon",    100, 0., 5., 100, 0., 5.);
 
-  Int_t tdim = (Int_t)( (tM - tm) / theta ) ; 
-  Int_t pdim = (Int_t)( (pM - pm) / phi ) ; 
 
-  tm -= theta ; 
-  tM += theta ; 
-  pm -= phi ; 
-  pM += phi ; 
+  if (fRunLoader == 0x0)
+   {
+     AliError(Form("Error Loading session"));
+     return;
+   }
+  
+  AliPHOSLoader* gime = dynamic_cast<AliPHOSLoader*>(fRunLoader->GetLoader("PHOSLoader"));
+  if ( gime == 0 ) 
+   {
+     AliError(Form("Could not obtain the Loader object !")); 
+     return ;
+   } 
 
-  TH2F * histoparticle = new TH2F("histoparticle",  histoname, 
-                                         pdim, pm, pM, tdim, tm, tM) ; 
-  histoparticle->SetStats(kFALSE) ;
 
-  // Get pointers to Alice Particle TClonesArray
+  AliPHOSGeometry * phosgeom =  AliPHOSGeometry::GetInstance() ;
 
-  TParticle * particle;
-  TClonesArray * particlearray  = gAlice->Particles();    
+  Int_t ievent;
+  Int_t maxevent = (Int_t)fRunLoader->TreeE()->GetEntries();
 
-  Text_t canvasname[80];
-  sprintf(canvasname,"Particles incident in PHOS/EMC module # %d",module) ;
-  TCanvas * kinecanvas = new TCanvas("kinecanvas", canvasname, 650, 500) ; 
+  fRunLoader->LoadKinematics("READ");
+  gime->LoadTracks("READ");
+  
+  for ( ievent=0; ievent < maxevent ; ievent++){
 
-  // get the KINE Tree
+    //read the current event
+    fRunLoader->GetEvent(ievent) ;
 
-  TTree * kine =  gAlice->TreeK() ; 
-  Stat_t nParticles =  kine->GetEntries() ; 
-  cout << "DisplayKineEvent > events in kine " << nParticles << endl ; 
-  
-  // loop over particles
-
-  Double_t kRADDEG = 180. / TMath::Pi() ; 
-  Int_t index1 ; 
-  Int_t nparticlein = 0 ; 
-  for (index1 = 0 ; index1 < nParticles ; index1++){
-    Int_t nparticle = particlearray->GetEntriesFast() ;
-    Int_t index2 ; 
-    for( index2 = 0 ; index2 < nparticle ; index2++) {         
-      particle            = (TParticle*)particlearray->UncheckedAt(index2) ;
-      Int_t  particletype = particle->GetPdgCode() ;
-      if (testparticle == -999 || testparticle == particletype) { 
-       Double_t phi        = particle->Phi() ;
-       Double_t theta      = particle->Theta() ;
-       Int_t mod ; 
-       Double_t x, z ; 
-       fGeom->ImpactOnEmc(theta, phi, mod, z, x) ;
-       if ( mod == module ) {
-         nparticlein++ ; 
-         if (particle->Energy() >  fClu->GetEmcClusteringThreshold()  )
-           histoparticle->Fill(phi*kRADDEG, theta*kRADDEG, particle->Energy() ) ; 
-       } 
-      } 
-    }
-  }
-  kinecanvas->Draw() ; 
-  histoparticle->Draw("color") ; 
-  TPaveText *  pavetext = new TPaveText(294, 100, 300, 101); 
-  Text_t text[40] ; 
-  sprintf(text, "Particles: %d ", nparticlein) ;
-  pavetext->AddText(text) ; 
-  pavetext->Draw() ; 
-  kinecanvas->Update(); 
+    Double_t vtx[3]={0.,0.,0.} ;  
 
-}
-//____________________________________________________________________________
-void AliPHOSAnalyze::DisplayRecParticles()
-{
-  // Display reconstructed particles in global Alice(theta, phi) coordinates. 
-  // One PHOS module at the time.
-  // Click on symbols indicate the reconstructed particle type. 
-
-  if (fEvt == -999) {
-    cout << "DisplayRecParticles > Analyze an event first ... (y/n) " ; 
-    Text_t answer[1] ; 
-    cin >> answer ; cout << answer ; 
-//     if ( answer == "y" ) 
-//       AnalyzeOneEvent() ;
-  } 
-    if (fEvt != -999) {
+    const AliPHOSRecParticle * recParticle ;
+    Int_t iRecParticle ;
+    TClonesArray * rp = gime->RecParticles() ;
+    if(!rp) {
+      AliError(Form("Event %d,  Can't find RecParticles ", ievent)) ;  
+      return ;
+    }
+    TClonesArray * ts = gime->TrackSegments() ;
+    if(!ts) {
+      AliError(Form("Event %d,  Can't find TrackSegments", ievent)) ;  
+      return ;
+    }
+    TObjArray * emcrp = gime->EmcRecPoints() ;
+    if(!emcrp){
+      AliError(Form("Event %d,  Can't find EmcRecPoints", ievent)) ; 
+      return ;
+    }
+      
+    for(iRecParticle = 0; iRecParticle < rp->GetEntriesFast() ;iRecParticle++ ){
+      recParticle = (AliPHOSRecParticle *) rp->At(iRecParticle) ;
       
-      Int_t module ; 
-      cout <<  "DisplayRecParticles > which module (1-5,  -1: all) ? " ; 
-      cin >> module ; cout << module << endl ;
-      Text_t histoname[80] ; 
-      sprintf(histoname,"Event %d: Reconstructed particles in module %d", fEvt, module) ; 
-      Double_t tm, tM, pm, pM ; // min and Max theta and phi covered by module   
-      fGeom->EmcModuleCoverage(module, tm, tM, pm, pM, AliPHOSGeometry::Degre() ) ;
-      Double_t theta, phi ; 
-      fGeom->EmcXtalCoverage(theta, phi, AliPHOSGeometry::Degre() ) ;
-      Int_t tdim = (Int_t)( (tM - tm) / theta ) ; 
-      Int_t pdim = (Int_t)( (pM - pm) / phi ) ; 
-      tm -= theta ; 
-      tM += theta ; 
-      pm -= phi ; 
-      TH2F * histoRparticle = new TH2F("histoRparticle",  histoname, 
-                                      pdim, pm, pM, tdim, tm, tM) ; 
-      histoRparticle->SetStats(kFALSE) ;
-      Text_t canvasname[80] ; 
-      sprintf(canvasname, "Reconstructed particles in PHOSmodule # %d", module) ;
-      TCanvas * rparticlecanvas = new TCanvas("RparticleCanvas", canvasname, 650, 500) ; 
-      AliPHOSRecParticle::RecParticlesList * rpl = *fPHOS->RecParticles() ; 
-      Int_t nRecParticles = rpl->GetEntries() ; 
-      Int_t nRecParticlesInModule = 0 ; 
-      TIter nextRecPart(rpl) ; 
-      AliPHOSRecParticle * rp ; 
-      cout << "DisplayRecParticles > " << nRecParticles << " reconstructed particles " << endl ; 
-      Double_t kRADDEG = 180. / TMath::Pi() ; 
-      while ( (rp = (AliPHOSRecParticle *)nextRecPart() ) ) {
-       AliPHOSTrackSegment * ts = rp->GetPHOSTrackSegment() ; 
-       if ( ts->GetPHOSMod() == module ) {
-         Int_t numberofprimaries = 0 ;
-         Int_t * listofprimaries = 0;
-         rp->GetPrimaries(numberofprimaries) ;
-         cout << "Number of primaries = " << numberofprimaries << endl ; 
-         Int_t index ;
-         for ( index = 0 ; index < numberofprimaries ; index++)
-           cout << "    primary # " << index << " =  " << listofprimaries[index] << endl ;  
-         
-         nRecParticlesInModule++ ; 
-         Double_t theta = rp->Theta() * kRADDEG ;
-         Double_t phi   = rp->Phi() * kRADDEG ;
-         Double_t energy = rp->Energy() ; 
-         histoRparticle->Fill(phi, theta, energy) ;
-       }
+      //find the closest primary
+      Int_t moduleNumberRec ;
+      Double_t recX, recZ ;
+      phosgeom->ImpactOnEmc(vtx,recParticle->Theta(), recParticle->Phi(), moduleNumberRec, recX, recZ) ;
+      
+      Double_t minDistance  = 100. ;
+      Int_t closestPrimary = -1 ;
+      
+      //extract list of primaries: it is stored at EMC RecPoints
+      Int_t emcIndex = ((AliPHOSTrackSegment*) ts->At(recParticle->GetPHOSTSIndex()))->GetEmcIndex() ;
+      Int_t numberofprimaries ;
+      Int_t * listofprimaries  = ((AliPHOSEmcRecPoint*) emcrp->At(emcIndex))->GetPrimaries(numberofprimaries)  ;
+      
+      Int_t index ;
+      const TParticle * primary ;
+      Double_t distance = minDistance ;
+      Double_t dX, dZ; 
+      Double_t dXmin = 0.; 
+      Double_t dZmin = 0. ;
+      for ( index = 0 ; index < numberofprimaries ; index++){
+
+       primary = fRunLoader->Stack()->Particle(listofprimaries[index]) ;
+
+       Int_t moduleNumber ;
+       Double_t primX, primZ ;
+       phosgeom->ImpactOnEmc(vtx,primary->Theta(), primary->Phi(), moduleNumber, primX, primZ) ;
+       if(moduleNumberRec == moduleNumber) {
+         dX = recX - primX;
+         dZ = recZ - primZ;
+         distance = TMath::Sqrt(dX*dX + dZ*dZ) ;
+         if(minDistance > distance) {
+           minDistance = distance ;
+           dXmin = dX;
+           dZmin = dZ;
+           closestPrimary = listofprimaries[index] ;
+         }
+       }
       }
-      histoRparticle->Draw("color") ; 
 
-      nextRecPart.Reset() ; 
-      while ( (rp = (AliPHOSRecParticle *)nextRecPart() ) ) {
-       AliPHOSTrackSegment * ts = rp->GetPHOSTrackSegment() ; 
-       if ( ts->GetPHOSMod() == module )  
-         rp->Draw("P") ; 
+      //if found primary, fill histograms
+      if(closestPrimary >=0 ){
+       primary = fRunLoader->Stack()->Particle(closestPrimary) ;
+       if(primary->GetPdgCode() == 22){
+         hAllEnergy->Fill(primary->Energy(), recParticle->Energy()) ;
+         if(recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST){
+           hPhotEnergy->Fill(primary->Energy(), recParticle->Energy() ) ; 
+           hEMEnergy->Fill(primary->Energy(), recParticle->Energy() ) ; 
+         }
+         else
+           if(recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMSLOW)
+             hEMEnergy->Fill(primary->Energy(), recParticle->Energy() ) ; 
+       }
       }
-
-      Text_t text[80] ; 
-      sprintf(text, "reconstructed particles: %d", nRecParticlesInModule) ;
-      TPaveText *  pavetext = new TPaveText(292, 100, 300, 101); 
-      pavetext->AddText(text) ; 
-      pavetext->Draw() ; 
-      rparticlecanvas->Update() ; 
     }
-}
+  }
+
+  //write filled histograms
+  efile->cd() ;
+  hAllEnergy->Write(0,kOverwrite) ;
+  hPhotEnergy->Write(0,kOverwrite) ;
+  hEMEnergy->Write(0,kOverwrite)  ;
+  //  efile->Write() ;
+  efile->Close() ;
+  delete efile ;
 
+}
 //____________________________________________________________________________
-void AliPHOSAnalyze::DisplayRecPoints()
+void AliPHOSAnalyze::PositionResolution()
 {
-  // Display reconstructed points in local PHOS-module (x, z) coordinates. 
-  // One PHOS module at the time.
-  // Click on symbols displays the EMC cluster, or PPSD information.
-
-  if (fEvt == -999) {
-    cout << "DisplayRecPoints > Analyze an event first ... (y/n) " ; 
-    Text_t answer[1] ; 
-    cin >> answer ; cout << answer ; 
-//     if ( answer == "y" ) 
-//       AnalyzeOneEvent() ;
-  } 
-    if (fEvt != -999) {
-      
-      Int_t module ; 
-      cout <<  "DisplayRecPoints > which module (1-5,  -1: all) ? " ; 
-      cin >> module ; cout << module << endl ; 
+  //fills two dimentional histo: energy vs. primary - reconstructed distance  
+
+
+
+  TH2F * hAllPosition  = 0;     // Position of any RP with primary photon
+  TH2F * hPhotPosition = 0;    // Position of kGAMMA with primary photon
+  TH2F * hEMPosition   = 0;      // Position of EM with primary photon
+
+  TH1F * hAllPositionX = 0;    // X-Position Resolution of photons with photon primary
+  TH1F * hAllPositionZ = 0;    // Z-Position Resolution of photons with photon primary
+
+
+  //opening file and reading histograms if any
+  TFile * pfile = new TFile("position.root","update");
+
+  hAllPosition = (TH2F*)pfile->Get("hAllPosition");
+  if(hAllPosition == 0)
+    hAllPosition  = new TH2F("hAllPosition",  
+                          "Position of any RP with primary photon",100, 0., 5., 100, 0., 5.);
+  hPhotPosition= (TH2F*)pfile->Get("hPhotPosition");
+  if(hPhotPosition == 0)
+    hPhotPosition = new TH2F("hPhotPosition", 
+                          "Position of kGAMMA with primary photon",100, 0., 5., 100, 0., 5.);
+  hEMPosition= (TH2F*)pfile->Get("hEMPosition") ;
+  if(hEMPosition == 0)
+    hEMPosition   = new TH2F("hEMPosition",   
+                          "Position of EM with primary photon",    100, 0., 5., 100, 0., 5.);
+  hAllPositionX = (TH1F*)pfile->Get("hAllPositionX") ;                        
+  if(hAllPositionX == 0)
+    hAllPositionX = new TH1F("hAllPositionX", 
+                          "Delta X of any RP with primary photon",100, -2., 2.);
+  hAllPositionZ =(TH1F*) pfile->Get("hAllPositionZ") ;
+  if(hAllPositionZ == 0)
+    hAllPositionZ = new TH1F("hAllPositionZ", 
+                          "Delta X of any RP with primary photon",100, -2., 2.);
+
+  if (fRunLoader == 0x0)
+   {
+     AliError(Form("Error Loading session"));
+     return;
+   }
+  
+  AliPHOSLoader* gime = dynamic_cast<AliPHOSLoader*>(fRunLoader->GetLoader("PHOSLoader"));
+  if ( gime == 0 ) 
+   {
+     AliError(Form("Could not obtain the Loader object !")); 
+     return ;
+   } 
+  
+  if (fRunLoader->TreeE() == 0x0) fRunLoader->LoadHeader();
+
+  AliPHOSGeometry * phosgeom =  AliPHOSGeometry::GetInstance() ;
 
-      Text_t canvasname[80];
-      sprintf(canvasname,"Digits in PHOS/EMC module # %d",module) ;
-      TCanvas * modulecanvas = new TCanvas("module", canvasname, 650, 500) ; 
-      modulecanvas->Draw() ;
+  Int_t ievent;
+  Int_t maxevent = (Int_t)fRunLoader->TreeE()->GetEntries() ; 
+  for ( ievent=0; ievent < maxevent ; ievent++){
+    
+    //read the current event
+    fRunLoader->GetEvent(ievent) ;
 
-      //=========== Creating 2d-histogram of the PHOS module
-      // a little bit junkie but is used to test Geom functinalities
+    //DP:Extract vertex position
+    Double_t vtx[3]={0.,0.,0.} ;  
 
-      Double_t tm, tM, pm, pM ; // min and Max theta and phi covered by module   
+    TClonesArray * rp = gime->RecParticles() ;
+    if(!rp) {
+      AliError(Form("Event %d,  Can't find RecParticles", ievent)) ;
+      return ;
+    }
+    TClonesArray * ts = gime->TrackSegments() ;
+    if(!ts) {
+      AliError(Form("Event %d,  Can't find TrackSegments", ievent)) ;
+      return ;
+    }
+    TObjArray * emcrp = gime->EmcRecPoints() ;
+    if(!emcrp){
+      AliError(Form("Event %d,  Can't find EmcRecPoints", ievent)) ;
+      return ;
+    }
+    
+    const AliPHOSRecParticle * recParticle ;
+    Int_t iRecParticle ;
+    for(iRecParticle = 0; iRecParticle < rp->GetEntriesFast(); iRecParticle++ ){
+      recParticle = (AliPHOSRecParticle *) rp->At(iRecParticle) ;
       
-      fGeom->EmcModuleCoverage(module, tm, tM, pm, pM); 
-      // convert angles into coordinates local to the EMC module of interest
-
-      Int_t emcModuleNumber ;
-      Double_t emcModulexm, emcModulezm ; // minimum local coordinate in a given EMCA module
-      Double_t emcModulexM, emcModulezM ; // maximum local coordinate in a given EMCA module
-      fGeom->ImpactOnEmc(tm, pm, emcModuleNumber, emcModulezm, emcModulexm) ;
-      fGeom->ImpactOnEmc(tM, pM, emcModuleNumber, emcModulezM, emcModulexM) ;
-      Int_t xdim = (Int_t)( ( emcModulexM - emcModulexm ) / fGeom->GetCrystalSize(0) ) ;  
-      Int_t zdim = (Int_t)( ( emcModulezM - emcModulezm ) / fGeom->GetCrystalSize(2) ) ;
-      Float_t xmin = emcModulexm - fGeom->GetCrystalSize(0) ; 
-      Float_t xMax = emcModulexM + fGeom->GetCrystalSize(0) ; 
-      Float_t zmin = emcModulezm - fGeom->GetCrystalSize(2) ; 
-      Float_t zMax = emcModulezM + fGeom->GetCrystalSize(2) ;     
-      Text_t histoname[80];
-      sprintf(histoname,"Event %d: Digits and RecPoints in module %d", fEvt, module) ;
-      TH2F * hModule = new TH2F("HistoReconstructed", histoname,
-                               xdim, xmin, xMax, zdim, zmin, zMax) ;  
-      hModule->SetMaximum(2.0);
-      hModule->SetMinimum(0.0);
-      hModule->SetStats(kFALSE); 
-
-      TIter next(fPHOS->Digits()) ;
-      Float_t energy, y, z;
-      Float_t etot=0.;
-      Int_t relid[4]; Int_t nDigits = 0 ;
-      AliPHOSDigit * digit ; 
-
-      // Making 2D histogram of the EMC module
-      while((digit = (AliPHOSDigit *)next())) 
-       {  
-         fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
-         if (relid[0] == module && relid[1] == 0)  
-           {  
-             energy = fClu->Calibrate(digit->GetAmp()) ;
-              cout << "Energy is " << energy << " and threshold is " << fClu->GetEmcEnergyThreshold() << endl; 
-             if (energy >  fClu->GetEmcEnergyThreshold()  ){
-               nDigits++ ;
-               etot += energy ; 
-               fGeom->RelPosInModule(relid,y,z) ;   
-               hModule->Fill(y, z, energy) ;
-             }
-           } 
-       }
-      cout <<"DrawRecPoints >  Found in module " 
-          << module << " " << nDigits << "  digits with total energy " << etot << endl ;
-      hModule->Draw("col2") ;
-
-      //=========== Cluster in module
-
-      //      TClonesArray * emcRP = fPHOS->EmcClusters() ; 
-      TObjArray * emcRP = *(fPHOS->EmcRecPoints()) ; 
+      //find the closest primary
+      Int_t moduleNumberRec ;
+      Double_t recX, recZ ;
+      phosgeom->ImpactOnEmc(vtx,recParticle->Theta(), recParticle->Phi(), moduleNumberRec, recX, recZ) ;
       
-      etot = 0.; 
-      Int_t totalnClusters = 0 ; 
-      Int_t nClusters = 0 ;
-      TIter nextemc(emcRP) ;
-      AliPHOSEmcRecPoint * emc ;
-      while((emc = (AliPHOSEmcRecPoint *)nextemc())) 
-       {
-         //      Int_t numberofprimaries ;
-         //      Int_t * primariesarray = new Int_t[10] ;
-         //      emc->GetPrimaries(numberofprimaries, primariesarray) ;
-         totalnClusters++ ;
-         if ( emc->GetPHOSMod() == module )
-           { 
-             nClusters++ ; 
-             energy = emc->GetTotalEnergy() ;   
-             etot+= energy ;  
-             emc->Draw("M") ;
-           }
-       }
-      cout << "DrawRecPoints > Found " << totalnClusters << " EMC Clusters in PHOS" << endl ; 
-      cout << "DrawRecPoints > Found in module " << module << "  " << nClusters << " EMC Clusters " << endl ;
-      cout << "DrawRecPoints > total energy  " << etot << endl ; 
-
-      TPaveText *  pavetext = new TPaveText(22, 80, 83, 90); 
-      Text_t text[40] ; 
-      sprintf(text, "digits: %d;  clusters: %d", nDigits, nClusters) ;
-      pavetext->AddText(text) ; 
-      pavetext->Draw() ; 
-      modulecanvas->Update(); 
-      //=========== Cluster in module PPSD Down
+      Double_t minDistance  = 100. ;
+      Int_t closestPrimary = -1 ;
+      
+      //extract list of primaries: it is stored at EMC RecPoints
+      Int_t emcIndex = ((AliPHOSTrackSegment*) ts->At(recParticle->GetPHOSTSIndex()))->GetEmcIndex() ;
+      Int_t numberofprimaries ;
+      Int_t * listofprimaries  = ((AliPHOSEmcRecPoint *) emcrp->At(emcIndex))->GetPrimaries(numberofprimaries)  ;
 
-      //      TClonesArray * ppsdRP = fPHOS->PpsdClusters() ;
-      TObjArray * ppsdRP = *(fPHOS->PpsdRecPoints() );
+      Int_t index ;
+      const TParticle * primary ;
+      Double_t distance = minDistance ;
+      Double_t dX = 1000; // incredible number
+      Double_t dZ = 1000; // for the case if no primary will be found
+      Double_t dXmin = 0.; 
+      Double_t dZmin = 0. ;
+      for ( index = 0 ; index < numberofprimaries ; index++){
+       primary = fRunLoader->Stack()->Particle(listofprimaries[index]) ;
+       Int_t moduleNumber ;
+       Double_t primX, primZ ;
+       phosgeom->ImpactOnEmc(vtx,primary->Theta(), primary->Phi(), moduleNumber, primX, primZ) ;
+       if(moduleNumberRec == moduleNumber) {
+         dX = recX - primX;
+         dZ = recZ - primZ;
+         distance = TMath::Sqrt(dX*dX + dZ*dZ) ;
+         if(minDistance > distance) {
+           minDistance = distance ;
+           dXmin = dX;
+           dZmin = dZ;
+           closestPrimary = listofprimaries[index] ;
+         }
+       }
+      }
+      
+      //if found primary, fill histograms
+      if(closestPrimary >=0 ){
+       primary = fRunLoader->Stack()->Particle(closestPrimary) ;
+       if(primary->GetPdgCode() == 22){
+         hAllPosition->Fill(primary->Energy(), minDistance) ;
+         hAllPositionX->Fill(primary->Energy(), dX) ;
+         hAllPositionZ->Fill(primary->Energy(), dZ) ;
+         if(recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST){
+           hPhotPosition->Fill(primary->Energy(), minDistance ) ; 
+           hEMPosition->Fill(primary->Energy(), minDistance ) ; 
+         }
+         else
+           if(recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMSLOW)
+             hEMPosition->Fill(primary->Energy(), minDistance ) ; 
+       }
+      }
+    }
+  }
+  
+  //Write output histgrams
+  pfile->cd() ;
+  hAllPosition->Write(0,kOverwrite) ;
+  hAllPositionX->Write(0,kOverwrite) ;
+  hAllPositionZ->Write(0,kOverwrite) ;
+  hPhotPosition->Write(0,kOverwrite) ;
+  hEMPosition->Write(0,kOverwrite) ;
+  pfile->Write() ;
+  pfile->Close() ;
+  delete pfile ;
+
+
+}
+//____________________________________________________________________________
+void AliPHOSAnalyze::Contamination(){
+// fills spectra of primary photons and several kinds of 
+// reconstructed particles, so that analyzing them one can 
+// estimate conatmination, efficiency of registration etc.
+
+  //define several general histograms
+  TH1F * hPrimary = 0;   //spectrum (P_t distribution) of primary photons         
+  TH1F * hAllRP   = 0;   //spectrum of all RecParticles in PHOS
+  TH1F * hPhot    = 0;   //spectrum of kGAMMA RecParticles
+  TH1F * hShape   = 0;   //spectrum of all EM RecParticles
+  TH1F * hVeto    = 0;   //spectrum of all neutral RecParticles
+
+  //Now separate histograms in accoradance with primary
+  //primary - photon
+  TH1F * hPhotReg = 0;   //Registeres as photon
+  TH1F * hPhotEM  = 0;   //Registered as EM       
+
+  //primary - n
+  TH1F * hNReg = 0;   //Registeres as photon          
+  TH1F * hNEM  = 0;   //Registered as EM            
+
+  //primary - nBar
+  TH1F * hNBarReg = 0;   //Registeres as photon
+  TH1F * hNBarEM  = 0;   //Registered as EM          
+
+  //primary - charged hadron (pBar excluded)
+  TH1F * hChargedReg = 0;   //Registeres as photon  
+  TH1F * hChargedEM  = 0;   //Registered as EM           
+
+  //primary - pBar
+  TH1F * hPbarReg = 0;   //Registeres as photon  
+  TH1F * hPbarEM  = 0;   //Registered as EM 
+
+
+  //Reading histograms from the file
+  TFile * cfile = new TFile("contamination.root","update") ;
+
+  //read general histograms
+  hPrimary = (TH1F*) cfile->Get("hPrimary") ;
+  if(hPrimary == 0)
+    hPrimary= new TH1F("hPrimary", "Primary photon spectrum", 100, 0., 5.);
+  hAllRP = (TH1F*)cfile->Get("hAllRP") ;
+  if(hAllRP == 0)
+    hAllRP = new TH1F("hAllRP","All Reconstructed particles", 100, 0., 5.);
+  hPhot  = (TH1F*)cfile->Get("hPhot") ;
+  if(hPhot == 0)
+    hPhot = new TH1F("hPhot","All kGAMMA RecParticles",100, 0., 5.);
+  hShape = (TH1F*) cfile->Get("hShape") ;
+  if(hShape == 0)
+    hShape = new TH1F("hShape","All particles with EM shower",100, 0., 5.);
+  hVeto= (TH1F*)cfile->Get("hVeto") ;
+  if(hVeto == 0) 
+    hVeto  = new TH1F("hVeto", "All uncharged particles",      100, 0., 5.);
+
+
+  //primary - photon
+  hPhotReg = (TH1F*)cfile->Get("hPhotReg");
+  if(hPhotReg == 0)
+    hPhotReg   = new TH1F("hPhotReg","Photon registered as photon",100, 0., 5.);
+  hPhotEM  =(TH1F*)cfile->Get("hPhotEM");
+  if(hPhotEM== 0)
+    hPhotEM   = new TH1F("hPhotEM",  "Photon registered as EM", 100, 0., 5.);
+
+  //primary - n
+  hNReg = (TH1F*)cfile->Get("hNReg");
+  if(hNReg== 0)
+   hNReg      = new TH1F("hNReg",   "N registered as photon",              100, 0., 5.);
+  hNEM  = (TH1F*)cfile->Get("hNEM"); 
+  if(hNEM== 0)
+    hNEM      = new TH1F("hNEM",    "N registered as EM",      100, 0., 5.);
+
+  //primary - nBar
+  hNBarReg =(TH1F*)cfile->Get("hNBarReg");
+  if(hNBarReg== 0)
+   hNBarReg   = new TH1F("hNBarReg", "NBar registered as photon",           100, 0., 5.);
+  hNBarEM  =(TH1F*)cfile->Get("hNBarEM"); 
+  if(hNBarEM== 0)
+    hNBarEM   = new TH1F("hNBarEM",  "NBar registered as EM",   100, 0., 5.);
+
+  //primary - charged hadron (pBar excluded)
+  hChargedReg = (TH1F*)cfile->Get("hChargedReg");
+  if(hChargedReg== 0)
+    hChargedReg= new TH1F("hChargedReg", "Charged hadron registered as photon",100, 0., 5.);
+  hChargedEM  = (TH1F*)cfile->Get("hChargedEM"); 
+  if(hChargedEM== 0)
+    hChargedEM= new TH1F("hChargedEM","Charged registered as EM",100, 0., 5.);
  
-      etot = 0.; 
-      TIter nextPpsd(ppsdRP) ;
-      AliPHOSPpsdRecPoint * ppsd ;
-      while((ppsd = (AliPHOSPpsdRecPoint *)nextPpsd())) 
-       {
-         totalnClusters++ ;
-         if ( ppsd->GetPHOSMod() == module )
-           { 
-             nClusters++ ; 
-             energy = ppsd->GetEnergy() ;   
-             etot+=energy ;  
-             if (!ppsd->GetUp()) ppsd->Draw("P") ;
-           }
-       }
-      cout << "DrawRecPoints > Found " << totalnClusters << " Ppsd Down Clusters in PHOS" << endl ; 
-      cout << "DrawRecPoints > Found in module " << module << "  " << nClusters << " Ppsd Down Clusters " << endl ;
-      cout << "DrawRecPoints > total energy  " << etot << endl ; 
-
-      //=========== Cluster in module PPSD Up
+  //primary - pBar
+  hPbarReg = (TH1F*)cfile->Get("hPbarReg");
+  if(hPbarReg== 0)
+    hPbarReg= new TH1F("hPbarReg", "pBar registered as photon",100, 0., 5.);
+  hPbarEM  = (TH1F*)cfile->Get("hPbarEM");
+  if(hPbarEM== 0)
+    hPbarEM= new TH1F("hPbarEM","Pbar registered as EM",100, 0., 5.);
   
-      ppsdRP = *(fPHOS->PpsdRecPoints()) ;
-     
-      etot = 0.; 
-      TIter nextPpsdUp(ppsdRP) ;
-      while((ppsd = (AliPHOSPpsdRecPoint *)nextPpsdUp())) 
-       {
-         totalnClusters++ ;
-         if ( ppsd->GetPHOSMod() == module )
-           { 
-             nClusters++ ; 
-             energy = ppsd->GetEnergy() ;   
-             etot+=energy ;  
-             if (ppsd->GetUp()) ppsd->Draw("P") ;
-           }
-       }
-  cout << "DrawRecPoints > Found " << totalnClusters << " Ppsd Up Clusters in PHOS" << endl ; 
-  cout << "DrawRecPoints > Found in module " << module << "  " << nClusters << " Ppsd Up Clusters " << endl ;
-  cout << "DrawRecPoints > total energy  " << etot << endl ; 
+
+  //Now make some initializations
+
+  Int_t counter[8][5] ;      //# of registered particles 
+  Int_t i1,i2 ;
+  for(i1 = 0; i1<8; i1++)
+    for(i2 = 0; i2<5; i2++)
+      counter[i1][i2] = 0 ;
+
+
+
+  if (fRunLoader == 0x0)
+   {
+     AliError(Form("Error Loading session"));
+     return;
+   }
+  
+  AliPHOSLoader* gime = dynamic_cast<AliPHOSLoader*>(fRunLoader->GetLoader("PHOSLoader"));
+  if ( gime == 0 ) 
+   {
+     AliError(Form("Could not obtain the Loader object !")); 
+     return ;
+   } 
+  
+  if (fRunLoader->TreeE() == 0x0) fRunLoader->LoadHeader();
+  AliPHOSGeometry * phosgeom =  AliPHOSGeometry::GetInstance() ;
+  
+  Int_t ievent;
+  Int_t maxevent = (Int_t)fRunLoader->TreeE()->GetEntries() ; 
+  for ( ievent=0; ievent < maxevent ; ievent++){
     
-    } // if !-999
-}
+    fRunLoader->GetEvent(ievent) ;
+    
+    //DP:Extract vertex position
+    Double_t vtx[3]={0.,0.,0.} ;
 
-//____________________________________________________________________________
-void AliPHOSAnalyze::DisplayTrackSegments()
-{
-  // Display track segments in local PHOS-module (x, z) coordinates. 
-  // One PHOS module at the time.
-  // One symbol per PHOS subsystem: EMC, upper PPSD, lower PPSD.
-
-  if (fEvt == -999) {
-    cout << "DisplayTrackSegments > Analyze an event first ... (y/n) " ; 
-    Text_t answer[1] ; 
-    cin >> answer ; cout << answer ; 
-//     if ( answer == "y" ) 
-//       AnalyzeOneEvent() ;
-  } 
-    if (fEvt != -999) {
-
-      Int_t module ; 
-      cout <<  "DisplayTrackSegments > which module (1-5,  -1: all) ? " ; 
-      cin >> module ; cout << module << endl ; 
-      //=========== Creating 2d-histogram of the PHOS module
-      // a little bit junkie but is used to test Geom functinalities
+    TClonesArray * rp = gime->RecParticles() ;
+    if(!rp) {
+      AliError(Form("Event %d,  Can't find RecParticles", ievent)) ;
+      return ;
+    }
+    TClonesArray * ts = gime->TrackSegments() ;
+    if(!ts) {
+      AliError(Form("Event %d,  Can't find TrackSegments", ievent)) ;
+      return ;
+    }
+    TObjArray * emcrp = gime->EmcRecPoints() ;
+    if(!emcrp){
+      AliError(Form("Event %d,  Can't find EmcRecPoints", ievent)) ;
+      return ;
+    }
+    
+    
+    //=========== Make spectrum of the primary photons
+    const TParticle * primary ;
+    Int_t iPrimary ;
+    for( iPrimary = 0 ; iPrimary < fRunLoader->Stack()->GetNprimary() ; iPrimary++){
+      primary = fRunLoader->Stack()->Particle(iPrimary) ;
+      Int_t primaryType = primary->GetPdgCode() ;
+      if( primaryType == 22 ) {
+       //check, if photons folls onto PHOS
+       Int_t moduleNumber ;
+       Double_t primX, primZ ;
+       phosgeom->ImpactOnEmc(vtx,primary->Theta(), primary->Phi(), moduleNumber, primX, primZ) ;
+       if(moduleNumber)
+         hPrimary->Fill(primary->Energy()) ;
+       
+      }
       
-      Double_t tm, tM, pm, pM ; // min and Max theta and phi covered by module   
+    }
+    
+    //========== Now scan over RecParticles            
+    const AliPHOSRecParticle * recParticle ;
+    Int_t iRecParticle ;
+    for(iRecParticle = 0; iRecParticle < rp->GetEntriesFast(); iRecParticle++ ){
+      recParticle = (AliPHOSRecParticle *) rp->At(iRecParticle) ;
+      //fill histo spectrum of all RecParticles
+      hAllRP->Fill(CorrectedEnergy(recParticle->Energy())) ;
       
-      fGeom->EmcModuleCoverage(module, tm, tM, pm, pM); 
-      // convert angles into coordinates local to the EMC module of interest
+      //==========find the closest primary       
+      Int_t moduleNumberRec ;
+      Double_t recX, recZ ;
+      phosgeom->ImpactOnEmc(vtx,recParticle->Theta(), recParticle->Phi(), moduleNumberRec, recX, recZ) ;
       
-      Int_t emcModuleNumber ;
-      Double_t emcModulexm, emcModulezm ; // minimum local coordinate in a given EMCA module
-      Double_t emcModulexM, emcModulezM ; // maximum local coordinate in a given EMCA module
-      fGeom->ImpactOnEmc(tm, pm, emcModuleNumber, emcModulezm, emcModulexm) ;
-      fGeom->ImpactOnEmc(tM, pM, emcModuleNumber, emcModulezM, emcModulexM) ;
-      Int_t xdim = (Int_t)( ( emcModulexM - emcModulexm ) / fGeom->GetCrystalSize(0) ) ;  
-      Int_t zdim = (Int_t)( ( emcModulezM - emcModulezm ) / fGeom->GetCrystalSize(2) ) ;
-      Float_t xmin = emcModulexm - fGeom->GetCrystalSize(0) ; 
-      Float_t xMax = emcModulexM + fGeom->GetCrystalSize(0) ; 
-      Float_t zmin = emcModulezm - fGeom->GetCrystalSize(2) ; 
-      Float_t zMax = emcModulezM + fGeom->GetCrystalSize(2) ;     
-      Text_t histoname[80];
-      sprintf(histoname,"Event %d: Track Segments in module %d", fEvt, module) ; 
-      TH2F * histotrack = new TH2F("histotrack",  histoname, 
-                                  xdim, xmin, xMax, zdim, zmin, zMax) ;  
-      histotrack->SetStats(kFALSE); 
-      Text_t canvasname[80];
-      sprintf(canvasname,"Track segments in PHOS/EMC-PPSD module # %d", module) ;
-      TCanvas * trackcanvas = new TCanvas("TrackSegmentCanvas", canvasname, 650, 500) ; 
-      histotrack->Draw() ; 
-
-      AliPHOSTrackSegment::TrackSegmentsList * trsegl = *(fPHOS->TrackSegments()) ;
-      AliPHOSTrackSegment * trseg ;
-      Int_t nTrackSegments = trsegl->GetEntries() ;
+      Double_t minDistance  = 100. ;
+      Int_t closestPrimary = -1 ;
+      
+      //extract list of primaries: it is stored at EMC RecPoints
+      Int_t emcIndex = ((AliPHOSTrackSegment *) ts->At(recParticle->GetPHOSTSIndex()))->GetEmcIndex() ;
+      Int_t numberofprimaries ;
+      Int_t * listofprimaries  = ((AliPHOSEmcRecPoint *) emcrp->At(emcIndex))->GetPrimaries(numberofprimaries)  ;
       Int_t index ;
-      Float_t etot = 0 ;
-      Int_t nTrackSegmentsInModule = 0 ; 
-      for(index = 0; index < nTrackSegments ; index++){
-       trseg = (AliPHOSTrackSegment * )trsegl->At(index) ;
-       etot+= trseg->GetEnergy() ;
-       if ( trseg->GetPHOSMod() == module ) { 
-         nTrackSegmentsInModule++ ; 
-         trseg->Draw("P");
-       }
-      } 
-      Text_t text[80] ; 
-      sprintf(text, "track segments: %d", nTrackSegmentsInModule) ;
-      TPaveText *  pavetext = new TPaveText(22, 80, 83, 90); 
-      pavetext->AddText(text) ; 
-      pavetext->Draw() ; 
-      trackcanvas->Update() ; 
-      cout << "DisplayTrackSegments > Found " << trsegl->GetEntries() << " Track segments with total energy "<< etot << endl ;
-    
-   }
-}
-//____________________________________________________________________________
-Bool_t AliPHOSAnalyze::OpenRootFile(Text_t * name)
-{
-  // Open the root file named "name"
+      Double_t distance = minDistance ;
+      Double_t dX, dZ; 
+      Double_t dXmin = 0.; 
+      Double_t dZmin = 0. ;
+      for ( index = 0 ; index < numberofprimaries ; index++){
+       primary = fRunLoader->Stack()->Particle(listofprimaries[index]) ;
+       Int_t moduleNumber ;
+       Double_t primX, primZ ;
+       phosgeom->ImpactOnEmc(vtx,primary->Theta(), primary->Phi(), moduleNumber, primX, primZ) ;
+       if(moduleNumberRec == moduleNumber) {
+         dX = recX - primX;
+         dZ = recZ - primZ;
+         distance = TMath::Sqrt(dX*dX + dZ*dZ) ;
+         if(minDistance > distance) {
+           minDistance = distance ;
+           dXmin = dX;
+           dZmin = dZ;
+           closestPrimary = listofprimaries[index] ;
+         }
+       }
+      }
+      
+      //===========define the "type" of closest primary
+      if(closestPrimary >=0 ){
+       Int_t primaryCode = -1;
+       primary = fRunLoader->Stack()->Particle(closestPrimary) ;
+       Int_t primaryType = primary->GetPdgCode() ;
+       if(primaryType == 22) // photon ?
+         primaryCode = 0 ;
+       else
+         if(primaryType == 2112) // neutron
+           primaryCode = 1 ; 
+         else
+           if(primaryType == -2112) // Anti neutron
+             primaryCode = 2 ;
+           else
+             if(primaryType == -2122) //Anti proton
+              primaryCode = 4 ;
+             else {
+              TParticle tempo(*primary) ; 
+              if(tempo.GetPDG()->Charge())
+                primaryCode = 3 ;
+             }
+
+       //==========Now look at the type of RecParticle
+       Float_t energy = CorrectedEnergy(recParticle->Energy()) ;
+       if(recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST){
+         hPhot->Fill(energy ) ;        
+         switch(primaryCode){
+         case 0:
+           hPhotReg->Fill(energy ) ; 
+           break ;
+         case 1:
+           hNReg->Fill(energy ) ; 
+           break ;
+         case 2:
+           hNBarReg->Fill(energy ) ; 
+           break ;
+         case 3:
+           hChargedReg->Fill(energy ) ;
+           break ;
+         case 4:
+           hPbarReg->Fill(energy ) ;
+           break ;
+         default:
+           break ;
+         }
+       }
+       if((recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST)||
+          (recParticle->GetType() == AliPHOSFastRecParticle::kCHARGEDEMFAST)||
+          (recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMSLOW)||
+          (recParticle->GetType() == AliPHOSFastRecParticle::kCHARGEDEMSLOW) ){ //with EM shower
+         hShape->Fill(energy ) ;
+         switch(primaryCode){
+         case 0:
+           hPhotEM->Fill(energy ) ; 
+           break ;
+         case 1:
+           hNEM->Fill(energy ) ; 
+           break ;
+         case 2:
+           hNBarEM->Fill(energy ) ; 
+           break ;
+         case 3:
+           hChargedEM->Fill(energy ) ; 
+           break ;
+         case 4:
+           hPbarEM->Fill(energy ) ; 
+           break ;
+         default:
+           break ;
+         }
+       }
+       
+       if((recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMFAST)||
+          (recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALHAFAST) ||
+          (recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALEMSLOW) ||
+          (recParticle->GetType() == AliPHOSFastRecParticle::kNEUTRALHASLOW) ) //nuetral
+         hVeto->Fill(energy ) ;
+       
+       //fill number of primaries identified as ...
+       if(primaryCode >= 0) // Primary code defined
+         counter[recParticle->GetType()][primaryCode]++ ; 
+       
+      }
+      
+    } // no closest primary found
+  }     
   
-  fRootFile   = new TFile(name, "update") ;
-  return  fRootFile->IsOpen() ; 
-}
-//____________________________________________________________________________
-// void AliPHOSAnalyze::SavingHistograms()
-// {
-//   // Saves the histograms in a root file named "name.analyzed" 
-
-//   Text_t outputname[80] ;
-//   sprintf(outputname,"%s.analyzed",fRootFile->GetName());
-//   TFile output(outputname,"RECREATE");
-//   output.cd();
-//   if (fhEmcDigit )         
-//     fhEmcDigit->Write()  ;
-//   if (fhVetoDigit )  
-//     fhVetoDigit->Write()  ;
-//   if (fhConvertorDigit ) 
-//     fhConvertorDigit->Write()   ;
-//   if (fhEmcCluster   )
-//     fhEmcCluster->Write()   ;
-//   if (fhVetoCluster ) 
-//     fhVetoCluster->Write()   ;
-//   if (fhConvertorCluster )
-//     fhConvertorCluster->Write()  ;
-//   if (fhConvertorEmc ) 
-//     fhConvertorEmc->Write()  ;
-//   if (fhPhotonEnergy)    
-//     fhPhotonEnergy->Write() ;
-//   if (fhPhotonPositionX)  
-//     fhPhotonPositionX->Write() ;
-//   if (fhPhotonPositionY)  
-//     fhPhotonPositionX->Write() ;
-//   if (fhElectronEnergy)  
-//     fhElectronEnergy->Write() ;
-//   if (fhElectronPositionX)
-//     fhElectronPositionX->Write() ;
-//   if (fhElectronPositionY) 
-//     fhElectronPositionX->Write() ;
-//   if (fhNeutralHadronEnergy) 
-//     fhNeutralHadronEnergy->Write() ;
-//   if (fhNeutralHadronPositionX)
-//     fhNeutralHadronPositionX->Write() ;
-//   if (fhNeutralHadronPositionY) 
-//     fhNeutralHadronPositionX->Write() ;
-//   if (fhNeutralEMEnergy)   
-//     fhNeutralEMEnergy->Write() ;
-//   if (fhNeutralEMPositionX)
-//     fhNeutralEMPositionX->Write() ;
-//   if (fhNeutralEMPositionY) 
-//     fhNeutralEMPositionX->Write() ;
-//   if (fhChargedHadronEnergy) 
-//     fhChargedHadronEnergy->Write() ;
-//   if (fhChargedHadronPositionX) 
-//     fhChargedHadronPositionX->Write() ;
-//   if (fhChargedHadronPositionY)
-//     fhChargedHadronPositionX->Write() ;
-//   if (fhPhotonHadronEnergy) 
-//     fhPhotonHadronEnergy->Write() ;
-//   if (fhPhotonHadronPositionX) 
-//     fhPhotonHadronPositionX->Write() ;
-//   if (fhPhotonHadronPositionY)
-//     fhPhotonHadronPositionX->Write() ;
-
-//   output.Write();
-//   output.Close();
-// }
-//____________________________________________________________________________
-void AliPHOSAnalyze::SaveHistograms()
-{
-  // Saves the histograms in a root file named "name.analyzed" 
-
-  Text_t outputname[80] ;
-  sprintf(outputname,"%s.analyzed",fRootFile->GetName());
-  TFile output(outputname,"RECREATE");
-  output.cd();
-
-  if (fhPhotonEnergy)    
-    fhPhotonEnergy->Write() ;
-  if (fhPhotonAllEnergy)    
-    fhPhotonAllEnergy->Write() ;
-  if (fhPhotonPosition)  
-    fhPhotonPosition->Write() ;
-  if (fhPhotonAllPosition)  
-    fhPhotonAllPosition->Write() ;
-  if (fhElectronEnergy)  
-    fhElectronEnergy->Write() ;
-  if (fhElectronAllEnergy)  
-    fhElectronAllEnergy->Write() ;
-  if (fhElectronPosition)
-    fhElectronPosition->Write() ;
-  if (fhElectronAllPosition)
-    fhElectronAllPosition->Write() ;
-  if (fhNeutralHadronEnergy) 
-    fhNeutralHadronEnergy->Write() ;
-  if (fhNeutralHadronPosition)
-    fhNeutralHadronPosition->Write() ;
-  if (fhNeutralEMEnergy)   
-    fhNeutralEMEnergy->Write() ;
-  if (fhNeutralEMAllEnergy)   
-    fhNeutralEMAllEnergy->Write() ;
-  if (fhNeutralEMPosition)
-    fhNeutralEMPosition->Write() ;
-  if (fhNeutralEMAllPosition)
-    fhNeutralEMAllPosition->Write() ;
-  if (fhChargedHadronEnergy) 
-    fhChargedHadronEnergy->Write() ;
-  if (fhChargedHadronPosition) 
-    fhChargedHadronPosition->Write() ;
-  if (fhPhotonHadronEnergy) 
-    fhPhotonHadronEnergy->Write() ;
-  if (fhPhotonHadronPosition) 
-    fhPhotonHadronPosition->Write() ;
-  if (fhPhotonReg) 
-    fhPhotonReg->Write() ;
-  if (fhAllReg) 
-    fhAllReg->Write() ;
-  if(fhNReg)
-    fhNReg->Write() ;
-  if(fhNBarReg)
-    fhNBarReg->Write() ;
-  if(fhChargedReg)
-    fhChargedReg->Write() ;
-  if (fhPhotonEM) 
-    fhPhotonEM->Write() ;
-  if (fhAllEM) 
-    fhAllEM->Write() ;
-  if(fhNEM)
-    fhNEM->Write() ;
-  if(fhNBarEM)
-    fhNBarEM->Write() ;
-  if(fhChargedEM)
-    fhChargedEM->Write() ;
-  if(fhPrimary)
-    fhPrimary->Write() ;
-  if(fhPhotPhot)
-    fhPhotPhot->Write() ;
-  if(fhPhotElec)
-    fhPhotElec->Write() ;
-  if(fhPhotNeuH)
-    fhPhotNeuH->Write() ;
-  if(fhPhotNuEM)
-    fhPhotNuEM->Write() ;
-  if(fhPhotNuEM)
-    fhPhotNuEM->Write() ;
-  if(fhPhotChHa)
-    fhPhotChHa->Write() ;
-  if(fhPhotGaHa)
-    fhPhotGaHa->Write() ;
-  if(fhEnergyCorrelations)
-    fhEnergyCorrelations->Write() ;
   
-  output.Write();
-  output.Close();
-}
-//____________________________________________________________________________
-void AliPHOSAnalyze::ResetHistograms()
-{
-   fhEnergyCorrelations = 0 ;     //Energy correlations between Eloss in Convertor and PPSD(2)
-
-   fhEmcDigit = 0 ;               // Histo of digit energies in the Emc 
-   fhVetoDigit = 0 ;              // Histo of digit energies in the Veto 
-   fhConvertorDigit = 0 ;         // Histo of digit energies in the Convertor
-   fhEmcCluster = 0 ;             // Histo of Cluster energies in Emc
-   fhVetoCluster = 0 ;            // Histo of Cluster energies in Veto
-   fhConvertorCluster = 0 ;       // Histo of Cluster energies in Convertor
-   fhConvertorEmc = 0 ;           // 2d Convertor versus Emc energies
-
-   fhPhotonEnergy = 0 ;           // Spectrum of detected photons with photon primary
-   fhPhotonAllEnergy = 0 ;        // Total spectrum of detected photons
-   fhElectronEnergy = 0 ;         // Spectrum of detected electrons with electron primary
-   fhElectronAllEnergy = 0 ;      // Total spectrum of detected electrons
-   fhNeutralHadronEnergy = 0 ;    // Spectrum of detected neutral hadron
-   fhNeutralEMEnergy = 0 ;        // Spectrum of detected neutral EM with EM primary
-   fhNeutralEMAllEnergy = 0 ;     // Spectrum of detected neutral EM
-   fhChargedHadronEnergy = 0 ;    // Spectrum of detected charged
-   fhPhotonHadronEnergy = 0 ;     // Spectrum of detected Photon-Hadron
-   fhPhotonPosition = 0 ;        // Position Resolution of  photons with photon primary
-   fhPhotonAllPosition = 0 ;     // Position Resolution of  photons
-   fhElectronPosition = 0 ;      // Position Resolution of electrons with electron primary
-   fhElectronAllPosition = 0 ;   // Position Resolution of electrons
-   fhNeutralHadronPosition = 0 ; // Position Resolution of neutral hadron
-   fhNeutralEMPosition = 0 ;     // Position Resolution of neutral EM with EM primary
-   fhNeutralEMAllPosition = 0 ;  // Position Resolution of neutral EM
-   fhChargedHadronPosition = 0 ; // Position Resolution of charged
-   fhPhotonHadronPosition = 0 ;  // Position Resolution of Photon-Hadron
-   fhPhotonPositionY = 0 ;        // Y distribution of detected photons
-   fhElectronPositionY = 0 ;      // Y distribution of detected electrons
-   fhNeutralHadronPositionY = 0 ; // Y distribution of detected neutral hadron
-   fhNeutralEMPositionY = 0 ;     // Y distribution of detected neutral EM
-   fhChargedHadronPositionY = 0 ; // Y distribution of detected charged
-   fhPhotonHadronPositionY = 0 ;  // Y distribution of detected Photon-Hadron
-   fhPhotonReg = 0 ;          
-   fhAllReg = 0 ;          
-   fhNReg = 0 ;          
-   fhNBarReg = 0 ;          
-   fhChargedReg = 0 ;          
-   fhPhotonEM = 0 ;          
-   fhAllEM = 0 ;          
-   fhNEM = 0 ;          
-   fhNBarEM = 0 ;          
-   fhChargedEM = 0 ;          
-   fhPrimary = 0 ;          
-
-   fhPhotPhot = 0 ;
-   fhPhotElec = 0 ;
-   fhPhotNeuH = 0 ;
-   fhPhotNuEM = 0 ; 
-   fhPhotChHa = 0 ;
-   fhPhotGaHa = 0 ;
-
+  //===================  SaveHistograms
+  cfile->cd() ;
+  hPrimary->Write(0,kOverwrite); 
+  hAllRP->Write(0,kOverwrite);  
+  hPhot->Write(0,kOverwrite);  
+  hShape->Write(0,kOverwrite); 
+  hVeto->Write(0,kOverwrite);  
+  hPhotReg->Write(0,kOverwrite); 
+  hPhotEM->Write(0,kOverwrite);   
+  hNReg ->Write(0,kOverwrite);  
+  hNEM  ->Write(0,kOverwrite); 
+  hNBarReg ->Write(0,kOverwrite); 
+  hNBarEM  ->Write(0,kOverwrite); 
+  hChargedReg ->Write(0,kOverwrite); 
+  hChargedEM  ->Write(0,kOverwrite); 
+  hPbarReg ->Write(0,kOverwrite); 
+  hPbarEM  ->Write(0,kOverwrite); 
+  
+  cfile->Write(0,kOverwrite); 
+  cfile->Close();
+  delete cfile ;
+  
+  //print Final Table
+  maxevent = (Int_t)AliRunLoader::Instance()->TreeE()->GetEntries() ; 
 
-}
+  TString message ; 
+  message  = "Resolutions: Analyzed %d event(s)\n" ; 
+  message += "        Primary:    Photon  Neutron  Antineutron  Charged hadron   AntiProton\n" ; 
+  message += "--------------------------------------------------------------------------------\n" ;
+  message += "         kGAMMA: " ; 
+  message += "%d %d %d %d %d\n" ; 
+  message += "       kGAMMAHA: " ;
+  message += "%d %d %d %d %d\n" ; 
+  message += "     kNEUTRALEM: " ; 
+  message += "%d %d %d %d %d\n" ; 
+  message += "     kNEUTRALHA: " ; 
+  message += "%d %d %d %d %d\n" ;
+  message += "      kABSURDEM: ";
+  message += "%d %d %d %d %d\n" ;
+  message += "      kABSURDHA: " ;
+  message += "%d %d %d %d %d\n" ;
+  message += "      kELECTRON: " ;
+  message += "%d %d %d %d %d\n" ;
+  message += "     kCHARGEDHA: " ;  
+  message += "%d %d %d %d %d\n" ;
+   
+  message += "--------------------------------------------------------------------------------" ;
 
+  Int_t totalInd = 0 ;
+  for(i1 = 0; i1<8; i1++)
+    for(i2 = 0; i2<5; i2++)
+      totalInd+=counter[i1][i2] ;
+  message += "Indentified particles: %d" ; 
+  
+ AliInfo(Form(message.Data(), maxevent, 
+      counter[2][0], counter[2][1], counter[2][2], counter[2][3], counter[2][4], 
+      counter[3][0], counter[3][1], counter[3][2], counter[3][3], counter[3][4], 
+      counter[0][0], counter[0][1], counter[0][2], counter[0][3], counter[0][4], 
+      counter[1][0], counter[1][1], counter[1][2], counter[1][3], counter[1][4], 
+      counter[4][0], counter[4][1], counter[4][2], counter[4][3], counter[4][4], 
+      counter[5][0], counter[5][1], counter[5][2], counter[5][3], counter[5][4], 
+      counter[6][0], counter[6][1], counter[6][2], counter[6][3], counter[6][4], 
+      counter[7][0], counter[7][1], counter[7][2], counter[7][3], counter[7][4], 
+      totalInd )) ;
 
+}