Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / MUON / AliMUONChamberCalibrationTask.cxx
CommitLineData
e4af7295 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$
17
18//-----------------------------------------------------------------------------
19/// \file AliMUONChamberCalibrationTask.cxx
20/// \brief Implementation of the AliMUONChamberCalibrationTask
21/// \author Andry Rakotozafindrabe CEA/IRFU/SPhN
22//-----------------------------------------------------------------------------
23
24#include <Riostream.h>
25
26#include <TBranch.h>
27#include <TChain.h>
ce8cd162 28#include <TObjArray.h>
e4af7295 29#include <TFile.h>
30#include <TMath.h>
31#include <TRandom.h>
32#include <TROOT.h>
33#include <TString.h>
34#include <TTree.h>
35
36#include "AliMUONChamberCalibrationTask.h"
37
38// STEER includes
39#include "AliCDBManager.h"
2111e28d 40#include "AliGRPManager.h"
e4af7295 41#include "AliESDEvent.h"
42#include "AliESDInputHandler.h"
43#include "AliESDtrack.h"
44#include "AliESDMuonTrack.h"
45#include "AliLog.h"
e4af7295 46#include "AliRecoParam.h"
47#include "AliTracker.h"
48
49// ANALYSIS includes
50#include "AliAnalysisDataSlot.h"
51#include "AliAnalysisManager.h"
52
53// MUON includes
54#include "AliMpConstants.h"
55#include "AliMpCDB.h"
56#include "AliMpPad.h"
57#include "AliMpSegmentation.h"
58#include "AliMpVSegmentation.h"
59#include "AliMUONCalibrationData.h"
60#include "AliMUONClusterInfo.h"
61#include "AliMUONESDInterface.h"
62#include "AliMUONPadInfo.h"
63#include "AliMUONRecoParam.h"
64#include "AliMUONTrack.h"
65#include "AliMUONTrackParam.h"
66#include "AliMUONVCalibParam.h"
67#include "AliMUONVCluster.h"
68//#include "AliMUONVClusterStore.h"
69#include "AliMUONVDigit.h"
70#include "AliMUONVDigitStore.h"
71
72/// \cond CLASSIMP
73ClassImp( AliMUONChamberCalibrationTask )
74/// \endcond CLASSIMP
75
76//______________________________________________________________
77AliMUONChamberCalibrationTask::AliMUONChamberCalibrationTask():
78 AliAnalysisTaskSE( "AliMUONChamberCalibrationTask" ),
3ef13338 79 fOCDBPath( "local://$ALICE_ROOT/OCDB" ),
9ee1d6ff 80 fCalibChoice(kNOGAIN),
e4af7295 81 fClusterInfoTree(0x0),
3ef13338 82 fMuonRecoParam(0x0),
83 fClusterInfo(0x0),
84 fCalibData(0x0),
85 fESDInterface(0x0),
e4af7295 86 fDigitStore(0x0),
87 fESDInputHandler(0x0),
3ef13338 88 fESDInputEvent(0x0)
e4af7295 89{
90 //
91 /// Default constructor
92 //
93
e4af7295 94}
95
96//______________________________________________________________
97AliMUONChamberCalibrationTask::AliMUONChamberCalibrationTask( const char* name,
98 char* ocdbpath,
99 const Int_t my_calib_option ):
100 AliAnalysisTaskSE( name ),
3ef13338 101 fOCDBPath( "local://$ALICE_ROOT/OCDB" ),
9ee1d6ff 102 fCalibChoice(kNOGAIN),
e4af7295 103 fClusterInfoTree(0x0),
3ef13338 104 fMuonRecoParam(0x0),
105 fClusterInfo(0x0),
106 fCalibData(0x0),
107 fESDInterface(0x0),
e4af7295 108 fDigitStore(0x0),
109 fESDInputHandler(0x0),
3ef13338 110 fESDInputEvent(0x0)
e4af7295 111{
112 //
113 /// constructor
114 //
115
e4af7295 116 fOCDBPath = ocdbpath;
9ee1d6ff 117 if ( (my_calib_option >= ((Int_t)kNOGAIN)) && (my_calib_option <= ((Int_t)kINJECTIONGAIN)) )
e4af7295 118 fCalibChoice = (Calibration_t)my_calib_option;
119 else {
120 AliWarning( Form("Wrong value of the calibration option %d not within [%d, %d] !!! Will use NOGAIN",
9ee1d6ff 121 my_calib_option, (Int_t)kNOGAIN, (Int_t)kINJECTIONGAIN ) );
122 fCalibChoice = kNOGAIN;
e4af7295 123 }
124}
125
126//______________________________________________________________
127AliMUONChamberCalibrationTask::~AliMUONChamberCalibrationTask()
128{
129 //
130 /// destructor
131 //
132
3ef13338 133 delete fMuonRecoParam;
134 delete fClusterInfo;
135 delete fESDInterface;
e4af7295 136
137}
138//______________________________________________________________
139void AliMUONChamberCalibrationTask::CreateOutputObjects()
140{
141 //
142 /// Creates the output TTree
143 //
144
145 AliDebug( 1, "" );
146
147 TFile* clusterInfoFile = OpenFile( 0, "RECREATE" );
148 if( clusterInfoFile ) clusterInfoFile->SetCompressionLevel(1);
149 else AliError( "no output file created !!!" );
150
151 if ( !fClusterInfoTree ) fClusterInfoTree = new TTree( "clusterInfoTree", "clusterInfoTree" );
152 fClusterInfoTree->Branch( "clusterInfo" , &fClusterInfo, 32000, 99);
153}
154
155//______________________________________________________________
156void AliMUONChamberCalibrationTask::LocalInit()
157{
158 //
159 /// Initialization
7f56b767 160 /// Initialize the cluster info and the ESD interface
e4af7295 161 /// Set the magnetic field, the mapping and the reconstruction parameters
162 //
163
164 AliDebug( 1, "" );
165
7f56b767 166 // initialize the cluster info and the ESD interface
167
168 fClusterInfo = new AliMUONClusterInfo();
169 fESDInterface = new AliMUONESDInterface();
170
e4af7295 171 gRandom->SetSeed(0);
172
173 // set mag field
174
175 if ( !TGeoGlobalMagField::Instance()->GetField() ) {
176 AliInfo( "Loading field map..." );
2111e28d 177 AliGRPManager *grpMan = new AliGRPManager();
178 grpMan->ReadGRPEntry();
179 grpMan->SetMagField();
180 delete grpMan;
e4af7295 181 }
182
183 // Load mapping
184
185 AliCDBManager* man = AliCDBManager::Instance();
186 man->SetDefaultStorage( fOCDBPath );
187 man->SetSpecificStorage( "MUON/Calib/MappingData", fOCDBPath );
7f56b767 188 man->SetSpecificStorage( "MUON/Calib/MappingRunData", fOCDBPath ); // for the manu serial numbers
e4af7295 189 man->SetRun(0);
7f56b767 190 man->Print();
e4af7295 191 if ( ! AliMpCDB::LoadDDLStore() ) {
192 AliFatal( "Could not access mapping from OCDB !" );
193 exit(-1);
194 }
195
196 // Set the reconstruction parameters for track refitting
197 // (needed when applying any of the with-gain options)
198
199 fMuonRecoParam = AliMUONRecoParam::GetCosmicParam();
200
201 TString caliboption1 = "NOGAIN";
202 TString caliboption2 = "GAINCONSTANTCAPA";
203 TString caliboption3 = "GAIN";
204 TString caliboption4 = "INJECTIONGAIN";
205
206 TString caliboption = caliboption1;
9ee1d6ff 207 if ( fCalibChoice == kGAINCONSTANTCAPA ) caliboption = caliboption2;
208 if ( fCalibChoice == kGAIN ) caliboption = caliboption3;
209 if ( fCalibChoice == kINJECTIONGAIN ) caliboption = caliboption4;
e4af7295 210 fMuonRecoParam->SetCalibrationMode(caliboption.Data());
211
212 for (Int_t iCh=0; iCh<10; iCh++) {
213 fMuonRecoParam->SetDefaultNonBendingReso( iCh, 0.152 ); // input ESD was aligned (default cosmic settings)
214 fMuonRecoParam->SetDefaultBendingReso( iCh, 0.027 );
215 }
216 fMuonRecoParam->SetMaxNonBendingDistanceToTrack(5.); // was at 1. in default cosmic settings
217 fMuonRecoParam->SetMaxBendingDistanceToTrack(5.);
218
219 fMuonRecoParam->RequestStation(1, kTRUE); // only St 4 and 5 enabled in default cosmic settings
220 fMuonRecoParam->ImproveTracks(kTRUE, 7.); // was 6. in default cosmic settings
221
222 AliInfo( "reconstruction parameters initialized as follows :" );
223 fMuonRecoParam->Print("FULL");
224
225 AliMUONESDInterface::ResetTracker(fMuonRecoParam);
226}
227
228//______________________________________________________________
229void AliMUONChamberCalibrationTask::ConnectInputData( Option_t* /*option*/ )
230{
231 //
232 /// Connect to ESD here
233 /// (called once)
234 //
235
236 AliDebug( 1, "" );
237
238 fESDInputHandler = dynamic_cast<AliESDInputHandler*>(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
239 TTree* tree = NULL;
240
241 if ( fESDInputHandler ) {
242
243 // The properly initialized ESD input handler reads ESD tree
244 // and connect it to ESD event, so we only need to retrieve the later
245 fESDInputEvent = fESDInputHandler->GetEvent();
246 if ( !fESDInputEvent ) {
247
248 AliFatal( "Could not get input ESD event !!! ");
249
f2deabf0 250 }
251 } else {
e4af7295 252
253 AliError( "Could not get input ESD handler !!!" );
254 // If no input event handler we need to get the tree once
255 // from input slot 0 for the chain
256 tree = dynamic_cast<TTree*> (GetInputData(0));
257 if ( tree ) tree->GetReadEntry();
f2deabf0 258 else AliFatal( "Could not read tree from input slot 0 !!!" );
e4af7295 259 }
e4af7295 260}
261
262//______________________________________________________________
263void AliMUONChamberCalibrationTask::Exec( Option_t* /*option*/ )
264{
265 //
266 /// Process the current event
267 /// (called for each event)
268 //
269
270 static Bool_t first = kTRUE;
271
272 if ( first ) {
273 AliDebug( 1, "" );
274 first = kFALSE;
275 }
276
277 if ( !fESDInputEvent ) {
278 AliError( "Input ESD event not available !!! " );
279 return;
280 }
281
282 // load the current ESD event
283 fESDInterface->LoadEvent( *fESDInputEvent );
284
285 // get digit store
286 fDigitStore = fESDInterface->GetDigits();
287
288 // prepare access to calibration data
289 if ( !fCalibData ) fCalibData = new AliMUONCalibrationData( fESDInputEvent->GetESDRun()->GetRunNumber() );
290
291 // --------------------------------------------------------------------
292 // fill cluster info from clusters attached to each track of this event
293 // --------------------------------------------------------------------
294
295 Int_t nTracks = (Int_t)fESDInputEvent->GetNumberOfMuonTracks();
296 if ( nTracks < 1 ) return;
297
298 TIter nextTrack( fESDInterface->CreateTrackIterator() );
299 AliMUONTrack* track;
300
301 while ( (track = static_cast<AliMUONTrack*>(nextTrack())) ) { // loop over tracks
302
303 UInt_t muonClusterMap = BuildClusterMap( *track );
304
305 AliMUONTrackParam* trackParam =
306 static_cast<AliMUONTrackParam*>(track->GetTrackParamAtCluster()->First());
307
308 while ( trackParam ) { // loop over clusters
309
310 fClusterInfo->Clear("C");
311
312 // fill cluster info
313 AliMUONVCluster* cluster = trackParam->GetClusterPtr();
314 fClusterInfo->SetRunId( fESDInputEvent->GetRunNumber() );
315 fClusterInfo->SetEventId( fESDInputEvent->GetEventNumberInFile() );
316 fClusterInfo->SetZ( cluster->GetZ() );
317 fClusterInfo->SetClusterId( cluster->GetUniqueID() );
318 fClusterInfo->SetClusterXY( cluster->GetX(), cluster->GetY() );
319 fClusterInfo->SetClusterXYErr( cluster->GetErrX(), cluster->GetErrY() );
320 fClusterInfo->SetClusterChi2( cluster->GetChi2() );
321 fClusterInfo->SetClusterCharge( cluster->GetCharge() );
322
323 // fill track info
324 fClusterInfo->SetTrackId( track->GetUniqueID() );
325 fClusterInfo->SetTrackXY( trackParam->GetNonBendingCoor(), trackParam->GetBendingCoor() );
326 fClusterInfo->SetTrackThetaXY( TMath::ATan( trackParam->GetNonBendingSlope() ),
327 TMath::ATan( trackParam->GetBendingSlope() ) );
328 fClusterInfo->SetTrackP( trackParam->P() );
329 const TMatrixD paramCov = trackParam->GetCovariances();
330 fClusterInfo->SetTrackXYErr( TMath::Sqrt( paramCov(0,0) ),
331 TMath::Sqrt( paramCov(2,2) ) );
332 fClusterInfo->SetTrackChi2( track->GetNormalizedChi2() );
333 fClusterInfo->SetTrackCharge( (Short_t)trackParam->GetCharge() );
334 fClusterInfo->SetTrackNHits( track->GetNClusters() );
335 fClusterInfo->SetTrackChamberHitMap( muonClusterMap );
336
337 // fill pad info if available
338 for ( Int_t i=0; i<cluster->GetNDigits(); i++ ) {
339
340 AliMUONVDigit* digit = fDigitStore->FindObject( cluster->GetDigitId(i) );
341 if ( !digit ) continue;
342
343 // pad location
344 const AliMpVSegmentation* seg = AliMpSegmentation::Instance()->
345 GetMpSegmentation( digit->DetElemId(), AliMp::GetCathodType( digit->Cathode() ) );
346 AliMpPad pad = seg->PadByIndices( digit->PadX(), digit->PadY() );
347
348 // calibration parameters
349 AliMUONVCalibParam* ped = fCalibData->Pedestals( digit->DetElemId(), digit->ManuId() );
350 AliMUONVCalibParam* gain = fCalibData->Gains( digit->DetElemId(), digit->ManuId() );
351 Int_t manuChannel = digit->ManuChannel();
352 Int_t planeType = 0;
353 if ( digit->ManuId() & AliMpConstants::ManuMask(AliMp::kNonBendingPlane) ) {
354 planeType = 1;
355 }
356
357 // fill pad info
358 AliMUONPadInfo padInfo;
359 padInfo.SetPadId( digit->GetUniqueID() );
360 padInfo.SetPadPlaneType( planeType );
361 padInfo.SetPadXY( pad.GetPositionX(), pad.GetPositionY() );
362 padInfo.SetPadDimXY( pad.GetDimensionX(), pad.GetDimensionY() );
363 padInfo.SetPadCharge( (Double_t)digit->Charge() );
364 padInfo.SetPadADC( digit->ADC() );
365 padInfo.SetSaturated( digit->IsSaturated() );
366 padInfo.SetCalibrated( digit->IsCalibrated() );
367 padInfo.SetPedestal( ped->ValueAsFloatFast(manuChannel,0), // mean
368 ped->ValueAsFloatFast(manuChannel,1) ); // sigma
369 padInfo.SetGain( gain->ValueAsFloatFast(manuChannel,0), // a0
370 gain->ValueAsFloatFast(manuChannel,1), // a1
7f56b767 371 (Int_t)gain->ValueAsFloatFast(manuChannel,2), // threshold
372 (Int_t)gain->ValueAsFloatFast(manuChannel,3) ); // fit quality
e4af7295 373
374 fClusterInfo->AddPad( padInfo );
375 }
376
377 // fill cluster info tree
378 fClusterInfoTree->Fill();
379
380 trackParam = static_cast<AliMUONTrackParam*>(track->GetTrackParamAtCluster()->After(trackParam));
381 }
382
383 }
384 // Added protection in case the derived task is not an AOD producer.
385 AliAnalysisDataSlot *out0 = GetOutputSlot(0);
386 if (out0 && out0->IsConnected()) PostData( 0, fClusterInfoTree );
387
388}
389
390
391//______________________________________________________________
392UInt_t AliMUONChamberCalibrationTask::BuildClusterMap( AliMUONTrack &track )
393{
394 //
395 /// Build the map of clusters in tracking chambers
396 //
397
398 UInt_t muonClusterMap = 0;
399
400 AliMUONTrackParam* trackParam = static_cast<AliMUONTrackParam*>(track.GetTrackParamAtCluster()->First());
401 while ( trackParam ) {
402
403 muonClusterMap |= BIT(trackParam->GetClusterPtr()->GetChamberId());
404
405 trackParam = static_cast<AliMUONTrackParam*>(track.GetTrackParamAtCluster()->After(trackParam));
406 }
407
408 return muonClusterMap;
409}
410
411//______________________________________________________________
412void AliMUONChamberCalibrationTask::Terminate( Option_t* /*option*/ )
413{
f2deabf0 414 //
415 /// Called once per task on the client machine at the end of the analysis.
416 //
417
e4af7295 418 AliDebug( 1, "" );
419}