]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONReAlignTask.cxx
Changes for #90436: Misuse of TClonesArray containing AliESDMuonCluster
[u/mrichter/AliRoot.git] / MUON / AliMUONReAlignTask.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 /// \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
25 ///
26 /// \author Javier Castillo, CEA/Saclay - Irfu/SPhN
27 //-----------------------------------------------------------------------------
28
29 #include <fstream>
30
31 #include <TString.h>
32 #include <TError.h>
33 #include <TTree.h>
34 #include <TChain.h>
35 #include <TClonesArray.h>
36 #include <TRandom.h>
37 #include <TGeoGlobalMagField.h>
38 #include <TGeoManager.h>
39 #include <Riostream.h>
40
41 #include "AliAnalysisTask.h"
42 #include "AliAnalysisManager.h"
43 #include "AliESDInputHandler.h"
44 #include "AliESDEvent.h"
45 #include "AliESDMuonTrack.h"
46 #include "AliMagF.h"
47 #include "AliCDBManager.h"
48 #include "AliGRPManager.h"
49 #include "AliGeomManager.h"
50
51 #include "AliMpConstants.h"
52 #include "AliMpCDB.h"
53 #include "AliMpSegmentation.h"
54 #include "AliMpVSegmentation.h"
55 #include "AliMpPad.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"
72
73 #include "AliMUONReAlignTask.h"
74
75 ///\cond CLASSIMP   
76 ClassImp(AliMUONReAlignTask)
77 ///\endcond
78
79 //________________________________________________________________________
80 AliMUONReAlignTask::AliMUONReAlignTask(const char *name, const char *geofilename, const char *defaultocdb, const char *misalignocdb) 
81   : AliAnalysisTask(name, ""),
82     fESD(0x0),
83     fClusterInfoTree(0x0),
84     fClusterInfo(0x0),
85     fESDInterface(0x0),
86     fRefitter(0x0),
87     fRecoParam(0x0),
88     fGeoFilename(geofilename),
89     fMisAlignOCDB(misalignocdb),
90     fDefaultOCDB(defaultocdb),
91     fGeoTransformer(0x0),
92     fNewGeoTransformer(0x0),
93     fGainStore(0x0),
94     fPedStore(0x0),
95     fPrintLevel(0),
96     fLastRun(-1)
97 {
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());
104
105   fClusterInfo = new AliMUONClusterInfo();
106   fESDInterface = new AliMUONESDInterface();
107   fGeoTransformer = new AliMUONGeometryTransformer();
108   fNewGeoTransformer = new AliMUONGeometryTransformer();
109 }
110
111 //________________________________________________________________________
112 AliMUONReAlignTask::AliMUONReAlignTask(const AliMUONReAlignTask& obj)
113   : AliAnalysisTask(obj),
114     fESD(0x0),
115     fClusterInfoTree(0x0),
116     fClusterInfo(0x0),
117     fESDInterface(0x0),
118     fRefitter(0x0),
119     fRecoParam(0x0),
120     fGeoFilename(""),
121     fMisAlignOCDB(""),
122     fDefaultOCDB(""),
123     fGeoTransformer(0x0),
124     fNewGeoTransformer(0x0),
125     fGainStore(0x0),
126     fPedStore(0x0),
127     fPrintLevel(0),
128     fLastRun(-1)
129 {
130   /// Copy constructor
131   fESD = obj.fESD;
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;
146 }
147
148 //________________________________________________________________________________
149 AliMUONReAlignTask& AliMUONReAlignTask::operator=(const AliMUONReAlignTask& other)
150 {
151   /// Assignment
152   if(&other == this) return *this;
153   AliAnalysisTask::operator=(other);
154   fESD = other.fESD;
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;
169
170   return *this;
171
172
173
174 //________________________________________________________________________
175 AliMUONReAlignTask::~AliMUONReAlignTask() 
176
177   /// Destructor
178   if (fESDInterface) delete fESDInterface;
179   if (fGeoTransformer) delete fGeoTransformer;
180   if (fNewGeoTransformer) delete fNewGeoTransformer;
181 }
182
183 //________________________________________________________________________
184 void AliMUONReAlignTask::LocalInit() 
185 {
186   /// Local initialization, called once per task on the client machine 
187   /// where the analysis train is assembled
188   AliMpCDB::LoadMpSegmentation();
189
190   // prepare the refitting
191   gRandom->SetSeed(0);
192   Prepare(fGeoFilename.Data(),fDefaultOCDB.Data(),fMisAlignOCDB.Data());
193
194   fRefitter = new AliMUONRefitter(fRecoParam);
195   fRefitter->Connect(fESDInterface);
196   
197   // Original geotransformer
198   fGeoTransformer->LoadGeometryData();   
199   // Apply mis alignments
200   AliGeomManager::ApplyAlignObjsFromCDB("MUON");    
201   // New geotransformer
202   fNewGeoTransformer->LoadGeometryData();   
203   
204 }
205
206 //________________________________________________________________________
207 void AliMUONReAlignTask::ConnectInputData(Option_t *) 
208 {
209   /// Connect ESD here. Called on each input data change.
210   // Connect ESD here
211   TTree* esdTree = dynamic_cast<TTree*> (GetInputData(0));
212   if (!esdTree) {
213     Printf("ERROR: Could not read chain from input slot 0");
214   } 
215   else {
216     AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());   
217     if (!esdH) {
218       Printf("ERROR: Could not get ESDInputHandler");
219     } 
220     else {
221       fESD = esdH->GetEvent();
222     }
223   }
224 }
225
226 //________________________________________________________________________
227 void AliMUONReAlignTask::CreateOutputObjects()
228 {
229   /// Executed once on each worker (machine actually running the analysis code)
230   //
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. 
235   OpenFile(0); 
236   
237   fClusterInfoTree = new TTree("clusterInfoTree","clusterInfoTree");
238   fClusterInfoTree->Branch("clusterInfo", &fClusterInfo, 32000, 99);
239
240 }
241
242 //________________________________________________________________________
243 void AliMUONReAlignTask::Exec(Option_t *) 
244 {
245   /// Main loop, called for each event
246   if (!fESD) {
247     Printf("ERROR: fESD not available");
248     return;
249   }
250
251   // Prepare Gain and Pedestal store
252   if (!fGainStore || fLastRun!=fESD->GetRunNumber()){
253     fGainStore = AliMUONCalibrationData::CreateGains(fESD->GetRunNumber());
254     fLastRun = fESD->GetRunNumber();
255   }
256   if (!fPedStore || fLastRun!=fESD->GetRunNumber()){
257     fPedStore = AliMUONCalibrationData::CreatePedestals(fESD->GetRunNumber());
258     fLastRun = fESD->GetRunNumber();
259   }
260
261   AliMUONPadInfo padInfo;
262
263   Int_t nTracks = (Int_t)fESD->GetNumberOfMuonTracks();
264   if (nTracks < 1) return;
265   
266   // load the current event
267   fESDInterface->LoadEvent(*fESD);
268   AliMUONVDigitStore* digitStore = fESDInterface->GetDigits();
269
270   Double_t lX = 0.;
271   Double_t lY = 0.;
272   Double_t lZ = 0.;
273   Double_t gX = 0.;
274   Double_t gY = 0.;
275   Double_t gZ = 0.;
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;
281     //       cluster->Print();
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;
289     //       cluster->Print();
290   }
291
292   // refit the tracks from digits
293   AliMUONVTrackStore* newTrackStore = fRefitter->ReconstructFromClusters();
294     
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++) {      
301     // get the ESD track
302     AliESDMuonTrack* esdTrack = (AliESDMuonTrack*) esdTracks->UncheckedAt(iTrack);
303     
304     // skip ghost tracks (leave them unchanged in the new ESD file)
305     if (!esdTrack->ContainTrackerData()) continue;
306       
307     // get the corresponding MUON track
308     AliMUONTrack* track = fESDInterface->FindTrack(esdTrack->GetUniqueID());
309       
310     // Find the corresponding re-fitted MUON track
311     AliMUONTrack* newTrack = (AliMUONTrack*) newTrackStore->FindObject(esdTrack->GetUniqueID());
312     
313     // print initial and re-fitted track parameters at first cluster if any
314     if (fPrintLevel>0) {
315       cout<<"            ----------------track #"<<iTrack+1<<"----------------"<<endl;
316       cout<<"before refit:"<<endl;
317       AliMUONTrackParam *param = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
318       param->Print("FULL");
319       if (fPrintLevel>1) param->GetCovariances().Print();
320       if (!newTrack) continue;
321       cout<<"after refit:"<<endl;
322       param = (AliMUONTrackParam*) newTrack->GetTrackParamAtCluster()->First();
323       param->Print("FULL");
324       if (fPrintLevel>1) param->GetCovariances().Print();
325       cout<<"            ----------------------------------------"<<endl;
326     }
327     
328     // Cluster Info part
329     UInt_t muonClusterMap = BuildClusterMap(*newTrack);
330         
331     // loop over clusters
332     AliMUONTrackParam* trackParam = static_cast<AliMUONTrackParam*>(newTrack->GetTrackParamAtCluster()->First());
333     while (trackParam) {
334       fClusterInfo->Clear("C");
335       
336       // fill cluster info
337       cluster = trackParam->GetClusterPtr();
338       fClusterInfo->SetRunId(fESD->GetRunNumber());
339       fClusterInfo->SetEventId(fESD->GetEventNumberInFile());
340       fClusterInfo->SetZ(cluster->GetZ());
341       fClusterInfo->SetClusterId(cluster->GetUniqueID());
342       fClusterInfo->SetClusterXY(cluster->GetX(), cluster->GetY());
343       fClusterInfo->SetClusterXYErr(cluster->GetErrX(), cluster->GetErrY());
344       fClusterInfo->SetClusterChi2(cluster->GetChi2());
345       fClusterInfo->SetClusterCharge(cluster->GetCharge());
346       
347       // fill track info
348       fClusterInfo->SetTrackId(newTrack->GetUniqueID());
349       fClusterInfo->SetTrackXY(trackParam->GetNonBendingCoor(), trackParam->GetBendingCoor());
350       fClusterInfo->SetTrackThetaXY(TMath::ATan(trackParam->GetNonBendingSlope()), TMath::ATan(trackParam->GetBendingSlope()));
351       fClusterInfo->SetTrackP(trackParam->P());
352       const TMatrixD paramCov = trackParam->GetCovariances();
353       fClusterInfo->SetTrackXYErr(TMath::Sqrt(paramCov(0,0)), TMath::Sqrt(paramCov(2,2)));
354       fClusterInfo->SetTrackChi2(newTrack->GetNormalizedChi2());
355       fClusterInfo->SetTrackCharge((Short_t)trackParam->GetCharge());
356       fClusterInfo->SetTrackNHits(newTrack->GetNClusters());
357       fClusterInfo->SetTrackChamberHitMap(muonClusterMap);
358       
359       // fill pad info if available       
360       for (Int_t i=0; i<cluster->GetNDigits(); i++) {
361         AliMUONVDigit* digit = digitStore->FindObject(cluster->GetDigitId(i));
362         if (!digit) continue;
363         
364         // pad location
365         const AliMpVSegmentation* seg = AliMpSegmentation::Instance()->
366           GetMpSegmentation(digit->DetElemId(),AliMp::GetCathodType(digit->Cathode()));
367         AliMpPad pad = seg->PadByIndices(digit->PadX(), digit->PadY());
368         
369         // calibration parameters
370         AliMUONVCalibParam* ped =  fPedStore ? static_cast<AliMUONVCalibParam*>(fPedStore->FindObject(digit->DetElemId(), digit->ManuId())) : 0x0;
371         AliMUONVCalibParam* gain = fGainStore ? static_cast<AliMUONVCalibParam*>(fGainStore->FindObject(digit->DetElemId(), digit->ManuId())) : 0x0;
372         Int_t manuChannel = digit->ManuChannel();
373         Int_t planeType = 0;
374         if ( digit->ManuId() & AliMpConstants::ManuMask(AliMp::kNonBendingPlane)) {
375           planeType = 1;
376         }
377         
378         // fill pad info
379         padInfo.SetPadId(digit->GetUniqueID());
380         padInfo.SetPadPlaneType(planeType);
381         padInfo.SetPadXY(pad.GetPositionX(), pad.GetPositionY());
382         padInfo.SetPadDimXY(pad.GetDimensionX(), pad.GetDimensionY());
383         padInfo.SetPadCharge((Double_t)digit->Charge());
384         padInfo.SetPadADC(digit->ADC());
385         padInfo.SetSaturated(digit->IsSaturated());
386         padInfo.SetCalibrated(digit->IsCalibrated());
387         if (ped) {
388           padInfo.SetPedestal(ped->ValueAsFloatFast(manuChannel,0), ped->ValueAsFloatFast(manuChannel,1));
389         } else {
390           padInfo.SetPedestal(-250.,-5.);
391         }
392         if (gain) {
393           padInfo.SetGain(gain->ValueAsFloatFast(manuChannel,0), gain->ValueAsFloatFast(manuChannel,1),
394                           gain->ValueAsIntFast(manuChannel,2), gain->ValueAsIntFast(manuChannel,3));
395         } else {
396           padInfo.SetGain(-1.,-0.1,-4095,-1);
397         }       
398         fClusterInfo->AddPad(padInfo);
399       }
400           
401       // fill cluster info tree
402       fClusterInfoTree->Fill();
403       trackParam = static_cast<AliMUONTrackParam*>(track->GetTrackParamAtCluster()->After(trackParam));
404     }
405   }
406   // free memory
407   delete newTrackStore;
408     
409   
410   // Post final data. Write histo list to a file with option "RECREATE"
411   PostData(0,fClusterInfoTree);
412 }      
413
414 //________________________________________________________________________
415 void AliMUONReAlignTask::Terminate(const Option_t*)
416 {
417   /// Called once per task on the client machine at the end of the analysis.
418
419 }
420
421 //-----------------------------------------------------------------------
422 void AliMUONReAlignTask::Prepare(const char* geoFilename, const char* defaultOCDB, const char* misAlignOCDB)
423 {
424   /// Set the geometry, the magnetic field, the mapping and the reconstruction parameters
425   
426   // Import TGeo geometry (needed by AliMUONTrackExtrap::ExtrapToVertex)
427   if (!gGeoManager) {
428     AliGeomManager::LoadGeometry(geoFilename);
429     if (!gGeoManager) {
430       Error("AliMUONReAlignTask", "getting geometry from file %s failed", "generated/galice.root");
431       return;
432     }
433   }
434     
435   // Load mapping
436   AliCDBManager* man = AliCDBManager::Instance();
437   man->SetDefaultStorage(defaultOCDB);
438   man->SetSpecificStorage("MUON/Align/Data",misAlignOCDB);
439   man->Print();
440   man->SetRun(0);
441   if ( ! AliMpCDB::LoadDDLStore() ) {
442     Error("MUONRefit","Could not access mapping from OCDB !");
443     exit(-1);
444   }
445
446   // set mag field
447   if (!TGeoGlobalMagField::Instance()->GetField()) {
448     printf("Loading field map...\n");
449     AliGRPManager *grpMan = new AliGRPManager();
450     grpMan->ReadGRPEntry();
451     grpMan->SetMagField();
452     delete grpMan;
453   }
454   // set the magnetic field for track extrapolations
455   AliMUONTrackExtrap::SetField();
456   
457   // Load initial reconstruction parameters from OCDB
458   // reconstruction parameters
459   fRecoParam = AliMUONRecoParam::GetCosmicParam();
460   
461   // digit selection
462   fRecoParam->SetPadGoodnessMask(0x400BE80);
463 //   TString caliboption = caliboption1;
464 //   if ( calib == 2 ) caliboption = caliboption2;
465 //   fRecoParam->SetCalibrationMode("NOGAIN"caliboption.Data());
466   fRecoParam->SetCalibrationMode("NOGAIN");
467   
468   // chamber resolution (incuding misalignment)
469   for (Int_t iCh=0; iCh<10; iCh++) {
470     fRecoParam->SetDefaultNonBendingReso(iCh,0.4);
471     fRecoParam->SetDefaultBendingReso(iCh,0.4);
472   }
473   fRecoParam->SetMaxNonBendingDistanceToTrack(10.);
474   fRecoParam->SetMaxBendingDistanceToTrack(10.);
475   
476   // cut on (non)bending slopes
477   //fRecoParam->SetMaxNonBendingSlope(0.6);
478   //fRecoParam->SetMaxBendingSlope(0.6);
479   
480   // tracking algorithm
481   //  fRecoParam->MakeMoreTrackCandidates(kTRUE);
482   fRecoParam->RequestStation(0, kFALSE);
483   fRecoParam->RequestStation(2, kFALSE);
484 //   fRecoParam->RequestStation(3, kFALSE);
485 //   fRecoParam->RequestStation(4, kFALSE);
486   fRecoParam->SetSigmaCutForTracking(7.);
487   fRecoParam->ImproveTracks(kTRUE, 7.);
488   Info("MUONRefit", "\n initial recontruction parameters:");
489   fRecoParam->Print("FULL");
490   
491   AliMUONESDInterface::ResetTracker(fRecoParam);  
492 }
493
494 //-----------------------------------------------------------------------
495 UInt_t AliMUONReAlignTask::BuildClusterMap(AliMUONTrack &track)
496 {
497   /// Build the map of clusters in tracking chambers
498   
499   UInt_t muonClusterMap = 0;
500   
501   AliMUONTrackParam* trackParam = static_cast<AliMUONTrackParam*>(track.GetTrackParamAtCluster()->First());
502   while (trackParam) {
503     
504     muonClusterMap |= BIT(trackParam->GetClusterPtr()->GetChamberId());
505     
506     trackParam = static_cast<AliMUONTrackParam*>(track.GetTrackParamAtCluster()->After(trackParam));
507   }
508   
509   return muonClusterMap;
510   
511 }