94648ff11633470bd8eae5f8203a61aa2d42962c
[u/mrichter/AliRoot.git] / test / vmctest / scripts / efficiency / SetupAnalysis.C
1 void SetupAnalysis(TString mode,
2                    TString analysisMode="full",
3                    Bool_t useMC = kFALSE,
4                    Int_t nEvents=1.0*1e9, 
5                    Int_t nEventsSkip=0,
6                    TString format="esd")
7 {
8   
9   // ALICE stuff
10   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
11   if (!mgr) mgr = new AliAnalysisManager("CAF train");
12   
13   // Create and configure the alien handler plugin 
14   gROOT->LoadMacro("CreateAnalysisPlugin.C"); 
15   AliAnalysisGrid *alienHandler = CreateAnalysisPlugin(analysisMode);   
16   if (!alienHandler) return;
17   mgr->SetGridHandler(alienHandler);
18   
19   // input handler for esd or AOD, real or MC data
20   InputHandlerSetup(format,useMC);
21
22   // physics selection
23   if(!format.CompareTo("esd")){
24     gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
25     AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection(kFALSE);
26     if(useMC) physSelTask->GetPhysicsSelection()->SetAnalyzeMC();   
27     AliPhysicsSelection* physSel = physSelTask->GetPhysicsSelection();
28     physSel->AddBackgroundIdentification(new AliBackgroundSelection());
29   }
30   
31   gROOT->ProcessLine(Form(".include %s/include",gSystem->ExpandPathName("$ALICE_ROOT")));
32   
33   gROOT->LoadMacro("AliAnalysisTaskEfficiency.cxx+g");
34   
35   // load and run AddTask macro
36   gROOT->LoadMacro("AddTaskEfficiency.C");
37   
38
39   AliAnalysisTaskSE* task1 = AddTaskEfficiency(-1);
40   if(!task1){
41     Printf("AddTask could not be run.");
42   }
43
44   // Run analysis
45   mgr->InitAnalysis();
46   
47   if ((!mode.CompareTo("proof")) ||(!mode.CompareTo("local"))) {
48     mgr->StartAnalysis(mode.Data(),nEvents,nEventsSkip);
49   }
50   else {
51     mgr->StartAnalysis(mode.Data());
52     
53   }
54   
55 }
56
57
58 TString GetFormatFromDataSet(TString dataset) {
59   
60   TString dsTreeName;
61   if (dataset.Contains("#")) {
62     Info("runSKAF.C",Form("Detecting format from dataset name '%s' ...",dataset.Data()));
63     dsTreeName=dataset(dataset.Last('#'),dataset.Length());
64   } else {
65     Info("runSKAF.C",Form("Detecting format from dataset '%s' (may take while, depends on network connection) ...",
66                           dataset.Data()));
67     TFileCollection *ds = gProof->GetDataSet(dataset.Data());
68     if (!ds) {
69       Error(Form("Dataset %s doesn't exist on proof cluster!!!!",dataset.Data()));
70       return "";
71     }
72     dsTreeName = ds->GetDefaultTreeName();
73   }
74   
75   if (dsTreeName.Contains("esdTree")) {
76     Info("runSKAF.C","ESD input format detected ...");
77     return "ESD";
78   } else if (dsTreeName.Contains("aodTree"))  {
79     Info("runSKAF.C","AOD input format detected ...");
80     return "AOD";
81   } else {
82     Error("runSKAF.C",Form("Tree %s is not supported !!!",dsTreeName.Data()));
83     Error("runSKAF.C",Form("Maybe set your DS to %s#esdTree or %s#aodTree",
84                            dataset.Data(),dataset.Data()));
85   }
86   
87   return "";
88 }
89
90 Bool_t InputHandlerSetup(TString format = "esd", Bool_t useKine = kTRUE)
91 {
92   format.ToLower();
93
94   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
95
96   AliAnalysisDataContainer *cin = mgr->GetCommonInputContainer();
97
98   if (cin) return;
99
100   if (!format.CompareTo("esd"))
101     {
102       AliESDInputHandler *esdInputHandler = 
103         dynamic_cast<AliESDInputHandler*>(AliAnalysisManager::GetAnalysisManager()->
104                                           GetInputEventHandler());
105
106       if (!esdInputHandler)
107         {
108           Info("CustomAnalysisTaskInputSetup", "Creating esdInputHandler ...");
109           esdInputHandler = new AliESDInputHandler();
110           mgr->SetInputEventHandler(esdInputHandler);
111         }
112
113       if (useKine)
114         {
115           AliMCEventHandler* mcInputHandler = 
116             dynamic_cast<AliMCEventHandler*>(AliAnalysisManager::GetAnalysisManager()->
117                                              GetMCtruthEventHandler());
118
119           if (!mcInputHandler)
120             {
121               Info("CustomAnalysisTaskInputSetup", "Creating mcInputHandler ...");
122               AliMCEventHandler* mcInputHandler = new AliMCEventHandler();
123               mgr->SetMCtruthEventHandler(mcInputHandler);
124             }
125         }
126
127     }
128   else if (!format.CompareTo("aod"))
129     {
130       AliAODInputHandler *aodInputHandler = 
131         dynamic_cast<AliAODInputHandler*>(AliAnalysisManager::GetAnalysisManager()->
132                                           GetInputEventHandler());
133
134       if (!aodInputHandler)
135         {
136           Info("CustomAnalysisTaskInputSetup", "Creating aodInputHandler ...");
137           aodInputHandler = new AliAODInputHandler();
138           mgr->SetInputEventHandler(aodInputHandler);
139         }
140     }
141   else
142     {
143       AliWarning("Wrong input format!!! Only ESD and AOD are supported. Skipping Task ...");
144       return kFALSE;
145     }
146
147   return kTRUE;
148 }