ROOT logo
//
// This is an example steering macro for running RSN analysis task
// locally with a collection of files specified in a text file:
//
// Inputs:
//   - nReadFiles  = number of files to process from the list
//   - nSkipFiles  = how many lines to be skipped when reading the list
//   - addTaskName = name of the macro to add the RSN analysis task
//                   (assumed to have inside it a function named like the file)
//   - inputSource = name of the file containing all the inputs
//                   ---> to run on a local collection, the collection file
//                        must contain on each line the full path
//                        of one input file and it must have the ".txt" extension
//                   ---> to run on an AliEn collection, the collection file must be an XML
//                        file collection like those built from the "find -x" method in aliensh.
//   - options     = a label which is used to know what kind of data are being read
//                   (it is propagated to the 'addTask' macro for eventual setting up of something
//   - outName     = name for the file with RSN package outputs (without ROOT extension)
//
// Notes:
//   - in case the source is an ESD, and if inputs are a MC production
//     the MC input handler is created by default
//
//
// In principle, the user should never modify this macro.
//
void runLocal
(
   Int_t       nReadFiles      = 0,
   Int_t       nSkipFiles      = 0,
   Int_t       nmix            = 0,
   const char *inputSource     = "pbpb_data.txt",
   const char *runOptions      = "esd_data_phys_cent",
   const char *analysisOptions = "tpcpid_tofpid_mult",
   const char *outName         = "test.root",
   const char *taskList        = "AddRsnAnalysisTask.C",
   //const char *taskPath        = "$(ALICE_ROOT)/PWG2/RESONANCES/macros/train"
   const char *taskPath        = "$(HOME)/code/resonances/alice-rsn-package/PWG2resonances/RESONANCES/macros/test/pulvir"
)
{
   //
   // === PREPARATION ==============================================================================
   //
   
   // this option is not needed when using plugin
   // gEnv->SetValue("XSec.GSI.DelegProxy","2");
   
   // stopwatch
   TStopwatch timer;
   timer.Start();
   
   // some options
   TString opt(runOptions);
   opt.ToUpper();
   Bool_t useTender = opt.Contains("TENDER");
   Bool_t isMC      = opt.Contains("MC") || (!opt.Contains("DATA"));
   Bool_t isESD     = opt.Contains("ESD");
   ::Info("runPlugin.C", "useTender = %d", useTender);
   ::Info("runPlugin.C", "isMC      = %d", isMC     );
   ::Info("runPlugin.C", "runOpts   = %s", runOptions);
   ::Info("runPlugin.C", "anaOpts   = %s", analysisOptions);
   
   // basic configurations
   gROOT->LoadMacro(Form("%s/AnalysisSetup.C", taskPath));
   AnalysisSetup(isMC, nmix, runOptions, outName, taskPath);
   
   // define tree name
   Char_t treeName[200];
   if (isESD) sprintf(treeName, "esdTree"); else sprintf(treeName, "aodTree");
   
   //
   // === BUILD INPUT LIST =========================================================================
   //

   // check extension of input to distinguish between XML and TXT
   TString sInput(inputSource);
   sInput.ToLower();
   Bool_t isTXT = (!strcmp(sInput(sInput.Length() - 3, 3).Data(), "txt"));
   cout << "Input = " << (isTXT ? "TXT" : "XML") << endl;

   // if input is XML, connect to AliEn
   if (!isTXT) TGrid::Connect("alien://");

   // create TChain of input events
   TChain *analysisChain = 0x0;
   if (isTXT) analysisChain = CreateChainFromText(inputSource, treeName, nReadFiles, nSkipFiles);
   else       analysisChain = CreateChainFromXML (inputSource, treeName, nReadFiles, nSkipFiles);
   if (!analysisChain) {
      Error("runLocal", "Analysis chain not properly initialized");
      return;
   }
   
   //
   // === CONFIGURATION ============================================================================
   //

   //AliLog::SetGlobalDebugLevel(AliLog::kDebug + 3);
   //AliLog::SetClassDebugLevel("AliRsnCutESD2010", AliLog::kDebug+3);
   //AliLog::SetClassDebugLevel("AliRsnCutValue", AliLog::kDebug+3);
   //AliLog::SetClassDebugLevel("AliRsnCutESDCutMultiplicity", AliLog::kDebug+3);
   //AliLog::SetClassDebugLevel("AliRsnValue", AliLog::kDebug+3);
   //AliLog::SetClassDebugLevel("AliRsnCutTrackQuality", AliLog::kDebug+3);
   //AliLog::SetGlobalDebugLevel(AliLog::kDebug+3);
   
   //
   // === ANALYSIS TASK CREATION AND INCLUSION =====================================================
   //
   
   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
   if (!mgr) return;
   
   gROOT->LoadMacro(Form("%s/AddRsnAnalysisTask.C", taskPath));
   AddRsnAnalysisTask(isMC, (nmix > 0), runOptions, analysisOptions);

   // initialize and start analysis
   if (!mgr->InitAnalysis()) {
      ::Error("runPlugin.C", "Failed to init analysis");
      return;
   }
   mgr->PrintStatus();
   if (isTXT) mgr->StartAnalysis("local", analysisChain);
   else       mgr->StartAnalysis("alien", analysisChain);
   
   // final operation
   // gObjectTable->Print();
   timer.Stop();
   timer.Print();
}

//_________________________________________________________________________________________________
TChain* CreateChainFromXML
(const char *xmlFileName, const char *treeName, Int_t nread, Int_t nskip)
{
//
// Create a TChain with all required files listed into an XML collection.
// Necessary to run analysis in AliEn jobs.
// ---
// Arguments:
//  - xmlFileName = input list
//  - treeName    = "esdTree" or "aodTree"
//  - nread       = how many files to read (0 = all)
//  - nskip       = how many files to skip from beginning
//

   // if nread argument is 0, it is disabled
   if (nread == 0) nread = 1000000000;

   // initialize output object
   TChain *chain = new TChain(treeName);

   // initialize the AliEn collection
   TAlienCollection *myCollection = TAlienCollection::Open(xmlFileName);
   if (!myCollection) {
      Error("CreateChainFromXML", "Cannot create an AliEn collection from %s", xmlFileName);
      return 0x0;
   }

   // loop on collection
   myCollection->Reset();
   while (myCollection->Next()) {
      // skip until reached required number of offset
      if (nskip > 0) {--nskip; continue;}

      // stop if required number of read files is reached
      // otherwise update the counter
      if (nread <= 0) break;
      nread--;

      // recovery file and add it
      Info("CreateChainFromXML", Form("Adding: %s", myCollection->GetTURL("")));
      chain->Add(myCollection->GetTURL(""));
   }

   return chain;
}

//_________________________________________________________________________________________________
TChain* CreateChainFromText(const char *fileName, const char *treeName, Int_t nread, Int_t nskip)
{
//
// Create a TChain with all required files listed into a text file.
// Necessary to run analysis in local jobs.
// ---
// Arguments:
//  - xmlFileName = input file list
//  - treeName    = "esdTree" or "aodTree"
//  - nread       = how many files to read (0 = all)
//  - nskip       = how many files to skip from beginning
//

   // if third argument is 0, it is interpreted
   // as "read all lines"
   Bool_t readAll = (nread <= 0);

   // initialize output object
   TChain* target = new TChain(treeName);

   // open text file
   ifstream fileIn(fileName);

   // loop on collection
   TString line;
   while (fileIn.good()) {
      fileIn >> line;
      if (line.IsNull()) continue;

      // skip until reached required number of offset
      if (nskip > 0) {--nskip; continue;}

      // stop if required number of read files is reached
      // otherwise update the counter
      if (!readAll && nread <= 0) break;
      nread--;

      // add file
      Info("CreateChainFromText", "Adding '%s'", line.Data());
      target->Add(line.Data());
   }

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