ROOT logo
/* $Id$ */
//-------------------------------------------------
enum anaModes {mLocal, mGRID};
//mLocal    = 0: Analyze locally files in your computer
//mGRID     = 3: Analyze files on GRID

//---------------------------------------------------------------------------
//Settings to read locally several files, only for "mLocal" mode
//The different values are default, they can be set with environmental 
//variables: INDIR, PATTERN, NFILES, respectively
char * kInDir = "/Users/ymao/group/ana/7TeV/corr";
char * kPattern = ""; // Data are in files kInDir/kPattern+i
Int_t kFile = 1; // Number of files
//---------------------------------------------------------------------------
//Collection file for grid analysis
char * kXML = "collection.xml";

//---------------------------------------------------------------------------

const Bool_t kMC = kFALSE; //With real data kMC = kFALSE
TString kInputData = "ESD";//ESD, AOD, MC
TString kTreeName ;
const TString calorimeter = "EMCAL" ;
const Bool_t kUsePAR = kFALSE; //set to kFALSE for libraries
//const Bool_t kUsePAR = kTRUE; //set to kFALSE for libraries
const Bool_t kDoESDFilter = kFALSE;  //filter the tracks from the esd

Int_t mode = mGRID;

void anaM()
{
  // Main
  //--------------------------------------------------------------------
  // Load analysis libraries
  // Look at the method below, 
  // change whatever you need for your analysis case
  // ------------------------------------------------------------------
  LoadLibraries() ;
  
  //-------------------------------------------------------------------------------------------------
  //Create chain from ESD and from cross sections files, look below for options.
  //------------------------------------------------------------------------------------------------- 
  if(kInputData == "ESD") kTreeName = "esdTree" ;
  else if(kInputData == "AOD") kTreeName = "aodTree" ;
  else if (kInputData == "MC") kTreeName = "TE" ;
  else {
    cout<<"Wrong  data type "<<kInputData<<endl;
    break;
  }
  
  TChain * chain   = new TChain(kTreeName) ;
  
	CreateChain(mode, chain);//, chainxs);  
  cout<<"Chain created"<<endl;
  
  if( chain ){
    AliLog::SetGlobalLogLevel(AliLog::kError);//Minimum prints on screen
    
    //--------------------------------------
    // Make the analysis manager
    //-------------------------------------
    AliAnalysisManager *mgr  = new AliAnalysisManager("Manager", "Manager");
    // MC handler
    if( (kMC && (kInputData == "ESD")) || kInputData == "MC"){
      AliMCEventHandler* mcHandler = new AliMCEventHandler();
      mcHandler->SetReadTR(kFALSE);//Do not search TrackRef file
      mgr->SetMCtruthEventHandler(mcHandler);
      if( kInputData == "MC") mgr->SetInputEventHandler(NULL);
    }
    
//    // AOD output handler
//    AliAODHandler* aodoutHandler   = new AliAODHandler();
//    aodoutHandler->SetOutputFileName("AliAOD.root");
//    mgr->SetOutputEventHandler(aodoutHandler);
    
    //input
    Int_t maxiterations = 1;
    AliEventPoolLoop* pool = new AliEventPoolLoop(maxiterations);
    pool->SetChain(chain);
    Int_t eventsInPool = 10;
    AliMultiEventInputHandler *inpHandler = NULL ; 
    if(kInputData == "ESD"){
      // ESD handler
      printf("ESD MultiInput \n");
      inpHandler = new AliMultiEventInputHandler(eventsInPool, 0);
    }
    if(kInputData == "AOD"){
      // AOD handler
      inpHandler = new AliMultiEventInputHandler(eventsInPool, 1);	   	  
    }
    mgr->SetInputEventHandler(inpHandler);
    cout<<"Input handler "<<mgr->GetInputEventHandler()<<endl;
    mgr->SetEventPool(pool);
    inpHandler->SetEventPool(pool);
    
    //mgr->SetDebugLevel(-1); // For debugging, do not uncomment if you want no messages.
    
    // select triigger events for physics run
    
//    if(!kMC){
//      gROOT->LoadMacro("AddTaskPhysicsSelection.C");
//      AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection();
//      mgr->AddTask(physSelTask);    
//    }
    
    //-------------------------------------------------------------------------
    //Define task, put here any other task that you want to use.
    //-------------------------------------------------------------------------
    
    //correlation analysis
    gROOT->LoadMacro("AddTaskCaloTrackCorrM.C");
    
    AliAnalysisTaskCaloTrackCorrelationM *taskEMCAL = AddTaskCaloTrackCorrM(kInputData,"EMCAL",kFALSE);

    mgr->AddTask(taskEMCAL);
    
    AliAnalysisTaskCaloTrackCorrelationM *taskPHOS  = AddTaskCaloTrackCorrM(kInputData,"PHOS", kFALSE);

    mgr->AddTask(taskPHOS);
    
    //gROOT->LoadMacro("AddTaskChargeCorr.C");
    AliAnalysisTaskCaloTrackCorrelationM *taskCharge  = AddTaskCaloTrackCorrM(kInputData, "CTS",kFALSE);
//    if(!kMC)
//      taskCharge->SelectCollisionCandidates();
    mgr->AddTask(taskCharge);
    
     //-----------------------
    // Run the analysis
    //-----------------------    
    //mgr->ResetAnalysis();
    mgr->InitAnalysis();
    mgr->PrintStatus();
    mgr->StartAnalysis("mix",chain);
    
    cout <<" Analysis ended sucessfully "<< endl ;
  }
  else cout << "Chain was not produced ! "<<endl;
  
}

void  LoadLibraries() {
  //--------------------------------------
  // Load the needed libraries most of them already loaded by aliroot
  //--------------------------------------
  gSystem->Load("libTree.so");
  gSystem->Load("libGeom.so");
  gSystem->Load("libVMC.so");
  gSystem->Load("libXMLIO.so");
  if(kUsePAR){
    //--------------------------------------------------------
    //If you want to use root and par files from aliroot
    //--------------------------------------------------------  
    SetupPar("STEERBase");
    SetupPar("ESD");
    SetupPar("AOD");
    SetupPar("ANALYSIS");
    SetupPar("ANALYSISalice");
    SetupPar("PHOSUtils");
    SetupPar("EMCALUtils");
    
    SetupPar("PWGCaloTrackCorrBase");
    SetupPar("PWGGACaloTrackCorrelations");
  }
  else{
    //--------------------------------------------------------
    // If you want to use already compiled libraries 
    // in the aliroot distribution
    //--------------------------------------------------------
    gSystem->Load("libSTEERBase");
    gSystem->Load("libESD");
    gSystem->Load("libAOD");
    gSystem->Load("libANALYSIS");
    gSystem->Load("libANALYSISalice");
	  gSystem->Load("libPHOSUtils");
	  gSystem->Load("libEMCALUtils");
    gSystem->Load("libPWGCaloTrackCorrBase");
    gSystem->Load("libPWGGACaloTrackCorrelations");
  }
  
}

void SetupPar(char* pararchivename)
{
  //Load par files, create analysis libraries
  //For testing, if par file already decompressed and modified
  //classes then do not decompress.
  
  TString cdir(Form("%s", gSystem->WorkingDirectory() )) ; 
  TString parpar(Form("%s.par", pararchivename)) ; 
  if ( gSystem->AccessPathName(pararchivename) ) {  
    TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
    gROOT->ProcessLine(processline.Data());
  }
  
  TString ocwd = gSystem->WorkingDirectory();
  gSystem->ChangeDirectory(pararchivename);
  
  // check for BUILD.sh and execute
  if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
    printf("*******************************\n");
    printf("*** Building PAR archive    ***\n");
    cout<<pararchivename<<endl;
    printf("*******************************\n");
    
    if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
      Error("runProcess","Cannot Build the PAR Archive! - Abort!");
      return -1;
    }
  }
  // check for SETUP.C and execute
  if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
    printf("*******************************\n");
    printf("*** Setup PAR archive       ***\n");
    cout<<pararchivename<<endl;
    printf("*******************************\n");
    gROOT->Macro("PROOF-INF/SETUP.C");
  }
  
  gSystem->ChangeDirectory(ocwd.Data());
  printf("Current dir: %s\n", ocwd.Data());
}



void CreateChain(const anaModes mode, TChain * chain){//, TChain * chainxs){
  //Fills chain with data
  
  TString datafileName="";
  if(kInputData == "ESD") datafileName = "AliESDs.root" ;
  else if(kInputData == "AOD") datafileName = "AliAOD.root" ;
  else if(kInputData == "MC")  datafileName = "galice.root" ;
  
  TString ocwd = gSystem->WorkingDirectory();
  
  //---------------------------------------
  //Local files analysis
  //---------------------------------------
  if(mode == mLocal){
    //If you want to add several ESD files sitting in a common directory INDIR
    //Specify as environmental variables the directory (INDIR), the number of files 
    //to analyze (NFILES) and the pattern name of the directories with files (PATTERN)
    
    cout<<"INDIR : "<<kInDir<<endl;
    cout<<"NFILES : "<<kFile<<endl;
    cout<<"PATTERN: " <<kPattern<<endl;
    
    
    //Loop on ESD files, add them to chain
    TString FileName ;      
    for (Int_t iFile = 0 ; iFile < kFile ; iFile++) {
      FileName = Form("%s/%s%d/%s", kInDir,kPattern,iFile,datafileName.Data()) ; 
      //cout << "FileName: " << FileName <<endl ;
      TFile * dataFile = 0 ; 
      //Check if file exists and add it, if not skip it
      if ( dataFile = TFile::Open(FileName.Data())) {
        if ( dataFile->Get(kTreeName) ) { 
          Int_t nEventsPerFile = ((TTree*) dataFile->Get(kTreeName)) ->GetEntries();
          printf(" ++++ Adding %s, with %d events \n", FileName.Data(), nEventsPerFile) ;
          chain->AddFile(FileName);
        }
      }
    }    
    printf("number of entries # %lld \n", chain->GetEntries()) ; 	
  }// local files analysis
  
  //------------------------------
  //GRID xml files
  //-----------------------------
  else if(mode == mGRID){
    //Load necessary libraries and connect to the GRID
    gSystem->Load("libNetx.so") ; 
    gSystem->Load("libRAliEn.so"); 
    TGrid::Connect("alien://") ;
    
    //Feed Grid with collection file
    //TGridCollection * collection =  (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
    TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
    if (! collection) {
      AliError(Form("%s not found", kXML)) ; 
      return kFALSE ; 
    }
    TGridResult* result = collection->GetGridResult("",0 ,0);
    
    // Makes the ESD chain 
    printf("*** Getting the Chain       ***\n");
    Int_t nEventsPerFile = 0;
    for (Int_t index = 0; index < result->GetEntries(); index++) {
      TString alienURL = result->GetKey(index, "turl") ; 
      cout << "================== " << alienURL << endl ; 
      chain->Add(alienURL) ; 
      
    }
  }// xml analysis
  
  gSystem->ChangeDirectory(ocwd.Data());
}
 anaM.C:1
 anaM.C:2
 anaM.C:3
 anaM.C:4
 anaM.C:5
 anaM.C:6
 anaM.C:7
 anaM.C:8
 anaM.C:9
 anaM.C:10
 anaM.C:11
 anaM.C:12
 anaM.C:13
 anaM.C:14
 anaM.C:15
 anaM.C:16
 anaM.C:17
 anaM.C:18
 anaM.C:19
 anaM.C:20
 anaM.C:21
 anaM.C:22
 anaM.C:23
 anaM.C:24
 anaM.C:25
 anaM.C:26
 anaM.C:27
 anaM.C:28
 anaM.C:29
 anaM.C:30
 anaM.C:31
 anaM.C:32
 anaM.C:33
 anaM.C:34
 anaM.C:35
 anaM.C:36
 anaM.C:37
 anaM.C:38
 anaM.C:39
 anaM.C:40
 anaM.C:41
 anaM.C:42
 anaM.C:43
 anaM.C:44
 anaM.C:45
 anaM.C:46
 anaM.C:47
 anaM.C:48
 anaM.C:49
 anaM.C:50
 anaM.C:51
 anaM.C:52
 anaM.C:53
 anaM.C:54
 anaM.C:55
 anaM.C:56
 anaM.C:57
 anaM.C:58
 anaM.C:59
 anaM.C:60
 anaM.C:61
 anaM.C:62
 anaM.C:63
 anaM.C:64
 anaM.C:65
 anaM.C:66
 anaM.C:67
 anaM.C:68
 anaM.C:69
 anaM.C:70
 anaM.C:71
 anaM.C:72
 anaM.C:73
 anaM.C:74
 anaM.C:75
 anaM.C:76
 anaM.C:77
 anaM.C:78
 anaM.C:79
 anaM.C:80
 anaM.C:81
 anaM.C:82
 anaM.C:83
 anaM.C:84
 anaM.C:85
 anaM.C:86
 anaM.C:87
 anaM.C:88
 anaM.C:89
 anaM.C:90
 anaM.C:91
 anaM.C:92
 anaM.C:93
 anaM.C:94
 anaM.C:95
 anaM.C:96
 anaM.C:97
 anaM.C:98
 anaM.C:99
 anaM.C:100
 anaM.C:101
 anaM.C:102
 anaM.C:103
 anaM.C:104
 anaM.C:105
 anaM.C:106
 anaM.C:107
 anaM.C:108
 anaM.C:109
 anaM.C:110
 anaM.C:111
 anaM.C:112
 anaM.C:113
 anaM.C:114
 anaM.C:115
 anaM.C:116
 anaM.C:117
 anaM.C:118
 anaM.C:119
 anaM.C:120
 anaM.C:121
 anaM.C:122
 anaM.C:123
 anaM.C:124
 anaM.C:125
 anaM.C:126
 anaM.C:127
 anaM.C:128
 anaM.C:129
 anaM.C:130
 anaM.C:131
 anaM.C:132
 anaM.C:133
 anaM.C:134
 anaM.C:135
 anaM.C:136
 anaM.C:137
 anaM.C:138
 anaM.C:139
 anaM.C:140
 anaM.C:141
 anaM.C:142
 anaM.C:143
 anaM.C:144
 anaM.C:145
 anaM.C:146
 anaM.C:147
 anaM.C:148
 anaM.C:149
 anaM.C:150
 anaM.C:151
 anaM.C:152
 anaM.C:153
 anaM.C:154
 anaM.C:155
 anaM.C:156
 anaM.C:157
 anaM.C:158
 anaM.C:159
 anaM.C:160
 anaM.C:161
 anaM.C:162
 anaM.C:163
 anaM.C:164
 anaM.C:165
 anaM.C:166
 anaM.C:167
 anaM.C:168
 anaM.C:169
 anaM.C:170
 anaM.C:171
 anaM.C:172
 anaM.C:173
 anaM.C:174
 anaM.C:175
 anaM.C:176
 anaM.C:177
 anaM.C:178
 anaM.C:179
 anaM.C:180
 anaM.C:181
 anaM.C:182
 anaM.C:183
 anaM.C:184
 anaM.C:185
 anaM.C:186
 anaM.C:187
 anaM.C:188
 anaM.C:189
 anaM.C:190
 anaM.C:191
 anaM.C:192
 anaM.C:193
 anaM.C:194
 anaM.C:195
 anaM.C:196
 anaM.C:197
 anaM.C:198
 anaM.C:199
 anaM.C:200
 anaM.C:201
 anaM.C:202
 anaM.C:203
 anaM.C:204
 anaM.C:205
 anaM.C:206
 anaM.C:207
 anaM.C:208
 anaM.C:209
 anaM.C:210
 anaM.C:211
 anaM.C:212
 anaM.C:213
 anaM.C:214
 anaM.C:215
 anaM.C:216
 anaM.C:217
 anaM.C:218
 anaM.C:219
 anaM.C:220
 anaM.C:221
 anaM.C:222
 anaM.C:223
 anaM.C:224
 anaM.C:225
 anaM.C:226
 anaM.C:227
 anaM.C:228
 anaM.C:229
 anaM.C:230
 anaM.C:231
 anaM.C:232
 anaM.C:233
 anaM.C:234
 anaM.C:235
 anaM.C:236
 anaM.C:237
 anaM.C:238
 anaM.C:239
 anaM.C:240
 anaM.C:241
 anaM.C:242
 anaM.C:243
 anaM.C:244
 anaM.C:245
 anaM.C:246
 anaM.C:247
 anaM.C:248
 anaM.C:249
 anaM.C:250
 anaM.C:251
 anaM.C:252
 anaM.C:253
 anaM.C:254
 anaM.C:255
 anaM.C:256
 anaM.C:257
 anaM.C:258
 anaM.C:259
 anaM.C:260
 anaM.C:261
 anaM.C:262
 anaM.C:263
 anaM.C:264
 anaM.C:265
 anaM.C:266
 anaM.C:267
 anaM.C:268
 anaM.C:269
 anaM.C:270
 anaM.C:271
 anaM.C:272
 anaM.C:273
 anaM.C:274
 anaM.C:275
 anaM.C:276
 anaM.C:277
 anaM.C:278
 anaM.C:279
 anaM.C:280
 anaM.C:281
 anaM.C:282
 anaM.C:283
 anaM.C:284
 anaM.C:285
 anaM.C:286
 anaM.C:287
 anaM.C:288
 anaM.C:289
 anaM.C:290
 anaM.C:291
 anaM.C:292
 anaM.C:293
 anaM.C:294
 anaM.C:295
 anaM.C:296
 anaM.C:297