Possibility to filter the ESD friends and add objects to AliESDfriends.root. Major...
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskFilterFriendSecond.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 //   Test task
21 //
22 // /////////////////////////////////////////////////////////////
23
24 #include <TTree.h>
25 #include <TChain.h>
26
27 #include "AliLog.h"
28 #include "AliESDInputHandler.h"
29 #include "AliESDtrack.h"
30 #include "AliESDEvent.h"
31 #include "AliESDfriend.h"
32 #include "AliAnalysisTaskFilter.h"
33 #include "AliAnalysisManager.h"
34 #include "AliAnalysisTaskFilterFriendSecond.h"
35
36
37 ClassImp(AliAnalysisTaskFilterFriendSecond)
38
39
40 //________________________________________________________________________
41 AliAnalysisTaskFilterFriendSecond::AliAnalysisTaskFilterFriendSecond():
42 AliAnalysisTaskFilter(),
43 fESDInput(0),
44 fESDfriendInput(0)
45 {
46         // Constructor
47         
48         // Define input and output slots here
49         // Input slot #0 works with a TChain
50         DefineInput(0, TChain::Class());
51         // Output slot #0 writes into a TTree
52         //      DefineOutput(0,TTree::Class());  
53 }
54
55 //________________________________________________________________________
56 AliAnalysisTaskFilterFriendSecond::AliAnalysisTaskFilterFriendSecond(const char* name):
57 AliAnalysisTaskFilter(name),
58 fESDInput(0),
59 fESDfriendInput(0)
60 {
61         // Constructor
62         
63         // Define input and output slots here
64         // Input slot #0 works with a TChain
65         DefineInput(0, TChain::Class());
66         // Output slot #0 writes into a TTree
67         //DefineOutput(0,TTree::Class());  
68 }
69
70 //________________________________________________________________________
71 AliAnalysisTaskFilterFriendSecond::~AliAnalysisTaskFilterFriendSecond()
72 {
73
74         // dtor
75
76 }  
77
78 //________________________________________________________________________
79 void AliAnalysisTaskFilterFriendSecond::Init()
80 {
81         // Initialization
82         
83         return;
84 }
85
86 //________________________________________________________________________
87 void AliAnalysisTaskFilterFriendSecond::UserCreateOutputObjects()
88 {
89         //
90         // Create the output container
91         //
92         
93         return;
94 }
95
96 //________________________________________________________________________
97 void AliAnalysisTaskFilterFriendSecond::UserExec(Option_t */*option*/)
98 {
99
100         AliInfo("Filling Friends");
101
102         fESDInput = dynamic_cast<AliESDEvent*>(InputEvent()); // get the input ESD
103         fESDfriendInput = (AliESDfriend*)(fESDInput->FindListObject("AliESDfriend"));
104         if(!fESDInput) {
105                 printf("AliAnalysisTaskFilterFriendSecond::Exec(): no ESD \n");
106                 return;
107         } 
108         if(!fESDfriendInput) {
109                 printf("AliAnalysisTaskFilterFriendSecond::Exec(): no ESDfriend \n");
110                 return;
111         } 
112         // attach ESDfriend
113         AliESDEvent* esdEventOutput = (AliESDEvent*)ESDEvent();
114         
115         AliESDfriend* esdFriendOutput = (AliESDfriend*)ESDfriend();  
116         AliInfo(Form("Number of ESD tracks in input = %d ",fESDInput->GetNumberOfTracks()));
117         AliInfo(Form("Number of ESD tracks in output = %d ",esdEventOutput->GetNumberOfTracks()));
118         AliInfo(Form("Number of tracks in input friends = %d ",fESDfriendInput->GetNumberOfTracks()));
119         AliInfo(Form("Number of tracks in output friendsNew before filtering = %d ",esdFriendOutput->GetNumberOfTracks()));
120         
121         AliESDfriendTrack* tNull = new AliESDfriendTrack();
122
123         for (Int_t i = 0; i< fESDInput->GetNumberOfTracks(); i++){
124                 if (i%3 == 0){
125                         // keep friend
126                         AliInfo(Form("Keeping %d-th track",i));
127                         esdEventOutput->GetTrack(i);
128                         AliESDfriendTrack* tOld = (AliESDfriendTrack*)fESDfriendInput->GetTrack(i);
129                         AliDebug(2,Form("1P of the %d-th track = %f",i,tOld->Get1P()));
130                         AddFriendTrackAt(tOld,i);
131                         //      tOld->Dump();
132                 }
133                 else {
134                         //discard friend 
135                         AddFriendTrackAt(tNull,i);
136                 }
137         } 
138         AliInfo(Form("Number of tracks in output friendsNew after filtering = %d ",esdFriendOutput->GetNumberOfTracks()));
139         AliInfo(Form("Number of tracks in output friendsNew after filtering with GetEntries() = %d ",esdFriendOutput->GetEntriesInTracks()));
140         return;
141 }
142
143 //________________________________________________________________________
144 void AliAnalysisTaskFilterFriendSecond::Terminate(Option_t */*option*/)
145 {
146         // Terminate analysis
147         //
148         AliDebug(2,"AliAnalysisTaskFilterFriendSecond: Terminate() \n");
149         
150         return;
151 }
152 //________________________________________________________________________
153 Bool_t AliAnalysisTaskFilterFriendSecond::UserSelectESDfriendForCurrentEvent()
154 {
155         // 
156         // Selecting or discarding current event
157         //
158
159                 
160         fESDInput = dynamic_cast<AliESDEvent*>(InputEvent()); // get the input ESD
161         if ((fESDInput->GetNumberOfTracks())%2 == 1){
162                 AliInfo("Selecting event");
163                 return kTRUE;
164         }
165         
166         AliInfo("Discarding event");    
167         return kFALSE;
168         
169         //return kTRUE;
170 }