]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpDEStore.cxx
AliMUONDigitCalibrator
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpDEStore.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 // $MpId: AliMpDEStore.cxx,v 1.4 2006/05/24 13:58:34 ivana Exp $
18 // Category: management
19
20 //-----------------------------------------------------------------------------
21 // Class AliMpDEStore
22 // --------------------
23 // The container class for detection element objects
24 // Authors: Ivana Hrivnacova, IPN Orsay
25 //          Laurent Aphecetche, Christian Finck, SUBATECH Nantes
26 //-----------------------------------------------------------------------------
27
28 #include <cstdlib>
29 #include "AliMpDEStore.h"
30 #include "AliMpDEManager.h"
31 #include "AliMpDetElement.h"
32 #include "AliMpConstants.h"
33 #include "AliMpFiles.h"
34 #include "AliMpHelper.h"
35 #include "AliMpIntPair.h"
36 #include "AliMpConstants.h"
37 #include "AliMpExMapIterator.h"
38
39 #include "AliLog.h"
40
41 #include <Riostream.h>
42 #include <TClass.h>
43 #include <TSystem.h>
44 #include <TObjString.h>
45 #include <TObjArray.h>
46 #include <TMap.h>
47
48 /// \cond CLASSIMP
49 ClassImp(AliMpDEStore)
50 /// \endcond
51
52 AliMpDEStore* AliMpDEStore::fgInstance = 0;
53 const char    AliMpDEStore::fgkCommentPrefix = '#'; 
54
55 //
56 // static methods
57 //
58
59 //______________________________________________________________________________
60 AliMpDEStore* AliMpDEStore::Instance(Bool_t warn)
61 {
62 /// Create the DE store if it does not yet exist
63 /// and return its instance
64
65   if ( ! fgInstance && warn  ) {
66     AliWarningClass("DE Store has not been loaded");
67   }  
68      
69   return fgInstance;
70 }    
71
72 //______________________________________________________________________________
73 AliMpDEStore* AliMpDEStore::ReadData(Bool_t warn)
74 {
75 /// Load the DE store data from ASCII data files
76 /// and return its instance
77
78   if ( fgInstance ) {
79     if ( warn )
80       AliWarningClass("DE Store has been already loaded");
81     return fgInstance;
82   }  
83   
84   AliInfoClass("Reading DE Store from ASCII files.");
85
86   fgInstance = new AliMpDEStore();
87   return fgInstance;
88 }    
89
90 //
91 // ctors, dtor
92 //
93
94 //______________________________________________________________________________
95 AliMpDEStore::AliMpDEStore()
96 : TObject(),
97   fDetElements()
98 {  
99 /// Standard constructor
100
101   AliDebug(1,"");
102   fDetElements.SetOwner(true);
103
104   // Create all detection elements
105   FillDEs();
106 }
107
108 //______________________________________________________________________________
109 AliMpDEStore::AliMpDEStore(TRootIOCtor* ioCtor)
110 : TObject(),
111   fDetElements(ioCtor)
112 {  
113 /// Constructor for IO
114
115   AliDebug(1,"");
116
117   fgInstance = this;
118 }
119
120
121 //______________________________________________________________________________
122 AliMpDEStore::~AliMpDEStore()
123 {
124 /// Destructor
125
126   AliDebug(1,"");
127
128   // Segmentations are deleted with fMpSegmentations 
129   // El cards arrays are deleted with fElCardsMap
130   
131   fgInstance = 0;
132 }
133
134 //
135 // private methods
136 //
137
138 //______________________________________________________________________________
139 Bool_t AliMpDEStore::IsPlaneType(const TString& planeTypeName)
140 {
141 /// Return true if the planeTypeName corresponds to a valid plane type
142
143   if ( planeTypeName == PlaneTypeName(AliMp::kBendingPlane) ||
144        planeTypeName == PlaneTypeName(AliMp::kNonBendingPlane) ) 
145     return true;   
146
147   return false;
148 }  
149
150 //______________________________________________________________________________
151 AliMp::PlaneType AliMpDEStore::PlaneType(const TString& planeTypeName)
152 {
153 /// Return plane type for the given planeTypeName                            \n
154 /// Fatal error if planeTypeName is wrong 
155
156   if ( planeTypeName == PlaneTypeName(AliMp::kBendingPlane) ) 
157     return AliMp::kBendingPlane;
158
159   if ( planeTypeName == PlaneTypeName(AliMp::kNonBendingPlane) ) 
160     return AliMp::kNonBendingPlane;
161
162   // Should never reach this line
163   AliFatalClass(Form("No plane type defined for %s", planeTypeName.Data()));
164   return AliMp::kBendingPlane;
165 }       
166
167 //______________________________________________________________________________
168 AliMp::StationType AliMpDEStore::StationType(const TString& stationTypeName)
169 {
170 /// Return station type for the given stationTypeName                        \n
171 /// Fatal error if stationTypeName is wrong 
172
173   if ( stationTypeName == StationTypeName(AliMp::kStation1) )
174     return AliMp::kStation1;
175
176   if ( stationTypeName == StationTypeName(AliMp::kStation2) )
177     return AliMp::kStation2;
178
179   if ( stationTypeName == StationTypeName(AliMp::kStation345) )
180     return AliMp::kStation345;
181
182   if ( stationTypeName == StationTypeName(AliMp::kStationTrigger) ) 
183     return AliMp::kStationTrigger;
184
185   // Should never reach this line
186   AliFatalClass(Form("No station type defined for ", stationTypeName.Data()));
187   return AliMp::kStation1;
188 }
189
190 //______________________________________________________________________________
191 Bool_t AliMpDEStore::ReadManuToSerialNbs(AliMpDetElement* detElement, 
192                                        AliMp::StationType stationType)
193 {
194 /// Read manu serial numbers for the given detection element
195   static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
196
197   TString deName = detElement->GetDEName();
198
199   TString infile = AliMpFiles::ManuToSerialPath(deName, stationType);
200   ifstream in(infile, ios::in);
201   
202   // Change to Error when all files available
203   //if ( !in.is_open() && stationType == AliMp::kStation345 ) {
204   //   AliWarningStream() << "File " << infile << " not found." << endl;
205   //  return false;
206   //}   
207        
208   char line[80];
209
210   while ( in.getline(line,80) ) {
211
212     if ( line[0] == '#' ) continue;
213
214     TString tmp(AliMpHelper::Normalize(line));
215
216     TObjArray* stringList = tmp.Tokenize(TString(" "));
217
218     Int_t manuId     = atoi( ((TObjString*)stringList->At(0))->GetName());
219     Int_t manuSerial = atoi( ((TObjString*)stringList->At(2))->GetName());
220       
221     TString sPlane = ((TObjString*)stringList->At(1))->GetString();
222
223     // filling manuId <> manuSerial
224     if (!sPlane.CompareTo(PlaneTypeName(AliMp::kBendingPlane)))
225         detElement->AddManuSerial(manuId, manuSerial);
226     else 
227         detElement->AddManuSerial(manuId + manuMask, manuSerial);
228
229     delete stringList;
230   }
231    
232   in.close();
233   return true;
234 }
235
236 //______________________________________________________________________________
237 Bool_t
238 AliMpDEStore::ReadDENames(AliMp::StationType station)
239
240 /// Read det element names for cath = 0 from the file specified by name
241 /// and fill the map 
242
243   // Open file
244   TString filePath = AliMpFiles::DENamesFilePath(station);
245   std::ifstream in(filePath);
246   if (!in.good()) {
247     AliErrorClassStream() << "Cannot open file " << filePath << endl;;
248     return false;
249   }
250   
251   // Read plane types per cathods
252   //
253   char line[80];
254   TString word;
255   TString cathName1, cathName2;
256   in >> word;
257   while ( ! in.eof() && cathName1.Length() == 0 ) {
258     if ( word[0] == '#' ) 
259       in.getline(line, 80);
260     else { 
261       cathName1 = word;
262       in >> cathName2;
263     }
264     in >> word;
265   }
266   
267   Bool_t isCathNameDefined = false;
268   if ( IsPlaneType(cathName1) &&  IsPlaneType(cathName2) )
269     isCathNameDefined = true;
270     
271   // Read DE names
272   //
273   Int_t detElemId;
274   TString name, name0, name1, name2;
275   AliMp::PlaneType planeForCathode[2];
276   
277   while ( ! in.eof() ) 
278   {
279     if ( word[0] == '#' ) 
280     {
281       in.getline(line, 80);
282     }
283     else 
284     {  
285       detElemId = word.Atoi();
286       in >> name;
287       in >> name0;
288       // warning : important to check non bending first (=nbp),
289       // as bp is contained within nbp...
290       if ( name0.Contains(PlaneTypeName(AliMp::kNonBendingPlane)) )
291       {
292         planeForCathode[0] = AliMp::kNonBendingPlane;
293       }
294       else
295       {
296         planeForCathode[0] = AliMp::kBendingPlane;
297       }
298  
299       if ( !isCathNameDefined ) 
300       { 
301         in >> name2;
302         name1 = name0; 
303         Ssiz_t pos = name1.First(AliMpDetElement::GetNameSeparator());
304         name0 = name1(0,pos);
305
306         // Other cathode is other plane...
307         planeForCathode[1] = OtherPlaneType(planeForCathode[0]);
308       }
309       else 
310       {
311         name1 = name0 + AliMpDetElement::GetNameSeparator() + cathName1;
312         name2 = name0 + AliMpDetElement::GetNameSeparator() + cathName2;
313         if ( name2.Contains(PlaneTypeName(AliMp::kNonBendingPlane)) )
314         {
315           planeForCathode[1] = AliMp::kNonBendingPlane;
316         }
317         else
318         {
319           planeForCathode[1] = AliMp::kBendingPlane;
320         }        
321       }   
322
323       if ( planeForCathode[0]==planeForCathode[1] )
324       {
325         AliFatalClass(Form("Got the same cathode type for both planes"
326                       " of DetElemId %d",detElemId));
327       }
328       
329       AliMpDetElement* detElement = new AliMpDetElement(detElemId, name, name0, planeForCathode[0]);
330       
331       if ( ! fDetElements.GetValue(detElemId) ) 
332       {
333         AliDebugClassStream(3)  
334           << "Adding DE name "  << detElemId << "  " << name << endl;
335         fDetElements.Add(detElemId, detElement); 
336         
337         // Read manu serial numbers for this det element
338         ReadManuToSerialNbs(detElement, station);
339       } 
340       else 
341       {
342         AliWarningClassStream()
343           << "Det element "  << detElemId << "  " << name << " already defined." << endl;
344       } 
345     } 
346     in >> word;
347   }
348
349   // Close file
350   in.close();
351   
352   return true;
353 }
354
355 //______________________________________________________________________________
356 void AliMpDEStore::FillDEs()
357 {
358 /// Fill DE names from files
359   AliDebugClass(2,"");
360   Bool_t result1 = ReadDENames(AliMp::kStation1);
361   Bool_t result2 = ReadDENames(AliMp::kStation2);
362   Bool_t result3 = ReadDENames(AliMp::kStation345);
363   Bool_t result4 = ReadDENames(AliMp::kStationTrigger);
364   
365   Bool_t result = result1 && result2 && result3 && result4;
366   if ( ! result ) {
367     AliErrorClassStream() << "Error in reading DE names files" << endl;
368   }  
369 }
370
371 //
372 // public methods
373 //
374
375
376 //______________________________________________________________________________
377 AliMpDetElement* AliMpDEStore::GetDetElement(Int_t detElemId, Bool_t warn) const
378 {
379 /// Return det element for given detElemId
380
381   AliMpDetElement* detElement
382     = (AliMpDetElement*)fDetElements.GetValue(detElemId);
383     
384   if ( ! detElement && warn ) {  
385     AliErrorClassStream() 
386         << "Detection element " << detElemId << " not defined." << endl;
387   }     
388
389   return detElement;
390 }    
391
392 //______________________________________________________________________________
393 AliMpDetElement* AliMpDEStore::GetDetElement(const TString& deName, Bool_t warn) const
394 {
395 /// Return det element for given deName
396
397   TIter next(fDetElements.CreateIterator());
398   AliMpDetElement* detElement;
399   
400   while ( ( detElement = static_cast<AliMpDetElement*>(next()) ) )
401   {
402              
403     if (deName.CompareTo(detElement->GetDEName()) == 0) 
404
405       return detElement;
406   }
407
408   if (warn) {  
409     AliErrorClassStream() 
410         << "Detection element with name" << deName.Data() << " not defined." << endl;
411   }     
412
413   return 0x0;   
414
415 }
416 //______________________________________________________________________________
417 AliMpIntPair  AliMpDEStore::GetDetElemIdManu(Int_t manuSerial) const
418 {
419 /// Return the detElemId and manuId for given serial manu number
420
421   TIter next(fDetElements.CreateIterator());
422   AliMpDetElement* detElement;
423   
424   while ( ( detElement = static_cast<AliMpDetElement*>(next()) ) )
425   {
426     Int_t manuId = detElement->GetManuIdFromSerial(manuSerial);
427     if ( manuId ) return AliMpIntPair(detElement->GetId(), manuId);
428   }    
429
430   // manu with this serial number does not exist
431   return AliMpIntPair::Invalid();
432 }  
433