ROOT logo
//-*- Mode: C++ -*-
// $Id$

/// @file   setupPi0Flow.C
/// @author Matthias.Richter@cern.ch, modified for PHOSPi0Flow by Henrik.Qvigstad@cern.ch
/// @date   2012-09-12
/// @brief  Setup the environment specifically for running Pi0Flow analysis
///
/// Helper macro to initialize the environment for Pi0Flow. The macro can just
/// prepend other macros like run-single-task in the command line.
/// Usage:
/// aliroot -b -q -l setupPi0Flow.C'("localAodDirectory", nFiles)' run-single-task.C'(...)'
/// aliroot -b -q -l setupPi0Flow.C'("lhcPeriod", "mcPeriod")' run-single-task.C'(...)'
///
/// Example:
/// aliroot -b -q -l setupPi0Flow.C run-single-task.C'(...)'
///
/// The macro has the following tasks:
/// - load the necessary libraries, in order to have those library names also
///   available for the alien handler initialization, a specific configuration
///   object is created
/// - setting a default analysis name via a configuration object
/// - the optional parameter 'localAodDirectory' allows to create an input chain from
///   local AODs; either a single AliAOD.root, or a folder containing directories
///   named "1, 2, ...", the number of directories is specified as parameter
///   nFiles
/// - loading the runs defined by AddGoodRuns of vertexingHF using lhcPeriod and
///   optional mcPeriod

///////////////////////////////////////////////////////////////////////////////////////////////////
//
// environment specific for Pi0Flow
//
const char* analysisName="PHOSPi0Flow";
const char* includePath="-I$ALICE_ROOT/include -I$ALICE_ROOT/PHOS -I$ALICE_ROOT/PWGGA/PHOSTasks/PHOS_PbPb";
const char* libraryDependencies="libCore.so libTree.so libGeom.so libVMC.so libPhysics.so libMinuit.so libGui.so libXMLParser.so libMinuit2.so libProof.so libSTEERBase.so libESD.so libAOD.so libOADB.so libANALYSIS.so libANALYSISalice.so libCDB.so libRAWDatabase.so libSTEER.so libCORRFW.so libPHOSUtils.so libPHOSbase.so libPHOSpi0Calib.so libPHOSrec.so libPHOSshuttle.so libPHOSsim.so libPWGGAPHOSTasks.so libTENDER.so libTRDbase.so libVZERObase.so libVZEROrec.so libTENDERSupplies.so libPWGGAUtils.so";

void setupPi0Flow(const char* localAodDirectory, int nFiles, const char* lhcPeriod, const char* mcProd="", const TString& dataType="AOD")
{
  //
  // adding include path and libraries
  //
  gSystem->AddIncludePath(includePath);
  TString libraries=libraryDependencies;
  TObjArray* pTokens=libraries.Tokenize(" ");
  if (pTokens) {
    for (int i=0; i<pTokens->GetEntriesFast(); i++) {
      if (gSystem->Load(pTokens->At(i)->GetName())==0) {
	cout << "loading " << pTokens->At(i)->GetName() << endl;
      }
    }
    delete pTokens;
  }
  libraries="";

  //
  // allow run-single-task to fetch the analysis name and library names
  //
  if (gDirectory) gDirectory->Add(new TNamed("analysis_name", analysisName));
  if (gDirectory) gDirectory->Add(new TNamed("analysis_libraries", libraryDependencies));

  if (lhcPeriod) {
    //
    // setting up the runs for the dpecified period
    //
    TString alienHandlerName(analysisName); alienHandlerName+="Handler";
    AliAnalysisAlien* alienHandler=new AliAnalysisAlien(alienHandlerName);
    gROOT->LoadMacro("$ALICE_ROOT/PWGGA/PHOSTasks/PHOS_PbPb/macros/single-task/AddGoodRuns.C");
    int nruns=AddGoodRuns(alienHandler, lhcPeriod, mcProd);
    if (nruns<=0) {
      ::Error("setupPi0Flow.C", Form("can not find any good runs for period %s", lhcPeriod));
      return;
    }
    gDirectory->Add(alienHandler);
    ::Info("setupPi0Flow.C", Form("setting up alien plugin '%s' for period %s\n>>>>> please use '%s' as input parameter for run-single-task.C <<<<<<", alienHandlerName.Data(), lhcPeriod, alienHandlerName.Data()));

  } else if (localAodDirectory) {
    //
    // create AOD tree from local files
    // the created object is added automatically to gDirectory and can be fetched
    // from there later
    //
    //gROOT->LoadMacro("$ALICE_ROOT/PWGHF/vertexingHF/MakeAODInputChain.C");
    TChain* chain = 0x0;
    if( dataType.EqualTo("AOD") ) {
      gROOT->LoadMacro("$ALICE_ROOT/PWGGA/PHOSTasks/PHOS_PbPb/macros/single-task/MakeAODInputChain.C");
      chain = MakeAODInputChain(localAodDirectory, nFiles);
    } 
    else if ( dataType.EqualTo("ESD") ) {
      gROOT->LoadMacro("$ALICE_ROOT/PWG/EMCAL/macros/CreateESDChain.C");
      chain = CreateESDChain(localAodDirectory, nFiles);
    }
    // TString aodPathName(localAodDirectory);
    // if (!aodPathName.EndsWith("/")) aodPathName+="/";
    // aodPathName+="AliAOD.root";
    // if (gSystem->AccessPathName(aodPathName)==0) {
    //   // Create a chain with one set of AliAOD.root and AliAOD.VertexingHF.root. The set needs 
    //   // to be located in the same folder as you run from (physically or linked)
    //   ::Info("setupPi0Flow.C", Form("make chain from single chunk %s", aodPathName));
    //   chain = MakeAODInputChain(localAodDirectory ,1, -1);
    // } else {
    //   // Assume several folders containing different AODs. 
    //   // The AODs need to be in folders named 1, 2,...
    //   ::Info("setupPi0Flow.C", Form("make chain from directory %s", localAodDirectory));
    //   chain=MakeAODInputChain(localAodDirectory, nFiles);
    // }
    ::Info("setupPi0Flow.C", Form("local AOD chain: %d entries", chain->GetEntries()));
  }
}

void setupPi0Flow(const char* lhcPeriod=NULL, const char* mcProd="")
{
  // Grid mode with optional calling of AddGoodRuns for specified
  // period
  setupPi0Flow(NULL, 0, lhcPeriod, mcProd);
}

void setupPi0Flow(const char* localAodDirectory, int nFiles)
{
  // local mode for AOD data
  setupPi0Flow(localAodDirectory, nFiles, NULL);
}
 setupPi0Flow.C:1
 setupPi0Flow.C:2
 setupPi0Flow.C:3
 setupPi0Flow.C:4
 setupPi0Flow.C:5
 setupPi0Flow.C:6
 setupPi0Flow.C:7
 setupPi0Flow.C:8
 setupPi0Flow.C:9
 setupPi0Flow.C:10
 setupPi0Flow.C:11
 setupPi0Flow.C:12
 setupPi0Flow.C:13
 setupPi0Flow.C:14
 setupPi0Flow.C:15
 setupPi0Flow.C:16
 setupPi0Flow.C:17
 setupPi0Flow.C:18
 setupPi0Flow.C:19
 setupPi0Flow.C:20
 setupPi0Flow.C:21
 setupPi0Flow.C:22
 setupPi0Flow.C:23
 setupPi0Flow.C:24
 setupPi0Flow.C:25
 setupPi0Flow.C:26
 setupPi0Flow.C:27
 setupPi0Flow.C:28
 setupPi0Flow.C:29
 setupPi0Flow.C:30
 setupPi0Flow.C:31
 setupPi0Flow.C:32
 setupPi0Flow.C:33
 setupPi0Flow.C:34
 setupPi0Flow.C:35
 setupPi0Flow.C:36
 setupPi0Flow.C:37
 setupPi0Flow.C:38
 setupPi0Flow.C:39
 setupPi0Flow.C:40
 setupPi0Flow.C:41
 setupPi0Flow.C:42
 setupPi0Flow.C:43
 setupPi0Flow.C:44
 setupPi0Flow.C:45
 setupPi0Flow.C:46
 setupPi0Flow.C:47
 setupPi0Flow.C:48
 setupPi0Flow.C:49
 setupPi0Flow.C:50
 setupPi0Flow.C:51
 setupPi0Flow.C:52
 setupPi0Flow.C:53
 setupPi0Flow.C:54
 setupPi0Flow.C:55
 setupPi0Flow.C:56
 setupPi0Flow.C:57
 setupPi0Flow.C:58
 setupPi0Flow.C:59
 setupPi0Flow.C:60
 setupPi0Flow.C:61
 setupPi0Flow.C:62
 setupPi0Flow.C:63
 setupPi0Flow.C:64
 setupPi0Flow.C:65
 setupPi0Flow.C:66
 setupPi0Flow.C:67
 setupPi0Flow.C:68
 setupPi0Flow.C:69
 setupPi0Flow.C:70
 setupPi0Flow.C:71
 setupPi0Flow.C:72
 setupPi0Flow.C:73
 setupPi0Flow.C:74
 setupPi0Flow.C:75
 setupPi0Flow.C:76
 setupPi0Flow.C:77
 setupPi0Flow.C:78
 setupPi0Flow.C:79
 setupPi0Flow.C:80
 setupPi0Flow.C:81
 setupPi0Flow.C:82
 setupPi0Flow.C:83
 setupPi0Flow.C:84
 setupPi0Flow.C:85
 setupPi0Flow.C:86
 setupPi0Flow.C:87
 setupPi0Flow.C:88
 setupPi0Flow.C:89
 setupPi0Flow.C:90
 setupPi0Flow.C:91
 setupPi0Flow.C:92
 setupPi0Flow.C:93
 setupPi0Flow.C:94
 setupPi0Flow.C:95
 setupPi0Flow.C:96
 setupPi0Flow.C:97
 setupPi0Flow.C:98
 setupPi0Flow.C:99
 setupPi0Flow.C:100
 setupPi0Flow.C:101
 setupPi0Flow.C:102
 setupPi0Flow.C:103
 setupPi0Flow.C:104
 setupPi0Flow.C:105
 setupPi0Flow.C:106
 setupPi0Flow.C:107
 setupPi0Flow.C:108
 setupPi0Flow.C:109
 setupPi0Flow.C:110
 setupPi0Flow.C:111
 setupPi0Flow.C:112
 setupPi0Flow.C:113
 setupPi0Flow.C:114
 setupPi0Flow.C:115
 setupPi0Flow.C:116
 setupPi0Flow.C:117
 setupPi0Flow.C:118
 setupPi0Flow.C:119
 setupPi0Flow.C:120
 setupPi0Flow.C:121
 setupPi0Flow.C:122
 setupPi0Flow.C:123