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