ROOT logo
#if !defined(__CINT__) || defined(__MAKECINT__)
#include "TFile.h"
#include "TGeoManager.h"
#include "TInterpreter.h"
#include "TClassTable.h"
#include "TNtuple.h"
#include "TTree.h"
#include "TArrayF.h"
#include "TParticle.h"
#include "AliESD.h"
#include "AliGenPythiaEventHeader.h"
#include "AliTracker.h"
#include "AliHeader.h"
#include "AliITSLoader.h"
#include "AliITSsegmentationSPD.h"
#include "AliVertexerTracks.h"
#include "AliCDBManager.h"
#include "AliGeomManager.h"
#include "AliGRPManager.h"
#include "AliITSDetTypeRec.h"
#include "AliITSVertexer3D.h"
#include "AliITSVertexerZ.h"
#include "AliESDVertex.h"
#include "AliESDEvent.h"
#include "AliRun.h"
#include "AliRunLoader.h"
#include "AliGenEventHeader.h"
#include "AliStack.h"
#endif

/*  $Id$    */

Bool_t DoVerticesSPD(Bool_t isMC=kFALSE, Int_t pileupalgo=1, Int_t optdebug=0){

  TFile *fint = new TFile("VertexSPD.root","recreate");
  TNtuple *nt = new TNtuple("ntvert","vertices","xtrue:ytrue:ztrue:zZ:zdiffZ:zerrZ:ntrksZ:x3D:xdiff3D:xerr3D:y3D:ydiff3D:yerr3D:z3D:zdiff3D:zerr3D:ntrks3D:nchgen:ntrklets:nrp1:ptyp:is3D:isTriggered");

  if (gClassTable->GetID("AliRun") < 0) {
    gInterpreter->ExecuteMacro("loadlibs.C");
  }
  // Set OCDB if needed
  AliCDBManager* man = AliCDBManager::Instance();
  if (!man->IsDefaultStorageSet()) {
    printf("Setting a local default storage and run number 0\n");
    man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
    //    man->SetSpecificStorage("GRP/GRP/Data",Form("local://%s",gSystem->pwd()));
    man->SetRun(0);
  }
  else {
    printf("Using deafult storage \n");
  }
 
  // retrives geometry 
  if(!gGeoManager){
    AliGeomManager::LoadGeometry("geometry.root");
  }
  AliGeomManager::ApplyAlignObjsFromCDB("ITS");

  AliRunLoader* runLoader = AliRunLoader::Open("galice.root");
  if (!runLoader) {
    Error("DoVertices", "getting run loader from file %s failed", 
	  "galice.root");
    return kFALSE;
  }
  
  if(isMC){
    runLoader->LoadgAlice();
    gAlice = runLoader->GetAliRun();
    if (!gAlice) {
      Error("DoVertices", "no galice object found");
      return kFALSE;
    }
    runLoader->LoadKinematics();
    runLoader->LoadHeader();
  }
  AliITSLoader* ITSloader =  (AliITSLoader*) runLoader->GetLoader("ITSLoader");
  ITSloader->LoadRecPoints("read");

  Int_t totev=runLoader->GetNumberOfEvents();
  if(optdebug)  printf("Number of events= %d\n",totev);

  TFile* esdFile = TFile::Open("AliESDs.root");
  if (!esdFile || !esdFile->IsOpen()) {
    printf("Error in opening ESD file");
    return kFALSE;
  }

  AliESDEvent * esd = new AliESDEvent;
  TTree* tree = (TTree*) esdFile->Get("esdTree");
  if (!tree) {
    printf("Error: no ESD tree found");
    return kFALSE;
  }
  esd->ReadFromTree(tree);
  AliGRPManager * grpMan=new AliGRPManager();
  grpMan->ReadGRPEntry();
  grpMan->SetMagField();
  printf("Magnetic field set to %f T\n",AliTracker::GetBz()/10.);

  AliITSDetTypeRec* detTypeRec = new AliITSDetTypeRec();
  AliITSsegmentation* seg = new AliITSsegmentationSPD();  
  detTypeRec->SetSegmentationModel(0,seg);

  Double_t xnom=0.,ynom=0.;
  AliITSVertexerZ *vertz = new AliITSVertexerZ(xnom,ynom);
  vertz->Init("default");
  AliITSVertexer3D *vert3d = new AliITSVertexer3D();  
  vert3d->Init("default");
  vert3d->SetWideFiducialRegion(40.,1.);
  vert3d->SetPileupAlgo(pileupalgo);
  vert3d->PrintStatus();
  vertz->SetDetTypeRec(detTypeRec);
  vert3d->SetDetTypeRec(detTypeRec);
  vert3d->SetComputeMultiplicity(kTRUE);
  /* uncomment these lines to use diamond constrain */
//   Double_t posdiam[3]={0.03,0.1,0.};
//   Double_t sigdiam[3]={0.01,0.01,10.0};
//   AliESDVertex* diam=new AliESDVertex(posdiam,sigdiam);
//   vertz->SetVtxStart(diam);
//   vert3d->SetVtxStart(diam);
  /* end lines to be uncommented to use diamond constrain */

  Int_t goodz=0,good3d=0;
  // Trigger mask
  ULong64_t spdFO = (1 << 14);
  ULong64_t v0left = (1 << 11);
  ULong64_t v0right = (1 << 12);

  for (Int_t iEvent = 0; iEvent < totev; iEvent++) {
    TArrayF mcVertex(3); 
    runLoader->GetEvent(iEvent);
    Double_t nChGen = 0.;
    Int_t ptype = 0;
    if(optdebug){
      printf("==============================================================\n");
      printf("Event: %d \n",iEvent);
    }
    if(isMC){
      runLoader->GetHeader()->GenEventHeader()->PrimaryVertex(mcVertex);
      AliGenPythiaEventHeader *evh=(AliGenPythiaEventHeader*)runLoader->GetHeader()->GenEventHeader();
      ptype = evh->ProcessType();

      AliStack* stack = runLoader->Stack();
      TTree *treek=(TTree*)runLoader->TreeK();
      Int_t npart = (Int_t)treek->GetEntries();
      if(optdebug) printf("Process Type = %d --- Particles  %d\n",ptype,npart);
      
      // loop on particles to get generated dN/dy
      for(Int_t iPart=0; iPart<npart; iPart++) {
	if(!stack->IsPhysicalPrimary(iPart)) continue;
	TParticle* part = (TParticle*)stack->Particle(iPart);
	if(part->GetPDG()->Charge() == 0) continue;
	Double_t eta=part->Eta();
	
	if(TMath::Abs(eta)<1.5) nChGen+=1.; 
      }
      if(optdebug) printf(" N. of generated charged primaries in |eta|<1.5 = %f\n",nChGen);
    }
    tree->GetEvent(iEvent);

    TTree* cltree = ITSloader->TreeR();
    ULong64_t triggerMask=esd->GetTriggerMask();
    // MB1: SPDFO || V0L || V0R
    Bool_t eventTriggered = (triggerMask & spdFO || ((triggerMask & v0left) || (triggerMask & v0right)));
    AliESDVertex* vtxz = vertz->FindVertexForCurrentEvent(cltree);
    AliESDVertex* vtx3d = vert3d->FindVertexForCurrentEvent(cltree);
    AliMultiplicity *alimult = vert3d->GetMultiplicity();
    Int_t  ntrklets=0;
    Int_t nrecp1=0;
    if(alimult){
      ntrklets=alimult->GetNumberOfTracklets() ;
      nrecp1=ntrklets+alimult->GetNumberOfSingleClusters();
    }


    TDirectory *current = gDirectory;
    fint->cd();
    Float_t xnt[23];

    Double_t zz = 0.;
    Double_t zresz = 0.;
    Double_t zdiffz = 0.;

    Int_t ntrkz = 0;
    if(vtxz){
 
      ntrkz = vtxz->GetNContributors();
      if(ntrkz>0)goodz++;
      zz=vtxz->GetZ();
      zresz =vtxz->GetZRes(); // microns
      zdiffz = 10000.*(zz-mcVertex[2]); // microns
    }

    Double_t x3d = 0.;
    Double_t xerr3d = 0.;
    Double_t xdiff3d = 0.;

    Double_t y3d = 0.;
    Double_t yerr3d = 0.;
    Double_t ydiff3d = 0.;

    Double_t z3d = 0.;
    Double_t zerr3d = 0.;
    Double_t zdiff3d = 0.;

    Int_t ntrk3d = 0;
    Bool_t is3d=kFALSE;
    if(vtx3d){

      if(vtx3d->IsFromVertexer3D()) is3d=kTRUE;
      ntrk3d = vtx3d->GetNContributors();
      if(is3d && ntrk3d>0)good3d++;
      x3d = vtx3d->GetX();
      xerr3d=vtx3d->GetXRes();
      xdiff3d = 10000.*(x3d-mcVertex[0]);  // microns

      y3d = vtx3d->GetY();
      yerr3d=vtx3d->GetYRes();
      ydiff3d = 10000.*(y3d-mcVertex[1]);  // microns

      z3d = vtx3d->GetZ();
      zerr3d=vtx3d->GetZRes();
      zdiff3d = 10000.*(z3d-mcVertex[2]);  // microns

    }

    xnt[0]=mcVertex[0];//x
    xnt[1]=mcVertex[1];//y
    xnt[2]=mcVertex[2];//z

    xnt[3]=zz;
    xnt[4]=zdiffz;
    xnt[5]=zresz;
    xnt[6]=ntrkz;

    xnt[7]=x3d;
    xnt[8]=xdiff3d;
    xnt[9]=xerr3d;
    xnt[10]=y3d;
    xnt[11]=ydiff3d;
    xnt[12]=yerr3d;
    xnt[13]=z3d;
    xnt[14]=zdiff3d;
    xnt[15]=zerr3d;
    xnt[16]=ntrk3d;

    xnt[17]=nChGen;
    xnt[18]=float(ntrklets);
    xnt[19]=float(nrecp1);
    xnt[20]=float(ptype);
    xnt[21]=float(is3d);
    xnt[22]=float(eventTriggered);
    nt->Fill(xnt);
    current->cd();
    
    if(optdebug){
      printf("Vertexer3D: \tx=%.5f \ty=%.5f \tz(cm)=%.5f \tContributors=%d \n",x3d,y3d,z3d,ntrk3d);
      printf("VertexerZ:  \tx=%.5f \ty=%.5f \tz(cm)=%.5f \tContributors=%d \n",0.,0.,zz,ntrkz);
      if(isMC) printf("True Pos.: \tx=%.5f \ty=%.5f \tz(cm)=%.5f \tdN/dy=%.1f\n",mcVertex[0],mcVertex[1],mcVertex[2],nChGen);
      printf("Multiplicity: Tracklets=%d  ClustersLay1=%d\n",ntrklets,nrecp1);
    }
    
  }
  fint->cd();
  nt->Write();
  fint->Close();
  delete fint;
  if(optdebug){
   printf("***********************************************\n");
   printf("Number of Z vertices found= %d\n",goodz);
   printf("efficiency=%f\n",float(goodz)/float(totev));
   printf("Number of 3D vertices found= %d\n",good3d);
   printf("efficiency=%f\n",float(good3d)/float(totev));
  }
  return kTRUE;
}
 DoVerticesSPD.C:1
 DoVerticesSPD.C:2
 DoVerticesSPD.C:3
 DoVerticesSPD.C:4
 DoVerticesSPD.C:5
 DoVerticesSPD.C:6
 DoVerticesSPD.C:7
 DoVerticesSPD.C:8
 DoVerticesSPD.C:9
 DoVerticesSPD.C:10
 DoVerticesSPD.C:11
 DoVerticesSPD.C:12
 DoVerticesSPD.C:13
 DoVerticesSPD.C:14
 DoVerticesSPD.C:15
 DoVerticesSPD.C:16
 DoVerticesSPD.C:17
 DoVerticesSPD.C:18
 DoVerticesSPD.C:19
 DoVerticesSPD.C:20
 DoVerticesSPD.C:21
 DoVerticesSPD.C:22
 DoVerticesSPD.C:23
 DoVerticesSPD.C:24
 DoVerticesSPD.C:25
 DoVerticesSPD.C:26
 DoVerticesSPD.C:27
 DoVerticesSPD.C:28
 DoVerticesSPD.C:29
 DoVerticesSPD.C:30
 DoVerticesSPD.C:31
 DoVerticesSPD.C:32
 DoVerticesSPD.C:33
 DoVerticesSPD.C:34
 DoVerticesSPD.C:35
 DoVerticesSPD.C:36
 DoVerticesSPD.C:37
 DoVerticesSPD.C:38
 DoVerticesSPD.C:39
 DoVerticesSPD.C:40
 DoVerticesSPD.C:41
 DoVerticesSPD.C:42
 DoVerticesSPD.C:43
 DoVerticesSPD.C:44
 DoVerticesSPD.C:45
 DoVerticesSPD.C:46
 DoVerticesSPD.C:47
 DoVerticesSPD.C:48
 DoVerticesSPD.C:49
 DoVerticesSPD.C:50
 DoVerticesSPD.C:51
 DoVerticesSPD.C:52
 DoVerticesSPD.C:53
 DoVerticesSPD.C:54
 DoVerticesSPD.C:55
 DoVerticesSPD.C:56
 DoVerticesSPD.C:57
 DoVerticesSPD.C:58
 DoVerticesSPD.C:59
 DoVerticesSPD.C:60
 DoVerticesSPD.C:61
 DoVerticesSPD.C:62
 DoVerticesSPD.C:63
 DoVerticesSPD.C:64
 DoVerticesSPD.C:65
 DoVerticesSPD.C:66
 DoVerticesSPD.C:67
 DoVerticesSPD.C:68
 DoVerticesSPD.C:69
 DoVerticesSPD.C:70
 DoVerticesSPD.C:71
 DoVerticesSPD.C:72
 DoVerticesSPD.C:73
 DoVerticesSPD.C:74
 DoVerticesSPD.C:75
 DoVerticesSPD.C:76
 DoVerticesSPD.C:77
 DoVerticesSPD.C:78
 DoVerticesSPD.C:79
 DoVerticesSPD.C:80
 DoVerticesSPD.C:81
 DoVerticesSPD.C:82
 DoVerticesSPD.C:83
 DoVerticesSPD.C:84
 DoVerticesSPD.C:85
 DoVerticesSPD.C:86
 DoVerticesSPD.C:87
 DoVerticesSPD.C:88
 DoVerticesSPD.C:89
 DoVerticesSPD.C:90
 DoVerticesSPD.C:91
 DoVerticesSPD.C:92
 DoVerticesSPD.C:93
 DoVerticesSPD.C:94
 DoVerticesSPD.C:95
 DoVerticesSPD.C:96
 DoVerticesSPD.C:97
 DoVerticesSPD.C:98
 DoVerticesSPD.C:99
 DoVerticesSPD.C:100
 DoVerticesSPD.C:101
 DoVerticesSPD.C:102
 DoVerticesSPD.C:103
 DoVerticesSPD.C:104
 DoVerticesSPD.C:105
 DoVerticesSPD.C:106
 DoVerticesSPD.C:107
 DoVerticesSPD.C:108
 DoVerticesSPD.C:109
 DoVerticesSPD.C:110
 DoVerticesSPD.C:111
 DoVerticesSPD.C:112
 DoVerticesSPD.C:113
 DoVerticesSPD.C:114
 DoVerticesSPD.C:115
 DoVerticesSPD.C:116
 DoVerticesSPD.C:117
 DoVerticesSPD.C:118
 DoVerticesSPD.C:119
 DoVerticesSPD.C:120
 DoVerticesSPD.C:121
 DoVerticesSPD.C:122
 DoVerticesSPD.C:123
 DoVerticesSPD.C:124
 DoVerticesSPD.C:125
 DoVerticesSPD.C:126
 DoVerticesSPD.C:127
 DoVerticesSPD.C:128
 DoVerticesSPD.C:129
 DoVerticesSPD.C:130
 DoVerticesSPD.C:131
 DoVerticesSPD.C:132
 DoVerticesSPD.C:133
 DoVerticesSPD.C:134
 DoVerticesSPD.C:135
 DoVerticesSPD.C:136
 DoVerticesSPD.C:137
 DoVerticesSPD.C:138
 DoVerticesSPD.C:139
 DoVerticesSPD.C:140
 DoVerticesSPD.C:141
 DoVerticesSPD.C:142
 DoVerticesSPD.C:143
 DoVerticesSPD.C:144
 DoVerticesSPD.C:145
 DoVerticesSPD.C:146
 DoVerticesSPD.C:147
 DoVerticesSPD.C:148
 DoVerticesSPD.C:149
 DoVerticesSPD.C:150
 DoVerticesSPD.C:151
 DoVerticesSPD.C:152
 DoVerticesSPD.C:153
 DoVerticesSPD.C:154
 DoVerticesSPD.C:155
 DoVerticesSPD.C:156
 DoVerticesSPD.C:157
 DoVerticesSPD.C:158
 DoVerticesSPD.C:159
 DoVerticesSPD.C:160
 DoVerticesSPD.C:161
 DoVerticesSPD.C:162
 DoVerticesSPD.C:163
 DoVerticesSPD.C:164
 DoVerticesSPD.C:165
 DoVerticesSPD.C:166
 DoVerticesSPD.C:167
 DoVerticesSPD.C:168
 DoVerticesSPD.C:169
 DoVerticesSPD.C:170
 DoVerticesSPD.C:171
 DoVerticesSPD.C:172
 DoVerticesSPD.C:173
 DoVerticesSPD.C:174
 DoVerticesSPD.C:175
 DoVerticesSPD.C:176
 DoVerticesSPD.C:177
 DoVerticesSPD.C:178
 DoVerticesSPD.C:179
 DoVerticesSPD.C:180
 DoVerticesSPD.C:181
 DoVerticesSPD.C:182
 DoVerticesSPD.C:183
 DoVerticesSPD.C:184
 DoVerticesSPD.C:185
 DoVerticesSPD.C:186
 DoVerticesSPD.C:187
 DoVerticesSPD.C:188
 DoVerticesSPD.C:189
 DoVerticesSPD.C:190
 DoVerticesSPD.C:191
 DoVerticesSPD.C:192
 DoVerticesSPD.C:193
 DoVerticesSPD.C:194
 DoVerticesSPD.C:195
 DoVerticesSPD.C:196
 DoVerticesSPD.C:197
 DoVerticesSPD.C:198
 DoVerticesSPD.C:199
 DoVerticesSPD.C:200
 DoVerticesSPD.C:201
 DoVerticesSPD.C:202
 DoVerticesSPD.C:203
 DoVerticesSPD.C:204
 DoVerticesSPD.C:205
 DoVerticesSPD.C:206
 DoVerticesSPD.C:207
 DoVerticesSPD.C:208
 DoVerticesSPD.C:209
 DoVerticesSPD.C:210
 DoVerticesSPD.C:211
 DoVerticesSPD.C:212
 DoVerticesSPD.C:213
 DoVerticesSPD.C:214
 DoVerticesSPD.C:215
 DoVerticesSPD.C:216
 DoVerticesSPD.C:217
 DoVerticesSPD.C:218
 DoVerticesSPD.C:219
 DoVerticesSPD.C:220
 DoVerticesSPD.C:221
 DoVerticesSPD.C:222
 DoVerticesSPD.C:223
 DoVerticesSPD.C:224
 DoVerticesSPD.C:225
 DoVerticesSPD.C:226
 DoVerticesSPD.C:227
 DoVerticesSPD.C:228
 DoVerticesSPD.C:229
 DoVerticesSPD.C:230
 DoVerticesSPD.C:231
 DoVerticesSPD.C:232
 DoVerticesSPD.C:233
 DoVerticesSPD.C:234
 DoVerticesSPD.C:235
 DoVerticesSPD.C:236
 DoVerticesSPD.C:237
 DoVerticesSPD.C:238
 DoVerticesSPD.C:239
 DoVerticesSPD.C:240
 DoVerticesSPD.C:241
 DoVerticesSPD.C:242
 DoVerticesSPD.C:243
 DoVerticesSPD.C:244
 DoVerticesSPD.C:245
 DoVerticesSPD.C:246
 DoVerticesSPD.C:247
 DoVerticesSPD.C:248
 DoVerticesSPD.C:249
 DoVerticesSPD.C:250
 DoVerticesSPD.C:251
 DoVerticesSPD.C:252
 DoVerticesSPD.C:253
 DoVerticesSPD.C:254
 DoVerticesSPD.C:255
 DoVerticesSPD.C:256
 DoVerticesSPD.C:257
 DoVerticesSPD.C:258
 DoVerticesSPD.C:259
 DoVerticesSPD.C:260
 DoVerticesSPD.C:261
 DoVerticesSPD.C:262
 DoVerticesSPD.C:263
 DoVerticesSPD.C:264
 DoVerticesSPD.C:265
 DoVerticesSPD.C:266
 DoVerticesSPD.C:267
 DoVerticesSPD.C:268
 DoVerticesSPD.C:269
 DoVerticesSPD.C:270
 DoVerticesSPD.C:271
 DoVerticesSPD.C:272
 DoVerticesSPD.C:273
 DoVerticesSPD.C:274
 DoVerticesSPD.C:275
 DoVerticesSPD.C:276
 DoVerticesSPD.C:277