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