removed ^M end of lines
authorssakai <ssakai@f7af4fe6-9843-0410-8265-dc069ae4e863>
Thu, 18 Apr 2013 13:31:48 +0000 (13:31 +0000)
committerssakai <ssakai@f7af4fe6-9843-0410-8265-dc069ae4e863>
Thu, 18 Apr 2013 13:31:48 +0000 (13:31 +0000)
PWGHF/hfe/macros/test/runGridpPb.C

index 17ed7cc..521975b 100644 (file)
-#include <fstream>\r
-#include <iostream>\r
-#include <string>\r
-\r
-TString g_aliroot_version;\r
-TString g_root_version;\r
-TString g_sample;\r
-TString g_plugin_mode;\r
-TString g_train_dir;\r
-TArrayI g_runlist;\r
-\r
-AliAnalysisAlien *CreateGridHandler(){\r
-        //\r
-        // Setup main settings of the Alien plugin\r
-        //\r
-        AliAnalysisAlien *plugin = new AliAnalysisAlien();\r
-        plugin->SetRunMode(g_plugin_mode.Data());\r
-        if(!g_plugin_mode.CompareTo("Terminate"))\r
-                plugin->SetMergeViaJDL(kFALSE);\r
-        else\r
-                plugin->SetMergeViaJDL(kTRUE);\r
-        plugin->SetOverwriteMode();\r
-        plugin->SetNtestFiles(1);\r
-\r
-        plugin->SetAPIVersion("V1.1x");\r
-        plugin->SetROOTVersion(g_root_version.Data());\r
-        plugin->SetAliROOTVersion(g_aliroot_version.Data());\r
-\r
-        plugin->SetOutputToRunNo();\r
-        plugin->AddIncludePath("-I. .I$ALIEN_ROOT/api/lib -I$ROOTSYS/lib -I$ROOTSYS/include -I$ALICE_ROOT/include -I$ALICE_ROOT/PWGHF/ -I$ALICE_ROOT/PWGHF/hfe/macros -I$ALICE_ROOT/PWGHF/hfe -I$ALICE_ROOT/ANALYSIS -I$ALICE_ROOT/ANALYSIS/Tender -I$ALICE_ROOT/ANALYSIS/TenderSupplies -I$ALICE_ROOT/PWG/ -I$ALICE_ROOT/PWG/FLOW -I$ALICE_ROOT/PWG/Base -I$ALICE_ROOT/PWG/Tasks");\r
-        plugin->SetAdditionalLibs("libGui.so  libXMLParser.so libSTEERBase.so libESD.so libAOD.so libCDB.so libANALYSIS.so libANALYSISalice.so libCORRFW.so  libPWGflowBase.so libPWGflowTasks.so libPWGHFhfe.so libTENDER.so libProof.so libRAWDatabase.so libSTEER.so libTOFbase.so");\r
-   \r
-        plugin->SetDefaultOutputs(kFALSE);\r
-        plugin->SetOutputFiles("AnalysisResults.root"); \r
-        plugin->SetExecutableCommand("aliroot -b -q");\r
-        plugin->SetTTL(30000);\r
-        plugin->SetInputFormat("xml-single");\r
-        plugin->SetPrice(1);      \r
-        plugin->SetSplitMode("se");\r
-        return plugin;\r
-}\r
-\r
-void SplitConfigEntry(const std::string &input, TString &key, TString &value){\r
-        //\r
-        // Decode key and value of a config entry\r
-        //\r
-        std::istringstream stream(input, istringstream::in);\r
-        std::string tmp;\r
-        stream >> tmp;\r
-        key = tmp.c_str();\r
-        stream >> tmp;\r
-        value = tmp.c_str();\r
-}\r
-\r
-void DecodeRunlist(const TString &val){\r
-        //\r
-        // Tokenize run list\r
-        //\r
-        TObjArray *runstrings = val.Tokenize(",");\r
-        TObjString *os;\r
-        TString runstr;\r
-        TIter runIter(runstrings);\r
-        g_runlist.Set(runstrings->GetEntries());\r
-        int nruns(0);\r
-        while((os = dynamic_cast<TObjString *>(runIter()))){\r
-                runstr = os->String();\r
-                g_runlist[nruns++] = runstr.Atoi();\r
-        }\r
-        delete runstrings;\r
-}\r
-\r
-bool IsMC(const TString &val){\r
-        // \r
-        // Determine whether sample is MC or Data\r
-        //\r
-        if(!val.CompareTo("MC")) return true;\r
-        return false;\r
-}\r
-\r
-bool FindDataSample(const TMap &lookup, TObjArray &sampleinfis){\r
-        //\r
-        // Find Data sample in the list of samples\r
-        //\r
-        TObjArray *entry = dynamic_cast<TObjArray *>(lookup.GetValue(g_sample.Data()));\r
-        if(!entry){\r
-                printf("Sample %s not found in the list of samples", g_sample.Data());\r
-                return false;\r
-        }\r
-        // Copy to output container\r
-        sampleinfis.SetOwner(kFALSE);\r
-        for(int ival = 0; ival < 4; ival++) sampleinfis.AddAt(entry->At(ival), ival);\r
-        return true;\r
-}\r
-\r
-bool GetData(TObjArray &in, TString &out, int pos){\r
-        //\r
-        // Helper function reading data string\r
-        //\r
-        TObjString *entry = dynamic_cast<TObjString *>(in.At(pos));\r
-        if(!entry){\r
-                printf("Entry at pos %d not a string\n", pos);\r
-                return false;\r
-        }\r
-        out = entry->String();\r
-        return true;\r
-}\r
-\r
-void AddSample(TMap &lookup,\r
-                const char *key, const char* datadir, const char * pattern, const char *sampletype, const char *dataformat){\r
-        //\r
-        // Add sample entry to the lookup table\r
-        //\r
-        TObjArray *infos = new TObjArray(); \r
-        infos->AddAt(new TObjString(datadir), 0);\r
-        infos->AddAt(new TObjString(pattern), 1);\r
-        infos->AddAt(new TObjString(sampletype), 2);\r
-        infos->AddAt(new TObjString(dataformat), 3);\r
-        lookup.Add(new TObjString(key), infos);\r
-}\r
-\r
-void Generate_Sample_Lookup(TMap &lookup){\r
-        // \r
-        // Create Lookup table for each period\r
-        // Vector contains \r
-        //   - path\r
-        //   - pattern\r
-        //   - MC/Data \r
-        //   - ESD/AOD\r
-        //\r
-        AddSample(lookup, "LHC13b.pass2", "/alice/data/2013/LHC13b", "*/pass2/*/AliESDs.root", "Data", "ESD");\r
-        AddSample(lookup, "LHC13b.pass2.AOD", "/alice/data/2013/LHC13b", "*/pass2/AOD/*/AliAOD.root", "Data", "AOD"); \r
-        AddSample(lookup, "LHC13b.pass2.AOD126", "/alice/data/2013/LHC13b", "*/pass2/AOD126/*/AliAOD.root", "Data", "AOD"); \r
-        AddSample(lookup, "LHC13c.pass1", "/alice/data/2013/LHC13c/", "*/pass1/*/AliESDs.root", "Data", "ESD");\r
-        AddSample(lookup, "LHC13c.pass1.AOD", "/alice/data/2013/LHC13c/", "*/pass1/AOD/*/AliAOD.root", "Data", "AOD");\r
-        AddSample(lookup, "LHC13c.pass1.AOD126", "/alice/data/2013/LHC13c/", "*/pass1/AOD126/*/AliAOD.root", "Data", "AOD");\r
-        AddSample(lookup, "LHC13b2", "/alice/sim/2013/LHC13b2", "*/*/AliESDs.root", "MC", "ESD");\r
-        AddSample(lookup, "LHC13b2.AOD", "/alice/sim/2013/LHC13b2", "*/AliAOD.root", "MC", "AOD");\r
-        AddSample(lookup, "LHC13b2plus", "/alice/sim/2013/LHC13b2_plus", "*/*/AliESDs.root", "MC", "ESD");\r
-        AddSample(lookup, "LHC13b2plus.AOD", "/alice/sim/2013/LHC13b2_plus", "*/AliAOD.root", "MC", "AOD");\r
-        AddSample(lookup, "LHC13b3", "/alice/sim/2013/LHC13b3", "*/*/AliESDs.root", "MC", "ESD");\r
-       AddSample(lookup, "LHC13b3.AOD", "/alice/sim/2013/LHC13b3", "*/AliAOD.root", "MC", "AOD");\r
+#include <fstream>
+#include <iostream>
+#include <string>
+
+TString g_aliroot_version;
+TString g_root_version;
+TString g_sample;
+TString g_plugin_mode;
+TString g_train_dir;
+TArrayI g_runlist;
+
+AliAnalysisAlien *CreateGridHandler(){
+        //
+        // Setup main settings of the Alien plugin
+        //
+        AliAnalysisAlien *plugin = new AliAnalysisAlien();
+        plugin->SetRunMode(g_plugin_mode.Data());
+        if(!g_plugin_mode.CompareTo("Terminate"))
+                plugin->SetMergeViaJDL(kFALSE);
+        else
+                plugin->SetMergeViaJDL(kTRUE);
+        plugin->SetOverwriteMode();
+        plugin->SetNtestFiles(1);
+
+        plugin->SetAPIVersion("V1.1x");
+        plugin->SetROOTVersion(g_root_version.Data());
+        plugin->SetAliROOTVersion(g_aliroot_version.Data());
+
+        plugin->SetOutputToRunNo();
+        plugin->AddIncludePath("-I. .I$ALIEN_ROOT/api/lib -I$ROOTSYS/lib -I$ROOTSYS/include -I$ALICE_ROOT/include -I$ALICE_ROOT/PWGHF/ -I$ALICE_ROOT/PWGHF/hfe/macros -I$ALICE_ROOT/PWGHF/hfe -I$ALICE_ROOT/ANALYSIS -I$ALICE_ROOT/ANALYSIS/Tender -I$ALICE_ROOT/ANALYSIS/TenderSupplies -I$ALICE_ROOT/PWG/ -I$ALICE_ROOT/PWG/FLOW -I$ALICE_ROOT/PWG/Base -I$ALICE_ROOT/PWG/Tasks");
+        plugin->SetAdditionalLibs("libGui.so  libXMLParser.so libSTEERBase.so libESD.so libAOD.so libCDB.so libANALYSIS.so libANALYSISalice.so libCORRFW.so  libPWGflowBase.so libPWGflowTasks.so libPWGHFhfe.so libTENDER.so libProof.so libRAWDatabase.so libSTEER.so libTOFbase.so");
+   
+        plugin->SetDefaultOutputs(kFALSE);
+        plugin->SetOutputFiles("AnalysisResults.root"); 
+        plugin->SetExecutableCommand("aliroot -b -q");
+        plugin->SetTTL(30000);
+        plugin->SetInputFormat("xml-single");
+        plugin->SetPrice(1);      
+        plugin->SetSplitMode("se");
+        return plugin;
+}
+
+void SplitConfigEntry(const std::string &input, TString &key, TString &value){
+        //
+        // Decode key and value of a config entry
+        //
+        std::istringstream stream(input, istringstream::in);
+        std::string tmp;
+        stream >> tmp;
+        key = tmp.c_str();
+        stream >> tmp;
+        value = tmp.c_str();
+}
+
+void DecodeRunlist(const TString &val){
+        //
+        // Tokenize run list
+        //
+        TObjArray *runstrings = val.Tokenize(",");
+        TObjString *os;
+        TString runstr;
+        TIter runIter(runstrings);
+        g_runlist.Set(runstrings->GetEntries());
+        int nruns(0);
+        while((os = dynamic_cast<TObjString *>(runIter()))){
+                runstr = os->String();
+                g_runlist[nruns++] = runstr.Atoi();
+        }
+        delete runstrings;
+}
+
+bool IsMC(const TString &val){
+        // 
+        // Determine whether sample is MC or Data
+        //
+        if(!val.CompareTo("MC")) return true;
+        return false;
+}
+
+bool FindDataSample(const TMap &lookup, TObjArray &sampleinfis){
+        //
+        // Find Data sample in the list of samples
+        //
+        TObjArray *entry = dynamic_cast<TObjArray *>(lookup.GetValue(g_sample.Data()));
+        if(!entry){
+                printf("Sample %s not found in the list of samples", g_sample.Data());
+                return false;
+        }
+        // Copy to output container
+        sampleinfis.SetOwner(kFALSE);
+        for(int ival = 0; ival < 4; ival++) sampleinfis.AddAt(entry->At(ival), ival);
+        return true;
+}
+
+bool GetData(TObjArray &in, TString &out, int pos){
+        //
+        // Helper function reading data string
+        //
+        TObjString *entry = dynamic_cast<TObjString *>(in.At(pos));
+        if(!entry){
+                printf("Entry at pos %d not a string\n", pos);
+                return false;
+        }
+        out = entry->String();
+        return true;
+}
+
+void AddSample(TMap &lookup,
+                const char *key, const char* datadir, const char * pattern, const char *sampletype, const char *dataformat){
+        //
+        // Add sample entry to the lookup table
+        //
+        TObjArray *infos = new TObjArray(); 
+        infos->AddAt(new TObjString(datadir), 0);
+        infos->AddAt(new TObjString(pattern), 1);
+        infos->AddAt(new TObjString(sampletype), 2);
+        infos->AddAt(new TObjString(dataformat), 3);
+        lookup.Add(new TObjString(key), infos);
+}
+
+void Generate_Sample_Lookup(TMap &lookup){
+        // 
+        // Create Lookup table for each period
+        // Vector contains 
+        //   - path
+        //   - pattern
+        //   - MC/Data 
+        //   - ESD/AOD
+        //
+        AddSample(lookup, "LHC13b.pass2", "/alice/data/2013/LHC13b", "*/pass2/*/AliESDs.root", "Data", "ESD");
+        AddSample(lookup, "LHC13b.pass2.AOD", "/alice/data/2013/LHC13b", "*/pass2/AOD/*/AliAOD.root", "Data", "AOD"); 
+        AddSample(lookup, "LHC13b.pass2.AOD126", "/alice/data/2013/LHC13b", "*/pass2/AOD126/*/AliAOD.root", "Data", "AOD"); 
+        AddSample(lookup, "LHC13c.pass1", "/alice/data/2013/LHC13c/", "*/pass1/*/AliESDs.root", "Data", "ESD");
+        AddSample(lookup, "LHC13c.pass1.AOD", "/alice/data/2013/LHC13c/", "*/pass1/AOD/*/AliAOD.root", "Data", "AOD");
+        AddSample(lookup, "LHC13c.pass1.AOD126", "/alice/data/2013/LHC13c/", "*/pass1/AOD126/*/AliAOD.root", "Data", "AOD");
+        AddSample(lookup, "LHC13b2", "/alice/sim/2013/LHC13b2", "*/*/AliESDs.root", "MC", "ESD");
+        AddSample(lookup, "LHC13b2.AOD", "/alice/sim/2013/LHC13b2", "*/AliAOD.root", "MC", "AOD");
+        AddSample(lookup, "LHC13b2plus", "/alice/sim/2013/LHC13b2_plus", "*/*/AliESDs.root", "MC", "ESD");
+        AddSample(lookup, "LHC13b2plus.AOD", "/alice/sim/2013/LHC13b2_plus", "*/AliAOD.root", "MC", "AOD");
+        AddSample(lookup, "LHC13b3", "/alice/sim/2013/LHC13b3", "*/*/AliESDs.root", "MC", "ESD");
+       AddSample(lookup, "LHC13b3.AOD", "/alice/sim/2013/LHC13b3", "*/AliAOD.root", "MC", "AOD");
        AddSample(lookup, "LHC13d.pass1", "/alice/data/2013/LHC13d/", "pass1/*/AliESDs.root", "Data", "ESD");
        AddSample(lookup, "LHC13e.pass1", "/alice/data/2013/LHC13e/", "pass1/*/AliESDs.root", "Data", "ESD");
        AddSample(lookup, "LHC13f.pass1", "/alice/data/2013/LHC13f/", "pass1/*/AliESDs.root", "Data", "ESD");
-        printf("Lookup table with sample information generated\n");\r
-}\r
-\r
-void ConfigParser(const char *configname){\r
-        //\r
-        // Parse configuration\r
-        //\r
-        std::ifstream in(configname);\r
-        std::string config;\r
-        TString key, value;\r
-        while(getline(in, config)){\r
-                SplitConfigEntry(config, key, value);\r
-                key.ToLower();\r
-                if(!key.CompareTo("aliroot")){\r
-                        // Aliroot version\r
-                        g_aliroot_version = value;\r
-                        continue;\r
-                }\r
-                if(!key.CompareTo("root")){\r
-                        // root version\r
-                        g_root_version = value;\r
-                        continue;\r
-                }\r
-                if(!key.CompareTo("sample")){\r
-                        // sample name\r
-                        g_sample = value; \r
-                        continue;\r
-                }\r
-                if(!key.CompareTo("runlist")){\r
-                        // Runlist\r
-                        DecodeRunlist(value); \r
-                        continue;\r
-                }\r
-                if(!key.CompareTo("mode")){\r
-                        g_plugin_mode = value;\r
-                        continue;\r
-                }\r
-                if(!key.CompareTo("traindir")){\r
-                        g_train_dir = value;\r
-                        continue;\r
-                }\r
-                printf("Unknown key: %s\n", key.Data());\r
-        }\r
-}\r
-\r
-bool MakeSample(AliAnalysisAlien *plugin, TMap &lookup){\r
-        //\r
-        // Fill Sample information (Data dir, pattern, run list) to the Alien plugin\r
-        //\r
-        TObjArray infos;\r
-        bool found = FindDataSample(lookup, infos);\r
-        if(!found){\r
-                printf("sample %s not found\n", g_sample.Data());\r
-                return false;\r
-        }\r
-        TString datadir, pattern, type;\r
-        GetData(infos, datadir, 0);\r
-        GetData(infos, pattern, 1);\r
-        GetData(infos, type, 2);\r
-        plugin->SetGridDataDir(datadir.Data());\r
-        plugin->SetDataPattern(pattern.Data());\r
-        if(!IsMC(type)) plugin->SetRunPrefix("000");\r
-        // Add runs to the sample\r
-        for(int irun = 0; irun < g_runlist.GetSize(); irun++){\r
-                plugin->AddRunNumber(g_runlist[irun]);\r
-        }\r
-        return true;\r
-}\r
-\r
-bool CreateTrainDir(AliAnalysisAlien *plugin, const TMap &lookup){\r
-        //\r
-        // Make train data dir name and JDL, C and sh file names\r
-        //\r
-        TObjArray infos;\r
-        bool found = FindDataSample(lookup, infos);\r
-        if(!found){\r
-                printf("sample %s not found\n", g_sample.Data());\r
-                return false;\r
-        }\r
-        TString type; GetData(infos, type, 2);\r
-\r
-        // check whether the train dir is already provided or needs to be specified\r
-        if(!g_train_dir.Length()){\r
-                // Query number of train runs before\r
-                const char *gridhome = gGrid->GetHomeDirectory();\r
-                const char gridoutdir[256];\r
-                sprintf(gridoutdir, "%sAnalysis_pPb/%s", gridhome, type.Data());\r
-                TGridResult *trainruns = gGrid->Ls(gridoutdir);\r
-                int nruns = trainruns->GetEntries();\r
-                // Get Date and time\r
-                TDatime time;\r
-                g_train_dir = Form("%d_%d%02d%02d_%02d%02d", nruns, time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute());\r
-        }\r
-        \r
-        plugin->SetGridWorkingDir(Form("Analysis_pPb/%s/%s", type.Data(), g_train_dir.Data()));\r
-        plugin->SetJDLName(Form("TPCTOFanalysispPb_%s_%s.jdl", type.Data(), g_train_dir.Data()));\r
-        plugin->SetExecutable(Form("TPCTOFanalysispPb_%s_%s.sh", type.Data(), g_train_dir.Data()));\r
-        plugin->SetAnalysisMacro(Form("TPCTOFanalysispPb_%s_%s.C", type.Data(), g_train_dir.Data()));\r
-        return true;\r
-}\r
-\r
-void SetupUtil(bool isMC, bool isAOD){\r
-        //\r
-        // Setup utility packages\r
-        //\r
-        // 1. Physics Selection (ESD only)\r
-        // 2. Tender (ESD only)\r
-        // 3. PID Response (always)\r
-        // 4. Centrality Task (ESD only)\r
-        //\r
-\r
-        //==== Physics Selection ====\r
-        if(!isAOD){\r
-                gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");\r
-                AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection(isMC);\r
-        }\r
-\r
-        //==== Add tender ====\r
-        if(!isAOD){\r
-                gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/TenderSupplies/AddTaskTender.C");\r
-                AddTaskTender();\r
-        }\r
-\r
-        //===== ADD PID RESPONSE: ===\r
-        gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");\r
-        AddTaskPIDResponse(isMC);\r
-\r
-        //===== ADD CENTRALITY: ===\r
-        if(!isAOD){\r
-                gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");\r
-                AddTaskCentrality();\r
-        }\r
-}\r
-\r
-void SetupHandlers(bool isMC, bool isAOD){\r
-        //\r
-        // Setup Handlers\r
-        //\r
-        TString macrobase = "$ALICE_ROOT/ANALYSIS/macros/train/";\r
-        TString macroname = macrobase;\r
-        if(isAOD)\r
-                macroname += "AddAODHandler.C";\r
-        else\r
-                macroname += "AddESDHandler.C";\r
-        gROOT->Macro(macroname.Data());\r
-\r
-        if(isMC && !isAOD){\r
-                // Add MC truth event handler, only in case of ESDs\r
-                gROOT->LoadMacro(Form("%s/AddMCHandler.C", macrobase.Data()));\r
-                AddMCHandler();\r
-        }\r
-}\r
-\r
-void SetupHFEtask(bool isMC, bool isAOD){\r
-        gROOT->LoadMacro("$ALICE_ROOT/PWGHF/hfe/macros/AddTaskHFEpPb.C");\r
-        AddTaskHFEpPb(isMC, isAOD);\r
-        if(!isAOD){\r
-                gROOT->LoadMacro("$ALICE_ROOT/PWGHF/hfe/macros/AddTaskHFEnpepPb.C");\r
-                AddTaskHFEnpepPb();\r
-        }\r
-}\r
-\r
-void SetupTrain(const TMap &lookup){\r
-        //\r
-        // Setup train:\r
-        //   Determine whether the trains run on MC or Data \r
-        //   and ESDs or AODs and Configure Handlers, utils\r
-        //   and HFE task according to this\r
-        //\r
-        bool isMC(false), isAOD(false);\r
-        TObjArray infos;\r
-        bool found = FindDataSample(lookup, infos);\r
-        if(!found) return;\r
-        TString type, mode;\r
-        GetData(infos, type, 2);\r
-        GetData(infos, mode, 3);\r
-        isMC = IsMC(type);\r
-        if(!mode.CompareTo("AOD")) isAOD = true;\r
-      \r
-        SetupHandlers(isMC, isAOD);\r
-        SetupUtil(isMC, isAOD);\r
-        SetupHFEtask(isMC, isAOD);\r
-}\r
-\r
-void GenerateMergeConfigs(){\r
-        //\r
-        // Generate configurations for merging \r
-        // (MergeViaJDL and Terminate)\r
-        //\r
-\r
-        // Write config for MergeViaJDL\r
-        std::ofstream outMerge("configMerge.txt");\r
-        outMerge << "aliroot " << g_aliroot_version.Data() << std::endl;\r
-        outMerge << "root " << g_root_version.Data() << std::endl;\r
-        outMerge << "sample " << g_sample.Data() << std::endl;\r
-        outMerge << "mode MergeViaJDL\n";\r
-        outMerge << "traindir " << g_train_dir.Data() << std::endl; \r
-        outMerge << "runlist ";\r
-        for(int irun = 0; irun < g_runlist.GetSize()-1; irun++) outMerge << g_runlist[irun] << ",";\r
-        outMerge << g_runlist[g_runlist.GetSize()-1] << std::endl;\r
-        outMerge.close();\r
-        // Write config for Terminate\r
-        std::ofstream outTerminate("configTerminate.txt");\r
-        outTerminate << "aliroot " << g_aliroot_version.Data() << std::endl;\r
-        outTerminate << "root " << g_root_version.Data() << std::endl;\r
-        outTerminate << "sample " << g_sample.Data() << std::endl;\r
-        outTerminate << "mode Terminate\n";\r
-        outTerminate << "traindir " << g_train_dir.Data() << std::endl; \r
-        outTerminate << "runlist ";\r
-        for(int irun = 0; irun < g_runlist.GetSize()-1; irun++) outTerminate << g_runlist[irun] << ",";\r
-        outTerminate << g_runlist[g_runlist.GetSize()-1] << std::endl;\r
-        outTerminate.close();\r
-\r
-        printf("Configurations for MergeViaJDL and terminate generated\n");\r
-}\r
-\r
-void runGridpPb(const char *config = "config.txt"){\r
-        //\r
-        // run analysis \r
-        //\r
-\r
-        TGrid::Connect("alien://");\r
-\r
-        // Create Lookup with sample information\r
-        TMap sampleinfos;\r
-        Generate_Sample_Lookup(sampleinfos);\r
-\r
-        ConfigParser(config);\r
-\r
-        // Configure alien plugin\r
-        AliAnalysisAlien *plugin = CreateGridHandler();\r
-        if(!CreateTrainDir(plugin, sampleinfos)){\r
-                printf("Cannot setup output directory\n");\r
-                return;\r
-        }\r
-        if(!MakeSample(plugin, sampleinfos)){\r
-                printf("Cannot create data sample\n");\r
-                return;\r
-        }\r
-        if(!g_plugin_mode.CompareTo("full")){\r
-                // full mode, creating config files for the merging stage\r
-                GenerateMergeConfigs();\r
-        }\r
-\r
-        AliAnalysisManager *mgr = new AliAnalysisManager("tpctofanalysis");\r
-        mgr->SetGridHandler(plugin);\r
-        \r
-        SetupTrain(sampleinfos);\r
-\r
-        // Run train\r
-        if (!mgr->InitAnalysis()) return;\r
-        mgr->PrintStatus();\r
-        // Start analysis in grid.\r
-        mgr->StartAnalysis("grid");\r
-} \r
-\r
+        printf("Lookup table with sample information generated\n");
+}
+
+void ConfigParser(const char *configname){
+        //
+        // Parse configuration
+        //
+        std::ifstream in(configname);
+        std::string config;
+        TString key, value;
+        while(getline(in, config)){
+                SplitConfigEntry(config, key, value);
+                key.ToLower();
+                if(!key.CompareTo("aliroot")){
+                        // Aliroot version
+                        g_aliroot_version = value;
+                        continue;
+                }
+                if(!key.CompareTo("root")){
+                        // root version
+                        g_root_version = value;
+                        continue;
+                }
+                if(!key.CompareTo("sample")){
+                        // sample name
+                        g_sample = value; 
+                        continue;
+                }
+                if(!key.CompareTo("runlist")){
+                        // Runlist
+                        DecodeRunlist(value); 
+                        continue;
+                }
+                if(!key.CompareTo("mode")){
+                        g_plugin_mode = value;
+                        continue;
+                }
+                if(!key.CompareTo("traindir")){
+                        g_train_dir = value;
+                        continue;
+                }
+                printf("Unknown key: %s\n", key.Data());
+        }
+}
+
+bool MakeSample(AliAnalysisAlien *plugin, TMap &lookup){
+        //
+        // Fill Sample information (Data dir, pattern, run list) to the Alien plugin
+        //
+        TObjArray infos;
+        bool found = FindDataSample(lookup, infos);
+        if(!found){
+                printf("sample %s not found\n", g_sample.Data());
+                return false;
+        }
+        TString datadir, pattern, type;
+        GetData(infos, datadir, 0);
+        GetData(infos, pattern, 1);
+        GetData(infos, type, 2);
+        plugin->SetGridDataDir(datadir.Data());
+        plugin->SetDataPattern(pattern.Data());
+        if(!IsMC(type)) plugin->SetRunPrefix("000");
+        // Add runs to the sample
+        for(int irun = 0; irun < g_runlist.GetSize(); irun++){
+                plugin->AddRunNumber(g_runlist[irun]);
+        }
+        return true;
+}
+
+bool CreateTrainDir(AliAnalysisAlien *plugin, const TMap &lookup){
+        //
+        // Make train data dir name and JDL, C and sh file names
+        //
+        TObjArray infos;
+        bool found = FindDataSample(lookup, infos);
+        if(!found){
+                printf("sample %s not found\n", g_sample.Data());
+                return false;
+        }
+        TString type; GetData(infos, type, 2);
+
+        // check whether the train dir is already provided or needs to be specified
+        if(!g_train_dir.Length()){
+                // Query number of train runs before
+                const char *gridhome = gGrid->GetHomeDirectory();
+                const char gridoutdir[256];
+                sprintf(gridoutdir, "%sAnalysis_pPb/%s", gridhome, type.Data());
+                TGridResult *trainruns = gGrid->Ls(gridoutdir);
+                int nruns = trainruns->GetEntries();
+                // Get Date and time
+                TDatime time;
+                g_train_dir = Form("%d_%d%02d%02d_%02d%02d", nruns, time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute());
+        }
+        
+        plugin->SetGridWorkingDir(Form("Analysis_pPb/%s/%s", type.Data(), g_train_dir.Data()));
+        plugin->SetJDLName(Form("TPCTOFanalysispPb_%s_%s.jdl", type.Data(), g_train_dir.Data()));
+        plugin->SetExecutable(Form("TPCTOFanalysispPb_%s_%s.sh", type.Data(), g_train_dir.Data()));
+        plugin->SetAnalysisMacro(Form("TPCTOFanalysispPb_%s_%s.C", type.Data(), g_train_dir.Data()));
+        return true;
+}
+
+void SetupUtil(bool isMC, bool isAOD){
+        //
+        // Setup utility packages
+        //
+        // 1. Physics Selection (ESD only)
+        // 2. Tender (ESD only)
+        // 3. PID Response (always)
+        // 4. Centrality Task (ESD only)
+        //
+
+        //==== Physics Selection ====
+        if(!isAOD){
+                gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
+                AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection(isMC);
+        }
+
+        //==== Add tender ====
+        if(!isAOD){
+                gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/TenderSupplies/AddTaskTender.C");
+                AddTaskTender();
+        }
+
+        //===== ADD PID RESPONSE: ===
+        gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
+        AddTaskPIDResponse(isMC);
+
+        //===== ADD CENTRALITY: ===
+        if(!isAOD){
+                gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
+                AddTaskCentrality();
+        }
+}
+
+void SetupHandlers(bool isMC, bool isAOD){
+        //
+        // Setup Handlers
+        //
+        TString macrobase = "$ALICE_ROOT/ANALYSIS/macros/train/";
+        TString macroname = macrobase;
+        if(isAOD)
+                macroname += "AddAODHandler.C";
+        else
+                macroname += "AddESDHandler.C";
+        gROOT->Macro(macroname.Data());
+
+        if(isMC && !isAOD){
+                // Add MC truth event handler, only in case of ESDs
+                gROOT->LoadMacro(Form("%s/AddMCHandler.C", macrobase.Data()));
+                AddMCHandler();
+        }
+}
+
+void SetupHFEtask(bool isMC, bool isAOD){
+        gROOT->LoadMacro("$ALICE_ROOT/PWGHF/hfe/macros/AddTaskHFEpPb.C");
+        AddTaskHFEpPb(isMC, isAOD);
+        if(!isAOD){
+                gROOT->LoadMacro("$ALICE_ROOT/PWGHF/hfe/macros/AddTaskHFEnpepPb.C");
+                AddTaskHFEnpepPb();
+        }
+}
+
+void SetupTrain(const TMap &lookup){
+        //
+        // Setup train:
+        //   Determine whether the trains run on MC or Data 
+        //   and ESDs or AODs and Configure Handlers, utils
+        //   and HFE task according to this
+        //
+        bool isMC(false), isAOD(false);
+        TObjArray infos;
+        bool found = FindDataSample(lookup, infos);
+        if(!found) return;
+        TString type, mode;
+        GetData(infos, type, 2);
+        GetData(infos, mode, 3);
+        isMC = IsMC(type);
+        if(!mode.CompareTo("AOD")) isAOD = true;
+      
+        SetupHandlers(isMC, isAOD);
+        SetupUtil(isMC, isAOD);
+        SetupHFEtask(isMC, isAOD);
+}
+
+void GenerateMergeConfigs(){
+        //
+        // Generate configurations for merging 
+        // (MergeViaJDL and Terminate)
+        //
+
+        // Write config for MergeViaJDL
+        std::ofstream outMerge("configMerge.txt");
+        outMerge << "aliroot " << g_aliroot_version.Data() << std::endl;
+        outMerge << "root " << g_root_version.Data() << std::endl;
+        outMerge << "sample " << g_sample.Data() << std::endl;
+        outMerge << "mode MergeViaJDL\n";
+        outMerge << "traindir " << g_train_dir.Data() << std::endl; 
+        outMerge << "runlist ";
+        for(int irun = 0; irun < g_runlist.GetSize()-1; irun++) outMerge << g_runlist[irun] << ",";
+        outMerge << g_runlist[g_runlist.GetSize()-1] << std::endl;
+        outMerge.close();
+        // Write config for Terminate
+        std::ofstream outTerminate("configTerminate.txt");
+        outTerminate << "aliroot " << g_aliroot_version.Data() << std::endl;
+        outTerminate << "root " << g_root_version.Data() << std::endl;
+        outTerminate << "sample " << g_sample.Data() << std::endl;
+        outTerminate << "mode Terminate\n";
+        outTerminate << "traindir " << g_train_dir.Data() << std::endl; 
+        outTerminate << "runlist ";
+        for(int irun = 0; irun < g_runlist.GetSize()-1; irun++) outTerminate << g_runlist[irun] << ",";
+        outTerminate << g_runlist[g_runlist.GetSize()-1] << std::endl;
+        outTerminate.close();
+
+        printf("Configurations for MergeViaJDL and terminate generated\n");
+}
+
+void runGridpPb(const char *config = "config.txt"){
+        //
+        // run analysis 
+        //
+
+        TGrid::Connect("alien://");
+
+        // Create Lookup with sample information
+        TMap sampleinfos;
+        Generate_Sample_Lookup(sampleinfos);
+
+        ConfigParser(config);
+
+        // Configure alien plugin
+        AliAnalysisAlien *plugin = CreateGridHandler();
+        if(!CreateTrainDir(plugin, sampleinfos)){
+                printf("Cannot setup output directory\n");
+                return;
+        }
+        if(!MakeSample(plugin, sampleinfos)){
+                printf("Cannot create data sample\n");
+                return;
+        }
+        if(!g_plugin_mode.CompareTo("full")){
+                // full mode, creating config files for the merging stage
+                GenerateMergeConfigs();
+        }
+
+        AliAnalysisManager *mgr = new AliAnalysisManager("tpctofanalysis");
+        mgr->SetGridHandler(plugin);
+        
+        SetupTrain(sampleinfos);
+
+        // Run train
+        if (!mgr->InitAnalysis()) return;
+        mgr->PrintStatus();
+        // Start analysis in grid.
+        mgr->StartAnalysis("grid");
+} 
+