]> git.uio.no Git - u/mrichter/AliRoot.git/blob - doc/Distributed-Analysis/Analysis.tex
- AliMUONRecoParam.cxx:
[u/mrichter/AliRoot.git] / doc / Distributed-Analysis / Analysis.tex
1 %________________________________________________________
2 \section{Analysis framework}
3 \label{Note:ANALYSIS}
4
5 Recently, a new attempt has started that led to the development of a a new analysis framework \cite{Note:RefAnalysisFramework}. By the time this note was being written the framework has been validated in processes that concern this document (GRID analysis). We will review the basic classes and functionalities of this system in this paragraph (for further details the reader should look in \cite{Note:RefAnalysisFramework,Note:RefAlienTutorial}).
6
7 The basic classes that form this development are the following (we will give a full example of an overall implementation later):
8
9 \begin{itemize}
10 \item {\ttfamily AliAnalysisDataContainer:} The class that allows the user to define the basic input and output containers.
11 \item {\ttfamily AliAnalysisTask:} This is the class the lines of which should be implemented by the user. In the source file we can write the analysis code. 
12 \item {\ttfamily AliAnalysisManager:} Inside such a manager the user defines the analysis containers, the relevant tasks as well as the connection between them.
13 \end{itemize}
14
15 A practical example of a simple usage of this framework is given below where we extract a $P_{T}$ spectrum of all charged tracks (histogram) from an ESD chain (the examples can be found under the AnalysisMacros/Local/ directory of the PWG2 module of AliRoot).
16
17 \vspace{0.5 cm}
18 \textbf{AliAnalysisTaskPt.h}
19 \begin{lstlisting}[language=C++]
20   #include "TH1.h"
21   #include "AliESD.h"
22   #include "AliAnalysisTask.h"
23
24   class AliAnalysisTaskPt : public AliAnalysisTask {
25     public:
26     AliAnalysisTaskPt(const char *name);
27     virtual ~AliAnalysisTaskPt() {}
28     
29     virtual void   Init(Option_t *);
30     virtual void   Exec(Option_t *option);
31     virtual void   Terminate(Option_t *);
32     
33     private:
34     AliESD *fESD; //ESD object
35     TH1F   *fHistPt; //Pt spectrum
36     
37     ClassDef(AliAnalysisTaskPt, 0); // example of analysis
38   };
39 \end{lstlisting}
40
41 \vspace{0.5 cm}
42 \textbf{AliAnalysisTaskPt.cxx}
43 \begin{lstlisting}[language=C++]
44   #define AliAnalysisTaskPt_cxx
45
46   #include "TChain.h"
47   #include "TH1.h"
48   #include "TCanvas.h"
49   #include "TSystem.h"
50   #include "AliAnalysisTask.h"
51   #include "AliESD.h"
52   
53   #include "AliAnalysisTaskPt.h"
54   
55   ClassImp(AliAnalysisTaskPt)
56   
57   //________________________________________________________________________
58   AliAnalysisTaskPt::AliAnalysisTaskPt(const char *name) 
59   :AliAnalysisTask(name,""), fESD(0), fHistPt(0) {
60     // Constructor.
61     // Input slot #0 works with a TChain
62     DefineInput(0, TChain::Class());
63     // Output slot #0 writes into a TH1 container
64     DefineOutput(0, TH1F::Class());
65   }
66   
67   //________________________________________________________________________
68   void AliAnalysisTaskPt::Init(Option_t *) {
69     printf("   Init %s\n", GetName());
70     
71     if (!fESD) {
72       char ** address = (char **)GetBranchAddress(0, "ESD");
73       if (address) fESD = (AliESD*)(*address);
74       if (!fESD) {
75         fESD = new AliESD();
76         SetBranchAddress(0, "ESD", &fESD);
77       }
78       OpenFile(0,"Pt.ESD.1.root","RECREATE");
79     }
80     
81     if (!fHistPt) {
82       fHistPt = new TH1F("fHistPt","This is the Pt distribution",15,0.1,3.1);
83       fHistPt->SetStats(kTRUE);
84       fHistPt->GetXaxis()->SetTitle("P_{T} [GeV]");
85       fHistPt->GetYaxis()->SetTitle("#frac{dN}{dP_{T}}");
86       fHistPt->GetXaxis()->SetTitleColor(1);
87       fHistPt->SetMarkerStyle(kFullCircle);
88     }
89   }
90   
91   //________________________________________________________________________
92   void AliAnalysisTaskPt::Exec(Option_t *) {
93     // Task making a pt distribution.
94     // Get input data
95     TChain *chain = (TChain*)GetInputData(0);
96     Long64_t ientry = chain->GetReadEntry();
97     if (!fESD) return;
98     
99     printf("Tracks: %d \n",fESD->GetNumberOfTracks());
100     for(Int_t iTracks = 0; iTracks < fESD->GetNumberOfTracks(); iTracks++) {
101       AliESDtrack * ESDTrack = fESD->GetTrack(iTracks);
102       Double_t momentum[3];
103       ESDTrack->GetPxPyPz(momentum);
104       Double_t Pt = sqrt(pow(momentum[0],2) + pow(momentum[1],2));
105       fHistPt->Fill(Pt);
106     }//track loop 
107     // Post final data. It will be written to a file with option "RECREATE"
108     PostData(0, fHistPt);
109   }      
110   
111   //________________________________________________________________________
112   void AliAnalysisTaskPt::Terminate(Option_t *) {
113     // Draw some histogram at the end.
114     if (!gROOT->IsBatch()) {
115       TCanvas *c1 = new TCanvas("c1","Pt",10,10,310,310);
116       c1->SetFillColor(10);
117       c1->SetHighLightColor(10);   
118       c1->cd(1)->SetLeftMargin(0.15);
119       c1->cd(1)->SetBottomMargin(0.15);  
120       c1->cd(1)->SetLogy();
121       fHistPt->DrawCopy("E");
122     }
123   }
124 \end{lstlisting}
125
126 The {\ttfamily AliAnalysisTaskPt} is a sample task that inherits from the {\ttfamily AliAnalysisTask} class. The main functions that need to be implemented are the following:
127
128 \begin{itemize}
129 \item Basic constructor: Inside the constructor we need to define the type of the input (if any input is to be used) as well as of the output of the task (in our example the input is of the form of a ROOT's {\ttfamily TChain} while the output is a histogram - {\ttfamily TH1F}).
130 \item {\ttfamily Init:} Inside this function we need to initialize the objects, assign the branch addresses to these objects, create the output file as well as the output histograms (as in our example) or objects.
131 \item {\ttfamily Exec:} The place where the analysis code should be implemented.
132 \item {\ttfamily Terminate:} The function inside of which we can draw histograms (as in our example) or perform any kind of actions (like merging of output).
133 \end{itemize}
134
135 \vspace{0.5 cm}
136 \textbf{Macro that creates an AliAnalysisManager}
137 \begin{lstlisting}[language=C++]
138   //____________________________________________//
139   // Make the analysis manager
140   AliAnalysisManager *mgr = new AliAnalysisManager();
141   //____________________________________________//
142   // 1st Pt task
143   AliAnalysisTask *task1 = new AliAnalysisTaskPt("TaskPt");
144   mgr->AddTask(task1);
145   // Create containers for input/output
146   AliAnalysisDataContainer *cinput1 = mgr->CreateContainer("cchain1",
147   TChain::Class(),AliAnalysisManager::kInputContainer);
148   AliAnalysisDataContainer *coutput1 = mgr->CreateContainer("chist1", 
149   TH1::Class(),AliAnalysisManager::kOutputContainer);
150   
151   //____________________________________________//
152   mgr->ConnectInput(task1,0,cinput1);
153   mgr->ConnectOutput(task1,0,coutput1);
154   cinput1->SetData(chain1);
155   
156   if (mgr->InitAnalysis()) {
157     mgr->PrintStatus();
158     chain1->Process(mgr);
159   }
160 \end{lstlisting}
161
162 \vspace{0.2 cm}
163 In the previous lines we firstly created a new analysis manager:\\
164 \\
165 {\ttfamily AliAnalysisManager *mgr = new AliAnalysisManager();}\\
166 \\
167 We then created a new {\ttfamily AliAnalysisTaskPt} object giving it a name and we assigned it to the manager:\\
168 \\
169 {\ttfamily AliAnalysisTask *task1 = new AliAnalysisTaskPt("TaskPt");}\\
170 {\ttfamily mgr->AddTask(task1);}\\
171 \\
172 Then the input and output containers were created while defining in parallel their types: \\
173 \\
174 {\ttfamily AliAnalysisDataContainer *cinput1 = mgr->CreateContainer("cchain1", TChain::Class(),AliAnalysisManager::kInputContainer);}\\
175 {\ttfamily AliAnalysisDataContainer *coutput1 = mgr->CreateContainer("chist1", TH1::Class(),AliAnalysisManager::kOutputContainer);}\\
176 \\
177 The next step was to link the task with the corresponding input and output container while setting the created chain of files (in the following section we will review how we can create this chain) to our input:\\
178 \\
179 {\ttfamily mgr->ConnectInput(task1,0,cinput1);}\\
180 {\ttfamily mgr->ConnectOutput(task1,0,coutput1);}\\
181 {\ttfamily cinput1->SetData(chain1);}\\
182 \\
183 Finally we process the chain with the manager:\\
184 \\
185 {\ttfamily   if (mgr->InitAnalysis()) chain1->Process(mgr);}\\
186
187 \vspace{0.5 cm}
188 In order for the user to have an idea of how the framework could look like in a complicated example, we provide figures \ref{Note:FigAnalysisFramework1} and \ref{Note:FigAnalysisFramework2}. In fig. \ref{Note:FigAnalysisFramework1} we show the flow of a typical analysis process:
189
190 A user interacts with the file catalog and creates a tag collection (the reader should check section \ref{Note:FLOW} for the instructions on how to perform this action). Then the \tag is queried (details on how we can do this will be provided in the next sections) and a chain of files is created. While looping the entries of this chain we split our analysis into two branches, on the first of which we analyze the (real or simulated) data and we provide as a last step an output ROOT file with histograms, while on the second we mix the events (event mixing method) in order to express our background. This second branch creates an output of the form of an Analysis Object Data (AOD) \cite{Note:RefComputingTDR}. A new chain is then created from this AOD and after analyzing the corresponding entries (analysis of mixed events) the output ROOT file with the relevant histograms is created. Finally, a last process is launched that compares the two output ROOT files and extracts the studied signal.
191
192
193 \begin{figure}[ht!]
194 \begin{center}
195 \includegraphics[width=15cm]{figures/AnalysisFramework-1.eps}
196 \end{center}
197 \caption{The flow of a typical physics analysis.}
198 \label{Note:FigAnalysisFramework1}
199 \end{figure}
200
201 \begin{figure}[ht!]
202 \begin{center}
203 \includegraphics[width=15cm]{figures/AnalysisFramework-2.eps}
204 \end{center}
205 \caption{This figure shows how the physics analysis described in fig. \ref{Note:FigAnalysisFramework1} can be integrated in the new framework.}
206 \label{Note:FigAnalysisFramework2}
207 \end{figure}
208
209 Fig. \ref{Note:FigAnalysisFramework2} shows how we can integrate the previous use case to the new framework. The first steps are identical: A user interacts with the file catalog and creates a tag collection (as described in section \ref{Note:FLOW}) which is used as an input to query the \tag ~and create a chain of files. This chain is the first input container and is assigned to the {\ttfamily AliAnalysisManager}. In parallel we define two tasks which are also assigned to the manager and are both linked to the same input container (chain): the first analyzes the input data and creates an output container (ROOT file with histograms - signal + background plots), while the second is designed to mix the events and create a second output container (AOD). In order to analyze our mixed events, we initialize a second analysis manager which links the input container (AOD) with the new task (analysis of mixed events) and create a third output container (ROOT file with histogram - background plots). Finally the comparison task is added to the second manager. This task is triggered by the ending of the analysis of mixed events and takes two input containers (ROOT files having the signal + background plots and the pure background plots) while creating one output (extracted signal).
210
211 In the next paragraphs, we will be using the simplest possible case of manager and task which has been described in the beginning of this section.
212
213
214
215