Fix bug in building local list of valid files.
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskPIDResponse.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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: AliAnalysisTaskPIDResponse.cxx 43811 2010-09-23 14:13:31Z wiechula $ */
17 #include <TFile.h>
18 #include <TChain.h>
19
20 #include <AliAnalysisManager.h>
21 #include <AliInputEventHandler.h>
22 #include <AliVEventHandler.h>
23 #include <AliVEvent.h>
24 #include <AliVParticle.h>
25 #include <AliVTrack.h>
26 #include <AliLog.h>
27 #include <AliPIDResponse.h>
28 #include <AliESDpid.h>
29 #include <AliProdInfo.h>
30
31 #include "AliAnalysisTaskPIDResponse.h"
32
33 ClassImp(AliAnalysisTaskPIDResponse)
34
35 //______________________________________________________________________________
36 AliAnalysisTaskPIDResponse::AliAnalysisTaskPIDResponse():
37 AliAnalysisTaskSE(),
38 fIsMC(kFALSE),
39 fCachePID(kTRUE),
40 fOADBPath(),
41 fSpecialDetResponse(),
42 fPIDResponse(0x0),
43 fRun(-1),
44 fOldRun(-1),
45 fRecoPass(0),
46 fIsTunedOnData(kFALSE),
47 fTunedOnDataMask(0),
48 fRecoPassTuned(0),
49 fUseTPCEtaCorrection(kTRUE),
50 fUseTPCMultiplicityCorrection(kTRUE),
51 fUserDataRecoPass(-1)
52 {
53   //
54   // Dummy constructor
55   //
56 }
57
58 //______________________________________________________________________________
59 AliAnalysisTaskPIDResponse::AliAnalysisTaskPIDResponse(const char* name):
60 AliAnalysisTaskSE(name),
61 fIsMC(kFALSE),
62 fCachePID(kTRUE),
63 fOADBPath(),
64 fSpecialDetResponse(),
65 fPIDResponse(0x0),
66 fRun(-1),
67 fOldRun(-1),
68 fRecoPass(0),
69 fIsTunedOnData(kFALSE),
70 fTunedOnDataMask(0),
71 fRecoPassTuned(0),
72 fUseTPCEtaCorrection(kTRUE),
73 fUseTPCMultiplicityCorrection(kTRUE),  
74 fUserDataRecoPass(-1)
75 {
76   //
77   // Default constructor
78   //
79   DefineInput(0,TChain::Class());
80 }
81
82 //______________________________________________________________________________
83 AliAnalysisTaskPIDResponse::~AliAnalysisTaskPIDResponse()
84 {
85   //
86   // Destructor
87   //
88 }
89
90 //______________________________________________________________________________
91 void AliAnalysisTaskPIDResponse::UserCreateOutputObjects()
92 {
93   //
94   // Create the output QA objects
95   //
96   
97   AliLog::SetClassDebugLevel("AliAnalysisTaskPIDResponse",10);
98   
99   //input hander
100   AliAnalysisManager *man=AliAnalysisManager::GetAnalysisManager();
101   AliInputEventHandler *inputHandler=dynamic_cast<AliInputEventHandler*>(man->GetInputEventHandler());
102   if (!inputHandler) AliFatal("Input handler needed");
103   
104   //pid response object
105   inputHandler->CreatePIDResponse(fIsMC);
106   fPIDResponse=inputHandler->GetPIDResponse();
107   if (!fPIDResponse) AliFatal("PIDResponse object was not created");
108
109   fPIDResponse->SetOADBPath(AliAnalysisManager::GetOADBPath());
110   fPIDResponse->SetCachePID(fCachePID);
111   if (!fOADBPath.IsNull()) fPIDResponse->SetOADBPath(fOADBPath.Data());
112
113   if(fIsTunedOnData) fPIDResponse->SetTunedOnData(kTRUE,fRecoPassTuned);
114   if(fTunedOnDataMask != 0) fPIDResponse->SetTunedOnDataMask(fTunedOnDataMask);
115
116   if (!fSpecialDetResponse.IsNull()){
117     TObjArray *arr=fSpecialDetResponse.Tokenize("; ");
118     for (Int_t i=0; i<arr->GetEntriesFast();++i){
119       TString resp(arr->At(i)->GetName());
120       if (resp.BeginsWith("TPC:")){
121         resp.ReplaceAll("TPC:","");
122         fPIDResponse->SetCustomTPCpidResponse(resp.Data());
123         AliInfo(Form("Setting custom TPC response file: '%s'",resp.Data()));
124       }
125       else if (resp.BeginsWith("TPC-Maps:")){
126         resp.ReplaceAll("TPC-Maps:","");
127         fPIDResponse->SetCustomTPCetaMaps(resp.Data());
128         AliInfo(Form("Setting custom TPC eta maps file: '%s'",resp.Data()));
129       }
130     }
131     delete arr;
132   }  
133 }
134
135 //______________________________________________________________________________
136 void AliAnalysisTaskPIDResponse::UserExec(Option_t */*option*/)
137 {
138   // Setup the PID response functions and fill the QA histograms
139   //
140   AliVEvent *event=InputEvent();
141   if (!event) return;
142   fRun=event->GetRunNumber();
143   
144   if (fRun!=fOldRun){
145     SetRecoInfo();
146     fOldRun=fRun;
147     
148     fPIDResponse->SetUseTPCEtaCorrection(fUseTPCEtaCorrection);
149     fPIDResponse->SetUseTPCMultiplicityCorrection(fUseTPCMultiplicityCorrection);
150   }
151
152   fPIDResponse->InitialiseEvent(event,fRecoPass);
153   AliESDpid *pidresp = dynamic_cast<AliESDpid*>(fPIDResponse);
154   if(pidresp && AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler()){
155       pidresp->SetEventHandler(AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
156   }
157   //create and attach transient PID object
158 //   if (fCachePID) {
159 //     fPIDResponse->FillTrackDetectorPID();
160 //   }
161 }
162
163 //______________________________________________________________________________
164 void AliAnalysisTaskPIDResponse::SetRecoInfo()
165 {
166   //
167   // Set reconstruction information
168   //
169   
170   //reset information
171   fRecoPass=0;
172   
173   //Get UserInfo from the current tree 
174   AliAnalysisManager *mgr=AliAnalysisManager::GetAnalysisManager();
175   AliVEventHandler *inputHandler=mgr->GetInputEventHandler();
176   if (!inputHandler) return;
177
178   TList *uiList = inputHandler->GetUserInfo();
179   AliProdInfo prodInfo(uiList);
180   prodInfo.List();
181
182   TTree *tree= (TTree*)inputHandler->GetTree();
183   TFile *file= (TFile*)tree->GetCurrentFile();
184   if (!file) {
185     AliError("Current file not found, cannot set reconstruction information");
186     return;
187   } else {
188     TString fileName(file->GetName());
189     fPIDResponse->SetCurrentFile(fileName.Data());
190   }
191
192   fPIDResponse->SetCurrentAliRootRev(prodInfo.GetAlirootSvnVersion());
193   
194   if (prodInfo.IsMC() == kTRUE) fIsMC=kTRUE;         // protection if user didn't use macro switch
195   if ( (prodInfo.IsMC() == kFALSE) && (fIsMC == kFALSE) ) {      // reco pass is needed only for data
196
197     if (fUserDataRecoPass > -1) {
198       AliInfo(Form("Data reconstruction pass is user specified. Setting pass #: %d",fUserDataRecoPass));
199       fRecoPass = fUserDataRecoPass;
200     } else {
201       fRecoPass = prodInfo.GetRecoPass();
202       if (fRecoPass < 0) {   // as last resort we find pass from file name (UGLY, but not stored in ESDs/AODs before LHC12d )
203         TString fileName(file->GetName());
204         if (fileName.Contains("pass1") ) {
205           fRecoPass=1;
206         } else if (fileName.Contains("pass2") ) {
207           fRecoPass=2;
208         } else if (fileName.Contains("pass3") ) {
209           fRecoPass=3;
210         } else if (fileName.Contains("pass4") ) {
211           fRecoPass=4;
212         } else if (fileName.Contains("pass5") ) {
213           fRecoPass=5;
214         }
215     } 
216     }
217     if (fRecoPass <= 0) {
218       AliError(" ******** Failed to find reconstruction pass number *********");
219       AliError(" ******** PID information loaded for 'pass 0': parameters unreliable ******");
220       AliError("      --> If these are MC data: please set kTRUE first argument of AddTaskPIDResponse");
221       AliError("      --> If these are real data: ");
222       AliError("          (a) please insert pass number inside the path of your local file  OR");
223       AliError("          (b) specify reconstruction pass number when adding PIDResponse task");
224       AliFatal(" no pass number specified for this data file, abort. Asserting AliFatal");
225     }
226   }
227
228 }