ROOT logo
#include <Riostream.h>

extern TRandom *gRandom;
extern TBenchmark *gBenchmark;
extern TSystem *gSystem;

void testCFContainers(){

  // simple example macros for usage of a N-dim container (AliCFContainer)
  // handling a set of grids to accumulate data at different 
  // selection steps, & derive efficiency (this is stored in AliCFEffGrid) 
  // book, fill and draw some histos
  // The efficiency is then used to correct the data (trivially self-correct, 
  // in this example)

  gROOT->SetStyle("Plain");
  gStyle->SetPalette(1);
  gStyle->SetOptStat(111110);
  gStyle->SetPalette(1);
  gStyle->SetCanvasColor(0);
  gStyle->SetFrameFillColor(0);

  gSystem->Load("libANALYSIS.so");
  gSystem->Load("libANALYSISalice.so");
  gSystem->Load("libCORRFW.so") ;
 
  //Setting up the container grid... 

  const Int_t nstep=2; //number of selection steps  (just 2 in this ex)

  const Int_t nvar=4; //number of variables on the grid:pt,vtx

  const Int_t nbin1=6; //bins in pt
  const Int_t nbin2=10; //bins in eta 
  const Int_t nbin3=18; //bins in phi
  const Int_t nbin4=10; //bins in vertex


  //Flag the sel steps. In this example, we have two, may be any nstep
  Int_t stepGen=0;
  Int_t stepRec=1;

  //the sensitive variables, their indeces
  Int_t ipt =0;
  Int_t ieta=1;
  Int_t iphi=2;
  Int_t ivtx=3;

  //arrays for the number of bins in each dimension
  const Int_t iBin[nvar] ={nbin1,nbin2,nbin3,nbin4};

  //arrays for bin limits
  Double_t binLim1[nbin1+1];
  Double_t binLim2[nbin2+1];
  Double_t binLim3[nbin3+1];
  Double_t binLim4[nbin4+1];
  
  for(Int_t i=0;i<=nbin1;i++){
    // pt [0,3] GeV/c
    binLim1[i]=i*0.5; 
  }

  for(Int_t i=0;i<=nbin2;i++){
    // eta [-1,1] 
    binLim2[i]=i*0.2-1.;
  }
  for(Int_t i=0;i<=nbin3;i++){
    //phi [0,360]
    binLim3[i]=i*20.;
  }
  for(Int_t i=0;i<=nbin4;i++){
    //vertex [-20,20] cm
    binLim4[i]=i*4.-20.;
  }
  
  
  //the nstep grids "container" 
  AliCFContainer *cont = new AliCFContainer("cont","example of  container",nstep,nvar,iBin);

  //setting the bin limits
  cont->SetBinLimits(ipt,binLim1);
  cont->SetBinLimits(ieta,binLim2);
  cont->SetBinLimits(iphi,binLim3);
  cont->SetBinLimits(ivtx,binLim4);

  //Start filling the mc and the data

  //data sample (1M tracks)
  Int_t nev=100000;
  Int_t seed =1234;
  gRandom->SetSeed(seed);
  Double_t Value[nvar];
  for(Int_t iev =0;iev<nev;iev++){
    Float_t y=gRandom->Rndm();
    Float_t pt=-TMath::Log(y)/0.5; //pt, exponential
    Double_t eta=2.*gRandom->Rndm()-1.;//flat in eta
    Double_t phi=360.*gRandom->Rndm(); //flat in phi
    Float_t vtx=gRandom->Gaus( 0,5.);//gaussian in vertex
    Value[ipt]=pt;
    Value[ieta]=eta;
    Value[iphi]=phi;
    Value[ivtx]=vtx;    
    cont->Fill(Value, stepGen); //fill the efficiency denominator, sel step=0
    Float_t rndm=gRandom->Rndm();
    //simulate 80% constant efficiency everywhere
    if(rndm<0.8){
      cont->Fill(Value,stepRec); //fill the efficiency denominator, sel step =1
    }		
  }   

  //   Save it to a file
   cont->Save("container.root");
  //delete it
   delete cont;

// Read the  container from file
   TFile *file = new TFile("container.root");
   AliCFContainer *data = (AliCFContainer*) (file->Get("cont"));

  // Make some 1 & 2-D projections..
  // pt and vertex, generator and reconstructed level
//   TCanvas *cmc =new TCanvas("cmc","The  distributions",0,300,900,900);
//   cmc->Divide(2,2);
//   cmc->cd(1);
//   TH1D *hpt1a = data->ShowProjection(ipt, stepGen);
//   hpt1a->SetMinimum(0.01);
//   hpt1a->Draw();
//   cmc->cd(2);
//   TH1D *hpt1b = data->ShowProjection(ipt, stepRec);
//   hpt1b->SetMinimum(0.01);
//   hpt1b->Draw();
//   cmc->cd(3);
//   TH2D *hptvtx1a = data->ShowProjection(ipt,ivtx, stepGen);
//   hptvtx1a->SetMinimum(0.01);
//   hptvtx1a->Draw("lego");
//   cmc->cd(4);
//   TH2D *hptvtx1b = data->ShowProjection(ipt,ivtx, stepRec);
//   hptvtx1b->SetMinimum(0.01);
//   hptvtx1b->Draw("lego");
//   cmc->Print("data.gif");

 
  //construct the efficiency grid from the data container 
  AliCFEffGrid *eff = new AliCFEffGrid("eff"," The efficiency",*data);
  eff->CalculateEfficiency(stepRec,stepGen); //eff= step1/step0

  //The efficiency along pt and vertex, and 2-D projection
  TCanvas *ceff =new TCanvas("ceff"," Efficiency",0,300,900,300);
  ceff->Divide(3,1);
  ceff->cd(1);
  TH1D *hpt2a = eff->Project(ipt); //the efficiency vs pt
  hpt2a->SetMinimum(0.01);
  hpt2a->Draw();
  ceff->cd(2);
  TH1D *hvtx2a = eff->Project(ivtx); //the efficiency vs vtx
  hvtx2a->SetMinimum(0.01);
  hvtx2a->Draw();
  ceff->cd(3);
  TH2D *hptvtx2a = eff->Project(ipt,ivtx); //look at the numerator
  hptvtx2a->SetMinimum(0.01);
  hptvtx2a->SetMinimum(0.01);
  hptvtx2a->Draw("lego");
  
  ceff->Print("eff.gif");

  //get the corrected data grid  
  AliCFDataGrid *corrdata = new AliCFDataGrid("corrdata","corrected data",*data);
  //correct selection step "reconstructed"
  corrdata->SetMeasured(stepRec); //set data to be corrected
  corrdata->ApplyEffCorrection(*eff);//apply the correction for efficiency

  //The observed data, the corrected ones and the "MC truth" distributions 
  //vs pt and vtx
  TCanvas *ccorrdata =new TCanvas("ccorrdata"," corrected data",0,300,900,900);
  ccorrdata->Divide(2,2);
  ccorrdata->cd(1);
  TH1D *hpt3a = corrdata->GetData()->Project(ipt); //uncorrected data
  hpt3a->SetMinimum(0.01);
  hpt3a->Draw();
  ccorrdata->cd(2);
  TH1D *hpt3b = corrdata->Project(ipt); //corrected data
  hpt3b->SetMinimum(0.01);
  hpt3b->Draw();
  ccorrdata->cd(3);
  TH1D *hvtx3a = corrdata->GetData()->Project(ivtx); //uncorrected data
  hvtx3a->SetMinimum(0.01);
  hvtx3a->Draw();
  ccorrdata->cd(4);
  TH1D *hvtx3b = corrdata->Project(ivtx); //corrected data
  hvtx3b->SetMinimum(0.01);
  hvtx3b->Draw();
  ccorrdata->Print("corrdata.gif");

}
 testCFContainers.C:1
 testCFContainers.C:2
 testCFContainers.C:3
 testCFContainers.C:4
 testCFContainers.C:5
 testCFContainers.C:6
 testCFContainers.C:7
 testCFContainers.C:8
 testCFContainers.C:9
 testCFContainers.C:10
 testCFContainers.C:11
 testCFContainers.C:12
 testCFContainers.C:13
 testCFContainers.C:14
 testCFContainers.C:15
 testCFContainers.C:16
 testCFContainers.C:17
 testCFContainers.C:18
 testCFContainers.C:19
 testCFContainers.C:20
 testCFContainers.C:21
 testCFContainers.C:22
 testCFContainers.C:23
 testCFContainers.C:24
 testCFContainers.C:25
 testCFContainers.C:26
 testCFContainers.C:27
 testCFContainers.C:28
 testCFContainers.C:29
 testCFContainers.C:30
 testCFContainers.C:31
 testCFContainers.C:32
 testCFContainers.C:33
 testCFContainers.C:34
 testCFContainers.C:35
 testCFContainers.C:36
 testCFContainers.C:37
 testCFContainers.C:38
 testCFContainers.C:39
 testCFContainers.C:40
 testCFContainers.C:41
 testCFContainers.C:42
 testCFContainers.C:43
 testCFContainers.C:44
 testCFContainers.C:45
 testCFContainers.C:46
 testCFContainers.C:47
 testCFContainers.C:48
 testCFContainers.C:49
 testCFContainers.C:50
 testCFContainers.C:51
 testCFContainers.C:52
 testCFContainers.C:53
 testCFContainers.C:54
 testCFContainers.C:55
 testCFContainers.C:56
 testCFContainers.C:57
 testCFContainers.C:58
 testCFContainers.C:59
 testCFContainers.C:60
 testCFContainers.C:61
 testCFContainers.C:62
 testCFContainers.C:63
 testCFContainers.C:64
 testCFContainers.C:65
 testCFContainers.C:66
 testCFContainers.C:67
 testCFContainers.C:68
 testCFContainers.C:69
 testCFContainers.C:70
 testCFContainers.C:71
 testCFContainers.C:72
 testCFContainers.C:73
 testCFContainers.C:74
 testCFContainers.C:75
 testCFContainers.C:76
 testCFContainers.C:77
 testCFContainers.C:78
 testCFContainers.C:79
 testCFContainers.C:80
 testCFContainers.C:81
 testCFContainers.C:82
 testCFContainers.C:83
 testCFContainers.C:84
 testCFContainers.C:85
 testCFContainers.C:86
 testCFContainers.C:87
 testCFContainers.C:88
 testCFContainers.C:89
 testCFContainers.C:90
 testCFContainers.C:91
 testCFContainers.C:92
 testCFContainers.C:93
 testCFContainers.C:94
 testCFContainers.C:95
 testCFContainers.C:96
 testCFContainers.C:97
 testCFContainers.C:98
 testCFContainers.C:99
 testCFContainers.C:100
 testCFContainers.C:101
 testCFContainers.C:102
 testCFContainers.C:103
 testCFContainers.C:104
 testCFContainers.C:105
 testCFContainers.C:106
 testCFContainers.C:107
 testCFContainers.C:108
 testCFContainers.C:109
 testCFContainers.C:110
 testCFContainers.C:111
 testCFContainers.C:112
 testCFContainers.C:113
 testCFContainers.C:114
 testCFContainers.C:115
 testCFContainers.C:116
 testCFContainers.C:117
 testCFContainers.C:118
 testCFContainers.C:119
 testCFContainers.C:120
 testCFContainers.C:121
 testCFContainers.C:122
 testCFContainers.C:123
 testCFContainers.C:124
 testCFContainers.C:125
 testCFContainers.C:126
 testCFContainers.C:127
 testCFContainers.C:128
 testCFContainers.C:129
 testCFContainers.C:130
 testCFContainers.C:131
 testCFContainers.C:132
 testCFContainers.C:133
 testCFContainers.C:134
 testCFContainers.C:135
 testCFContainers.C:136
 testCFContainers.C:137
 testCFContainers.C:138
 testCFContainers.C:139
 testCFContainers.C:140
 testCFContainers.C:141
 testCFContainers.C:142
 testCFContainers.C:143
 testCFContainers.C:144
 testCFContainers.C:145
 testCFContainers.C:146
 testCFContainers.C:147
 testCFContainers.C:148
 testCFContainers.C:149
 testCFContainers.C:150
 testCFContainers.C:151
 testCFContainers.C:152
 testCFContainers.C:153
 testCFContainers.C:154
 testCFContainers.C:155
 testCFContainers.C:156
 testCFContainers.C:157
 testCFContainers.C:158
 testCFContainers.C:159
 testCFContainers.C:160
 testCFContainers.C:161
 testCFContainers.C:162
 testCFContainers.C:163
 testCFContainers.C:164
 testCFContainers.C:165
 testCFContainers.C:166
 testCFContainers.C:167
 testCFContainers.C:168
 testCFContainers.C:169
 testCFContainers.C:170
 testCFContainers.C:171
 testCFContainers.C:172
 testCFContainers.C:173
 testCFContainers.C:174
 testCFContainers.C:175
 testCFContainers.C:176
 testCFContainers.C:177
 testCFContainers.C:178
 testCFContainers.C:179
 testCFContainers.C:180
 testCFContainers.C:181
 testCFContainers.C:182
 testCFContainers.C:183
 testCFContainers.C:184
 testCFContainers.C:185
 testCFContainers.C:186
 testCFContainers.C:187
 testCFContainers.C:188
 testCFContainers.C:189
 testCFContainers.C:190
 testCFContainers.C:191
 testCFContainers.C:192
 testCFContainers.C:193
 testCFContainers.C:194
 testCFContainers.C:195