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