committing on behalf of Silvia... new macros for HFE analysis on the train (Markus
[u/mrichter/AliRoot.git] / PWG3 / hfe / macros / runElectronTask.C
1 void SetupPar(char* pararchivename);
2 void SwitchBranches(TChain *c);
3 TChain * CreateXMLChain(char* xmlfile);
4 TChain * CreateAODChain(const char * name = 0x0);
5
6 void runElectronTask(const char *treelist = 0x0, Bool_t hasMC = kTRUE){
7   if(!treelist){
8     printf("Error: No ESD list specified\n");
9     return;
10   }
11   if(gSystem->Getenv("ALICE_ROOT")){
12     gSystem->Load("libANALYSIS");
13     gSystem->Load("libANALYSISalice");
14     gSystem->Load("libCORRFW");
15   }
16   else{
17     SetupPar("STEERBase");
18     SetupPar("ESD");
19     SetupPar("AOD");
20     SetupPar("ANALYSIS");
21     SetupPar("ANALYSISalice");
22     SetupPar("CORRFW");
23     SetupPar("Util");
24   }
25   //SetupPar("HFE");
26   gSystem->Load("libHFE.so");
27 //  gROOT->LoadMacro("AliAnalysisTaskHFE.cxx++");
28  // AliLog::SetGlobalLogLevel(AliLog::kError);
29   
30   // Make the ESD chain
31   TString treename = treelist;
32   TChain *inputChain = 0x0;
33   Bool_t isAOD = kFALSE;
34   if(treename.EndsWith(".xml")){
35     esdchain = CreateXMLChain(treelist);
36     SwitchBranches(inputChain);
37   }else{
38     inputChain = CreateAODChain(treelist);
39     if(inputChain) isAOD = kTRUE;
40     else{
41       gROOT->LoadMacro("CreateESDChain.C");
42       inputChain = CreateESDChain(treelist, -1);
43       SwitchBranches(inputChain);
44     }
45   }
46  
47   // Start the Analysis Manager and Create Handlers
48   AliAnalysisManager *hfeAnalysis = new AliAnalysisManager("Single Electron Analysis");
49   if(!isAOD) hfeAnalysis->SetInputEventHandler(new AliESDInputHandler);
50   else hfeAnalysis->SetInputEventHandler(new AliAODInputHandler);
51   if(hasMC && !isAOD) hfeAnalysis->SetMCtruthEventHandler(new AliMCEventHandler);
52   AliHFEcuts *hfecuts = new AliHFEcuts;
53   hfecuts->CreateStandardCuts();
54   AliAnalysisTaskHFE *task = new AliAnalysisTaskHFE;
55   if(hasMC) task->SetHasMCData();
56   if(isAOD) task->SetAODAnalysis();
57   task->SetHFECuts(hfecuts);
58   task->SetPIDStrategy(4);
59   task->SetQAOn(AliAnalysisTaskHFE::kPIDqa);
60   task->SetQAOn(AliAnalysisTaskHFE::kMCqa);
61   task->SwitchOnPlugin(AliAnalysisTaskHFE::kSecVtx);
62   //task->SwitchOnPlugin(AliAnalysisTaskHFE::kIsElecBackGround);
63   hfeAnalysis->AddTask(task);
64   task->ConnectInput(0, hfeAnalysis->GetCommonInputContainer());
65   task->ConnectOutput(1, hfeAnalysis->CreateContainer("nEvents", TH1I::Class(), AliAnalysisManager::kOutputContainer, "HFEtask.root"));
66   task->ConnectOutput(2, hfeAnalysis->CreateContainer("Results", TList::Class(), AliAnalysisManager::kOutputContainer, "HFEtask.root"));
67   task->ConnectOutput(3, hfeAnalysis->CreateContainer("QA", TList::Class(), AliAnalysisManager::kOutputContainer, "HFEtask.root"));
68   //task->ConnectOutput(3, hfeAnalysis->CreateContainer("QA-new", TList::Class(), AliAnalysisManager::kOutputContainer, "HFEtask.root"));  
69   // Run the Analysis
70   if(hfeAnalysis->InitAnalysis()){  
71     TStopwatch timer;
72     timer.Start();
73     hfeAnalysis->StartAnalysis("local", inputChain);
74     timer.Stop();
75     timer.Print();
76   }
77 }
78
79 //____________________________________________
80 TChain * CreateXMLChain(char* xmlfile)
81 {
82
83   //TChain *chain = 0x0;
84   const char *chainname="esdTree";
85   TChain* chain = new TChain(chainname);
86  
87   TString input =xmlfile;
88   cout<<" the input is::"<< xmlfile<<endl;
89
90   char kXML  [1000];
91   if(gSystem->Getenv("XML") )
92     kXML = gSystem->Getenv("XML");
93   else
94     sprintf(kXML, xmlfile) ; 
95
96   //    sprintf(kXML, "collection.xml") ; 
97   
98   cout<<"XML file "<<kXML<<endl;
99   
100   if (!TFile::Open(kXML)) {
101     printf("No collection file with name -- %s -- was found\n",kXML);
102     return ;
103   }
104   gSystem->Load("libNetx.so") ;
105   gSystem->Load("libRAliEn.so");
106   TGrid::Connect("alien://") ;
107
108
109
110   //  TGridCollection * collection =  (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
111   TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
112   if (! collection) {
113     AliError(Form("%s not found", kXML)) ; 
114     return kFALSE ; 
115   }
116   //collection->CheckIfOnline();
117
118   TGridResult* result = collection->GetGridResult("",0 ,0);
119
120   //  TList* analysisfilelist = result->GetFileInfoList();
121   
122   // Makes the ESD chain 
123   printf("*** Getting the Chain       ***\n");
124
125   for (Int_t index = 0; index < result->GetEntries(); index++) {
126     TString alienURL = result->GetKey(index, "turl") ; 
127     cout << "================== " << alienURL << endl ; 
128     chain->Add(alienURL) ; 
129     //alienURL.ReplaceAll("AliESDs.root",kXSFileName);
130     // chainxs->Add(alienURL) ; 
131   }
132
133
134   //  chain->AddFileInfoList(analysisfilelist);
135   if (chain) chain->ls();
136   chain->SetBranchStatus("*Calo*",0);
137   chain->SetBranchStatus("*FMD*",0);
138   return chain;
139 }
140
141 //______________________________________________________________________________
142 TChain *CreateAODChain(const char * name){
143   if(!name) return NULL;
144   ifstream filelist(name);
145   string filename;
146   TChain *c = new TChain("aodTree");
147   Bool_t isAODlist = kTRUE;
148   while(getline(filelist,filename)){
149     if(!strstr(filename.c_str(), "AliAOD")){
150       isAODlist = kFALSE;
151       break;
152     }
153     c->Add(filename.c_str());
154   }
155   if(!isAODlist){
156     printf("No AOD anlysis");
157     delete c;
158     return NULL;
159   }
160   return c;
161 }
162
163 //______________________________________________________________________________
164 void SetupPar(char* pararchivename)
165 {
166   if (pararchivename) {
167     char processline[1024];
168     sprintf(processline,".! tar xvzf %s.par",pararchivename);
169     gROOT->ProcessLine(processline);
170     TString ocwd = gSystem->WorkingDirectory();
171     gSystem->ChangeDirectory(pararchivename);
172     
173     // check for BUILD.sh and execute
174     if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
175       printf("*******************************\n");
176       printf("*** Building PAR archive    ***\n");
177       printf("*******************************\n");
178       
179       if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
180         Error("runProcess","Cannot Build the PAR Archive! - Abort!");
181         return -1;
182       }
183     }
184     // check for SETUP.C and execute
185     if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
186       printf("*******************************\n");
187       printf("*** Setup PAR archive       ***\n");
188       printf("*******************************\n");
189       gROOT->Macro("PROOF-INF/SETUP.C");
190     }
191     
192     gSystem->ChangeDirectory(ocwd.Data());
193     printf("Current dir: %s\n", ocwd.Data());
194   }
195 }
196
197 void SwitchBranches(TChain *c){
198   //esdchain->SetBranchStatus("*", 0);
199   c->SetBranchStatus("Calo*", 0);
200   c->SetBranchStatus("*FMD*", 1);
201   c->SetBranchStatus("Tracks", 1);
202 }