ROOT logo
// #include <iostream>
// #include "AliAnalysisManager.h"
// #include "AliESDInputHandler.h"
// #include "AliMCEventHandler.h"
// #include "AliAnalysisGrid.h"
// #include "AliCentralitySelectionTask.h"
// #include "AliAnalysisCentralitySelector.h"
// #include "AliAnalysisTaskLK0Spectra.h"
// #include "TString.h"
// #include "TChain.h"
// #include "TAlienCollection.h"
// #include <fstream>
// #include "TObjString.h"
// #include "TIterator.h"
// #include "TGrid.h"
// #include "TROOT.h"

// #include "CreateAlienHandler.C"
// #include 

using namespace std;

enum { kMyRunModeLocal = 0, kMyRunModeCAF, kMyRunModeGRID};

TList * listToLoad = new TList(); // Additional classes to be loaded, see InitAndLoadLibs

TChain * GetAnalysisChain(const char * incollection);
void InitAndLoadLibs(Int_t runMode=kMyRunModeLocal, Int_t workers=0,Bool_t debug=0) ;

void runLK0Spectra(const char * data, const char * passOrPath, Long64_t nev = -1, Long64_t offset = 0, Bool_t debug = kFALSE, Int_t runMode = 2, Bool_t isMC = 0, Bool_t usePID = kTRUE, const char* option = "",TString customSuffix = "", Int_t workers = -1, const char * gridMode="full", Int_t binMin=0, Int_t binMax = 10)
{
  // runMode:
  //
  // 0 local 
  // 1 proof
  // 2 grid

  if (nev < 0)
    nev = 1234567890;
  InitAndLoadLibs(runMode,workers,debug);

  // Create the analysis manager
  AliAnalysisManager * mgr = new AliAnalysisManager;

  // Add ESD handler
  AliESDInputHandler* esdH = new AliESDInputHandler;
  // Do I need any of this? 
  // esdH->SetInactiveBranches("AliESDACORDE FMD ALIESDTZERO ALIESDZDC AliRawDataErrorLogs CaloClusters Cascades EMCALCells EMCALTrigger ESDfriend Kinks AliESDTZERO ALIESDACORDE MuonTracks TrdTracks");
  mgr->SetInputEventHandler(esdH);

  if(isMC) {
    AliMCEventHandler* handler = new AliMCEventHandler;
    handler->SetPreReadMode(AliMCEventHandler::kLmPreRead);
    mgr->SetMCtruthEventHandler(handler);
  }


  // If we are running on grid, we need the alien handler
  if (runMode == kMyRunModeGRID) {
    // Create and configure the alien handler plugin
    TGrid::Connect("alien://");// Why do I need this? Without a get a bus error...
        gROOT->LoadMacro("CreateAlienHandler.C");
    AliAnalysisGrid *alienHandler = CreateAlienHandler(data, listToLoad, gridMode, isMC);  
    if (!alienHandler) {
      cout << "Cannot create alien handler" << endl;    
      exit(1);
    }
    mgr->SetGridHandler(alienHandler);  
  }
  
  // PID task
  gROOT->ProcessLine(".L $ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
  AddTaskPIDResponse(isMC,kTRUE);
  //AddTaskPIDResponse();


  // Physics selection
  gROOT->ProcessLine(".L $ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
  AliPhysicsSelectionTask * physicsSelectionTask = AddTaskPhysicsSelection(isMC,kTRUE,0);

  // Centrality
  gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
  AliCentralitySelectionTask *taskCentrality = AddTaskCentrality();
  //taskCentrality->SetPass(2);
  if(isMC) taskCentrality->SetMCInput();

// Parse option strings
  TString optionStr(option);
  
  // remove SAVE option if set
  Bool_t doSave = kFALSE;

  if (optionStr.Contains("SAVE"))
    {
      optionStr = optionStr(0,optionStr.Index("SAVE")) + optionStr(optionStr.Index("SAVE")+4, optionStr.Length());
      doSave = kTRUE;
    }
  TString pathsuffix = "";
  // Not used, but may be useful
  Bool_t useMCKinematics = isMC;
  if (optionStr.Contains("NOMCKIN")) {
    cout << ">>>> Ignoring MC kinematics" << endl;
    useMCKinematics=kFALSE;
    pathsuffix+="_NOMCKIN";
  }
  
  Int_t iCentralityBin = 0;
  gROOT->ProcessLine(".L AddTaskLK0Spectra.C");
  iCentralityBin = 0;
  AddTaskLK0Spectra("lambdak0.root", iCentralityBin,isMC, usePID, kFALSE); // FIXME also pass cuts, centrality bin type selection(5,10% percentiles, ranges...)
  iCentralityBin = 10;
  AddTaskLK0Spectra("lambdak0.root", iCentralityBin,isMC, usePID, kFALSE); // FIXME also pass cuts, centrality bin type selection(5,10% percentiles, ranges...)

  

  // Init and run the analysis
  if (!mgr->InitAnalysis()) return;

  mgr->PrintStatus();
  
  if (runMode == kMyRunModeLocal ) {
    // If running in local mode, create chain of ESD files
    cout << "RUNNING LOCAL, CHAIN" << endl;    
    TChain * chain = GetAnalysisChain(data);
    //    chain->Print();
    mgr->StartAnalysis("local",chain,nev);
  } else if (runMode == kMyRunModeCAF) {
    mgr->StartAnalysis("proof",TString(passOrPath)+data+"#esdTree",nev);
  } else if (runMode == kMyRunModeGRID) {
    mgr->StartAnalysis("grid");
  } else {
    cout << "ERROR: unknown run mode" << endl;        
  }

  pathsuffix += customSuffix;

  //if (doSave) MoveOutput(data, pathsuffix.Data());

  
}

void MoveOutput(const char * data, const char * suffix = ""){

  //  TString path("output10bins/");
  TString path("output10binsNew/");
  path = path + TString(data).Tokenize("/")->Last()->GetName() + suffix;
  
  TString fileName = "lambdak0.root";
  gSystem->mkdir(path, kTRUE);
  gSystem->Rename(fileName, path + "/" + fileName);
  for(Int_t iCentralityBin = 0; iCentralityBin < 20; iCentralityBin++){
    TString fileBin = fileName;
    fileBin.ReplaceAll(".root",Form("_%2.2d.root",iCentralityBin));
    gSystem->Rename(fileBin, path + "/" + fileBin);    
  }
  
  gSystem->Rename("event_stat.root", path + "/event_stat.root");      
  gSystem->Rename("EventStat_temp.root", path + "/EventStat_temp.root");      
  Printf(">>>>> Moved files to %s", path.Data());
}  



TChain * GetAnalysisChain(const char * incollection){
  // Builds a chain of esd files
  // incollection can be
  // - a single root file
  // - an xml collection of files on alien
  // - a ASCII containing a list of local root files

  TChain* analysisChain = 0;
  // chain
  analysisChain = new TChain("esdTree");
  if (TString(incollection).Contains(".root")){
    analysisChain->Add(incollection);
  }
  else if (TString(incollection).Contains("xml")){
    TGrid::Connect("alien://");
    TGridCollection * coll = TAlienCollection::Open (incollection);
    while(coll->Next()){
      analysisChain->Add(TString("alien://")+coll->GetLFN());
    }
  } else {
    ifstream file_collect(incollection);
    TString line;
    while (line.ReadLine(file_collect) ) {
      analysisChain->Add(line.Data());
    }
  }
  analysisChain->GetListOfFiles()->Print();

  return analysisChain;
}


void InitAndLoadLibs(Int_t runMode, Int_t workers,Bool_t debug) {
  // Loads libs and par files + custom task and classes (the order is important)
  // listToLoad->Add(new TObjString("$ALICE_ROOT/STEER/AliCentrality.cxx")); // FIXME: why do I have to load it?!?
  listToLoad->Add(new TObjString("AliAnalysisCentralitySelector.cxx"));
  listToLoad->Add(new TObjString("AliAnalysisTaskLK0Spectra.cxx"));

  if (runMode == kMyRunModeCAF)
    {
      cout << "Init in CAF mode" << endl;
    
      gEnv->SetValue("XSec.GSI.DelegProxy", "2");
      Char_t* alienuser = gSystem->Getenv("alien_API_USER");
      TProof * p = TProof::Open(alienuser!=0 ? Form("%s@alice-caf.cern.ch",alienuser) : "alice-caf.cern.ch", workers>0 ? Form("workers=%d",workers) : "");
      //TProof * p = TProof::Open("skaf.saske.sk", workers>0 ? Form("workers=%d",workers) : "");    
      p->Exec("TObject *o = gEnv->GetTable()->FindObject(\"Proof.UseMergers\"); gEnv->GetTable()->Remove(o);", kTRUE); // avoid submerging
      //gProof->EnablePackage("VO_ALICE@AliRoot::v4-21-18-AN");
      //gProof->EnablePackage("VO_ALICE@AliRoot::v4-21-20-AN");
      //gProof->EnablePackage("VO_ALICE@AliRoot::v4-21-21-AN");
      //gProof->EnablePackage("VO_ALICE@AliRoot::v4-21-28-AN");
      //gProof->EnablePackage("VO_ALICE@AliRoot::v4-21-31-AN");
      //      gProof->EnablePackage("VO_ALICE@AliRoot::v4-21-33-AN");
      //      gProof->EnablePackage("VO_ALICE@AliRoot::v5-02-04-AN");
      //gProof->EnablePackage("VO_ALICE@AliRoot::v5-02-08pA-AN");
	gProof->EnablePackage("VO_ALICE@AliRoot::v5-03-01-AN");

      // Enable the needed package
      // FIXME: what if I don't want to use par files?
      gSystem->AddIncludePath("-I${ALICE_ROOT}/include/");
      gSystem->AddIncludePath("-I${ALICE_ROOT}/STEER/");
      // gProof->UploadPackage("$ALICE_ROOT/obj/STEERBase");
      // gProof->EnablePackage("$ALICE_ROOT/obj/STEERBase");
      // gProof->UploadPackage("$ALICE_ROOT/obj/ESD");
      // gProof->EnablePackage("$ALICE_ROOT/obj/ESD");
      // gProof->UploadPackage("$ALICE_ROOT/obj/AOD");
      // gProof->EnablePackage("$ALICE_ROOT/obj/AOD");
      // gProof->UploadPackage("$ALICE_ROOT/obj/ANALYSIS");
      // gProof->EnablePackage("$ALICE_ROOT/obj/ANALYSIS");
      // gProof->UploadPackage("$ALICE_ROOT/obj/ANALYSISalice");
      // gProof->EnablePackage("$ALICE_ROOT/obj/ANALYSISalice");
      // gProof->UploadPackage("$ALICE_ROOT/obj/CORRFW");
      // gProof->EnablePackage("$ALICE_ROOT/obj/CORRFW");
      // gProof->UploadPackage("~/Desktop/OADB");//FIXME
      // gProof->EnablePackage("~/Desktop/OADB");//FIXME
      
    }
  else
    {
      cout << "Init in Local or Grid mode" << endl;
      gSystem->Load("libCore.so");  
      gSystem->Load("libTree.so");
      gSystem->Load("libGeom.so");
      gSystem->Load("libVMC.so");
      gSystem->Load("libPhysics.so");
      gSystem->Load("libSTEERBase");
      gSystem->Load("libESD");
      gSystem->Load("libAOD");
      gSystem->Load("libANALYSIS");
      gSystem->Load("libANALYSISalice");   

      // gSystem->Load("libVMC");
      gROOT->ProcessLine(".include $ALICE_ROOT/include");
      gROOT->ProcessLine(".include $ALICE_ROOT/STEER");
    }
  // Load helper classes
  TIterator * iter = listToLoad->MakeIterator();
  TObjString * name = 0;
  while ((name = (TObjString *)iter->Next())) {
    gSystem->ExpandPathName(name->String());
    cout << name->String().Data() << endl;
    if (runMode == kMyRunModeCAF) {
      gProof->Load(name->String()+(debug?"++g":"+"));   
    } else {
      gROOT->LoadMacro(name->String()+(debug?"++g":"+"));   
    }
  }

}
 runLK0Spectra.C:1
 runLK0Spectra.C:2
 runLK0Spectra.C:3
 runLK0Spectra.C:4
 runLK0Spectra.C:5
 runLK0Spectra.C:6
 runLK0Spectra.C:7
 runLK0Spectra.C:8
 runLK0Spectra.C:9
 runLK0Spectra.C:10
 runLK0Spectra.C:11
 runLK0Spectra.C:12
 runLK0Spectra.C:13
 runLK0Spectra.C:14
 runLK0Spectra.C:15
 runLK0Spectra.C:16
 runLK0Spectra.C:17
 runLK0Spectra.C:18
 runLK0Spectra.C:19
 runLK0Spectra.C:20
 runLK0Spectra.C:21
 runLK0Spectra.C:22
 runLK0Spectra.C:23
 runLK0Spectra.C:24
 runLK0Spectra.C:25
 runLK0Spectra.C:26
 runLK0Spectra.C:27
 runLK0Spectra.C:28
 runLK0Spectra.C:29
 runLK0Spectra.C:30
 runLK0Spectra.C:31
 runLK0Spectra.C:32
 runLK0Spectra.C:33
 runLK0Spectra.C:34
 runLK0Spectra.C:35
 runLK0Spectra.C:36
 runLK0Spectra.C:37
 runLK0Spectra.C:38
 runLK0Spectra.C:39
 runLK0Spectra.C:40
 runLK0Spectra.C:41
 runLK0Spectra.C:42
 runLK0Spectra.C:43
 runLK0Spectra.C:44
 runLK0Spectra.C:45
 runLK0Spectra.C:46
 runLK0Spectra.C:47
 runLK0Spectra.C:48
 runLK0Spectra.C:49
 runLK0Spectra.C:50
 runLK0Spectra.C:51
 runLK0Spectra.C:52
 runLK0Spectra.C:53
 runLK0Spectra.C:54
 runLK0Spectra.C:55
 runLK0Spectra.C:56
 runLK0Spectra.C:57
 runLK0Spectra.C:58
 runLK0Spectra.C:59
 runLK0Spectra.C:60
 runLK0Spectra.C:61
 runLK0Spectra.C:62
 runLK0Spectra.C:63
 runLK0Spectra.C:64
 runLK0Spectra.C:65
 runLK0Spectra.C:66
 runLK0Spectra.C:67
 runLK0Spectra.C:68
 runLK0Spectra.C:69
 runLK0Spectra.C:70
 runLK0Spectra.C:71
 runLK0Spectra.C:72
 runLK0Spectra.C:73
 runLK0Spectra.C:74
 runLK0Spectra.C:75
 runLK0Spectra.C:76
 runLK0Spectra.C:77
 runLK0Spectra.C:78
 runLK0Spectra.C:79
 runLK0Spectra.C:80
 runLK0Spectra.C:81
 runLK0Spectra.C:82
 runLK0Spectra.C:83
 runLK0Spectra.C:84
 runLK0Spectra.C:85
 runLK0Spectra.C:86
 runLK0Spectra.C:87
 runLK0Spectra.C:88
 runLK0Spectra.C:89
 runLK0Spectra.C:90
 runLK0Spectra.C:91
 runLK0Spectra.C:92
 runLK0Spectra.C:93
 runLK0Spectra.C:94
 runLK0Spectra.C:95
 runLK0Spectra.C:96
 runLK0Spectra.C:97
 runLK0Spectra.C:98
 runLK0Spectra.C:99
 runLK0Spectra.C:100
 runLK0Spectra.C:101
 runLK0Spectra.C:102
 runLK0Spectra.C:103
 runLK0Spectra.C:104
 runLK0Spectra.C:105
 runLK0Spectra.C:106
 runLK0Spectra.C:107
 runLK0Spectra.C:108
 runLK0Spectra.C:109
 runLK0Spectra.C:110
 runLK0Spectra.C:111
 runLK0Spectra.C:112
 runLK0Spectra.C:113
 runLK0Spectra.C:114
 runLK0Spectra.C:115
 runLK0Spectra.C:116
 runLK0Spectra.C:117
 runLK0Spectra.C:118
 runLK0Spectra.C:119
 runLK0Spectra.C:120
 runLK0Spectra.C:121
 runLK0Spectra.C:122
 runLK0Spectra.C:123
 runLK0Spectra.C:124
 runLK0Spectra.C:125
 runLK0Spectra.C:126
 runLK0Spectra.C:127
 runLK0Spectra.C:128
 runLK0Spectra.C:129
 runLK0Spectra.C:130
 runLK0Spectra.C:131
 runLK0Spectra.C:132
 runLK0Spectra.C:133
 runLK0Spectra.C:134
 runLK0Spectra.C:135
 runLK0Spectra.C:136
 runLK0Spectra.C:137
 runLK0Spectra.C:138
 runLK0Spectra.C:139
 runLK0Spectra.C:140
 runLK0Spectra.C:141
 runLK0Spectra.C:142
 runLK0Spectra.C:143
 runLK0Spectra.C:144
 runLK0Spectra.C:145
 runLK0Spectra.C:146
 runLK0Spectra.C:147
 runLK0Spectra.C:148
 runLK0Spectra.C:149
 runLK0Spectra.C:150
 runLK0Spectra.C:151
 runLK0Spectra.C:152
 runLK0Spectra.C:153
 runLK0Spectra.C:154
 runLK0Spectra.C:155
 runLK0Spectra.C:156
 runLK0Spectra.C:157
 runLK0Spectra.C:158
 runLK0Spectra.C:159
 runLK0Spectra.C:160
 runLK0Spectra.C:161
 runLK0Spectra.C:162
 runLK0Spectra.C:163
 runLK0Spectra.C:164
 runLK0Spectra.C:165
 runLK0Spectra.C:166
 runLK0Spectra.C:167
 runLK0Spectra.C:168
 runLK0Spectra.C:169
 runLK0Spectra.C:170
 runLK0Spectra.C:171
 runLK0Spectra.C:172
 runLK0Spectra.C:173
 runLK0Spectra.C:174
 runLK0Spectra.C:175
 runLK0Spectra.C:176
 runLK0Spectra.C:177
 runLK0Spectra.C:178
 runLK0Spectra.C:179
 runLK0Spectra.C:180
 runLK0Spectra.C:181
 runLK0Spectra.C:182
 runLK0Spectra.C:183
 runLK0Spectra.C:184
 runLK0Spectra.C:185
 runLK0Spectra.C:186
 runLK0Spectra.C:187
 runLK0Spectra.C:188
 runLK0Spectra.C:189
 runLK0Spectra.C:190
 runLK0Spectra.C:191
 runLK0Spectra.C:192
 runLK0Spectra.C:193
 runLK0Spectra.C:194
 runLK0Spectra.C:195
 runLK0Spectra.C:196
 runLK0Spectra.C:197
 runLK0Spectra.C:198
 runLK0Spectra.C:199
 runLK0Spectra.C:200
 runLK0Spectra.C:201
 runLK0Spectra.C:202
 runLK0Spectra.C:203
 runLK0Spectra.C:204
 runLK0Spectra.C:205
 runLK0Spectra.C:206
 runLK0Spectra.C:207
 runLK0Spectra.C:208
 runLK0Spectra.C:209
 runLK0Spectra.C:210
 runLK0Spectra.C:211
 runLK0Spectra.C:212
 runLK0Spectra.C:213
 runLK0Spectra.C:214
 runLK0Spectra.C:215
 runLK0Spectra.C:216
 runLK0Spectra.C:217
 runLK0Spectra.C:218
 runLK0Spectra.C:219
 runLK0Spectra.C:220
 runLK0Spectra.C:221
 runLK0Spectra.C:222
 runLK0Spectra.C:223
 runLK0Spectra.C:224
 runLK0Spectra.C:225
 runLK0Spectra.C:226
 runLK0Spectra.C:227
 runLK0Spectra.C:228
 runLK0Spectra.C:229
 runLK0Spectra.C:230
 runLK0Spectra.C:231
 runLK0Spectra.C:232
 runLK0Spectra.C:233
 runLK0Spectra.C:234
 runLK0Spectra.C:235
 runLK0Spectra.C:236
 runLK0Spectra.C:237
 runLK0Spectra.C:238
 runLK0Spectra.C:239
 runLK0Spectra.C:240
 runLK0Spectra.C:241
 runLK0Spectra.C:242
 runLK0Spectra.C:243
 runLK0Spectra.C:244
 runLK0Spectra.C:245
 runLK0Spectra.C:246
 runLK0Spectra.C:247
 runLK0Spectra.C:248
 runLK0Spectra.C:249
 runLK0Spectra.C:250
 runLK0Spectra.C:251
 runLK0Spectra.C:252
 runLK0Spectra.C:253
 runLK0Spectra.C:254
 runLK0Spectra.C:255
 runLK0Spectra.C:256
 runLK0Spectra.C:257
 runLK0Spectra.C:258
 runLK0Spectra.C:259
 runLK0Spectra.C:260
 runLK0Spectra.C:261
 runLK0Spectra.C:262
 runLK0Spectra.C:263
 runLK0Spectra.C:264
 runLK0Spectra.C:265
 runLK0Spectra.C:266
 runLK0Spectra.C:267
 runLK0Spectra.C:268
 runLK0Spectra.C:269
 runLK0Spectra.C:270
 runLK0Spectra.C:271
 runLK0Spectra.C:272
 runLK0Spectra.C:273