]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - JETAN/AliFastJetFinder.cxx
more consistent use of cuts, new cut number 1008
[u/mrichter/AliRoot.git] / JETAN / AliFastJetFinder.cxx
index f2c40c4bd42d24e1fe8d97a5e7d902618bf1ebd5..f15989bffd59ea8ea7adb715a2276608bcf8ac6b 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
  
+
 //---------------------------------------------------------------------
-// FastJet finder
-// interface to FastJet algorithm
-// Author: Rafael.Diaz.Valdes@cern.ch
-// kt using NlnN 
+// FastJet v2.3.4 finder algorithm interface
+// Last modification: Neutral cell energy included in the jet reconstruction
+//
+// Authors: Rafael.Diaz.Valdes@cern.ch
+//          Magali.estienne@subatech.in2p3.fr (neutral part + bg subtraction option)
+//
 //---------------------------------------------------------------------
 
+
 #include <Riostream.h>
 #include <TLorentzVector.h>
 #include <TFile.h>
 #include <TH1F.h>
 #include <TH2F.h>
 #include <TArrayF.h>
-#include <TRandom.h>
 #include <TClonesArray.h>
 
 #include "AliFastJetFinder.h"
-#include "AliFastJetHeader.h"
+#include "AliFastJetHeaderV1.h"
 #include "AliJetReaderHeader.h"
 #include "AliJetReader.h"
-#include "AliJet.h"
-//for FastJet finder
-#include "FjPseudoJet.hh"
-#include "FjClusterSequence.hh"
+#include "AliJetUnitArray.h"
+#include "AliFastJetInput.h"
+#include "AliJetBkg.h"
+#include "AliAODJetEventBackground.h"
+#include "AliAODTrack.h"
+
+#include "fastjet/PseudoJet.hh"
+#include "fastjet/ClusterSequenceArea.hh"
+#include "fastjet/AreaDefinition.hh"
+#include "fastjet/JetDefinition.hh"
+// get info on how fastjet was configured
+#include "fastjet/config.h"
+
+#ifdef ENABLE_PLUGIN_SISCONE
+#include "fastjet/SISConePlugin.hh"
+#endif
 
+#include<sstream>  // needed for internal io
+#include<vector> 
+#include <cmath> 
 
-ClassImp(AliFastJetFinder);
+using namespace std;
 
 
-////////////////////////////////////////////////////////////////////////
+ClassImp(AliFastJetFinder)
+
+
+//____________________________________________________________________________
 
 AliFastJetFinder::AliFastJetFinder():
-    AliJetFinder(),
-    fHeader(0x0),
-    fLego(0x0),
-    fLegoSignal(0x0)
+  AliJetFinder(),
+  fInputFJ(new AliFastJetInput()),
+  fJetBkg(new  AliJetBkg())
 {
   // Constructor
 }
 
-////////////////////////////////////////////////////////////////////////
+//____________________________________________________________________________
 
 AliFastJetFinder::~AliFastJetFinder()
-
 {
   // destructor
+  delete  fInputFJ; fInputFJ = 0;
+  delete  fJetBkg; fJetBkg = 0;
 }
 
-////////////////////////////////////////////////////////////////////////
+//______________________________________________________________________________
+void AliFastJetFinder::FindJets()
+{
 
+  //pick up fastjet header
+  AliFastJetHeaderV1 *header = (AliFastJetHeaderV1*)fHeader;
+  Int_t  debug  = header->GetDebug();     // debug option
+  Bool_t bgMode = header->GetBGMode();    // choose to subtract BG or not
+  if(debug)cout<<"----------in AliFastJetFinder::FindJets() ------------------"<<endl;
 
-void AliFastJetFinder::FindJets()
+  // check if we are reading AOD jets
+  TRefArray *refs = 0;
+  Bool_t fromAod = !strcmp(fReader->ClassName(),"AliJetAODReader");
+  if (fromAod) { refs = fReader->GetReferences(); }
 
-{
-  //create cells and jets array
-  // 1) transform input to pt,eta,phi plus lego
-  TClonesArray *lvArray = fReader->GetMomentumArray();
-  Int_t nIn =  lvArray->GetEntries();
-  if (nIn == 0) return;
-
-  // local arrays for particles input
-  Float_t* enT  = new Float_t[nIn];
-  Float_t* ptT  = new Float_t[nIn];
-  Float_t* etaT = new Float_t[nIn];
-  Float_t* phiT = new Float_t[nIn];
-  Int_t*   injet = new Int_t[nIn];
-
-
-  //total energy in array
-  Float_t  EtTotal = 0.0;
-  Float_t  meanptCell = 0.0;
-  Float_t  sqptCell = 0.0;
-
-
-  // load input vectors in fLego
-  for (Int_t i = 0; i < nIn; i++){
-    TLorentzVector *lv = (TLorentzVector*) lvArray->At(i);
-    enT[i]  = lv->Energy();
-    ptT[i]  = lv->Pt();
-    etaT[i] = lv->Eta();
-    phiT[i] = ((lv->Phi() < 0) ? (lv->Phi()) + 2 * TMath::Pi() : lv->Phi());
-    if (fReader->GetCutFlag(i) == 1){
-      fLego->Fill(etaT[i], phiT[i], ptT[i]);
-      if(fReader->GetSignalFlag(i) == 1)
-        fLegoSignal->Fill(etaT[i], phiT[i], ptT[i]);
-      EtTotal= EtTotal+ptT[i];
-    }
-  }
-  fJets->SetNinput(nIn);
-
-  // add soft background fixed
-  Int_t nsoft = (fHeader->GetLegoNbinEta())*(fHeader->GetLegoNbinPhi());
-  Float_t* ptRndm =  new Float_t[nsoft];
-  if(fHeader->AddSoftBackg()){
-    gRandom->RndmArray(nsoft,ptRndm);
-    for(Int_t isoft = 0; isoft < nsoft; isoft++){
-        Float_t ptsoft  = 0.005*ptRndm[isoft];
-        EtTotal= EtTotal+ptsoft;
-    }
-  }
+  // RUN ALGORITHM  
+  // read input particles -----------------------------
 
-  if(EtTotal == 0){
-     delete enT;
-     delete ptT;
-     delete etaT;
-     delete phiT;
-     return;
+  vector<fastjet::PseudoJet> inputParticles=fInputFJ->GetInputParticles();
+  if(inputParticles.size()==0){
+    if(debug)Printf("%s:%d No input particles found, skipping event",(char*)__FILE__,__LINE__);
+    return;
   }
 
-  //cell array
-  Float_t* etCell = new Float_t[90000];   //! Cell Energy // check enough space! *to be done*
-  Float_t* etaCell = new Float_t[90000];  //! Cell eta
-  Float_t* phiCell = new Float_t[90000];  //! Cell phi
-  Int_t*   jetflagCell = new Int_t[90000]; //! Cell flag for jets
-  Float_t* etsigCell = new Float_t[90000];   // signal in this cell
-
-  //jets array
-  Float_t* etaJet = new Float_t[200];
-  Float_t* phiJet = new Float_t[200];
-  Float_t* etJet  = new Float_t[200];
-  Float_t* etsigJet  = new Float_t[200]; //signal energy
-  Float_t* etallJet = new Float_t[200];  //total energy in jet area
-  Int_t*   ncellsJet = new Int_t[200];
-  memset(etaJet,0,sizeof(Float_t)*200);
-  memset(phiJet,0,sizeof(Float_t)*200);
-  memset(etJet,0,sizeof(Float_t)*200);
-  memset(etsigJet,0,sizeof(Float_t)*200);
-  memset(etallJet,0,sizeof(Float_t)*200);
-  memset(ncellsJet,0,sizeof(Int_t)*200);
-
-
-
-  // load cells arrays
-  Int_t nCell = 0;
-  TAxis* xaxis = fLego->GetXaxis();
-  TAxis* yaxis = fLego->GetYaxis();
-  Float_t e = 0.0; Float_t esig = 0.0;
-
-  for (Int_t ib = 1; ib <= fHeader->GetLegoNbinEta(); ib++) {
-      for (Int_t jb = 1; jb <= fHeader->GetLegoNbinPhi(); jb++) {
-              e = fLego->GetBinContent(ib,jb);
-              if (e < 0.0) continue; // don't include this cells
-              Float_t eta  = xaxis->GetBinCenter(ib);
-              Float_t phi  = yaxis->GetBinCenter(jb);
-          if(fHeader->AddSoftBackg())
-             etCell[nCell]  = e + 0.005*ptRndm[nCell];
-          else
-             etCell[nCell]  = e;
-          sqptCell = sqptCell + etCell[nCell]*etCell[nCell]; // xi^2 ////////
-              etaCell[nCell] = eta;
-              phiCell[nCell] = phi;
-          jetflagCell[nCell] = -1; //default
-          esig = fLegoSignal->GetBinContent(ib,jb);
-          if(esig > 0.0)
-             etsigCell[nCell] = esig;
-          else
-             etsigCell[nCell] = 0.0;
-              nCell++;
+  // create an object that represents your choice of jet algorithm, and 
+  // the associated parameters
+  double rParam = header->GetRparam();
+  double rBkgParam = header->GetRparamBkg();
+  fastjet::Strategy strategy = header->GetStrategy();
+  fastjet::RecombinationScheme recombScheme = header->GetRecombScheme();
+  fastjet::JetAlgorithm algorithm = header->GetAlgorithm(); 
+  fastjet::JetDefinition jetDef(algorithm, rParam, recombScheme, strategy);
+
+  // create an object that specifies how we to define the area
+  fastjet::AreaDefinition areaDef;
+  double ghostEtamax = header->GetGhostEtaMax(); 
+  double ghostArea   = header->GetGhostArea(); 
+  int    activeAreaRepeats = header->GetActiveAreaRepeats(); 
+  
+  // now create the object that holds info about ghosts
+  fastjet::GhostedAreaSpec ghostSpec(ghostEtamax, activeAreaRepeats, ghostArea);
+  // and from that get an area definition
+  fastjet::AreaType areaType = header->GetAreaType();
+  areaDef = fastjet::AreaDefinition(areaType,ghostSpec);
+  
+  //***************************** JETS FINDING
+  // run the jet clustering with the above jet definition
+  fastjet::ClusterSequenceArea clust_seq(inputParticles, jetDef, areaDef);
+
+  vector<fastjet::PseudoJet> jets;
+
+  if(bgMode) // Do BG subtraction directly with the same algorithm (cambridge or kt) for jet signal and background
+    {
+      //***************************** CLUSTER JETS FINDING FOR RHO ESTIMATION
+      // run the jet clustering with the above jet definition
+         fastjet::JetAlgorithm algorithmBkg = header->GetBGAlgorithm();
+         fastjet::JetDefinition jetDefBkg(algorithmBkg, rBkgParam, recombScheme, strategy);
+      fastjet::ClusterSequenceArea clust_seq_bkg(inputParticles, jetDefBkg, areaDef);
+
+      // save a comment in the header
+      TString comment = "Running FastJet algorithm with the following setup. ";
+      comment+= "Jet definition: ";
+      comment+= TString(jetDef.description());
+      comment+= "Jet bckg definition: ";
+      comment+= TString(jetDefBkg.description());
+      comment+= ". Area definition: ";
+      comment+= TString(areaDef.description());
+      comment+= ". Strategy adopted by FastJet and bkg: ";
+      comment+= TString(clust_seq.strategy_string());
+      header->SetComment(comment);
+      if(debug){
+          cout << "--------------------------------------------------------" << endl;
+          cout << comment << endl;
+          cout << "--------------------------------------------------------" << endl;
       }
-  }
+      //header->PrintParameters();
+      
+      // extract the inclusive jets with pt > ptmin, sorted by pt
+      double ptmin = header->GetPtMin(); 
+      vector<fastjet::PseudoJet> inclusiveJets = clust_seq.inclusive_jets();
+
+      //subtract background // ===========================================
+      // set the rapididty , phi range within which to study the background 
+      double rapMax = header->GetRapMax(); 
+      double rapMin = header->GetRapMin();
+      double phiMax = header->GetPhiMax();
+      double phiMin = header->GetPhiMin();
+      fastjet::RangeDefinition range(rapMin, rapMax, phiMin, phiMax);
+      
+      // Extract rho and sigma
+      Double_t rho = 0.;
+      Double_t sigma = 0.;
+      Double_t meanarea = 0.;
+      Bool_t Use4VectorArea = header->Use4VectorArea();
+      vector<fastjet::PseudoJet> bkgJets = clust_seq_bkg.inclusive_jets();
+      clust_seq_bkg.get_median_rho_and_sigma(bkgJets,range, Use4VectorArea, rho, sigma, meanarea, false);
+
+         // subtract background and extract jets bkg subtracted
+      vector<fastjet::PseudoJet> subJets = clust_seq.subtracted_jets(rho,ptmin);
+
+         // print out
+      //cout << "Printing inclusive sub jets with pt > "<< ptmin<<" GeV\n";
+      //cout << "---------------------------------------\n";
+      //cout << endl;
+      //printf(" ijet   rap      phi        Pt         area  +-   err\n");
+      
+      // sort jets into increasing pt
+      jets = sorted_by_pt(subJets);  
+
+    }
+  else { // No BG subtraction!!!!!!!! Default header is bgmode=0.
+
+         // save a comment in the header
+         TString comment = "Running FastJet algorithm with the following setup. ";
+         comment+= "Jet definition: ";
+         comment+= TString(jetDef.description());
+         comment+= ". Strategy adopted by FastJet: ";
+         comment+= TString(clust_seq.strategy_string());
+         header->SetComment(comment);
+         if(debug){
+                 cout << "--------------------------------------------------------" << endl;
+                 cout << comment << endl;
+                 cout << "--------------------------------------------------------" << endl;
+         }
+         //header->PrintParameters();
+
+         // extract the inclusive jets with pt > ptmin, sorted by pt
+         double ptmin = header->GetPtMin();
+         vector<fastjet::PseudoJet> inclusiveJets = clust_seq.inclusive_jets(ptmin);
+
+         jets = sorted_by_pt(inclusiveJets); // Added by me
 
-  meanptCell = EtTotal/(Float_t)nCell;
-  sqptCell = sqptCell/(Float_t)nCell;
-
-  Int_t nJets = 0;
-  //call to FastJet Algorithm
-  RunAlgorithm(nJets,etJet,etaJet,phiJet,etsigJet,etallJet,ncellsJet,
-               nCell,etCell,etaCell,phiCell,etsigCell,jetflagCell);
-
-
-  //subtract background
-  SubtractBackg(nCell,jetflagCell,etCell,
-                nJets,etJet,etallJet,ncellsJet,
-                meanptCell,sqptCell,EtTotal);
-
-
-  // add jets to list
-  Int_t* index = new Int_t[nJets];
-  Int_t nj = 0;
-  for(Int_t kj=0; kj<nJets; kj++){
-      if ((etaJet[kj] > (fHeader->GetJetEtaMax())) ||
-          (etaJet[kj] < (fHeader->GetJetEtaMin())) ||
-          (etJet[kj] < fHeader->GetMinJetEt())) continue; // acceptance eta range and etmin
-      Float_t px, py,pz,en; // convert to 4-vector
-      px = etJet[kj] * TMath::Cos(phiJet[kj]);
-      py = etJet[kj] * TMath::Sin(phiJet[kj]);
-      pz = etJet[kj] / TMath::Tan(2.0 * TMath::ATan(TMath::Exp(-etaJet[kj])));
-      en = TMath::Sqrt(px * px + py * py + pz * pz);
-      fJets->AddJet(px, py, pz, en);
-      index[nj] = kj;
-      nj++;
   }
 
-  //add signal percentage and total signal  in AliJets for analysis tool
-  Float_t* percentage  = new Float_t[nj];
-  Int_t* ncells      = new Int_t[nj];
-  Int_t* mult        = new Int_t[nj];
+  for (size_t j = 0; j < jets.size(); j++) { // loop for jets
+
+         double area      = clust_seq.area(jets[j]);
+         double areaError = clust_seq.area_error(jets[j]);
+
+         if(debug) printf("Jet found %5d %9.5f %8.5f %10.3f %8.3f +- %6.3f\n", (Int_t)j,jets[j].rap(),jets[j].phi(),jets[j].perp(), area, areaError);
+
+      vector<fastjet::PseudoJet> constituents = clust_seq.constituents(jets[j]);
+      int nCon= constituents.size();
+      TArrayI ind(nCon);
+      
+      if ((jets[j].eta() > (header->GetJetEtaMax())) ||
+          (jets[j].eta() < (header->GetJetEtaMin())) ||
+          (jets[j].phi() > (header->GetJetPhiMax())) ||
+          (jets[j].phi() < (header->GetJetPhiMin())) ||
+          (jets[j].perp() < header->GetPtMin())) continue; // acceptance eta range and etmin
+
+         // go to write AOD  info
+         AliAODJet aodjet (jets[j].px(), jets[j].py(), jets[j].pz(), jets[j].E());
+      aodjet.SetEffArea(area,areaError);
+         //cout << "Printing jet " << endl;
+         if(debug) aodjet.Print("");
+
+         Int_t count=0;
+         for (int i=0; i < nCon; i++)
+         {
+                 fastjet::PseudoJet mPart=constituents[i];
+                 ind[i]=mPart.user_index();
+                 //    cout<<i<<"  index="<<ind[i]<<endl;
+
+                 if(fromAod)
+                 {
+                         if(fReader->GetReaderHeader()->GetDetector()==0)
+                         {
+                                 for (Int_t iref = 0; iref < refs->GetEntries(); iref++)
+                                 {
+                                         if(iref==ind[i]){
+                                                 AliAODTrack * track = (AliAODTrack*)refs->At(iref);
+                                                 aodjet.AddTrack(track);
+                                         }
+                                 }
+
+                         } else {
+
+                                 TClonesArray* fUnit = fReader->GetUnitArray(); //Big mmentum array
+                                 if(fUnit == 0) { cout << "Could not get the momentum array" << endl; return; }
+                                 Int_t         nIn = fUnit->GetEntries();
+
+                                 //internal loop over all the unit cells
+                                 Int_t ipart = 0;
+
+                                 for(Int_t ii=0; ii<nIn; ii++)
+                                 {
+                                         AliJetUnitArray *uArray = (AliJetUnitArray*)fUnit->At(ii);
+                                         if(uArray->GetUnitEnergy()>0.){
+                                                 uArray->SetUnitTrackID(ipart);//used to have the index available in AliJEtBkg
+                                                 if(ipart==ind[i]){
+                                                         TRefArray* trackArray = (TRefArray*)uArray->GetUnitTrackRef();
+                                                         Int_t tracksInCell = trackArray->GetEntries();
+                                                         for(int ji = 0; ji < tracksInCell; ji++){
+                                                                 AliAODTrack * track = (AliAODTrack*)trackArray->At(ji);
+                                                                 aodjet.AddTrack(track);
+                                                         }
+
+                                                         count++;
+                                                 }
+                                                 ipart++;
+                                         }
+                                 }
+                         }
+                 }
+        } 
+
+ AddJet(aodjet);
+
+
+  } // End loop on jets
 
-  for(Int_t i = 0; i< nj; i++){
-     percentage[i] = etsigJet[index[i]]/etJet[index[i]];
-     ncells[i] = ncellsJet[index[i]];
-  }
+}
+
+//____________________________________________________________________________
+void AliFastJetFinder::RunTest(const char* datafile)
 
-   //reorder injet flags
-  for(Int_t ipar = 0; ipar < nIn; ipar++){
-     Float_t injetflag =0;
-     Int_t iparCell = fLego->FindBin(etaT[ipar], phiT[ipar]);
-     injet[ipar] = jetflagCell[iparCell];
-     for(Int_t js = 0; js < nj; js++){
-        if(injet[ipar] == index[js]){
-          injet[ipar] = js;  // set the new jet id value
-          mult[js]++; // add multiplicity in jet js
-          injetflag = 1;
-          break;
-        }
-     }
-     if(injetflag == 0) injet[ipar] = -1; // set default value
+{
+
+   // This simple test run the kt algorithm for an ascii file testdata.dat
+   // read input particles -----------------------------
+  vector<fastjet::PseudoJet> inputParticles;
+  Float_t px,py,pz,en;
+  ifstream in;
+  Int_t nlines = 0;
+  // we assume a file basic.dat in the current directory
+  // this file has 3 columns of float data
+  in.open(datafile);
+  while (1) {
+      in >> px >> py >> pz >> en;
+      if (!in.good()) break;
+      //printf("px=%8f, py=%8f, pz=%8fn",px,py,pz);
+      nlines++;
+      inputParticles.push_back(fastjet::PseudoJet(px,py,pz,en)); 
+   }
+   //printf(" found %d pointsn",nlines);
+   in.close();
+   //////////////////////////////////////////////////
+  // create an object that represents your choice of jet algorithm, and 
+  // the associated parameters
+  double rParam = 1.0;
+  fastjet::Strategy strategy = fastjet::Best;
+  fastjet::RecombinationScheme recombScheme = fastjet::BIpt_scheme;
+  fastjet::JetDefinition jetDef(fastjet::kt_algorithm, rParam, recombScheme, strategy);
+  
+  
+  // create an object that specifies how we to define the area
+  fastjet::AreaDefinition areaDef;
+  double ghostEtamax = 7.0;
+  double ghostArea    = 0.05;
+  int    activeAreaRepeats = 1;
+  
+
+  // now create the object that holds info about ghosts
+  fastjet::GhostedAreaSpec ghostSpec(ghostEtamax, activeAreaRepeats, ghostArea);
+  // and from that get an area definition
+  areaDef = fastjet::AreaDefinition(fastjet::active_area,ghostSpec);
+  
+
+  // run the jet clustering with the above jet definition
+  fastjet::ClusterSequenceArea clust_seq(inputParticles, jetDef, areaDef);
+  
+  
+  // tell the user what was done
+  cout << "--------------------------------------------------------" << endl;
+  cout << "Jet definition was: " << jetDef.description() << endl;
+  cout << "Area definition was: " << areaDef.description() << endl;
+  cout << "Strategy adopted by FastJet was "<< clust_seq.strategy_string()<<endl<<endl;
+  cout << "--------------------------------------------------------" << endl;
+  
+  // extract the inclusive jets with pt > 5 GeV, sorted by pt
+  double ptmin = 5.0;
+  vector<fastjet::PseudoJet> inclusiveJets = clust_seq.inclusive_jets(ptmin);
+  
+  cout << "Number of unclustered particles: " << clust_seq.unclustered_particles().size() << endl;
+  //subtract background // ===========================================
+  // set the rapididty range within which to study the background 
+  double rapMax = ghostEtamax - rParam;
+  fastjet::RangeDefinition range(rapMax);
+  // subtract background
+  vector<fastjet::PseudoJet> subJets =  clust_seq.subtracted_jets(range,ptmin);  
+  
+  // print them out //================================================
+  cout << "Printing inclusive jets  after background subtraction \n";
+  cout << "------------------------------------------------------\n";
+  // sort jets into increasing pt
+  vector<fastjet::PseudoJet> jets = sorted_by_pt(subJets);  
+
+  printf(" ijet   rap      phi        Pt         area  +-   err\n");
+  for (size_t j = 0; j < jets.size(); j++) {
+
+    double area     = clust_seq.area(jets[j]);
+    double areaError = clust_seq.area_error(jets[j]);
+
+    printf("%5d %9.5f %8.5f %10.3f %8.3f +- %6.3f\n",(Int_t)j,jets[j].rap(),
+          jets[j].phi(),jets[j].perp(), area, areaError);
   }
+  cout << endl;
+  // ================================================================
 
+  
+}
 
-  fJets->SetNCells(ncells);
-  fJets->SetPtFromSignal(percentage);
-  fJets->SetMultiplicities(mult);
-  fJets->SetInJet(injet);
-  fJets->SetEtaIn(etaT);
-  fJets->SetPhiIn(phiT);
-  fJets->SetPtIn(ptT);
-  fJets->SetEtAvg(meanptCell);
-
-   //delete
-  delete enT;
-  delete ptT;
-  delete etaT;
-  delete phiT;
-  delete injet;
-  //cells
-  delete etCell;
-  delete etaCell;
-  delete phiCell;
-  delete jetflagCell;
-  delete etsigCell;
-  //jets
-  delete etaJet;
-  delete phiJet;
-  delete etJet;
-  delete etsigJet;
-  delete etallJet;
-  delete ncellsJet;
-
-  delete index;
-  delete percentage;
-  delete ncells;
-  delete mult;
-  delete ptRndm;
+//____________________________________________________________________________
 
+void AliFastJetFinder::WriteJHeaderToFile() const
+{
+  fHeader->Write();
 }
 
-////////////////////////////////////////////////////////////////////////
-void AliFastJetFinder::RunAlgorithm(Int_t& nJets,Float_t* etJet,Float_t* etaJet,Float_t* phiJet,
-                                    Float_t* etsigJet, Float_t* etallJet, Int_t* ncellsJet,
-                                    Int_t& nCell,Float_t* etCell,Float_t* etaCell,Float_t* phiCell,
-                                    Float_t* etsigCell, Int_t* jetflagCell)
+//____________________________________________________________________________
+
+Float_t  AliFastJetFinder::EtaToTheta(Float_t arg)
 {
-   //FastJet objects
-   vector<FjPseudoJet> input_cells; // create a vector
-   for (Int_t i = 0; i < nCell; i++){
-      if(etCell[i] == 0.0) continue; // not include cell empty
-      Double_t px, py,pz,en; // convert to 4-vector
-      px = etCell[i]*TMath::Cos(phiCell[i]);
-      py = etCell[i]*TMath::Sin(phiCell[i]);
-      pz = etCell[i]/TMath::Tan(2.0 * TMath::ATan(TMath::Exp(-etaCell[i])));
-      en = TMath::Sqrt(px * px + py * py + pz * pz);
-      FjPseudoJet input_cell(px,py,pz,en); // create FjPseudoJet object
-      input_cell.set_user_index(i); //label the cell into Fastjet algortihm
-      //push FjPseudoJet of (px,py,pz,en) onto back of the input_cells
-      input_cells.push_back(input_cell);
-   }
+  //  return (180./TMath::Pi())*2.*atan(exp(-arg));
+  return 2.*atan(exp(-arg));
 
-   //run the jet clustering with option R=1.0 and strategy= Best
-   Double_t Rparam = fHeader->GetRadius(); // default 1.0;
-   FjClusterSequence clust_seq(input_cells,Rparam);
-
-
-   //vector to get clusters
-   vector<FjPseudoJet> clusters;
-
-   ///////////////////////////////////////////////////////////////////////////
-   //extract the inclusive jets with pt> ptmin sorted by pt
-   //Float_t areaT = 4*(fHeader->GetLegoEtaMax())*TMath::Pi();
-  // Double_t ptbgRc = EtBackgT*(Rparam*Rparam*TMath::Pi()/areaT);
-  // Double_t ptbgRcfluct = dEtTotal*Rparam*TMath::Sqrt(TMath::Pi()/areaT);
-  // Double_t ptmin = ptbgRc + ptbgRcfluct;
-   clusters = clust_seq.inclusive_jets(0);
-   //////////////////////////////////////////////////////////////////////////
-
-   /////////////////////////////////////////////////////////////////////////
-   //extract the exclusive jets with dcut = 25 GeV**2 and sort them in order
-   //of increasing pt
-   //Float_t areaT = 4*(fHeader->GetLegoEtaMax())*TMath::Pi();
-   //Double_t ptbgRc = EtBackgT*(Rparam*Rparam*TMath::Pi()/areaT);
-   //Double_t ptbgRcfluct = dEtTotal*Rparam*TMath::Sqrt(TMath::Pi()/areaT);
-   //Double_t ptmin = ptbgRc + ptbgRcfluct;
-   //Double_t ktbackg = (fHeader->GetKfactor())*ptmin;
-   //Double_t dcut = ktbackg*ktbackg;
-   //clusters = sorted_by_pt(clust_seq.exclusive_jets(dcut));
-   //clusters = sorted_by_pt(clust_seq.exclusive_jets(5));
-   /////////////////////////////////////////////////////////////////////////
-
-
-   //cout << " ktClusters " << clusters.size() << endl;
-   nJets = (Int_t)clusters.size();
-   ////////////////////////////////////////////////////////////////////////
-   // add all clusters to jet arrays
-   for(UInt_t ij = 0; ij < clusters.size(); ij++){
-       //constituents
-       vector<FjPseudoJet> constituents = clust_seq.constituents(clusters[ij]);
-       //fill jet array info
-       ncellsJet[ij] = (Int_t)constituents.size();
-       phiJet[ij] = clusters[ij].phi();
-       Float_t angle = TMath::ATan(clusters[ij].perp()/clusters[ij].pz());
-       angle = ((angle < 0) ? angle + TMath::Pi() : angle);
-       etaJet[ij] = - TMath::Log(TMath::Tan(angle/2.0));
-       etJet[ij] = clusters[ij].perp();
-       //get constituents cells
-       for(UInt_t jc = 0; jc < constituents.size(); jc++){ // loop for all cells in ij cluster
-           Int_t jcell = constituents[jc].user_index();
-           jetflagCell[jcell] = ij; //flag this cell for jet
-           etsigJet[ij] = etsigJet[ij] + etsigCell[jcell]; // add signal of this cell
-           etallJet[ij] = etallJet[ij] + etCell[jcell];   // add total of this cell
-       }
-   }
 
 }
-////////////////////////////////////////////////////////////////////////
-void AliFastJetFinder::SubtractBackg(Int_t& nCell, Int_t* jetflagCell, Float_t* etCell,
-                                     Int_t& nJets, Float_t* etJet, Float_t* etallJet, Int_t* ncellsJet,
-                                     Float_t& meanptCell, Float_t& sqptCell, Float_t& etBackg)
-{
-   // simplest method: subtract background from external region to jets
-
-   //tmp array to flag jets
-   Int_t flagJet[200];
-   //tmp array to flag jet-cell status
-   Int_t tmpjetflagCell[90000];
-
-   Float_t etBackgOld = 0;
-   Float_t prec  = fHeader->GetPrecBg();
-   Float_t bgprec = 1;
-
-   while(bgprec > prec){
-        //clear tmpjetflagCell
-        memset(tmpjetflagCell,-1,sizeof(Int_t)*90000); // init with -1 (all cells are background)
-        //clear flagjet
-        memset(flagJet,0,sizeof(Int_t)*200); // init with 0 (no flag jets)
-        // select clusters > meantmpCell
-        for(Int_t i = 0; i < nJets; i++){
-            Float_t iptcell = etallJet[i]/(Float_t)ncellsJet[i];
-            if(iptcell < meanptCell) continue; // cluster not selected
-            // convert tmp cell background to jet cell
-            for(Int_t ic = 0; ic < nCell; ic++){ //loop for all cells
-                if(jetflagCell[ic] != i) continue; // other cells
-                tmpjetflagCell[ic] = i; // convert to a jet cell
-            }
-            //load total energy in cluster
-            etJet[i] = etallJet[i];
-            flagJet[i] = 1; // flag jet
-       }
-       //subtract background
-       for(Int_t j = 0; j < nCell; j++){ // loop for all cells
-           Int_t idxjet = tmpjetflagCell[j];
-           if(idxjet == -1) continue; // background cell
-           if(idxjet == -2) continue; // background protected cell
-           etJet[idxjet] = etJet[idxjet] - meanptCell;
-       }
-       // evaluate background fluctuations (rms value)
-       Float_t rmsptCell = TMath::Sqrt(sqptCell - meanptCell*meanptCell);
-       //fake jets
-       for(Int_t k = 0; k < nJets; k++){
-           if(flagJet[k] != 1) continue; // only flaged jets
-           //if(etJet[k] > fHeader->GetMinJetEt()) continue;  // jet ok!!
-           if(etJet[k] > rmsptCell*ncellsJet[k]) continue;  // jet ok!!
-           //clear tmpjetflag in cells
-           for(Int_t kc = 0; kc < nCell; kc++){ //loop for all cells
-               if(tmpjetflagCell[kc] != k) continue; // other cells
-               tmpjetflagCell[kc] = -1; // convert to background tmp cell
-           }
-           // clear all previous jet flags
-           etJet[k] = 0;
-           flagJet[k] = 0;
-       }
-       // recalculate background
-       sqptCell = 0;
-       etBackgOld = etBackg;
-       etBackg = 0;
-       Int_t nCellBackg = 0;
-       for(Int_t l = 0; l < nCell; l++){ // loop for all cells
-          if(tmpjetflagCell[l] != -1) continue; // cell included in some jet or protected
-          nCellBackg++;
-          etBackg = etBackg + etCell[l]; //add cell to background
-          //calc sqptCell
-          sqptCell = sqptCell + etCell[l]*etCell[l];
-       }
-       if(nCellBackg){
-          meanptCell = etBackg/(Float_t)nCellBackg; // new pt cell mean value
-          sqptCell = sqptCell/(Float_t)nCellBackg;
-       }else{
-          meanptCell = 0;
-          sqptCell = 0;
-       }
-       // evaluate presicion values
-       if(etBackg)
-         bgprec = (etBackgOld - etBackg)/etBackg;
-       else
-         bgprec = 0;
-   }
 
-   // set etJet 0 for all clusters not flaged in order to
-   for(Int_t m = 0; m < nJets; m++){
-       if(flagJet[m] == 1) continue; // flaged jets
-       etJet[m] = 0; //others clusters
-   }
+//____________________________________________________________________________
+
+void AliFastJetFinder::InitTask(TChain *tree)
+{
 
+  printf("Fast jet finder initialization ******************");
+  fReader->CreateTasks(tree);
 
 }
 
-////////////////////////////////////////////////////////////////////////
-void AliFastJetFinder::SubtractBackgArea(Int_t& nCell, Int_t* jetflagCell,
-                                     Float_t* etCell,Int_t&nJets, Float_t* etJet, Float_t* etallJet)
+
+Bool_t AliFastJetFinder::ProcessEvent()
 {
-   // area method: subtract background from external region to jets
-   // using fixed area pi*Rc2
-
-   // n cells contained in a cone Rc
-   Double_t Rc = fHeader->GetRadius();
-   Float_t nCellRc = Rc*Rc*TMath::Pi()/(0.015*0.015); // change in future !!!!
-   //tmp array to flag fake jets
-   Int_t fakeflagJet[100];
-   memset(fakeflagJet,0,sizeof(Int_t)*100); // init with 0 (no fake jets)
-   Int_t njfake = nJets;
-   while(njfake > 0){
-       //calc background per cell
-       Int_t nCellBackg = 0;
-       Float_t EtBackg = 0.0;
-       for(Int_t i = 0; i < nCell; i++){ // loop for all cells
-           if(jetflagCell[i] != -1) continue; // cell included in some jet
-           nCellBackg++;
-           EtBackg = EtBackg + etCell[i]; //add cell to background
-       }
-       //subtract background energy per jet
-       for(Int_t l = 0; l < nJets; l++){
-           if(fakeflagJet[l] == 1) continue; // fake jet
-           etJet[l] = etallJet[l] - nCellRc*EtBackg/(Float_t)nCellBackg;
-       }
-       //fake jets analysis
-       njfake = 0;
-       for(Int_t k = 0; k < nJets; k++){
-           if(fakeflagJet[k] == 1) continue; // fake jet
-           if(etJet[k] < fHeader->GetMinJetEt()){  // a new fake jet
-              //clear jet flag in cells
-              for(Int_t kc = 0; kc < nCell; kc++){ //loop for all cells
-                  Int_t kidx = jetflagCell[kc];
-                  if(kidx != k) continue; // other cells
-                  jetflagCell[kc] = -1; // convert to background cell
-              }
-              fakeflagJet[k] = 1; // mark as a fake jet
-              njfake++; //count fakes in this loop
-           }
-       }
-   }
-}
+  //
+  // Process one event
+  // from meomntum array
+
+  Bool_t ok = fReader->FillMomentumArray();
+
+  if (!ok) return kFALSE;
+  fInputFJ->SetHeader(fHeader);
+  fInputFJ->SetReader(fReader);
+  fInputFJ->FillInput();
+  // Jets
+  FindJets(); 
+
+
+  if( fAODEvBkg){
+    fJetBkg->SetHeader(fHeader);
+    fJetBkg->SetReader(fReader);
+    Double_t sigma1 = 0,meanarea1= 0,sigma2 = 0,meanarea2 = 0;
+    Double_t bkg1 = 0,bkg2 = 0;
+    
+    fJetBkg->SetFastJetInput(fInputFJ);
+    fJetBkg->BkgFastJetb(bkg1,sigma1,meanarea1);
+    fJetBkg->BkgFastJetWoHardest(bkg2,sigma2,meanarea2);
+    fAODEvBkg->SetBackground(0,bkg1,sigma1,meanarea1);
+    fAODEvBkg->SetBackground(1,bkg2,sigma2,meanarea2);
+  }
 
-////////////////////////////////////////////////////////////////////////
 
 
 
-void AliFastJetFinder::Reset()
-{
-  fLego->Reset();
-  fLegoSignal->Reset();
-  fJets->ClearJets();
+  /*
+  fJetBkg->SetFastJetInput(fInputFJ);
+  Double_t bkg1=fJetBkg->BkgFastJet();
+  Double_t bkg2=fJetBkg->BkgChargedFastJet();
+  Double_t bkg3=fJetBkg->BkgFastJetCone(fAODjets);
+  Double_t bkg4=fJetBkg->BkgRemoveJetLeading(fAODjets);
+  
+  fAODEvBkg->SetBackground(0,bkg1);
+  fAODEvBkg->SetBackground(1,bkg2);
+  fAODEvBkg->SetBackground(2,bkg3);
+  fAODEvBkg->SetBackground(3,bkg4);
+  */
+  Reset();  
+  return kTRUE;
 
 }
-////////////////////////////////////////////////////////////////////////
 
-void AliFastJetFinder::WriteJHeaderToFile()
+Bool_t AliFastJetFinder::ProcessEvent2()
 {
-  fOut->cd();
-  fHeader->Write();
-}
+  //
+  // Process one event
+  // Charged only or charged+neutral jets
+  //
+
+  TRefArray* ref = new TRefArray();
+  Bool_t procid = kFALSE;
+  Bool_t ok = fReader->ExecTasks(procid,ref);
+
+  // Delete reference pointer  
+  if (!ok) {delete ref; return kFALSE;}
+  
+  // Leading particles
+  fInputFJ->SetHeader(fHeader);
+  fInputFJ->SetReader(fReader);
+  fInputFJ->FillInput();
+  
+  // Jets
+  FindJets();
+  
+  if( fAODEvBkg){
+    fJetBkg->SetHeader(fHeader);
+    fJetBkg->SetReader(fReader);
+    fJetBkg->SetFastJetInput(fInputFJ);
+    Double_t sigma1,meanarea1,sigma2,meanarea2;
+    Double_t bkg1,bkg2;
+    fJetBkg->BkgFastJetb(bkg1,sigma1,meanarea1);
+    fJetBkg->BkgFastJetWoHardest(bkg2,sigma2,meanarea2);
+    fAODEvBkg->SetBackground(0,bkg1,sigma1,meanarea1);
+    fAODEvBkg->SetBackground(1,bkg2,sigma2,meanarea2);
+  }
 
-////////////////////////////////////////////////////////////////////////
 
-void AliFastJetFinder::Init()
-{
-  // initializes some variables
-   // book lego
-  fLego = new
-    TH2F("legoH","eta-phi",
-        fHeader->GetLegoNbinEta(), fHeader->GetLegoEtaMin(),
-        fHeader->GetLegoEtaMax(),  fHeader->GetLegoNbinPhi(),
-        fHeader->GetLegoPhiMin(), fHeader->GetLegoPhiMax());
-  fLegoSignal = new
-    TH2F("legoSignalH","eta-phi signal",
-        fHeader->GetLegoNbinEta(), fHeader->GetLegoEtaMin(),
-        fHeader->GetLegoEtaMax(),  fHeader->GetLegoNbinPhi(),
-        fHeader->GetLegoPhiMin(), fHeader->GetLegoPhiMax());
+//  Double_t bkg1=fJetBkg->BkgFastJet();
+//  Double_t bkg2=fJetBkg->BkgChargedFastJet();
+//  Double_t bkg3=fJetBkg->BkgFastJetCone(fAODjets);
+//  Double_t bkg4=fJetBkg->BkgRemoveJetLeading(fAODjets);
+  
+//  fAODEvBkg->SetBackground(0,bkg1);
+//  fAODEvBkg->SetBackground(1,bkg2);
+//  fAODEvBkg->SetBackground(2,bkg3);
+//  fAODEvBkg->SetBackground(3,bkg4);
+  
+  Int_t nEntRef    = ref->GetEntries();
+
+  for(Int_t i=0; i<nEntRef; i++)
+    { 
+      // Reset the UnitArray content which were referenced
+      ((AliJetUnitArray*)ref->At(i))->SetUnitTrackID(0);
+      ((AliJetUnitArray*)ref->At(i))->SetUnitEnergy(0.);
+      ((AliJetUnitArray*)ref->At(i))->SetUnitCutFlag(kPtSmaller);
+      ((AliJetUnitArray*)ref->At(i))->SetUnitCutFlag2(kPtSmaller);
+      ((AliJetUnitArray*)ref->At(i))->SetUnitSignalFlag(kBad);
+      ((AliJetUnitArray*)ref->At(i))->SetUnitSignalFlagC(kTRUE,kBad);
+      ((AliJetUnitArray*)ref->At(i))->SetUnitDetectorFlag(kTpc);
+      ((AliJetUnitArray*)ref->At(i))->SetUnitFlag(kOutJet);
+      ((AliJetUnitArray*)ref->At(i))->ClearUnitTrackRef();
+
+      // Reset process ID
+      AliJetUnitArray* uA = (AliJetUnitArray*)ref->At(i);
+      uA->ResetBit(kIsReferenced);
+      uA->SetUniqueID(0);     
+    }
+
+  // Delete the reference pointer
+  ref->Delete();
+  delete ref;
+
+  Reset();
 
+  return kTRUE;
 }