]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpManuStore.cxx
Fix floating exception bug which can appear in few specific cases.
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpManuStore.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 // Category: management
18
19 //-----------------------------------------------------------------------------
20 // Class AliMpManuStore
21 // --------------------
22 // The container class for manu serial numbersd
23 // Authors: Ivana Hrivnacova, IPN Orsay
24 //          Christian Finck, SUBATECH Nantes
25 //-----------------------------------------------------------------------------
26
27 #include "AliMpManuStore.h"
28
29 #include "AliMpDEStore.h"
30 #include "AliMpDEManager.h"
31 #include "AliMpDetElement.h"
32 #include "AliMpConstants.h"
33 #include "AliMpDataStreams.h"
34 #include "AliMpFiles.h"
35 #include "AliMpHelper.h"
36 #include "AliMpConstants.h"
37 #include "AliMpEncodePair.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 #include <fstream>
49 #include <cstdlib>
50
51 /// \cond CLASSIMP
52 ClassImp(AliMpManuStore)
53 /// \endcond
54
55 AliMpManuStore* AliMpManuStore::fgInstance = 0;
56 Bool_t          AliMpManuStore::fgWarnIfDoublon = kFALSE;
57
58 //
59 // static methods
60 //
61
62 //______________________________________________________________________________
63 AliMpManuStore* AliMpManuStore::Instance(Bool_t warn) 
64 {
65     /// Create the DDL store if it does not yet exist
66     /// and return its instance
67
68     if ( ! fgInstance && warn  ) {
69         AliWarningClass("Manu Store has not been loaded");
70     }
71
72     return fgInstance;
73 }
74
75 //______________________________________________________________________________
76 AliMpManuStore* AliMpManuStore::ReadData(const AliMpDataStreams& dataStreams, 
77                                          Bool_t warn, Bool_t empty) 
78 {
79     /// Load the DDL store from ASCII data files
80     /// and return its instance
81
82     if ( fgInstance ) {
83         if ( warn )
84             AliWarningClass("Manu Store has been already loaded");
85         return fgInstance;
86     }
87
88     if ( ! empty ) {
89       if ( dataStreams.GetReadFromFiles() )
90         AliInfoClass("Reading Manu Store from ASCII files.");
91
92       fgInstance = new AliMpManuStore(dataStreams);
93     }
94     else {
95       AliInfoClass("Creating invalid Manu Store of minimum size.");
96       fgInstance = new AliMpManuStore(dataStreams, true);
97     }   
98         
99     return fgInstance;
100 }
101
102 //
103 // ctors, dtor
104 //
105
106
107 //______________________________________________________________________________
108 AliMpManuStore::AliMpManuStore(const AliMpDataStreams& dataStreams)
109 : TObject(),
110   fkDataStreams(dataStreams),
111   fManuToSerialNbs(),
112   fSerialNbToManus(),
113   fNofManusInDE(),
114   fNofManus(0)
115 {  
116 /// Standard constructor
117
118   AliDebug(1,"");
119
120   // Check if DE store is loaded
121   if ( ! AliMpDEStore::Instance(false) ) {
122      AliErrorStream()
123        << "Mapping segmentation has not be loaded. Cannont load Manu store"
124        << endl;
125      return;
126   }      
127   
128   ReadManuSerial();
129 }
130
131 //______________________________________________________________________________
132 AliMpManuStore::AliMpManuStore(const AliMpDataStreams& dataStreams, Bool_t empty)
133 : TObject(),
134   fkDataStreams(dataStreams),
135   fManuToSerialNbs(0),
136   fSerialNbToManus(0),
137   fNofManusInDE(0),
138   fNofManus(0)
139 {  
140 /// The special constructor for creating a minimum size object
141 /// The argument minSize is used only to distinguish this ctor
142 /// from the standard one, its value is not used.
143
144   AliDebug(1,"");
145 }
146
147 //______________________________________________________________________________
148 AliMpManuStore::AliMpManuStore(TRootIOCtor* ioCtor)
149 : TObject(),
150   fkDataStreams(ioCtor),
151   fManuToSerialNbs(),
152   fSerialNbToManus(),
153   fNofManusInDE(),
154   fNofManus(0)
155 {  
156 /// Constructor for IO
157
158   AliDebug(1,"");
159 }
160
161
162 //______________________________________________________________________________
163 AliMpManuStore::~AliMpManuStore()
164 {
165 /// Destructor
166
167   AliDebug(1,"");
168 }
169
170 //
171 // private methods
172 //
173
174 //______________________________________________________________________________
175 Bool_t AliMpManuStore::ReadData(const AliMpDetElement* de, Int_t& nofManus)
176 {
177 /// Read manu serial numbers for the given detection element
178
179   Int_t deId = de->GetId();
180   TString deName = de->GetDEName();
181   AliMp::StationType stationType 
182     =  AliMpDEManager::GetStationType(de->GetId());
183   AliMq::Station12Type station12Type 
184     =  AliMpDEManager::GetStation12Type(de->GetId());
185
186   // Nothing to be done for trigger
187   if ( stationType == AliMp::kStationTrigger ) {
188     nofManus = 0;
189     return kTRUE;
190   }  
191
192   static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
193
194   istream& in 
195     = fkDataStreams.
196         CreateDataStream(
197           AliMpFiles::ManuToSerialPath(deName, stationType, station12Type));
198
199   char line[80];
200
201   nofManus = 0;
202   while ( in.getline(line,80) ) {
203
204     if ( line[0] == '#' ) continue;
205
206     TString tmp(AliMpHelper::Normalize(line));
207
208     TObjArray* stringList = tmp.Tokenize(TString(" "));
209
210     Int_t manuId     = atoi( ((TObjString*)stringList->At(0))->GetName());
211     Int_t manuSerial = atoi( ((TObjString*)stringList->At(2))->GetName());
212       
213     TString sPlane = ((TObjString*)stringList->At(1))->GetString();
214
215     // filling manuId <> manuSerial
216     if (!sPlane.CompareTo(PlaneTypeName(AliMp::kBendingPlane)))
217         AddManu(deId, manuId, manuSerial);
218     else 
219         AddManu(deId, manuId + manuMask, manuSerial);
220         
221     ++nofManus;    
222
223     delete stringList;
224   }
225    
226   return kTRUE;
227 }
228
229 //______________________________________________________________________________
230 Bool_t  AliMpManuStore::ReadManuSerial()
231 {
232 /// Read data files for all detection elements.
233 /// Return true if reading was successful.
234
235   Bool_t isOk = kTRUE;
236
237   // Loop over DE
238   AliMpDEIterator it;
239   for ( it.First(); ! it.IsDone(); it.Next() ) {
240
241     AliMpDetElement* detElement = it.CurrentDE();
242
243     Int_t nofManus;
244     Bool_t result = ReadData(detElement, nofManus);  
245     fNofManusInDE.Add(detElement->GetId(), nofManus);
246     fNofManus += nofManus;
247     
248     AliDebugStream(2) 
249       << "Adding " << nofManus << " manus for de " 
250       << detElement->GetId() << endl;
251     
252     isOk = isOk && result;
253   }
254   
255   return isOk;  
256 }   
257
258 //______________________________________________________________________________
259 void  AliMpManuStore::ReplaceManu(Int_t detElemId, Int_t manuId, Int_t serialNb) 
260 {
261 /// Replace manu in the map.
262 /// As TExMap has no replcae function, we have to rebuild map once again.
263 /// Not yet in use, declared private.
264
265   Long_t index = AliMp::Pair(detElemId, manuId);
266
267   TExMap newManuToSerialNbs;
268   // Loop over map
269   TExMapIter it(&fManuToSerialNbs);
270
271 #if ROOT_SVN_REVISION >= 29598
272   Long64_t key;
273   Long64_t value;
274 #else
275   Long_t key;
276   Long_t value;
277 #endif
278   while ( ( it.Next(key, value) ) ) {
279
280     if ( key != index ) 
281       newManuToSerialNbs.Add(key, value);
282     else
283       newManuToSerialNbs.Add(index, Long_t(serialNb));
284   }
285       
286   TExMap newSerialNbToManus;
287   // Loop over map
288   TExMapIter it2(&fSerialNbToManus);
289   while ( ( it2.Next(key, value) ) ) {
290
291     if ( value != index ) 
292       newSerialNbToManus.Add(key, value);
293     else
294       newSerialNbToManus.Add(Long_t(serialNb), index);
295   }
296
297   // And now replace the maps
298   fManuToSerialNbs = newManuToSerialNbs;
299   fSerialNbToManus = newManuToSerialNbs;
300 }     
301
302
303 //______________________________________________________________________________
304 Bool_t  AliMpManuStore::WriteData(const TString& outDir)
305 {
306 /// Write data files for all detection elements.
307 /// Return true if reading was successful.
308 /// Not yet in use, declared private.
309
310   TString curDir = gSystem->pwd();
311
312   // Create top directory
313   //
314   if ( gSystem->OpenDirectory(outDir.Data()) ) {
315     AliErrorStream() 
316       << "Directory " << outDir.Data() << " already exists" << endl;
317     return kFALSE;
318   }
319   else {
320     AliDebugStream(2) << "Making directory " <<  outDir.Data() << endl;
321     gSystem->mkdir(outDir.Data());
322   }  
323
324   // Loop over DE
325   AliMpDEIterator it;
326   for ( it.First(); ! it.IsDone(); it.Next() ) {
327   
328     AliMpDetElement* detElement = it.CurrentDE();
329     Int_t detElemId = detElement->GetId();
330     TString deName = detElement->GetDEName();
331     AliMp::StationType stationType 
332       =  AliMpDEManager::GetStationType(detElemId);
333     AliMq::Station12Type station12Type 
334       =  AliMpDEManager::GetStation12Type(detElemId);
335       
336     if ( stationType == AliMp::kStationTrigger ) continue;  
337
338     // Create directory if it does not yet exist
339     //
340     TString dirPath = outDir + AliMpFiles::StationDataDir(stationType, station12Type);
341     if ( ! gSystem->OpenDirectory(dirPath.Data()) ) {
342       AliDebugStream(2) << "Making directory " <<  dirPath.Data() << endl;
343       gSystem->mkdir(dirPath.Data());
344     }  
345
346     // Compose output file path 
347     //
348     string dataPath = AliMpFiles::ManuToSerialPath(deName, stationType, station12Type).Data();
349     string top = AliMpFiles::GetTop().Data();
350     if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
351     dataPath.erase(0,dataPath.find('/')+1);
352     TString dataPath1 = dataPath;
353     TString filePath = outDir + "/" + dataPath1; 
354
355     // Open file
356     //
357     ofstream out(filePath.Data());
358     if ( ! out.good() ) {
359       AliErrorStream() 
360         << "Cannot open output file  " << filePath.Data() << endl;
361       return kFALSE;  
362     }
363     
364     // Loop over map
365     TExMapIter it2(&fManuToSerialNbs);
366 #if ROOT_SVN_REVISION >= 29598
367     Long64_t key;
368     Long64_t value;
369 #else
370     Long_t key;
371     Long_t value;
372 #endif
373     while ( ( it2.Next(key, value) ) ) {
374       Int_t pairFirst = AliMp::PairFirst(key);
375       
376       if ( pairFirst != detElemId ) continue;
377       
378       Int_t manuId = AliMp::PairSecond(key);
379
380       AliDebugStream(3) 
381         << "Go to write " << key << " " 
382         << pairFirst << " " << manuId << " " << value << endl;
383
384       static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
385
386       TString planeName = PlaneTypeName(AliMp::kBendingPlane);
387       if ( manuId> manuMask ) {
388         planeName = PlaneTypeName(AliMp::kNonBendingPlane);
389         manuId -= manuMask;
390       } 
391       out << manuId << " " << planeName.Data() <<  " " << value << endl;
392       
393       AliDebugStream(3) 
394         << manuId << " " << planeName.Data() <<  " " << value << endl;
395     }  
396     out.close();
397   }   
398   gSystem->cd(curDir); 
399   return kTRUE;  
400 }   
401
402 //
403 // public methods
404 //
405
406
407 //______________________________________________________________________________
408 Int_t  AliMpManuStore::NofManus() const
409
410 /// Return total number of manus in the store
411
412   return fNofManus;
413 }  
414   
415
416 //______________________________________________________________________________
417 Int_t  AliMpManuStore::NofManus(Int_t detElemId) const 
418
419 /// Return number of manus in given detection element
420
421    if ( ! AliMpDEManager::IsValidDetElemId(detElemId, kTRUE) ) return 0;   
422
423    return fNofManusInDE.GetValue(detElemId); 
424 }
425
426 //______________________________________________________________________________
427 Bool_t  AliMpManuStore::AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb) 
428 {
429 /// Add manu to the map
430
431   Long_t index = AliMp::Pair(detElemId, manuId);
432   
433   AliDebugStream(2)
434     << "Adding (" << detElemId << "," <<  manuId 
435     << ") as index=" << index << " and serialNb=" << serialNb << endl;
436   
437   fManuToSerialNbs.Add(index, Long_t(serialNb));
438   
439   Long_t value = fSerialNbToManus.GetValue(Long_t(serialNb));
440   if ( value ) {
441     if ( fgWarnIfDoublon ) {
442       AliWarningStream() 
443         << "Serial number " << serialNb 
444         << " already present for (detElemId, manuId) = " ;
445         AliMp::PairPut(AliWarningStream(), (MpPair_t) value) << endl;
446      }
447      return kFALSE;    
448   }
449   else {
450     fSerialNbToManus.Add(Long_t(serialNb), index);
451     return kTRUE;
452   }  
453 }     
454
455
456 //______________________________________________________________________________
457 Int_t AliMpManuStore::GetManuSerial(Int_t detElemId, Int_t manuId) const
458 {
459 /// Return manu serial number for given detElemId and manuId
460
461   Long_t index = AliMp::Pair(detElemId, manuId);
462   // cout << index << " " << fManuToSerialNbs.GetValue(index) << endl;
463   
464   return fManuToSerialNbs.GetValue(index);
465 }  
466
467 //______________________________________________________________________________
468 MpPair_t  AliMpManuStore::GetDetElemIdManu(Int_t manuSerial) const
469 {
470 /// Return detElemId and manuId for given manu serial number 
471 /// as encoded pair
472
473   return fSerialNbToManus.GetValue(Long_t(manuSerial));
474 }  
475