ROOT logo
#if !defined (__CINT__) || (defined(__MAKECINT__))
#include <iostream>
#include "AliAnalysisGrid.h"
#include "TSystem.h"
#include "TROOT.h"
#include "AliAnalysisManager.h"
#include "AliAnalysisGrid.h"
#include "AliVEventHandler.h"
#include "AliAODInputHandler.h"
#include "AliAnalysisAlien.h"
#include "AliMCEventHandler.h"
#include "AliPhysicsSelectionTask.h"
#include "TRegexp.h"
#include "AliTriggerAnalysis.h"
#include "TChain.h"
#include "AliAnalysisTaskPIDResponse.h"
#include "AliAODHandler.h"
#include "AliAnalysisTaskNanoAODFilter.h"
#include "AliESEHelpers.h"

#endif
void LoadLibs();

class AliAnalysisGrid;
class AliESETrkCut;
class AliESEEvtCut;

AliESETrkCut * TrkCuts() {

  AliESETrkCut * trk = new AliESETrkCut;

  AliSpectraAODTrackCuts  * trcuts = new AliSpectraAODTrackCuts("TrackCuts");  
  trcuts->SetDCA(100000);
  trcuts->SetTrackBits(1);
  trcuts->SetPt(50);
  trcuts->SetPtTOFMatching(0.6);   
  trcuts->SetEta(-0.8,0.8);
  trcuts->SetMinTPCcls(70);
  trcuts->PrintCuts();

  trk->SetTrackCuts(trcuts);
  trk->Init();

  return trk;

}

AliESEEvtCut * EvtCuts(Int_t mc) {

  AliESEEvtCut * evt = new AliESEEvtCut;

  AliSpectraAODEventCuts * evcuts = new AliSpectraAODEventCuts("EventCuts");
  evcuts->SetQVectorCut(0,100);
  evcuts->SetCentralityCutMax(100);  
  evcuts->SetCentralityCutMin(0);
  if(mc>0)evcuts->SetIsMC(kTRUE);
  TFile * fCalib = new TFile("./calibV0New.root");
  evcuts->SetCalibFile(fCalib);
  evcuts->SetIsLHC10h(kTRUE);
  evcuts->PrintCuts();
  //  evcuts->SetEventSelectionBit(AliVEvent::kAny);

  evt->SetEventCuts(evcuts);
  evt->Init();

  return evt;

}

//______________________________________________________________________________
void runGridESE(
		 const char *gridmode = "test", // Set the run mode (can be "full", "test", "offline", "submit" or "terminate"). Full & T		 
		 const char * taskname = "DevNanoAOD",
		 const int iMCtruth = 2, 
		 const char * addTaskString = ".x AddTaskNanoAODFilter.C(%d,1)" // 
		 )
{
  LoadLibs();
  // analysis manager
  AliAnalysisManager* mgr = new AliAnalysisManager("NanoAOD Filter", "NanoAOD filter for nanoAOD production");

  AliAnalysisGrid *plugin = CreateAlienHandler(taskname, gridmode); 
  plugin->SetFileForTestMode("files.txt"); // file should contain path name to a local directory containg *ESDs.root or AOD etc
  mgr->SetGridHandler(plugin);

    
  AliAODInputHandler* iH = new AliAODInputHandler();
  mgr->SetInputEventHandler(iH);

  // Define aod output handler
  AliAODHandler* aodOutputHandler = new AliAODHandler();
  aodOutputHandler->SetOutputFileName("AliAOD.NanoAOD.root");
  mgr->SetOutputEventHandler(aodOutputHandler);
  
  gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
  AliAnalysisTaskPIDResponse *taskPID=AddTaskPIDResponse(iMCtruth);
  taskPID->SetUseTPCEtaCorrection(kTRUE); 

  // create task
  cout << "Macro: "<< addTaskString << " " << Form(addTaskString, iMCtruth) << endl;

  AliAnalysisTaskNanoAODFilter * task = (AliAnalysisTaskNanoAODFilter*) gROOT->ProcessLine(Form(addTaskString, iMCtruth));

  // Set Track event and vertex cuts here!
  task->SetVarList("pt,theta,phi,cstNSigmaTPCPi,cstNSigmaTPCKa,cstNSigmaTPCPr,cstNSigmaTOFPi,cstNSigmaTOFKa,cstNSigmaTOFPr,cstBayesTPCPi,cstBayesTPCKa,cstBayesTPCPr,cstBayesTOFPi,cstBayesTOFKa,cstBayesTOFPr");
  task->SetVarListHead("cstCentr,cstQVec");
  AliESETrkCut * trkCuts = TrkCuts();
  AliESEEvtCut * evtCuts = EvtCuts(iMCtruth);
  evtCuts->SetTrackCuts(trkCuts->GetTrackCuts());
  AliAnalysisESESetter * setter  = new AliAnalysisESESetter;
  setter->SetEventCuts(evtCuts->GetEventCuts());

  task->SetTrkCuts(trkCuts);
  task->SetEvtCuts(evtCuts);
  task->SetSetter(setter);

  //task->SelectCollisionCandidates(AliVEvent::kMB);// FIXME
  // enable debug printouts
  mgr->SetDebugLevel(10);
  //    mgr->SetNSysInfo(100);
  if (!mgr->InitAnalysis()) return;
  mgr->PrintStatus();
  
  // start analysis
  // Always read the same file:
  TChain * chain = new TChain("aodTree");
  chain->Add("./AliAOD.root");

  Printf("Starting Analysis....");
  mgr->StartAnalysis("grid", chain,123456789);

}

//______________________________________________________________________________

AliAnalysisGrid* CreateAlienHandler(const char *taskname, const char *gridmode)
{
    AliAnalysisAlien *plugin = new AliAnalysisAlien();
    // Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
    plugin->SetRunMode(gridmode);

    // Set versions of used packages
    plugin->SetAPIVersion("V1.1x");
    plugin->SetROOTVersion("v5-34-08-5");
    plugin->SetAliROOTVersion("v5-05-73-AN");

    // Declare input data to be processed.
    //    plugin->SetCheckCopy(kFALSE);

    // Method 1: Create automatically XML collections using alien 'find' command.
    // Define production directory LFN
    plugin->SetGridDataDir("/alice/sim/LHC11a10a_bis");
    // On real reconstructed data:
    // plugin->SetGridDataDir("/alice/data/2009/LHC09d");
    // Set data search pattern
    //plugin->SetDataPattern("*ESDs.root"); // THIS CHOOSES ALL PASSES
    // Data pattern for reconstructed data
    plugin->SetDataPattern("*AOD120/*/AliAOD.root"); // CHECK LATEST PASS OF DATA SET IN ALIENSH
    //    plugin->SetDataPattern("ESDs/pass2/AOD038/*AliAOD.root"); // CHECK LATEST PASS OF DATA SET IN ALIENSH
    //    plugin->SetRunPrefix("000");   // real data
    // ...then add run numbers to be considered
    Int_t runlist[1]={137366};  
    for (Int_t ind=0; ind<1; ind++) {
//     plugin->AddRunNumber(138275);
      plugin->AddRunNumber(runlist[ind]);
    }
    //plugin->SetRunRange(114917,115322);
    plugin->SetNrunsPerMaster(10); // 1
    plugin->SetOutputToRunNo();
    // comment out the next line when using the "terminate" option, unless
    // you want separate merged files for each run
    plugin->SetMergeViaJDL();

    // Method 2: Declare existing data files (raw collections, xml collections, root file)
    // If no path mentioned data is supposed to be in the work directory (see SetGridWorkingDir())
    // XML collections added via this method can be combined with the first method if
    // the content is compatible (using or not tags)
    //   plugin->AddDataFile("tag.xml");
    //   plugin->AddDataFile("/alice/data/2008/LHC08c/000057657/raw/Run57657.Merged.RAW.tag.root");

    // Define alien work directory where all files will be copied. Relative to alien $HOME.
    plugin->SetGridWorkingDir(taskname);

    // Declare alien output directory. Relative to working directory.
    plugin->SetGridOutputDir("out"); // In this case will be $HOME/taskname/out

    // Declare the analysis source files names separated by blancs. To be compiled runtime
    // using ACLiC on the worker nodes.
    //    plugin->SetAnalysisSource("AliAnalysisTaskEx01.cxx");

    // Declare all libraries (other than the default ones for the framework. These will be
    // loaded by the generated analysis macro. Add all extra files (task .cxx/.h) here.
    plugin->SetAdditionalLibs("libPWGLFspectra.so libPWGDevNanoAOD.so");

    // Declare the output file names separated by blancs.
    // (can be like: file.root or file.root@ALICE::Niham::File)
    // To only save certain files, use SetDefaultOutputs(kFALSE), and then
    // SetOutputFiles("list.root other.filename") to choose which files to save
    plugin->SetDefaultOutputs();
    //plugin->SetOutputFiles("list.root");

    // Optionally set a name for the generated analysis macro (default MyAnalysis.C)
    plugin->SetAnalysisMacro(Form("%s.C",taskname));

    // Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
    plugin->SetSplitMaxInputFileNumber(100);

    // Optionally modify the executable name (default analysis.sh)
    plugin->SetExecutable(Form("%s.sh",taskname));

    // set number of test files to use in "test" mode
    plugin->SetNtestFiles(10);

    // Optionally resubmit threshold.
    plugin->SetMasterResubmitThreshold(90);

    // Optionally set time to live (default 30000 sec)
    plugin->SetTTL(30000);

    // Optionally set input format (default xml-single)
    plugin->SetInputFormat("xml-single");

    // Optionally modify the name of the generated JDL (default analysis.jdl)
    plugin->SetJDLName(Form("%s.jdl",taskname));

    // Optionally modify job price (default 1)
    plugin->SetPrice(1);      

    // Optionally modify split mode (default 'se')    
    plugin->SetSplitMode("se");
    
    return plugin;
}



void LoadLibs() {
  gSystem->Load("libCore.so");  
  gSystem->Load("libGeom.so");
  gSystem->Load("libPhysics.so");
  gSystem->Load("libVMC");
  gSystem->Load("libTree");
  gSystem->Load("libProof");
  gSystem->Load("libMatrix");
  gSystem->Load("libMinuit");
  gSystem->Load("libSTEERBase");
  gSystem->Load("libESD");
  gSystem->Load("libAOD");
  gSystem->Load("libANALYSIS");
  //  return;
  gSystem->Load("libOADB");
  gSystem->Load("libANALYSISalice");
  gSystem->Load("libTENDER");
  gSystem->Load("libCORRFW");

  //  gSystem->Load("libNanoAOD.so");
  gSystem->Load("libPWGLFspectra");
  gSystem->Load("libPWGDevNanoAOD");

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