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