#if !defined( __CINT__) || defined(__MAKECINT__)
#include <TMath.h>
#include <Riostream.h>
#include <TH1F.h>
#include <TH2F.h>
#include <TParticle.h>
#include <TCanvas.h>
#include <TBenchmark.h>
#include <TFile.h>
#include <TTree.h>
#include <TROOT.h>
#include <TRandom.h>
#include "AliTOFdigit.h"
#include "AliStack.h"
#include "AliRunLoader.h"
#include "AliRun.h"
#include "AliESD.h"
#include "AliESDtrack.h"
#include <TAxis.h>
#include <TObject.h>
#include <TObjArray.h>
#endif

  class UnCalib:public AliESDtrack{
  public:
    UnCalib(){}
    //UnCalib(AliESDtrack *track){
    ~UnCalib(){}
    Float_t ToT() const{return fToT;}
    Float_t UCValue() const{return fUCvalue;}
    Float_t CValue() const{return fCvalue;}
    void SetAll(Float_t ToT, Float_t UCvalue, Float_t Cvalue){
      fToT = ToT;
      fUCvalue = UCvalue;
      fCvalue = Cvalue;
    }
  private:
    Float_t fToT;
    Float_t fUCvalue;
    Float_t fCvalue;
    ClassDef(UnCalib,1);
  };

#ifdef __MAKECINT__
#pragma link C++ UnCalib+;
#endif

void UnCalib2()
{
  TObjArray UCdata(10);
  Char_t fname[100], dir[100];
  Int_t numberOfEvents=0;


   if (gAlice) {
      delete gAlice->GetRunLoader();
      delete gAlice;
      gAlice=0;
   }

   sprintf(fname,"galice.root");
   AliRunLoader *rl = AliRunLoader::Open(fname);

   if (rl == 0x0) {
      cerr<<"Can not open session"<<endl;
      return 1;
   }
   if (rl->LoadgAlice()) {
      cerr<<"LoadgAlice returned error"<<endl;
      delete rl;
      return 1;
   }
   if (rl->LoadHeader()) {
      cerr<<"LoadHeader returned error"<<endl;
      delete rl;
      return 1;
   }
   
   rl->LoadKinematics();
   rl->LoadHeader();

   AliLoader *tofl = rl->GetLoader("TOFLoader");
   if (tofl == 0x0) {
      cerr<<"Can not get the TOF Loader \n";
      delete rl;
      return 2;
   }

   gAlice=rl->GetAliRun();
   if (!gAlice) {
      cerr<<"Can't get gAlice !\n";
      delete rl;
      return 1;
   }
   
   if (numberOfEvents == 0) numberOfEvents = (Int_t)(rl->GetNumberOfEvents());
   cout << " number of events = " << numberOfEvents << endl;
   
   Char_t ESDfname[100];
   sprintf(ESDfname,"AliESDs.root");
   TFile *ef=TFile::Open(ESDfname);
   if (!ef || !ef->IsOpen()) {
     ::Error("UnCalib.C","Can't Open AliESDs.root !"); 
     return 1;
   }
   
   AliESD* event = new AliESD;
   TTree* tree = (TTree*) ef->Get("esdTree");
   if (!tree) {
     ::Error("Spectrum.C", "no ESD tree found");
     return 1;
   } 
   AliStack *stack = rl->Stack();
   tree->SetBranchAddress("ESD", &event);
   
   for (Int_t ievent = 0; ievent < numberOfEvents; ievent++) {
     cout<<endl<<endl<<"******* Processing event number: "<<ievent<<"*****\n";
     tree->GetEvent(ievent);
     rl->GetEvent(ievent);
     Int_t ntrk=event->GetNumberOfTracks();
     cout << " ntrk = " << ntrk << endl;
     Int_t ntracks = 0;
     for (Int_t itrk=0; itrk<10; itrk++) {
       AliESDtrack *t=event->GetTrack(itrk);
       UnCalib *ciccio = (UnCalib*) t ;

       UCdata[itrk] = new UnCalib(*ciccio);
       //UCdata[itrk] = ciccio;       
       Float_t tdc = t->GetTOFsignal();
       Float_t tot = tdc+3.;
       Float_t cal = tdc-3.;
       UnCalib *ciccino = (UnCalib*)UCdata.At(itrk);
       //       UCdata[itrk]->SetAll(tdc,tot,cal);
       ciccino->SetAll(tdc,tot,cal);
       //cout << "  tdc " << ciccino->GetTOFsignal() << endl;
     }
   }
   TFile *file = new TFile("UnCalibratedData.root","recreate");
   UCdata.Write();
   delete event;
   rl->UnloadHeader();
   rl->UnloadgAlice();
   
   if (gAlice) 
     {
       delete gAlice->GetRunLoader();
       delete gAlice;
       gAlice = 0x0;
     }
   //delete rl;  to be commented just for the time being
   return 0;
}
       


