]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGHF/hfe/macros/runAlien.C
Update of hfe code
[u/mrichter/AliRoot.git] / PWGHF / hfe / macros / runAlien.C
1 //-----------------Global variables for the the analysis------------------------
2 TString aliroot_version = "v4-21-11-AN";
3 TString analysis_mode = "";
4 TString output_file = "PWG3hfe.root";
5 TString root_version = "";
6 TString api_version = "V1.1x";
7 TString input_data = "";
8
9 //___________________________________________________________
10 Bool_t FindDependentPackages(){
11   // Find packages where aliroot depends on
12   // Method is the following: the dependencies are on a webpage on alimonitor
13   // we download file and try to find the aliroot expression in the file. The line
14   // we read into a string. The we tokenize the string and look for the expression VO_ALICE@ROOT::
15   // Then we have all information we need
16   const char *wd = gSystem->pwd(); 
17   gSystem->cd("/tmp");
18   gSystem->Exec("wget http://alimonitor.cern.ch/packages");
19   TString setting = gSystem->GetFromPipe(Form("cat index.html | grep VO_ALICE@AliRoot::%s", aliroot_version.Data()));
20   gSystem->Exec("rm index.html");
21   gSystem->cd(wd);
22   if(!setting.Length()) return kFALSE;
23   TObjArray *tokens = setting.Tokenize(";");
24   TIter tokiter(tokens);
25   TObjString *os = NULL;
26   while((os = dynamic_cast<TObjString *>(tokiter()))){
27     TString &mys = os->String();
28     if(!mys.Contains("VO_ALICE@ROOT")) continue;
29     mys.ReplaceAll("VO_ALICE@ROOT::", "");
30     mys.ReplaceAll("&quot","");
31     root_version = mys;
32     break;
33   }
34   // print results:
35   printf("Found Packages for Analysis:\n");
36   printf("=====================================\n");
37   printf("ALIROOT: %s\n", aliroot_version.Data());
38   printf("ROOT:    %s\n", root_version.Data());
39   printf("\n");
40   return kTRUE;
41 }
42
43 //___________________________________________________________
44 AliAnalysisAlien *CreateAlienHandler(Bool_t isProof){
45   //if(!AliAnalysisGrid::CreateToken()) return NULL;
46   if(!FindDependentPackages()) return NULL;
47   AliAnalysisAlien *alienplugin = new AliAnalysisAlien;
48    
49   // common settings
50   alienplugin->SetAliROOTVersion(aliroot_version);
51   alienplugin->SetROOTVersion(root_version);
52   alienplugin->SetAPIVersion(api_version);
53   alienplugin->SetAdditionalLibs("libPWGHFhfe.so");
54   alienplugin->SetDefaultOutputs(kTRUE);
55   if(isProof){
56     // proof mode
57     if(analysis_mode.Contains("test")) alienplugin->SetRunMode("test");
58     alienplugin->SetProofCluster("alice_caf.cern.ch");
59     alienplugin->SetRootVersionForProof(root_version);
60     alienplugin->SetAliRootMode("aliroot");
61   } else {
62     // grid mode
63     alienplugin->SetRunMode(analysis_mode.Data());
64     // default setting that need no deeper logic
65     alienplugin->SetJDLName("hfeanalysis.jdl");
66     alienplugin->SetExecutable("hfeanalysis.sh");
67     alienplugin->SetAnalysisMacro("hfeanalysis.C");
68     alienplugin->SetValidationScript("hfevalidate.sh");
69     alienplugin->SetGridWorkingDir("hfeanalysis");
70     /*alienplugin->SetOutputArchive();
71     alienplugin->SetDefaultOutputs();*/
72     alienplugin->SetOverwriteMode();
73     alienplugin->SetFastReadOption();
74     alienplugin->SetSplitMaxInputFileNumber(5);
75     alienplugin->SetTTL(30000);
76     alienplugin->SetInputFormat("xml-single");
77     alienplugin->SetPrice(1);
78     alienplugin->SetSplitMode("se");
79     alienplugin->SetNtestFiles(1);
80
81     // Merging setting, only needed in terminate or full mode
82     if(analysis_mode.Contains("full") || analysis_mode.Contains("terminate")){
83       alienplugin->SetMergeViaJDL();
84       alienplugin->SetMaxMergeFiles(50);
85       alienplugin->SetMaxMergeStages(5);
86     }
87   }
88   return alienplugin;
89 }
90
91 //___________________________________________________________
92 void DecodeDataString(const TString &datastring, TString &sample, TArrayI &listofruns){
93   TObjArray *toks = datastring.Tokenize(":");
94   sample = (dynamic_cast<TObjString *>(toks->At(0)))->String();
95   TString &listrunstring = (dynamic_cast<TObjString *>(toks->At(1)))->String();
96   TObjArray *runstrings = listrunstring.Tokenize(",");
97   TIter runiter(runstrings);
98   listofruns.Set(runstrings->GetEntriesFast());
99   TObjString *myrunstring = NULL;
100   Int_t counter = 0;
101   while((myrunstring = dynamic_cast<TObjString *>(runiter()))) listofruns[counter++] = myrunstring->String().Atoi();  
102   // Print summary:
103   printf("Selected sample: %s\n", sample.Data());
104   printf("========================================\n");
105   for(Int_t irun = 0; irun < listofruns.GetSize(); irun++){
106     printf("\trun %d\n", listofruns[irun]);
107   }
108   printf("\n");
109   delete toks; delete runstrings;
110 }
111
112 //___________________________________________________________
113 Int_t GetYear(TString &sample){
114   TString yearstring = sample(3,4); 
115   Int_t year = yearstring.Atoi();
116   return 2000 + year;
117 }
118
119 //___________________________________________________________
120 void AddInput(AliAnalysisAlien *alienhandler, TString sample, TArrayI &listofruns, Bool_t MC){
121   Int_t year = GetYear(sample);
122   TString specialpattern, datapattern;
123   if(!MC){
124     // handle Data
125     datapattern = "ESDs/pass2/*ESDs.root";
126     specialpattern = Form("data/%d", year);
127     //alienhandler->SetRunPrefix("%09d");
128   } else {
129     // handle MC
130     datapattern = "*ESDs.root";
131     specialpattern = "sim";
132   }
133   alienhandler->SetGridDataDir(Form("/alice/%s/%s", specialpattern.Data(), sample.Data()));
134   alienhandler->SetDataPattern(datapattern.Data());
135   for(Int_t ien = 0; ien < listofruns.GetSize(); ien++){
136     if(!MC) alienhandler->AddRunNumber(Form("%09d", listofruns[ien]));
137     else alienhandler->AddRunNumber(listofruns[ien]);
138   }
139   alienhandler->SetGridOutputDir(sample.Data());
140   alienhandler->SetOutputToRunNo();
141 }
142
143 //___________________________________________________________
144 void runAlien(TString data, TString mode = "test", Bool_t MC = kFALSE){  
145   if(!gSystem->Getenv("ALICE_ROOT")){
146     printf("AliRoot has to be initialized\n");  
147     return;
148   }
149   
150   // check for valid modes
151   const int kModes = 5;
152   TString allowed_modes[kModes] = {"proof", "prooftest", "test", "full", "submit"}; 
153   Bool_t isValid = kFALSE;
154   mode.ToLower();
155   for(int imode = 0; imode < kModes; imode++){
156     if(!mode.CompareTo(allowed_modes[imode])) isValid = kTRUE;
157   }
158   if(!isValid){
159     printf("invalid analysis mode selected\n");
160     return;
161   }
162   analysis_mode = mode; 
163   Bool_t proofmode = mode.Contains("proof");
164   // libraries to be loaded
165   gSystem->Load("libANALYSIS");
166   gSystem->Load("libANALYSISalice");
167   gSystem->Load("libCORRFW");
168   gSystem->Load("libPWGhfe");
169   
170   // Create Analysis Manager
171   AliAnalysisManager *runAnalysis = new AliAnalysisManager("Heavy Flavour Electron Analysis");
172   runAnalysis->SetCommonFileName(output_file.Data());
173   runAnalysis->SetInputEventHandler(new AliESDInputHandler); 
174   if(MC) runAnalysis->SetMCtruthEventHandler(new AliMCEventHandler);
175   AliAnalysisAlien *alienhandler = CreateAlienHandler(proofmode);
176   printf("alienhandler %p\n", alienhandler);
177   runAnalysis->SetGridHandler(alienhandler);
178   //return;
179   
180   // Specify input (runs or dataset)
181   if(!proofmode){
182     // Query sample ID and runs
183     TString sample;
184     TArrayI listofruns;
185     DecodeDataString(data, sample, listofruns);
186     AddInput(alienhandler, sample, listofruns, MC);
187   } else {
188     alienhandler->SetProofDataSet(data);
189   }
190
191   // Add Tasks
192   gROOT->LoadMacro(Form("%s/ANALYSIS/macros/AddTaskPhysicsSelection.C", gSystem->Getenv("ALICE_ROOT")));
193   gROOT->LoadMacro(Form("%s/PWG3/hfe/macros/AddTaskHFE.C", gSystem->Getenv("ALICE_ROOT")));
194   AddTaskPhysicsSelection(MC);
195   AddTaskHFE();     // @TODO: MC and PbPb flag to be fixed
196
197   // Run Analysis
198   TString anamode = proofmode ? "proof" : "grid";
199   if(runAnalysis->InitAnalysis()){
200     runAnalysis->PrintStatus();
201     runAnalysis->StartAnalysis(anamode);
202   }
203 }