1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // Category: management
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 //-----------------------------------------------------------------------------
27 #include "AliMpManuStore.h"
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"
41 #include <Riostream.h>
44 #include <TObjString.h>
45 #include <TObjArray.h>
52 ClassImp(AliMpManuStore)
55 AliMpManuStore* AliMpManuStore::fgInstance = 0;
56 Bool_t AliMpManuStore::fgWarnIfDoublon = kFALSE;
62 //______________________________________________________________________________
63 AliMpManuStore* AliMpManuStore::Instance(Bool_t warn)
65 /// Create the DDL store if it does not yet exist
66 /// and return its instance
68 if ( ! fgInstance && warn ) {
69 AliWarningClass("Manu Store has not been loaded");
75 //______________________________________________________________________________
76 AliMpManuStore* AliMpManuStore::ReadData(const AliMpDataStreams& dataStreams,
79 /// Load the DDL store from ASCII data files
80 /// and return its instance
84 AliWarningClass("DDL Store has been already loaded");
88 if ( dataStreams.GetReadFromFiles() )
89 AliInfoClass("Reading Manu Store from ASCII files.");
91 fgInstance = new AliMpManuStore(dataStreams);
100 //______________________________________________________________________________
101 AliMpManuStore::AliMpManuStore(const AliMpDataStreams& dataStreams)
103 fkDataStreams(dataStreams),
109 /// Standard constructor
113 // Check if DE store is loaded
114 if ( ! AliMpDEStore::Instance(false) ) {
116 << "Mapping segmentation has not be loaded. Cannont load Manu store"
124 //______________________________________________________________________________
125 AliMpManuStore::AliMpManuStore(TRootIOCtor* ioCtor)
127 fkDataStreams(ioCtor),
133 /// Constructor for IO
139 //______________________________________________________________________________
140 AliMpManuStore::~AliMpManuStore()
151 //______________________________________________________________________________
152 Bool_t AliMpManuStore::ReadData(const AliMpDetElement* de, Int_t& nofManus)
154 /// Read manu serial numbers for the given detection element
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());
163 // Nothing to be done for trigger
164 if ( stationType == AliMp::kStationTrigger ) {
169 static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
174 AliMpFiles::ManuToSerialPath(deName, stationType, station12Type));
179 while ( in.getline(line,80) ) {
181 if ( line[0] == '#' ) continue;
183 TString tmp(AliMpHelper::Normalize(line));
185 TObjArray* stringList = tmp.Tokenize(TString(" "));
187 Int_t manuId = atoi( ((TObjString*)stringList->At(0))->GetName());
188 Int_t manuSerial = atoi( ((TObjString*)stringList->At(2))->GetName());
190 TString sPlane = ((TObjString*)stringList->At(1))->GetString();
192 // filling manuId <> manuSerial
193 if (!sPlane.CompareTo(PlaneTypeName(AliMp::kBendingPlane)))
194 AddManu(deId, manuId, manuSerial);
196 AddManu(deId, manuId + manuMask, manuSerial);
206 //______________________________________________________________________________
207 Bool_t AliMpManuStore::ReadManuSerial()
209 /// Read data files for all detection elements.
210 /// Return true if reading was successful.
216 for ( it.First(); ! it.IsDone(); it.Next() ) {
218 AliMpDetElement* detElement = it.CurrentDE();
221 Bool_t result = ReadData(detElement, nofManus);
222 fNofManusInDE.Add(detElement->GetId(), nofManus);
223 fNofManus += nofManus;
226 << "Adding " << nofManus << " manus for de "
227 << detElement->GetId() << endl;
229 isOk = isOk && result;
235 //______________________________________________________________________________
236 void AliMpManuStore::ReplaceManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
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.
242 Long_t index = AliMp::Pair(detElemId, manuId);
244 TExMap newManuToSerialNbs;
246 TExMapIter it(&fManuToSerialNbs);
249 while ( ( it.Next(key, value) ) ) {
252 newManuToSerialNbs.Add(key, value);
254 newManuToSerialNbs.Add(index, Long_t(serialNb));
257 TExMap newSerialNbToManus;
259 TExMapIter it2(&fSerialNbToManus);
260 while ( ( it2.Next(key, value) ) ) {
262 if ( value != index )
263 newSerialNbToManus.Add(key, value);
265 newSerialNbToManus.Add(Long_t(serialNb), index);
268 // And now replace the maps
269 fManuToSerialNbs = newManuToSerialNbs;
270 fSerialNbToManus = newManuToSerialNbs;
274 //______________________________________________________________________________
275 Bool_t AliMpManuStore::WriteData(const TString& outDir)
277 /// Write data files for all detection elements.
278 /// Return true if reading was successful.
279 /// Not yet in use, declared private.
281 TString curDir = gSystem->pwd();
283 // Create top directory
285 if ( gSystem->OpenDirectory(outDir.Data()) ) {
287 << "Directory " << outDir.Data() << " already exists" << endl;
291 AliDebugStream(2) << "Making directory " << outDir.Data() << endl;
292 gSystem->mkdir(outDir.Data());
297 for ( it.First(); ! it.IsDone(); it.Next() ) {
299 AliMpDetElement* detElement = it.CurrentDE();
300 Int_t detElemId = detElement->GetId();
301 TString deName = detElement->GetDEName();
302 AliMp::StationType stationType
303 = AliMpDEManager::GetStationType(detElemId);
304 AliMq::Station12Type station12Type
305 = AliMpDEManager::GetStation12Type(detElemId);
307 if ( stationType == AliMp::kStationTrigger ) continue;
309 // Create directory if it does not yet exist
311 TString dirPath = outDir + AliMpFiles::StationDataDir(stationType, station12Type);
312 if ( ! gSystem->OpenDirectory(dirPath.Data()) ) {
313 AliDebugStream(2) << "Making directory " << dirPath.Data() << endl;
314 gSystem->mkdir(dirPath.Data());
317 // Compose output file path
319 string dataPath = AliMpFiles::ManuToSerialPath(deName, stationType, station12Type).Data();
320 string top = AliMpFiles::GetTop().Data();
321 if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
322 dataPath.erase(0,dataPath.find('/')+1);
323 TString dataPath1 = dataPath;
324 TString filePath = outDir + "/" + dataPath1;
328 ofstream out(filePath.Data());
329 if ( ! out.good() ) {
331 << "Cannot open output file " << filePath.Data() << endl;
336 TExMapIter it2(&fManuToSerialNbs);
339 while ( ( it2.Next(key, value) ) ) {
340 Int_t pairFirst = AliMp::PairFirst(key);
342 if ( pairFirst != detElemId ) continue;
344 Int_t manuId = AliMp::PairSecond(key);
347 << "Go to write " << key << " "
348 << pairFirst << " " << manuId << " " << value << endl;
350 static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
352 TString planeName = PlaneTypeName(AliMp::kBendingPlane);
353 if ( manuId> manuMask ) {
354 planeName = PlaneTypeName(AliMp::kNonBendingPlane);
357 out << manuId << " " << planeName.Data() << " " << value << endl;
360 << manuId << " " << planeName.Data() << " " << value << endl;
373 //______________________________________________________________________________
374 Int_t AliMpManuStore::NofManus() const
376 /// Return total number of manus in the store
382 //______________________________________________________________________________
383 Int_t AliMpManuStore::NofManus(Int_t detElemId) const
385 /// Return number of manus in given detection element
387 if ( ! AliMpDEManager::IsValidDetElemId(detElemId, kTRUE) ) return 0;
389 return fNofManusInDE.GetValue(detElemId);
392 //______________________________________________________________________________
393 Bool_t AliMpManuStore::AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
395 /// Add manu to the map
397 Long_t index = AliMp::Pair(detElemId, manuId);
400 << "Adding (" << detElemId << "," << manuId
401 << ") as index=" << index << " and serialNb=" << serialNb << endl;
403 fManuToSerialNbs.Add(index, Long_t(serialNb));
405 Long_t value = fSerialNbToManus.GetValue(Long_t(serialNb));
407 if ( fgWarnIfDoublon ) {
409 << "Serial number " << serialNb
410 << " already present for (detElemId, manuId) = " ;
411 AliMp::PairPut(AliWarningStream(), (MpPair_t) value) << endl;
416 fSerialNbToManus.Add(Long_t(serialNb), index);
422 //______________________________________________________________________________
423 Int_t AliMpManuStore::GetManuSerial(Int_t detElemId, Int_t manuId) const
425 /// Return manu serial number for given detElemId and manuId
427 Long_t index = AliMp::Pair(detElemId, manuId);
428 // cout << index << " " << fManuToSerialNbs.GetValue(index) << endl;
430 return fManuToSerialNbs.GetValue(index);
433 //______________________________________________________________________________
434 MpPair_t AliMpManuStore::GetDetElemIdManu(Int_t manuSerial) const
436 /// Return detElemId and manuId for given manu serial number
439 return fSerialNbToManus.GetValue(Long_t(manuSerial));