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