]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpDEStore.cxx
091e94daf79ec9854201d784f8fd594c76a7527d
[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 "AliMpDataStreams.h"
35 #include "AliMpHelper.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(const AliMpDataStreams& dataStreams, 
74                                      Bool_t warn)
75 {
76 /// Load the DE store data from ASCII data files
77 /// and return its instance
78
79   if ( fgInstance ) {
80     if ( warn )
81       AliWarningClass("DE Store has been already loaded");
82     return fgInstance;
83   }  
84   
85   if ( dataStreams.GetReadFromFiles() )
86     AliInfoClass("Reading DE Store from ASCII files.");
87
88   fgInstance = new AliMpDEStore(dataStreams);
89   return fgInstance;
90 }    
91
92 //
93 // ctors, dtor
94 //
95
96 //______________________________________________________________________________
97 AliMpDEStore::AliMpDEStore(const AliMpDataStreams& dataStreams)
98 : TObject(),
99   fkDataStreams(dataStreams),
100   fDetElements()
101 {  
102 /// Standard constructor
103
104   AliDebug(1,"");
105   fDetElements.SetOwner(true);
106
107   // Create all detection elements
108   FillDEs();
109 }
110
111 //______________________________________________________________________________
112 AliMpDEStore::AliMpDEStore(TRootIOCtor* ioCtor)
113 : TObject(),
114   fkDataStreams(ioCtor),
115   fDetElements(ioCtor)
116 {  
117 /// Constructor for IO
118
119   AliDebug(1,"");
120
121   fgInstance = this;
122 }
123
124
125 //______________________________________________________________________________
126 AliMpDEStore::~AliMpDEStore()
127 {
128 /// Destructor
129
130   AliDebug(1,"");
131
132   // Segmentations are deleted with fMpSegmentations 
133   // El cards arrays are deleted with fElCardsMap
134   
135   fgInstance = 0;
136 }
137
138 //
139 // private methods
140 //
141
142 //______________________________________________________________________________
143 Bool_t AliMpDEStore::IsPlaneType(const TString& planeTypeName)
144 {
145 /// Return true if the planeTypeName corresponds to a valid plane type
146
147   if ( planeTypeName == PlaneTypeName(AliMp::kBendingPlane) ||
148        planeTypeName == PlaneTypeName(AliMp::kNonBendingPlane) ) 
149     return true;   
150
151   return false;
152 }  
153
154 //______________________________________________________________________________
155 Bool_t
156 AliMpDEStore::ReadDENames(AliMp::StationType station, 
157                           AliMq::Station12Type station12)
158
159 /// Read det element names for cath = 0 from the file specified by name
160 /// and fill the map 
161
162   // Open stream
163   istream& in 
164     = fkDataStreams.
165         CreateDataStream(AliMpFiles::DENamesFilePath(station, station12));
166   
167   // Read plane types per cathods
168   //
169   char line[80];
170   TString word;
171   TString cathName1, cathName2;
172   in >> word;
173   while ( ! in.eof() && cathName1.Length() == 0 ) {
174     if ( word[0] == '#' ) 
175       in.getline(line, 80);
176     else { 
177       cathName1 = word;
178       in >> cathName2;
179     }
180     in >> word;
181   }
182   
183   Bool_t isCathNameDefined = false;
184   if ( IsPlaneType(cathName1) &&  IsPlaneType(cathName2) )
185     isCathNameDefined = true;
186     
187   // Read DE names
188   //
189   Int_t detElemId;
190   TString name, name0, name1, name2;
191   AliMp::PlaneType planeForCathode[2];
192   
193   while ( ! in.eof() ) 
194   {
195     if ( word[0] == '#' ) 
196     {
197       in.getline(line, 80);
198     }
199     else 
200     {  
201       detElemId = word.Atoi();
202       in >> name;
203       in >> name0;
204       // warning : important to check non bending first (=nbp),
205       // as bp is contained within nbp...
206       if ( name0.Contains(PlaneTypeName(AliMp::kNonBendingPlane)) )
207       {
208         planeForCathode[0] = AliMp::kNonBendingPlane;
209       }
210       else
211       {
212         planeForCathode[0] = AliMp::kBendingPlane;
213       }
214  
215       if ( !isCathNameDefined ) 
216       { 
217         in >> name2;
218         name1 = name0; 
219         Ssiz_t pos = name1.First(AliMpDetElement::GetNameSeparator());
220         name0 = name1(0,pos);
221
222         // Other cathode is other plane...
223         planeForCathode[1] = OtherPlaneType(planeForCathode[0]);
224       }
225       else 
226       {
227         name1 = name0 + AliMpDetElement::GetNameSeparator() + cathName1;
228         name2 = name0 + AliMpDetElement::GetNameSeparator() + cathName2;
229         if ( name2.Contains(PlaneTypeName(AliMp::kNonBendingPlane)) )
230         {
231           planeForCathode[1] = AliMp::kNonBendingPlane;
232         }
233         else
234         {
235           planeForCathode[1] = AliMp::kBendingPlane;
236         }        
237       }   
238
239       if ( planeForCathode[0]==planeForCathode[1] )
240       {
241         AliFatalClass(Form("Got the same cathode type for both planes"
242                       " of DetElemId %d",detElemId));
243       }
244       
245       AliMpDetElement* detElement = new AliMpDetElement(detElemId, name, name0, planeForCathode[0]);
246       
247       if ( ! fDetElements.GetValue(detElemId) ) 
248       {
249         AliDebugClassStream(3)  
250           << "Adding DE name "  << detElemId << "  " << name << endl;
251         fDetElements.Add(detElemId, detElement); 
252       } 
253       else 
254       {
255         AliWarningClassStream()
256           << "Det element "  << detElemId << "  " << name << " already defined." << endl;
257       } 
258     } 
259     in >> word;
260   }
261   
262   delete &in;
263
264   return true;
265 }
266
267 //______________________________________________________________________________
268 void AliMpDEStore::FillDEs()
269 {
270 /// Fill DE names from files
271   AliDebugClass(2,"");
272   Bool_t result1 = ReadDENames(AliMp::kStation12, AliMq::kStation1);
273   Bool_t result2 = ReadDENames(AliMp::kStation12, AliMq::kStation2);
274   Bool_t result3 = ReadDENames(AliMp::kStation345);
275   Bool_t result4 = ReadDENames(AliMp::kStationTrigger);
276   
277   Bool_t result = result1 && result2 && result3 && result4;
278   if ( ! result ) {
279     AliErrorClassStream() << "Error in reading DE names files" << endl;
280   }  
281   AliDebug(1,Form("%d detection elements were read in",fDetElements.GetSize()));
282 }
283
284 //
285 // public methods
286 //
287
288
289 //______________________________________________________________________________
290 AliMpDetElement* AliMpDEStore::GetDetElement(Int_t detElemId, Bool_t warn) const
291 {
292 /// Return det element for given detElemId
293
294   AliMpDetElement* detElement
295     = (AliMpDetElement*)fDetElements.GetValue(detElemId);
296     
297   if ( ! detElement && warn ) {  
298     AliErrorClassStream() 
299         << "Detection element " << detElemId << " not defined." << endl;
300   }     
301
302   return detElement;
303 }    
304
305 //______________________________________________________________________________
306 AliMpDetElement* AliMpDEStore::GetDetElement(const TString& deName, Bool_t warn) const
307 {
308 /// Return det element for given deName
309
310   TIter next(fDetElements.CreateIterator());
311   AliMpDetElement* detElement;
312   
313   while ( ( detElement = static_cast<AliMpDetElement*>(next()) ) )
314   {
315              
316     if (deName.CompareTo(detElement->GetDEName()) == 0) 
317
318       return detElement;
319   }
320
321   if (warn) {  
322     AliErrorClassStream() 
323         << "Detection element with name" << deName.Data() << " not defined." << endl;
324   }     
325
326   return 0x0;   
327
328 }