1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 //-----------------------------------------------------------------------------
19 /// \class AliMUONReAlignTask
20 /// AliAnalysisTask to realign the MUON spectrometer.
21 /// The Task reads as input ESDs moves the clusters of a MUONTrack acoording
22 /// to the re aligned geometry taken from a misalignment file in the OCDB
23 /// and refit the track. Then it produces a AliMUONClusterInfo object for each
24 /// cluster. The output is a TTree of AliMUONClusterInfo
26 /// \author Javier Castillo, CEA/Saclay - Irfu/SPhN
27 //-----------------------------------------------------------------------------
35 #include <TClonesArray.h>
37 #include <TGeoGlobalMagField.h>
38 #include <TGeoManager.h>
39 #include <Riostream.h>
41 #include "AliAnalysisTask.h"
42 #include "AliAnalysisManager.h"
43 #include "AliESDInputHandler.h"
44 #include "AliESDEvent.h"
45 #include "AliESDMuonTrack.h"
47 #include "AliCDBManager.h"
48 #include "AliGRPManager.h"
49 #include "AliGeomManager.h"
51 #include "AliMpConstants.h"
53 #include "AliMpSegmentation.h"
54 #include "AliMpVSegmentation.h"
56 #include "AliMUONCalibrationData.h"
57 #include "AliMUONVCalibParam.h"
58 #include "AliMUONPadInfo.h"
59 #include "AliMUONClusterInfo.h"
60 #include "AliMUONRecoParam.h"
61 #include "AliMUONESDInterface.h"
62 #include "AliMUONRefitter.h"
63 #include "AliMUONRecoParam.h"
64 #include "AliMUONVDigit.h"
65 #include "AliMUONVCluster.h"
66 #include "AliMUONTrack.h"
67 #include "AliMUONVTrackStore.h"
68 #include "AliMUONVDigitStore.h"
69 #include "AliMUONTrackParam.h"
70 #include "AliMUONTrackExtrap.h"
71 #include "AliMUONGeometryTransformer.h"
73 #include "AliMUONReAlignTask.h"
76 ClassImp(AliMUONReAlignTask)
79 //________________________________________________________________________
80 AliMUONReAlignTask::AliMUONReAlignTask(const char *name, const char *geofilename, const char *defaultocdb, const char *misalignocdb)
81 : AliAnalysisTask(name, ""),
83 fClusterInfoTree(0x0),
88 fGeoFilename(geofilename),
89 fMisAlignOCDB(misalignocdb),
90 fDefaultOCDB(defaultocdb),
92 fNewGeoTransformer(0x0),
98 /// Default Constructor
99 // Define input and output slots here
100 // Input slot #0 works with a TChain
101 DefineInput(0, TChain::Class());
102 // Output slot #0 writes a TTree
103 DefineOutput(0, TTree::Class());
105 fClusterInfo = new AliMUONClusterInfo();
106 fESDInterface = new AliMUONESDInterface();
107 fGeoTransformer = new AliMUONGeometryTransformer();
108 fNewGeoTransformer = new AliMUONGeometryTransformer();
111 //________________________________________________________________________
112 AliMUONReAlignTask::AliMUONReAlignTask(const AliMUONReAlignTask& obj)
113 : AliAnalysisTask(obj),
115 fClusterInfoTree(0x0),
123 fGeoTransformer(0x0),
124 fNewGeoTransformer(0x0),
132 fClusterInfoTree = obj.fClusterInfoTree;
133 fClusterInfo = obj.fClusterInfo;
134 fESDInterface = obj.fESDInterface;
135 fRefitter = obj.fRefitter;
136 fRecoParam = obj.fRecoParam;
137 fGeoFilename = obj.fGeoFilename;
138 fMisAlignOCDB = obj.fMisAlignOCDB;
139 fDefaultOCDB = obj.fDefaultOCDB;
140 fGeoTransformer = obj.fGeoTransformer;
141 fNewGeoTransformer = obj.fNewGeoTransformer;
142 fGainStore = obj.fGainStore;
143 fPedStore = obj.fPedStore;
144 fPrintLevel = obj.fPrintLevel;
145 fLastRun = obj.fLastRun;
148 //________________________________________________________________________________
149 AliMUONReAlignTask& AliMUONReAlignTask::operator=(const AliMUONReAlignTask& other)
152 if(&other == this) return *this;
153 AliAnalysisTask::operator=(other);
155 fClusterInfoTree = other.fClusterInfoTree;
156 fClusterInfo = other.fClusterInfo;
157 fESDInterface = other.fESDInterface;
158 fRefitter = other.fRefitter;
159 fRecoParam = other.fRecoParam;
160 fGeoFilename = other.fGeoFilename;
161 fMisAlignOCDB = other.fMisAlignOCDB;
162 fDefaultOCDB = other.fDefaultOCDB;
163 fGeoTransformer = other.fGeoTransformer;
164 fNewGeoTransformer = other.fNewGeoTransformer;
165 fGainStore = other.fGainStore;
166 fPedStore = other.fPedStore;
167 fPrintLevel = other.fPrintLevel;
168 fLastRun = other.fLastRun;
174 //________________________________________________________________________
175 AliMUONReAlignTask::~AliMUONReAlignTask()
178 if (fESDInterface) delete fESDInterface;
179 if (fGeoTransformer) delete fGeoTransformer;
180 if (fNewGeoTransformer) delete fNewGeoTransformer;
183 //________________________________________________________________________
184 void AliMUONReAlignTask::LocalInit()
186 /// Local initialization, called once per task on the client machine
187 /// where the analysis train is assembled
188 AliMpCDB::LoadMpSegmentation();
190 // prepare the refitting
192 Prepare(fGeoFilename.Data(),fDefaultOCDB.Data(),fMisAlignOCDB.Data());
194 fRefitter = new AliMUONRefitter(fRecoParam);
195 fRefitter->Connect(fESDInterface);
197 // Original geotransformer
198 fGeoTransformer->LoadGeometryData();
199 // Apply mis alignments
200 AliGeomManager::ApplyAlignObjsFromCDB("MUON");
201 // New geotransformer
202 fNewGeoTransformer->LoadGeometryData();
206 //________________________________________________________________________
207 void AliMUONReAlignTask::ConnectInputData(Option_t *)
209 /// Connect ESD here. Called on each input data change.
211 TTree* esdTree = dynamic_cast<TTree*> (GetInputData(0));
213 Printf("ERROR: Could not read chain from input slot 0");
216 AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
218 Printf("ERROR: Could not get ESDInputHandler");
221 fESD = esdH->GetEvent();
226 //________________________________________________________________________
227 void AliMUONReAlignTask::CreateOutputObjects()
229 /// Executed once on each worker (machine actually running the analysis code)
231 // This method has to be called INSIDE the user redefined CreateOutputObjects
232 // method, before creating each object corresponding to the output containers
233 // that are to be written to a file. This need to be done in general for the big output
234 // objects that may not fit memory during processing.
237 fClusterInfoTree = new TTree("clusterInfoTree","clusterInfoTree");
238 fClusterInfoTree->Branch("clusterInfo", &fClusterInfo, 32000, 99);
242 //________________________________________________________________________
243 void AliMUONReAlignTask::Exec(Option_t *)
245 /// Main loop, called for each event
247 Printf("ERROR: fESD not available");
251 // Prepare Gain and Pedestal store
252 if (!fGainStore || fLastRun!=fESD->GetRunNumber()){
253 fGainStore = AliMUONCalibrationData::CreateGains(fESD->GetRunNumber());
254 fLastRun = fESD->GetRunNumber();
256 if (!fPedStore || fLastRun!=fESD->GetRunNumber()){
257 fPedStore = AliMUONCalibrationData::CreatePedestals(fESD->GetRunNumber());
258 fLastRun = fESD->GetRunNumber();
261 AliMUONPadInfo padInfo;
263 Int_t nTracks = (Int_t)fESD->GetNumberOfMuonTracks();
264 if (nTracks < 1) return;
266 // load the current event
267 fESDInterface->LoadEvent(*fESD);
268 AliMUONVDigitStore* digitStore = fESDInterface->GetDigits();
276 // loop over cluster to modify their position
277 AliMUONVCluster *cluster;
278 TIter next(fESDInterface->CreateClusterIterator());
279 while ((cluster = static_cast<AliMUONVCluster*>(next()))) {
280 // cout << "Original cluster" << endl;
282 gX = cluster->GetX();
283 gY = cluster->GetY();
284 gZ = cluster->GetZ();
285 fGeoTransformer->Global2Local(cluster->GetDetElemId(),gX,gY,gZ,lX,lY,lZ);
286 fNewGeoTransformer->Local2Global(cluster->GetDetElemId(),lX,lY,lZ,gX,gY,gZ);
287 cluster->SetXYZ(gX,gY,gZ);
288 // cout << "Aligned cluster" << endl;
292 // refit the tracks from digits
293 AliMUONVTrackStore* newTrackStore = fRefitter->ReconstructFromClusters();
295 //----------------------------------------------//
296 // ------ fill new ESD and print results ------ //
297 //----------------------------------------------//
298 // loop over the list of ESD tracks
299 TClonesArray *esdTracks = (TClonesArray*) fESD->FindListObject("MuonTracks");
300 for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
302 AliESDMuonTrack* esdTrack = (AliESDMuonTrack*) esdTracks->UncheckedAt(iTrack);
304 // skip ghost tracks (leave them unchanged in the new ESD file)
305 if (!esdTrack->ContainTrackerData()) continue;
307 // get the corresponding MUON track
308 AliMUONTrack* track = fESDInterface->FindTrack(esdTrack->GetUniqueID());
310 // Find the corresponding re-fitted MUON track
311 AliMUONTrack* newTrack = (AliMUONTrack*) newTrackStore->FindObject(esdTrack->GetUniqueID());
313 // // replace the content of the current ESD track or remove it if the refitting has failed
315 // Double_t vertex[3] = {esdTrack->GetNonBendingCoor(), esdTrack->GetBendingCoor(), esdTrack->GetZ()};
316 // AliMUONESDInterface::MUONToESD(*newTrack, *esdTrack, vertex, esdInterface.GetDigits());
318 // esdTracks->Remove(esdTrack);
321 // print initial and re-fitted track parameters at first cluster if any
323 cout<<" ----------------track #"<<iTrack+1<<"----------------"<<endl;
324 cout<<"before refit:"<<endl;
325 AliMUONTrackParam *param = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
326 param->Print("FULL");
327 if (fPrintLevel>1) param->GetCovariances().Print();
328 if (!newTrack) continue;
329 cout<<"after refit:"<<endl;
330 param = (AliMUONTrackParam*) newTrack->GetTrackParamAtCluster()->First();
331 param->Print("FULL");
332 if (fPrintLevel>1) param->GetCovariances().Print();
333 cout<<" ----------------------------------------"<<endl;
337 UInt_t muonClusterMap = BuildClusterMap(*newTrack);
339 // loop over clusters
340 AliMUONTrackParam* trackParam = static_cast<AliMUONTrackParam*>(newTrack->GetTrackParamAtCluster()->First());
342 fClusterInfo->Clear("C");
345 cluster = trackParam->GetClusterPtr();
346 fClusterInfo->SetRunId(fESD->GetRunNumber());
347 fClusterInfo->SetEventId(fESD->GetEventNumberInFile());
348 fClusterInfo->SetZ(cluster->GetZ());
349 fClusterInfo->SetClusterId(cluster->GetUniqueID());
350 fClusterInfo->SetClusterXY(cluster->GetX(), cluster->GetY());
351 fClusterInfo->SetClusterXYErr(cluster->GetErrX(), cluster->GetErrY());
352 fClusterInfo->SetClusterChi2(cluster->GetChi2());
353 fClusterInfo->SetClusterCharge(cluster->GetCharge());
356 fClusterInfo->SetTrackId(newTrack->GetUniqueID());
357 fClusterInfo->SetTrackXY(trackParam->GetNonBendingCoor(), trackParam->GetBendingCoor());
358 fClusterInfo->SetTrackThetaXY(TMath::ATan(trackParam->GetNonBendingSlope()), TMath::ATan(trackParam->GetBendingSlope()));
359 fClusterInfo->SetTrackP(trackParam->P());
360 const TMatrixD paramCov = trackParam->GetCovariances();
361 fClusterInfo->SetTrackXYErr(TMath::Sqrt(paramCov(0,0)), TMath::Sqrt(paramCov(2,2)));
362 fClusterInfo->SetTrackChi2(newTrack->GetNormalizedChi2());
363 fClusterInfo->SetTrackCharge((Short_t)trackParam->GetCharge());
364 fClusterInfo->SetTrackNHits(newTrack->GetNClusters());
365 fClusterInfo->SetTrackChamberHitMap(muonClusterMap);
367 // fill pad info if available
368 for (Int_t i=0; i<cluster->GetNDigits(); i++) {
369 AliMUONVDigit* digit = digitStore->FindObject(cluster->GetDigitId(i));
370 if (!digit) continue;
373 const AliMpVSegmentation* seg = AliMpSegmentation::Instance()->
374 GetMpSegmentation(digit->DetElemId(),AliMp::GetCathodType(digit->Cathode()));
375 AliMpPad pad = seg->PadByIndices(digit->PadX(), digit->PadY());
377 // calibration parameters
378 AliMUONVCalibParam* ped = fPedStore ? static_cast<AliMUONVCalibParam*>(fPedStore->FindObject(digit->DetElemId(), digit->ManuId())) : 0x0;
379 AliMUONVCalibParam* gain = fGainStore ? static_cast<AliMUONVCalibParam*>(fGainStore->FindObject(digit->DetElemId(), digit->ManuId())) : 0x0;
380 Int_t manuChannel = digit->ManuChannel();
382 if ( digit->ManuId() & AliMpConstants::ManuMask(AliMp::kNonBendingPlane)) {
387 padInfo.SetPadId(digit->GetUniqueID());
388 padInfo.SetPadPlaneType(planeType);
389 padInfo.SetPadXY(pad.GetPositionX(), pad.GetPositionY());
390 padInfo.SetPadDimXY(pad.GetDimensionX(), pad.GetDimensionY());
391 padInfo.SetPadCharge((Double_t)digit->Charge());
392 padInfo.SetPadADC(digit->ADC());
393 padInfo.SetSaturated(digit->IsSaturated());
394 padInfo.SetCalibrated(digit->IsCalibrated());
396 padInfo.SetPedestal(ped->ValueAsFloatFast(manuChannel,0), ped->ValueAsFloatFast(manuChannel,1));
398 padInfo.SetPedestal(-250.,-5.);
401 padInfo.SetGain(gain->ValueAsFloatFast(manuChannel,0), gain->ValueAsFloatFast(manuChannel,1),
402 gain->ValueAsIntFast(manuChannel,2), gain->ValueAsIntFast(manuChannel,3));
404 padInfo.SetGain(-1.,-0.1,-4095,-1);
406 fClusterInfo->AddPad(padInfo);
409 // fill cluster info tree
410 fClusterInfoTree->Fill();
411 trackParam = static_cast<AliMUONTrackParam*>(track->GetTrackParamAtCluster()->After(trackParam));
415 delete newTrackStore;
418 // Post final data. Write histo list to a file with option "RECREATE"
419 PostData(0,fClusterInfoTree);
422 //________________________________________________________________________
423 void AliMUONReAlignTask::Terminate(const Option_t*)
425 /// Called once per task on the client machine at the end of the analysis.
429 //-----------------------------------------------------------------------
430 void AliMUONReAlignTask::Prepare(const char* geoFilename, const char* defaultOCDB, const char* misAlignOCDB)
432 /// Set the geometry, the magnetic field, the mapping and the reconstruction parameters
434 // Import TGeo geometry (needed by AliMUONTrackExtrap::ExtrapToVertex)
436 AliGeomManager::LoadGeometry(geoFilename);
438 Error("AliMUONReAlignTask", "getting geometry from file %s failed", "generated/galice.root");
444 AliCDBManager* man = AliCDBManager::Instance();
445 man->SetDefaultStorage(defaultOCDB);
446 man->SetSpecificStorage("MUON/Align/Data",misAlignOCDB);
449 if ( ! AliMpCDB::LoadDDLStore() ) {
450 Error("MUONRefit","Could not access mapping from OCDB !");
455 if (!TGeoGlobalMagField::Instance()->GetField()) {
456 printf("Loading field map...\n");
457 AliGRPManager *grpMan = new AliGRPManager();
458 grpMan->ReadGRPEntry();
459 grpMan->SetMagField();
462 // set the magnetic field for track extrapolations
463 AliMUONTrackExtrap::SetField();
465 // Load initial reconstruction parameters from OCDB
466 // reconstruction parameters
467 fRecoParam = AliMUONRecoParam::GetCosmicParam();
470 fRecoParam->SetPadGoodnessMask(0x400BE80);
471 // TString caliboption = caliboption1;
472 // if ( calib == 2 ) caliboption = caliboption2;
473 // fRecoParam->SetCalibrationMode("NOGAIN"caliboption.Data());
474 fRecoParam->SetCalibrationMode("NOGAIN");
476 // chamber resolution (incuding misalignment)
477 for (Int_t iCh=0; iCh<10; iCh++) {
478 fRecoParam->SetDefaultNonBendingReso(iCh,0.4);
479 fRecoParam->SetDefaultBendingReso(iCh,0.4);
481 fRecoParam->SetMaxNonBendingDistanceToTrack(10.);
482 fRecoParam->SetMaxBendingDistanceToTrack(10.);
484 // cut on (non)bending slopes
485 //fRecoParam->SetMaxNonBendingSlope(0.6);
486 //fRecoParam->SetMaxBendingSlope(0.6);
488 // tracking algorithm
489 // fRecoParam->MakeMoreTrackCandidates(kTRUE);
490 fRecoParam->RequestStation(0, kFALSE);
491 fRecoParam->RequestStation(2, kFALSE);
492 // fRecoParam->RequestStation(3, kFALSE);
493 // fRecoParam->RequestStation(4, kFALSE);
494 fRecoParam->SetSigmaCutForTracking(7.);
495 fRecoParam->ImproveTracks(kTRUE, 7.);
496 Info("MUONRefit", "\n initial recontruction parameters:");
497 fRecoParam->Print("FULL");
499 AliMUONESDInterface::ResetTracker(fRecoParam);
502 //-----------------------------------------------------------------------
503 UInt_t AliMUONReAlignTask::BuildClusterMap(AliMUONTrack &track)
505 /// Build the map of clusters in tracking chambers
507 UInt_t muonClusterMap = 0;
509 AliMUONTrackParam* trackParam = static_cast<AliMUONTrackParam*>(track.GetTrackParamAtCluster()->First());
512 muonClusterMap |= BIT(trackParam->GetClusterPtr()->GetChamberId());
514 trackParam = static_cast<AliMUONTrackParam*>(track.GetTrackParamAtCluster()->After(trackParam));
517 return muonClusterMap;