]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpSegmentation.cxx
No need to be a singleton
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpSegmentation.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: AliMpSegmentation.cxx,v 1.7 2006/05/24 13:58:34 ivana Exp $
18 // Category: management
19
20 //-----------------------------------------------------------------------------
21 // Class AliMpSegmentation
22 // -----------------------
23 // Singleton container class for mapping segmentations
24 // Authors: Ivana Hrivnacova, IPN Orsay
25 //          Laurent Aphecetche, SUBATECH
26 //-----------------------------------------------------------------------------
27
28 #include "AliMpSegmentation.h"
29
30 #include "AliMpDataStreams.h"
31 #include "AliMpDetElement.h"
32 #include "AliMpDEStore.h"
33 #include "AliMpDEManager.h"
34 #include "AliMpDEIterator.h"
35 #include "AliMpExMap.h"
36 #include "AliMpSector.h"
37 #include "AliMpSectorReader.h"
38 #include "AliMpSectorSegmentation.h"
39 #include "AliMpSlat.h"
40 #include "AliMpSlatSegmentation.h"
41 #include "AliMpSt345Reader.h"
42 #include "AliMpTrigger.h"
43 #include "AliMpTriggerReader.h"
44 #include "AliMpTriggerSegmentation.h"
45 #include "AliMpCathodType.h"
46 #include "AliMpSlatMotifMap.h"
47
48
49 #include "AliLog.h"
50
51 #include <Riostream.h>
52 #include <TMap.h>
53 #include <TObjString.h>
54 #include <TSystem.h>
55 #include <TClass.h>
56
57 #include <cassert>
58
59 /// \cond CLASSIMP
60 ClassImp(AliMpSegmentation)
61 /// \endcond
62
63 AliMpSegmentation* AliMpSegmentation::fgInstance = 0;
64
65 //
66 // static methods
67 //
68
69 //______________________________________________________________________________
70 AliMpSegmentation* AliMpSegmentation::Instance(Bool_t warn)
71 {
72 /// Return its instance
73
74   if ( ! fgInstance && warn ) {
75     AliWarningClass("Segmentation has not been loaded");
76   }  
77     
78   return fgInstance;
79 }    
80
81 //______________________________________________________________________________
82 AliMpSegmentation* AliMpSegmentation::ReadData(const AliMpDataStreams& dataStreams,
83                                                Bool_t warn)
84 {
85 /// Load the sementation from ASCII data files
86 /// and return its instance
87
88   if ( fgInstance ) {
89     if ( warn )
90       AliWarningClass("Segmentation has been already loaded");
91     return fgInstance;
92   }  
93   
94   if ( dataStreams.GetReadFromFiles() )
95     AliInfoClass("Reading segmentation from ASCII files.");
96
97   fgInstance = new AliMpSegmentation(dataStreams);
98   return fgInstance;
99 }    
100
101 //
102 // ctors, dtor
103 //
104
105 //______________________________________________________________________________
106 AliMpSegmentation::AliMpSegmentation(const AliMpDataStreams& dataStreams)
107 : TObject(),
108   fDataStreams(dataStreams),
109   fDetElements(0),
110   fMpSegmentations(),
111   fElCardsMap(),
112   fSlatMotifMap(AliMpSlatMotifMap::Instance())
113 {  
114 /// Standard constructor - segmentation is loaded from ASCII data files
115
116   AliDebug(1,"");
117   
118   // Load DE data
119   if ( ! AliMpDEStore::Instance(false) )  
120     AliMpDEStore::ReadData(dataStreams);
121   fDetElements = AliMpDEStore::Instance();  
122
123   // Create mapping segmentations for all detection elements
124   AliMpDEIterator it;
125   for ( it.First(); ! it.IsDone(); it.Next() ) 
126   {
127     Int_t n(0);
128     
129     for ( Int_t cath = AliMp::kCath0; cath <= AliMp::kCath1; ++cath ) 
130     { 
131       if ( CreateMpSegmentation(it.CurrentDEId(), AliMp::GetCathodType(cath)) ) ++n;
132     }
133      
134     if ( n == 2 &&  // should always be the case except when we play with the CreateMpSegmentation method...
135         AliMpDEManager::GetStationType(it.CurrentDEId()) != AliMp::kStationTrigger ) // only for tracker
136     {
137         // Fill el cards map for all detection elements
138         // of tracking chambers
139         FillElCardsMap(it.CurrentDEId());
140     }      
141   } 
142 }
143
144 //______________________________________________________________________________
145 AliMpSegmentation::AliMpSegmentation(TRootIOCtor* ioCtor)
146 : TObject(),
147   fDataStreams(ioCtor),
148   fDetElements(0),
149   fMpSegmentations(),
150   fElCardsMap(ioCtor),
151   fSlatMotifMap(0)
152 {  
153 /// Constructor for IO
154
155   AliDebug(1,"");
156
157   fgInstance = this;
158 }
159
160 //______________________________________________________________________________
161 AliMpSegmentation::~AliMpSegmentation()
162 {
163 /// Destructor
164
165   AliDebug(1,"");
166
167   delete fDetElements;
168
169   // Segmentations are deleted with fMpSegmentations 
170   // El cards arrays are deleted with fElCardsMap
171   
172   fgInstance = 0;
173 }
174
175 //
176 // private methods
177 //
178
179 //______________________________________________________________________________
180 AliMpVSegmentation* 
181 AliMpSegmentation::CreateMpSegmentation(Int_t detElemId, AliMp::CathodType cath)
182 {
183 /// Create mapping segmentation for given detElemId and cath
184 /// or return it if it was already built
185
186   // Check detElemId & cath  
187   if ( ! AliMpDEManager::IsValidDetElemId(detElemId, true) ) return 0;
188
189   // If segmentation is already built, just return it
190   //
191   AliMpDetElement* detElement = AliMpDEManager::GetDetElement(detElemId);
192   TString deSegName = detElement->GetSegName(cath);
193   TObject* object = fMpSegmentations.Get(deSegName);
194   if ( object ) return (AliMpVSegmentation*)object;
195
196   AliDebugStream(3)
197     << "Creating segmentation for detElemId=" << detElemId 
198     << " cath=" << cath << endl;
199   
200   // Read mapping data and create segmentation
201   //
202   AliMp::StationType stationType = detElement->GetStationType();
203   AliMp::PlaneType planeType = detElement->GetPlaneType(cath);
204   TString deTypeName = detElement->GetSegType();
205
206   AliMpVSegmentation* mpSegmentation = 0;
207
208   if ( stationType == AliMp::kStation1 || stationType == AliMp::kStation2 ) {
209     AliMpSectorReader reader(fDataStreams, stationType, planeType);
210     AliMpSector* sector = reader.BuildSector();
211     mpSegmentation = new AliMpSectorSegmentation(sector, true);
212   }
213   else if ( stationType == AliMp::kStation345 ) { 
214     AliMpSt345Reader reader(fDataStreams);
215     AliMpSlat* slat = reader.ReadSlat(deTypeName, planeType);
216     mpSegmentation =  new AliMpSlatSegmentation(slat, true);
217   }
218   else if ( stationType == AliMp::kStationTrigger ) {
219     AliMpTriggerReader reader(fDataStreams);
220     AliMpTrigger* trigger = reader.ReadSlat(deTypeName, planeType);
221     mpSegmentation = new AliMpTriggerSegmentation(trigger, true);
222   }
223   else   
224     AliErrorStream() << "Unknown station type" << endl;
225
226   if ( mpSegmentation ) fMpSegmentations.Add(deSegName, mpSegmentation); 
227   
228 //  StdoutToAliDebug(3, fSlatMotifMap.Print(););
229   
230   return mpSegmentation;
231
232
233 //_____________________________________________________________________________
234 AliMpExMap*
235 AliMpSegmentation::FillElCardsMap(Int_t detElemId)
236 {
237 /// Fill the map of electronic cards IDs to segmentations for
238 /// given detElemId
239
240   AliDebugStream(2) << "detElemId=" << detElemId << endl;;
241   
242   AliMpExMap* mde = new AliMpExMap;
243   mde->SetOwner(kFALSE);
244   fElCardsMap.Add(detElemId,mde);
245
246   const AliMpVSegmentation* seg[2];
247   TArrayI ecn[2];
248   
249   for ( Int_t cathode = AliMp::kCath0; cathode <= AliMp::kCath1; ++cathode )
250   {
251     seg[cathode] = GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
252     seg[cathode]->GetAllElectronicCardIDs(ecn[cathode]);
253     for ( Int_t i = 0; i < ecn[cathode].GetSize(); ++i )
254     {
255       mde->Add(ecn[cathode][i],const_cast<AliMpVSegmentation*>(seg[cathode]));
256     }
257   }
258   
259   assert( mde->GetSize() > 0 );
260   
261   return mde;
262   
263 }
264
265 //
266 // public methods
267 //
268
269 //______________________________________________________________________________
270 const AliMpVSegmentation* 
271 AliMpSegmentation::GetMpSegmentation(
272                       Int_t detElemId, AliMp::CathodType cath, Bool_t warn) const
273 {
274 /// Return mapping segmentation for given detElemId and cath
275
276   // Check detElemId & cath  
277   if ( ! AliMpDEManager::IsValidDetElemId(detElemId, false) ) {
278     
279     if ( warn ) {
280       AliWarningStream() 
281         << "Invalid detElemId " << detElemId << endl;
282     }   
283     return 0;
284   }  
285
286   // If segmentation is already built, just return it
287   //
288   AliMpDetElement* detElement = AliMpDEManager::GetDetElement(detElemId);
289   TString deSegName = detElement->GetSegName(cath);
290   TObject* object = fMpSegmentations.Get(deSegName);
291   if ( ! object ) {
292     // Should never happen
293     AliErrorStream() 
294       << "Segmentation for detElemId/cathod " 
295         << detElemId << ", " << cath << " not defined" << endl;
296     return 0;
297   }  
298   
299   return static_cast<AliMpVSegmentation*>(object);
300
301
302 //_____________________________________________________________________________
303 const AliMpVSegmentation* 
304 AliMpSegmentation::GetMpSegmentationByElectronics(
305                       Int_t detElemId, Int_t ecId, Bool_t warn) const
306 {
307 /// Return mapping segmentation for given detElemId and electronic card Id
308 /// (motif position Id)
309
310   AliMpExMap* m = static_cast<AliMpExMap*>(fElCardsMap.GetValue(detElemId));
311   
312   if (!m) {
313     // Should never happen
314     AliErrorStream() 
315       << "Cannot find the el cards map for detElemId " << detElemId << endl;
316     return 0;
317   }  
318
319   TObject* object = m->GetValue(ecId);
320   if ( ! object ) {
321     if ( warn ) {
322       AliErrorStream() 
323         << "Segmentation for electronic card " 
324         << ecId << " not found" << endl;
325     }   
326     return 0;
327   }  
328    
329   return static_cast<AliMpVSegmentation*>(object);
330 }