Merge branch 'master', remote branch 'origin' into TPCdev
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskFilterSteer.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*$Id$
17
18 */
19
20 /////////////////////////////////////////////////////////////
21 //
22 //   Filtering task:  
23 //   Selection of only 1% of the events for which to keep  
24 //   the ESD friend
25 //
26 // /////////////////////////////////////////////////////////////
27
28 #include <TTree.h>
29 #include <TChain.h>
30
31 #include "AliLog.h"
32 #include "AliESDInputHandler.h"
33 #include "AliESDtrack.h"
34 #include "AliESDEvent.h"
35 #include "AliESDfriend.h"
36 #include "AliAnalysisTaskFilter.h"
37 #include "AliAnalysisManager.h"
38 #include "AliAnalysisTaskFilterSteer.h"
39 #include "TRandom.h"
40
41
42 ClassImp(AliAnalysisTaskFilterSteer)
43
44
45 //________________________________________________________________________
46 AliAnalysisTaskFilterSteer::AliAnalysisTaskFilterSteer():
47 AliAnalysisTaskFilter(),
48 fFraction(0.01),
49 fESDInput(0),
50 fESDfriendInput(0)
51 {
52         // Constructor
53         
54         // Define input and output slots here
55         // Input slot #0 works with a TChain
56         DefineInput(0, TChain::Class());
57         // Output slot #0 writes into a TTree
58         //DefineOutput(0,TTree::Class());  
59 }
60
61 //________________________________________________________________________
62 AliAnalysisTaskFilterSteer::AliAnalysisTaskFilterSteer(const char* name):
63 AliAnalysisTaskFilter(name),
64 fFraction(0.01),
65 fESDInput(0),
66 fESDfriendInput(0)
67 {
68         // Constructor
69         
70         // Define input and output slots here
71         // Input slot #0 works with a TChain
72         DefineInput(0, TChain::Class());
73         // Output slot #0 writes into a TTree
74         //DefineOutput(0,TTree::Class());  
75 }
76
77 //________________________________________________________________________
78 AliAnalysisTaskFilterSteer::~AliAnalysisTaskFilterSteer()
79 {
80
81         // dtor
82
83 }  
84
85 //________________________________________________________________________
86 void AliAnalysisTaskFilterSteer::Init()
87 {
88         // Initialization
89         
90         return;
91 }
92
93 //________________________________________________________________________
94 void AliAnalysisTaskFilterSteer::UserCreateOutputObjects()
95 {
96         //
97         // Create the output container
98         //
99         
100         return;
101 }
102
103 //________________________________________________________________________
104 void AliAnalysisTaskFilterSteer::UserExec(Option_t */*option*/)
105 {
106
107         // 
108         // Applying random selection of the events
109
110         fESDInput = dynamic_cast<AliESDEvent*>(InputEvent()); // get the input ESD
111         fESDfriendInput = InputFriend();  // get the input friend
112         if(!fESDInput) {
113                 printf("AliAnalysisTaskFilterSteer::Exec(): no ESD \n");
114                 return;
115         } 
116         if(!fESDfriendInput) {
117                 printf("AliAnalysisTaskFilterSteer::Exec(): no ESDfriend \n");
118                 return;
119         } 
120
121         // attach ESDfriend
122         
123         AliESDfriend* esdFriendOutput = (AliESDfriend*)ESDfriend();  
124         AliDebug(3,Form("Number of ESD tracks in input = %d ",fESDInput->GetNumberOfTracks()));
125         AliDebug(3,Form("Number of tracks in input friends = %d ",fESDfriendInput->GetNumberOfTracks()));
126         AliDebug(3,Form("Number of tracks in output friendsNew before filtering = %d ",esdFriendOutput->GetNumberOfTracks()));
127         
128         //
129         //  keeping all the tracks for the randomly "fFraction" of the total number of events
130         //
131
132         for (Int_t i = 0; i< fESDInput->GetNumberOfTracks(); i++){
133                 AliESDfriendTrack* tOld = (AliESDfriendTrack*)fESDfriendInput->GetTrack(i);
134                 AddFriendTrackAt(tOld,i);
135         }                        
136         AliDebug(2,Form("Number of tracks in output friendsNew after filtering = %d ",esdFriendOutput->GetNumberOfTracks()));
137         AliDebug(2,Form("Number of tracks in output friendsNew after filtering with GetEntries() = %d ",esdFriendOutput->GetEntriesInTracks()));
138
139         return;
140 }
141
142 //________________________________________________________________________
143 void AliAnalysisTaskFilterSteer::Terminate(Option_t */*option*/)
144 {
145         // Terminate analysis
146         //
147         AliDebug(2,"AliAnalysisTaskFilterSteer: Terminate() \n");
148         
149         return;
150 }
151 //________________________________________________________________________
152 Bool_t AliAnalysisTaskFilterSteer::UserSelectESDfriendForCurrentEvent()
153 {
154         // 
155         // Selecting or discarding current event
156         //
157         Double_t number = gRandom->Rndm();
158         if (number<fFraction){
159                 // keeping event
160                 AliDebug(2,Form("*****************Selecting event (number = %f)",number));
161                 return kTRUE;   
162         }
163         AliDebug(2,Form("*****************Skipping event (number = %f)",number));
164         return kFALSE;
165 }