]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ANALYSIS/TenderSupplies/AliTRDTenderSupply.cxx
count correctly the number of libraries
[u/mrichter/AliRoot.git] / ANALYSIS / TenderSupplies / AliTRDTenderSupply.cxx
CommitLineData
e75408ba 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
17///////////////////////////////////////////////////////////////////////////////
18// //
19// TRD tender: reapply pid on the fly //
20// //
21///////////////////////////////////////////////////////////////////////////////
22
00685073 23#include <TChain.h>
24#include <TList.h>
25#include <TObjString.h>
26#include <TTree.h>
27#include <TString.h>
28
29#include <AliCDBEntry.h>
30#include <AliCDBId.h>
31#include <AliCDBManager.h>
32#include <AliTRDCalDet.h>
e75408ba 33
34#include <AliLog.h>
35#include <TTree.h>
36#include <TChain.h>
37#include <AliPID.h>
38#include <AliVEvent.h>
39#include <AliESDEvent.h>
40#include <AliESDpid.h>
41#include <AliESDtrack.h>
42#include <AliESDInputHandler.h>
43#include <AliAnalysisManager.h>
51a0ce25 44#include <AliTRDPIDReference.h>
e75408ba 45#include <AliTRDPIDResponse.h>
46#include <AliTender.h>
47
48#include "AliTRDTenderSupply.h"
49
00685073 50ClassImp(AliTRDTenderSupply)
e75408ba 51
00685073 52//_____________________________________________________
e75408ba 53AliTRDTenderSupply::AliTRDTenderSupply() :
54 AliTenderSupply(),
00685073 55 fESD(NULL),
56 fESDpid(NULL),
57 fChamberGainOld(NULL),
58 fChamberGainNew(NULL),
59 fChamberVdriftOld(NULL),
60 fChamberVdriftNew(NULL),
51a0ce25 61 fRefFilename(""),
9daf22f8 62 fPIDmethod(kNNpid),
51a0ce25 63 fNormalizationFactor(1.),
00685073 64 fPthreshold(0.8),
65 fNBadChambers(0),
51a0ce25 66 fGainCorrection(kTRUE),
67 fLoadReferencesFromCDB(kFALSE),
68 fHasReferences(kFALSE)
e75408ba 69{
70 //
71 // default ctor
72 //
39a88d47 73 memset(fBadChamberID, 0, sizeof(Int_t) * kNChambers);
e75408ba 74}
75
76//_____________________________________________________
77AliTRDTenderSupply::AliTRDTenderSupply(const char *name, const AliTender *tender) :
78 AliTenderSupply(name,tender),
00685073 79 fESD(NULL),
80 fESDpid(NULL),
81 fChamberGainOld(NULL),
82 fChamberGainNew(NULL),
83 fChamberVdriftOld(NULL),
84 fChamberVdriftNew(NULL),
51a0ce25 85 fRefFilename(""),
9daf22f8 86 fPIDmethod(kNNpid),
51a0ce25 87 fNormalizationFactor(1.),
00685073 88 fPthreshold(0.8),
89 fNBadChambers(0),
51a0ce25 90 fGainCorrection(kTRUE),
91 fLoadReferencesFromCDB(kFALSE),
92 fHasReferences(kFALSE)
e75408ba 93{
94 //
95 // named ctor
96 //
00685073 97 memset(fBadChamberID, 0, sizeof(Int_t) * kNChambers);
e75408ba 98}
99
00685073 100//_____________________________________________________
e75408ba 101AliTRDTenderSupply::~AliTRDTenderSupply()
102{
103 //
104 // dtor
105 //
106}
107
108//_____________________________________________________
109void AliTRDTenderSupply::Init()
110{
111 //
112 // Initialise TRD tender
113 //
114
e75408ba 115 AliAnalysisManager *mgr=AliAnalysisManager::GetAnalysisManager();
116
117 // 1DLQ PID implemented in the AliESD object
118 fESDpid=fTender->GetESDhandler()->GetESDpid();
119 if (!fESDpid) {
120 fESDpid=new AliESDpid;
121 fTender->GetESDhandler()->SetESDpid(fESDpid);
122 }
51a0ce25 123 // Load References
124 if(!fLoadReferencesFromCDB) LoadReferences();
125 fESDpid->GetTRDResponse().SetGainNormalisationFactor(fNormalizationFactor);
126
e75408ba 127 // Set Normalisation Factors
128 if(mgr->GetMCtruthEventHandler()){
129 // Assume MC
9daf22f8 130 //fESDpid->GetTRDResponse().SetGainNormalisationFactor(1.);
00685073 131 SwitchOffGainCorrection();
e75408ba 132 }
133 else{
134 // Assume Data
9daf22f8 135 //if(fPIDmethod == kNNpid) fPidRecal->SetGainScaleFactor(1.14);
136 //fESDpid->GetTRDResponse().SetGainNormalisationFactor(1.14);
00685073 137 SwitchOnGainCorrection();
e75408ba 138 }
e75408ba 139}
140
141//_____________________________________________________
142void AliTRDTenderSupply::ProcessEvent()
143{
144 //
145 // Reapply pid information
146 //
00685073 147 if (fTender->RunChanged()){
148 AliDebug(0, Form("AliTPCTenderSupply::ProcessEvent - Run Changed (%d)\n",fTender->GetRun()));
149 if (fGainCorrection) SetChamberGain();
51a0ce25 150 if(!fHasReferences) LoadReferences();
00685073 151 }
e75408ba 152
00685073 153 fESD = fTender->GetEvent();
154 if (!fESD) return;
155 Int_t ntracks=fESD->GetNumberOfTracks();
156
157 //
158 // recalculate PID probabilities
159 //
160 for(Int_t itrack = 0; itrack < ntracks; itrack++){
161 AliESDtrack *track=fESD->GetTrack(itrack);
9daf22f8 162 // Recalculate likelihoods
00685073 163 if(!(track->GetStatus() & AliESDtrack::kTRDout)) continue;
164 if(fGainCorrection) ApplyGainCorrection(track);
9daf22f8 165 switch(fPIDmethod){
166 case kNNpid:
167 break;
168 case k1DLQpid:
169 fESDpid->MakeTRDPID(fESD->GetTrack(itrack));
170 break;
171 default:
172 AliError("PID Method not implemented (yet)");
173 }
00685073 174 }
175}
e75408ba 176
51a0ce25 177//_____________________________________________________
178void AliTRDTenderSupply::LoadReferences(){
179 //
180 // Load Reference from the OCDB/OADB into the PID Response
181 //
182 if(fLoadReferencesFromCDB){
183 AliDebug(1, "Loading Reference Distributions from the OCDB");
184 AliCDBEntry *en = fTender->GetCDBManager()->Get("TRD/Calib/PIDLQ1D");
185 if(!en){
186 AliError("References for 1D Likelihood Method not in OCDB");
187 return;
188 }
189 en->GetId().Print();
190 TObjArray *arr = dynamic_cast<TObjArray *>(en->GetObject());
191 if(!arr) AliError("List with the references not found");
192
193 // Get new references
194 TIter refs(arr);
195 TObject *o = NULL;
196 AliTRDPIDReference *ref = NULL;
197 while((o = refs())){
198 if(!TString(o->IsA()->GetName()).CompareTo("AliTRDPIDReference")){
199 ref = dynamic_cast<AliTRDPIDReference *>(o);
200 break;
201 }
202 }
203 if(ref){
204 fESDpid->GetTRDResponse().Load(ref);
205 fHasReferences = kTRUE;
206 AliInfo("Reference distributions loaded into the PID Response");
207 } else {
208 AliError("References not found");
209 }
210 } else {
211 // Backward compatibility mode
212 AliInfo("Loading Reference Distributions from ROOT file");
213 if(fRefFilename.Length() != 0){
214 fESDpid->GetTRDResponse().Load(fRefFilename.Data());
215 fHasReferences = kTRUE;
216 } else{
217 AliError("No file defined");
218 }
219 }
220}
221
00685073 222//_____________________________________________________
223void AliTRDTenderSupply::SetChamberGain(){
224 //
225 // Load Chamber Gain factors into the Tender supply
226 //
e75408ba 227
00685073 228 //find previous entry from the UserInfo
51a0ce25 229 TTree *tree=((TChain*)fTender->GetInputData(0))->GetTree();
00685073 230 if (!tree) {
231 AliError("Tree not found in ESDhandler");
232 return;
233 }
234
235 TList *userInfo=(TList*)tree->GetUserInfo();
236 if (!userInfo) {
237 AliError("No UserInfo found in tree");
238 return;
239 }
240
241 TList *cdbList=(TList*)userInfo->FindObject("cdbList");
242 if (!cdbList) {
243 AliError("No cdbList found in UserInfo");
244 if (AliLog::GetGlobalLogLevel()>=AliLog::kError) userInfo->Print();
245 return;
246 }
247
248 TIter nextCDB(cdbList);
249 TObjString *os=0x0;
250 while ( (os=(TObjString*)nextCDB()) ){
251 if(os->GetString().Contains("TRD/Calib/ChamberGainFactor")){
252 // Get Old gain calibration
253 AliCDBId *id=AliCDBId::MakeFromString(os->GetString());
254
9daf22f8 255 AliCDBEntry *entry=fTender->GetCDBManager()->Get(id->GetPath(), id->GetFirstRun(), id->GetVersion());
00685073 256 if (!entry) {
257 AliError("No previous gain calibration entry found");
258 return;
259 }
260
261 fChamberGainOld = dynamic_cast<AliTRDCalDet *>(entry->GetObject());
262
263 AliDebug(1, Form("Used old Gain entry: %s\n",entry->GetId().ToString().Data()));
264 } else if(os->GetString().Contains("TRD/Calib/ChamberVdrift")){
265 // Get Old drift velocity calibration
266 AliCDBId *id=AliCDBId::MakeFromString(os->GetString());
267
9daf22f8 268 AliCDBEntry *entry=fTender->GetCDBManager()->Get(id->GetPath(), id->GetFirstRun(), id->GetVersion());
00685073 269 if (!entry) {
270 AliError("No previous drift velocity calibration entry found");
271 return;
272 }
273
274 fChamberVdriftOld = dynamic_cast<AliTRDCalDet *>(entry->GetObject());
275
276 AliDebug(1, Form("Used old Drift velocity entry: %s\n",entry->GetId().ToString().Data()));
277
278 }
279 }
280
281 // Get Latest Gain Calib Object
282 AliCDBEntry *entryNew=fTender->GetCDBManager()->Get("TRD/Calib/ChamberGainFactor",fTender->GetRun());
283 if (entryNew) {
284 AliDebug(1, Form("Used new Gain entry: %s\n",entryNew->GetId().ToString().Data()));
285 fChamberGainNew = dynamic_cast<AliTRDCalDet *>(entryNew->GetObject());
286 } else
287 AliError("No new gain calibration entry found");
288
289 // Also get the latest Drift Velocity calibration object
290 entryNew=fTender->GetCDBManager()->Get("TRD/Calib/ChamberVdrift",fTender->GetRun());
291 if (entryNew) {
292 AliDebug(1, Form("Used new Drift velocity entry: %s\n",entryNew->GetId().ToString().Data()));
293 fChamberVdriftNew = dynamic_cast<AliTRDCalDet *>(entryNew->GetObject());
294 } else
295 AliError("No new drift velocity calibration entry found");
296
297 if(!fChamberGainNew || !fChamberVdriftNew) AliError("No recent calibration found");
298}
299
300//_____________________________________________________
301void AliTRDTenderSupply::ApplyGainCorrection(AliESDtrack * track){
e75408ba 302 //
00685073 303 // Apply new gain factors to the track
e75408ba 304 //
00685073 305 if(!fChamberGainNew || !fChamberGainOld){
306 AliError("Cannot apply gain correction.");
307 return;
308 }
309
310 if(!(track->GetStatus() & AliESDtrack::kTRDout)) return;
311 Double_t p = track->GetOuterParam() ? track->GetOuterParam()->P() : track->P();
312 if(p < fPthreshold) return; // Apply low momentum cutoff
313
314 Bool_t applyCorrectionVdrift = kFALSE;
315 if(fChamberVdriftOld && fChamberVdriftNew) applyCorrectionVdrift = kTRUE;
316
317 Int_t chamberID[kNPlanes];
318 for(Int_t il = 0; il < kNPlanes; il++) chamberID[il] = -1;
319 if(!GetTRDchamberID(track, chamberID)) return;
320 Int_t nTrackletsPID = 0, nTracklets = track->GetTRDntracklets();
321 for(Int_t iplane = 0; iplane < kNPlanes; iplane++){
322 if(chamberID[iplane] < 0) continue;
323
324 // Mask out bad chambers
325 Bool_t isMasked = kFALSE;
326 for(UInt_t icam = 0; icam < fNBadChambers; icam++)
327 if(fBadChamberID[icam] == chamberID[iplane]){
328 isMasked = kTRUE;
329 break;
330 }
331 if(isMasked){
332 for(Int_t islice = 0; islice < track->GetNumberOfTRDslices(); islice++){
333 track->SetTRDslice(0, iplane, islice);
334 }
335 continue;
336 }
337
338 // Take old and new gain factor and make ratio
339 Double_t facOld = fChamberGainOld->GetValue(chamberID[iplane]);
340 Double_t facNew = fChamberGainNew->GetValue(chamberID[iplane]);
341 Double_t correction = facNew/facOld;
342 if(applyCorrectionVdrift){
343 // apply also correction for drift velocity calibration
344 Double_t vDriftOld = fChamberVdriftOld->GetValue(chamberID[iplane]);
345 Double_t vDriftNew = fChamberVdriftNew->GetValue(chamberID[iplane]);
346 correction *= vDriftNew/vDriftOld;
347 }
348 AliDebug(2, Form("Applying correction factor %f\n", correction));
349 Int_t nSlices = 0;
350 for(Int_t islice = 0; islice < track->GetNumberOfTRDslices(); islice++){
351 Double_t qslice = track->GetTRDslice(iplane, islice);
352 if(qslice <= 0.) continue;
353 track->SetTRDslice(qslice / correction, iplane, islice);
354 nSlices++;
355 }
356 if(nSlices) nTrackletsPID++;
357 }
358 // Use nTrackletsPID to indicate the number of tracklets from good
359 // chambers so they are used for the PID
360 track->SetTRDntracklets(nTrackletsPID | (nTracklets << 3));
361}
362
363//_____________________________________________________
364Bool_t AliTRDTenderSupply::GetTRDchamberID(AliESDtrack * const track, Int_t *detectors) {
365 //
366 // Calculate TRD chamber ID
367 //
368 Double_t xLayer[kNPlanes] = {300.2, 312.8, 325.4, 338., 350.6, 363.2};
369 Double_t etamin[kNStacks] = {0.536, 0.157, -0.145, -0.527,-0.851};
370 Double_t etamax[kNStacks] = {0.851, 0.527, 0.145, -0.157,-0.536};
371 for(Int_t ily = 0; ily < kNPlanes; ily++) detectors[ily] = -1;
372
373 const AliExternalTrackParam *trueparam = NULL;
374 if(track->GetTPCInnerParam()) trueparam = track->GetTPCInnerParam();
375 else if(track->GetOuterParam()) trueparam = track->GetOuterParam();
376 else if(track->GetInnerParam()) trueparam = track->GetInnerParam();
377 if(!trueparam){
378 AliDebug(2, "No Track Params");
379 return kFALSE;
380 }
381
382 AliExternalTrackParam workparam(*trueparam); // Do calculation on working Copy
383 Double_t pos[3];
384 Int_t nDet = 0;
385 for(Int_t ily = 0; ily < kNPlanes; ily++){
386 if(!workparam.PropagateTo(xLayer[ily], fESD->GetMagneticField())) {
387 AliDebug(2, "Propagation failed");
e75408ba 388 break;
00685073 389 }
390 workparam.GetXYZ(pos);
391 Double_t trackAlpha = TMath::ATan2(pos[1], pos[0]);
392 if(trackAlpha < 0) trackAlpha = 2 * TMath::Pi() + trackAlpha;
393 Double_t secAlpha = 2 * TMath::Pi() / 18.;
394
395 Int_t sector = static_cast<Int_t>(trackAlpha/secAlpha);
396 Double_t etaTrack = track->Eta();
397 Int_t stack = -1;
398 for(Int_t istack = 0; istack < 5; istack++){
399 if(etaTrack >= etamin[istack] && etaTrack <= etamax[istack]){
400 stack = istack;
401 break;
402 }
403 }
404 if(stack < 0) {
405 AliDebug(2, "Dead Area");
406 continue;
407 }
408
409 detectors[ily] = sector * kNStacks * kNPlanes + stack * kNPlanes + ily;
410 nDet++;
e75408ba 411 }
00685073 412 return nDet ? kTRUE : kFALSE;
e75408ba 413}
00685073 414