]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONGMSSubprocessor.cxx
Fix coverity defects
[u/mrichter/AliRoot.git] / MUON / AliMUONGMSSubprocessor.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 AliMUONGMSSubprocessor
20 // -----------------------------
21 // The shuttle subprocessor for GMS data
22 // Author: Ivana Hrivnacova, IPN Orsay
23 // 16/09/2006
24 //-----------------------------------------------------------------------------
25
26 #include "AliMUONGMSSubprocessor.h"
27 #include "AliMUONPreprocessor.h"
28 #include "AliMpConstants.h"
29
30 #include "AliAlignObjMatrix.h"
31 #include "AliGeomManager.h"
32 #include "AliCDBMetaData.h"
33 #include "AliCDBEntry.h"
34
35 #include <TTimeStamp.h>
36 #include <TFile.h>
37 #include <TArrayI.h>
38 #include <TClonesArray.h>
39 #include <TGeoManager.h>
40 #include <TObjString.h>
41 #include <Riostream.h>
42
43 /// \cond CLASSIMP
44 ClassImp(AliMUONGMSSubprocessor)
45 /// \endcond
46
47 const Int_t    AliMUONGMSSubprocessor::fgkSystem = AliPreprocessor::kDCS;
48 const TString  AliMUONGMSSubprocessor::fgkDataId = "GMS";
49 const TString  AliMUONGMSSubprocessor::fgkMatrixArrayName = "GMSarray";
50
51 //______________________________________________________________________________
52 AliMUONGMSSubprocessor::AliMUONGMSSubprocessor(AliMUONPreprocessor* master) 
53   : AliMUONVSubprocessor(master, "GMS", "Upload GMS matrices to OCDB"),
54     fTransformer(0)
55 {
56 /// Constructor
57 }
58
59 //______________________________________________________________________________
60 AliMUONGMSSubprocessor::~AliMUONGMSSubprocessor()
61 {
62 /// Destructor
63
64   delete fTransformer;
65 }
66
67
68 //
69 // private methods
70 //
71
72
73 //______________________________________________________________________________
74 Bool_t  AliMUONGMSSubprocessor::Initialize(Int_t /*run*/, 
75                                          UInt_t /*startTime*/, UInt_t /*endTime*/)
76 {
77 /// Instantiate geometry transformer
78
79   if ( ! fTransformer ) {
80     fTransformer = new AliMUONGeometryTransformer();
81     fTransformer->CreateModules();
82   }  
83   return kTRUE;
84 }                                           
85
86 //______________________________________________________________________________
87 UInt_t AliMUONGMSSubprocessor::ProcessFile(const TString& fileName)
88 {
89 /// Convert TGeoHMatrix to AliAlignObjMatrix and fill them into AliTestDataDCS object
90
91   Master()->Log(Form("Processing GMS file %s", fileName.Data()));
92   
93   // Open root file
94   TFile f(fileName.Data());
95   if ( ! f.IsOpen() ) {
96     Master()->Log(Form("Cannot open file %s",fileName.Data()));
97     return 1;
98   }  
99   
100   // Get array with matrices
101   TClonesArray* array = (TClonesArray*)f.Get(fgkMatrixArrayName);
102   if ( ! array ) {
103     Master()->Log(Form("TClonesArray not found in file %s",fileName.Data()));
104     return 2;
105   }    
106   
107   // Array to store correspondance between the moduleId 
108   // and its corresponding entry in the GMS array.
109   TArrayI moduleIdToGMSIndex;
110   moduleIdToGMSIndex.Set(AliMpConstants::NofGeomModules());
111   for (Int_t i=0; i<AliMpConstants::NofGeomModules(); i++){
112     moduleIdToGMSIndex[i]=-1;
113   }
114   
115   Bool_t useGlobalDelta = kTRUE;
116   // Get geometry from OCDB
117   AliCDBEntry* geoEntry = Master()->GetGeometryFromOCDB();
118   TGeoManager *lGeometry = 0x0;
119   if (geoEntry) {
120     lGeometry = (TGeoManager*) geoEntry->GetObject();
121     if (lGeometry) {
122       // Set Geometry
123       AliGeomManager::SetGeometry(lGeometry);
124       useGlobalDelta = kFALSE;
125     }
126   }
127
128   // Second transformer to convert GMS matrices local delta-transformation into 
129   // ALICE alignment objects in the global delta convention
130   AliMUONGeometryTransformer *lTransformer = new AliMUONGeometryTransformer();
131
132   // Get mis alignment from reference run for GMS  
133   AliCDBEntry* cdbEntry = Master()->GetFromOCDB("Align", "Baseline");
134   TClonesArray* refArray = 0x0;
135   if (cdbEntry) {
136     refArray = (TClonesArray*)cdbEntry->GetObject();
137     if (lGeometry && refArray) {      
138       AliGeomManager::ApplyAlignObjsToGeom(*refArray);
139       lTransformer->LoadGeometryData();
140     }
141   }
142   
143   // Convert matrices into Alice alignment objects
144   for (Int_t i=0; i<array->GetEntriesFast(); i++ ) {
145     TGeoHMatrix* matrix = (TGeoHMatrix*)array->At(i);
146     printf("GMS local %i at %i \n",matrix->GetUniqueID(),i);
147     matrix->Print();
148     fTransformer->AddMisAlignModule(matrix->GetUniqueID(), *matrix, kTRUE);
149     lTransformer->AddMisAlignModule(matrix->GetUniqueID(), *matrix, useGlobalDelta);
150     moduleIdToGMSIndex[matrix->GetUniqueID()]=i;
151   }
152   // Store the GMS local delta-transformation 
153   TClonesArray* data = const_cast< TClonesArray*>(fTransformer->GetMisAlignmentData());
154   
155   //Now we have to store the final CDB file
156   Master()->Log("Storing GMS");
157   AliCDBMetaData metaData;
158   metaData.SetBeamPeriod(0);
159   metaData.SetResponsible("");
160   metaData.SetComment("This preprocessor fills GMS alignment objects.");
161   
162   Bool_t result = Master()->Store("Align", "GMS", (TObject*)data, &metaData, 0, 0);
163   
164   // This section apply the GMS misalignments on top of the misalignments 
165   // of the GMS reference run and stores the new misalignment array in the OCDB
166
167   // Reset the geoemtry.
168   if (geoEntry) {
169     lGeometry = (TGeoManager*) geoEntry->GetObject();
170   }
171
172   if (lGeometry) {
173     TGeoManager::UnlockGeometry();
174     // Set Geometry
175     AliGeomManager::SetGeometry(lGeometry);
176     useGlobalDelta = kFALSE;
177   }
178
179   AliAlignObjMatrix* refAOMat = 0x0;
180   // First we need to get a copy of the local delta-transformations
181   TClonesArray* matLocalArray = new TClonesArray("TGeoHMatrix",200);
182   if (lGeometry && refArray) {      
183     refArray->Sort(); // All Modules will be first then the DE
184     // Create new misalignment array
185     for (Int_t i=0; i<refArray->GetEntriesFast(); i++) {
186       refAOMat = (AliAlignObjMatrix*)refArray->At(i);      
187       refAOMat->Print("");
188       TGeoHMatrix* reflMat = new((*matLocalArray)[i]) TGeoHMatrix(); 
189       refAOMat->GetLocalMatrix(*reflMat);
190       printf("ref misalignment local \n");
191       reflMat->Print();
192     }
193   }
194   
195   AliAlignObjMatrix* newAOMat = 0x0;
196   // Get the GMS global delta-transformation 
197   data = const_cast< TClonesArray*>(lTransformer->GetMisAlignmentData());
198   if (geoEntry && cdbEntry) {
199     if (lGeometry && refArray) {      
200       // Create new misalignment array
201       TClonesArray* newArray = new TClonesArray("AliAlignObjMatrix", 200);      
202       for (Int_t i=0; i<refArray->GetEntriesFast(); i++) {
203         refAOMat = (AliAlignObjMatrix*)refArray->At(i);      
204         TGeoHMatrix refMat;
205         refAOMat->GetMatrix(refMat);
206         newAOMat = new((*newArray)[i]) AliAlignObjMatrix(*refAOMat); // Copy the reference misalignment object to the new array ...
207         // Need the module containing this module or detection element
208         TString sName = refAOMat->GetSymName(); //Format "/MUON/GMx" or "/MUON/GMx/DEy"
209         Int_t iGM = sName.Index("GM");
210         Int_t iLS = sName.Last('/');
211         if (iLS<iGM) { // This is a module
212           sName.Remove(0,iGM+2);
213           Int_t iMod = sName.Atoi();
214           if (moduleIdToGMSIndex[iMod]>=0) {
215             AliAlignObjMatrix* gmsAOMat = (AliAlignObjMatrix*)data->At(moduleIdToGMSIndex[iMod]);
216             TGeoHMatrix gmsMat;
217             gmsAOMat->GetMatrix(gmsMat);
218             printf("GMS global delta %i %i\n",iMod,moduleIdToGMSIndex[iMod]);
219             gmsMat.Print();
220             printf("ref misalignment \n");
221             refMat.Print();
222             refMat.MultiplyLeft(&gmsMat);
223             printf("new misalignment gms*ref\n");
224             refMat.Print();
225           }
226           else {            
227             Master()->Log(Form("Missing GMS entry for module %d",iMod));
228           }
229           newAOMat->SetMatrix(refMat); // ... and set its matrix        
230         }
231         else { // This is a module
232           newAOMat->SetLocalMatrix(*(TGeoHMatrix*)matLocalArray->At(i)); // ... and set its matrix
233         }
234       }
235       
236       //Now we have also to store this CDB file
237       TString sMDComment(cdbEntry->GetMetaData()->GetComment());
238       sMDComment += " GMS";
239       Master()->Log("Storing MisAlignment");
240       metaData.SetComment(sMDComment);
241       
242       result = result && Master()->Store("Align", "Data", newArray, &metaData, 0, 1);
243     }
244     else {
245       if (!refArray)
246         Master()->Log("Empty entry?");
247       if (!lGeometry)
248         Master()->Log("Couldn't find TGeoManager in the specified CDB entry?");
249     }
250   }
251   else {
252     if (!geoEntry)
253       Master()->Log("Could not get Geometry from OCDB! Will not add a new MUON/Align/Data entry!");    
254     if (!cdbEntry)
255       Master()->Log("Could not get GMS reference misalignment from OCDB! Will not add a new MUON/Align/Data entry!");    
256   }
257   // Done with applying GMS misalignments on top of misalignment of reference run
258   
259   // Clear MisAlignArray in transformer
260   fTransformer->ClearMisAlignmentData(); 
261   
262   return (result!=kTRUE);
263 }  
264
265 //
266 // public methods
267 //
268
269
270 //______________________________________________________________________________
271 UInt_t AliMUONGMSSubprocessor::Process(TMap* /*dcsAliasMap*/)
272 {
273 /// Process GMS alignment files.
274 /// Return failure (0) in case procession of some file has failed
275
276   UInt_t result = 1;
277   TList* sources = Master()->GetFileSources(fgkSystem, fgkDataId);
278   TIter next(sources);
279   TObjString* o(0x0);
280   while ( ( o = static_cast<TObjString*>(next()) ) ) {
281     TString fileName(Master()->GetFile(fgkSystem, fgkDataId, o->GetName()));
282     result *= ProcessFile(fileName);
283   }
284   delete sources;
285
286   return result;
287 }
288