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 "AliMpIntPair.h"
37 #include "AliMpConstants.h"
41 #include <Riostream.h>
44 #include <TObjString.h>
45 #include <TObjArray.h>
51 ClassImp(AliMpManuStore)
54 AliMpManuStore* AliMpManuStore::fgInstance = 0;
55 Bool_t AliMpManuStore::fgWarnIfDoublon = kFALSE;
61 //______________________________________________________________________________
62 AliMpManuStore* AliMpManuStore::Instance(Bool_t warn)
64 /// Create the DDL store if it does not yet exist
65 /// and return its instance
67 if ( ! fgInstance && warn ) {
68 AliWarningClass("Manu Store has not been loaded");
74 //______________________________________________________________________________
75 AliMpManuStore* AliMpManuStore::ReadData(const AliMpDataStreams& dataStreams,
78 /// Load the DDL store from ASCII data files
79 /// and return its instance
83 AliWarningClass("DDL Store has been already loaded");
87 if ( dataStreams.GetReadFromFiles() )
88 AliInfoClass("Reading Manu Store from ASCII files.");
90 fgInstance = new AliMpManuStore(dataStreams);
99 //______________________________________________________________________________
100 AliMpManuStore::AliMpManuStore(const AliMpDataStreams& dataStreams)
102 fDataStreams(dataStreams),
108 /// Standard constructor
112 // Check if DE store is loaded
113 if ( ! AliMpDEStore::Instance(false) ) {
115 << "Mapping segmentation has not be loaded. Cannont load Manu store"
123 //______________________________________________________________________________
124 AliMpManuStore::AliMpManuStore(TRootIOCtor* ioCtor)
126 fDataStreams(ioCtor),
132 /// Constructor for IO
138 //______________________________________________________________________________
139 AliMpManuStore::~AliMpManuStore()
150 //______________________________________________________________________________
151 Bool_t AliMpManuStore::ReadData(const AliMpDetElement* de, Int_t& nofManus)
153 /// Read manu serial numbers for the given detection element
155 Int_t deId = de->GetId();
156 TString deName = de->GetDEName();
157 AliMp::StationType stationType
158 = AliMpDEManager::GetStationType(de->GetId());
160 // Nothing to be done for trigger
161 if ( stationType == AliMp::kStationTrigger ) {
166 static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
170 CreateDataStream(AliMpFiles::ManuToSerialPath(deName, stationType));
175 while ( in.getline(line,80) ) {
177 if ( line[0] == '#' ) continue;
179 TString tmp(AliMpHelper::Normalize(line));
181 TObjArray* stringList = tmp.Tokenize(TString(" "));
183 Int_t manuId = atoi( ((TObjString*)stringList->At(0))->GetName());
184 Int_t manuSerial = atoi( ((TObjString*)stringList->At(2))->GetName());
186 TString sPlane = ((TObjString*)stringList->At(1))->GetString();
188 // filling manuId <> manuSerial
189 if (!sPlane.CompareTo(PlaneTypeName(AliMp::kBendingPlane)))
190 AddManu(deId, manuId, manuSerial);
192 AddManu(deId, manuId + manuMask, manuSerial);
202 //______________________________________________________________________________
203 Bool_t AliMpManuStore::ReadManuSerial()
205 /// Read data files for all detection elements.
206 /// Return true if reading was successful.
212 for ( it.First(); ! it.IsDone(); it.Next() ) {
214 AliMpDetElement* detElement = it.CurrentDE();
217 Bool_t result = ReadData(detElement, nofManus);
218 fNofManusInDE.Add(detElement->GetId(), nofManus);
219 fNofManus += nofManus;
222 << "Adding " << nofManus << " manus for de "
223 << detElement->GetId() << endl;
225 isOk = isOk && result;
231 //______________________________________________________________________________
232 void AliMpManuStore::ReplaceManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
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.
238 Long_t index = AliMpExMap::GetIndex(AliMpIntPair(detElemId, manuId));
240 TExMap newManuToSerialNbs;
242 TExMapIter it(&fManuToSerialNbs);
245 while ( ( it.Next(key, value) ) ) {
248 newManuToSerialNbs.Add(key, value);
250 newManuToSerialNbs.Add(index, Long_t(serialNb));
253 TExMap newSerialNbToManus;
255 TExMapIter it2(&fSerialNbToManus);
256 while ( ( it2.Next(key, value) ) ) {
258 if ( value != index )
259 newSerialNbToManus.Add(key, value);
261 newSerialNbToManus.Add(Long_t(serialNb), index);
264 // And now replace the maps
265 fManuToSerialNbs = newManuToSerialNbs;
266 fSerialNbToManus = newManuToSerialNbs;
270 //______________________________________________________________________________
271 Bool_t AliMpManuStore::WriteData(const TString& outDir)
273 /// Write data files for all detection elements.
274 /// Return true if reading was successful.
275 /// Not yet in use, declared private.
277 TString curDir = gSystem->pwd();
279 // Create top directory
281 if ( gSystem->OpenDirectory(outDir.Data()) ) {
283 << "Directory " << outDir.Data() << " already exists" << endl;
287 AliDebugStream(2) << "Making directory " << outDir.Data() << endl;
288 gSystem->mkdir(outDir.Data());
293 for ( it.First(); ! it.IsDone(); it.Next() ) {
295 AliMpDetElement* detElement = it.CurrentDE();
296 Int_t detElemId = detElement->GetId();
297 TString deName = detElement->GetDEName();
298 AliMp::StationType stationType
299 = AliMpDEManager::GetStationType(detElemId);
301 if ( stationType == AliMp::kStationTrigger ) continue;
303 // Create directory if it does not yet exist
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());
311 // Compose output file path
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;
321 ofstream out(filePath.Data());
322 if ( ! out.good() ) {
324 << "Cannot open output file " << filePath.Data() << endl;
329 TExMapIter it(&fManuToSerialNbs);
332 while ( ( it.Next(key, value) ) ) {
333 AliMpIntPair pair = AliMpExMap::GetPair(key);
335 if ( pair.GetFirst() != detElemId ) continue;
338 << "Go to write " << key << " " << pair << " " << value << endl;
340 Int_t manuId = pair.GetSecond();
341 static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
343 TString planeName = PlaneTypeName(AliMp::kBendingPlane);
344 if ( pair.GetSecond()> manuMask ) {
345 planeName = PlaneTypeName(AliMp::kNonBendingPlane);
348 out << manuId << " " << planeName.Data() << " " << value << endl;
351 << manuId << " " << planeName.Data() << " " << value << endl;
364 //______________________________________________________________________________
365 Int_t AliMpManuStore::NofManus() const
367 /// Return total number of manus in the store
373 //______________________________________________________________________________
374 Int_t AliMpManuStore::NofManus(Int_t detElemId) const
376 /// Return number of manus in given detection element
378 if ( ! AliMpDEManager::IsValidDetElemId(detElemId, kTRUE) ) return 0;
380 return fNofManusInDE.GetValue(detElemId);
383 //______________________________________________________________________________
384 Bool_t AliMpManuStore::AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
386 /// Add manu to the map
388 Long_t index = AliMpExMap::GetIndex(AliMpIntPair(detElemId, manuId));
391 << "Adding (" << detElemId << "," << manuId
392 << ") as index=" << index << " and serialNb=" << serialNb << endl;
394 fManuToSerialNbs.Add(index, Long_t(serialNb));
396 Long_t value = fSerialNbToManus.GetValue(Long_t(serialNb));
398 if ( fgWarnIfDoublon ) {
400 << "Serial number " << serialNb
401 << " already present for (detElemId, manuId) = " << AliMpExMap::GetPair(value)
402 << ", it will nod be added for ("
403 << detElemId << "," << manuId << ")" << endl;
408 fSerialNbToManus.Add(Long_t(serialNb), index);
414 //______________________________________________________________________________
415 Int_t AliMpManuStore::GetManuSerial(Int_t detElemId, Int_t manuId) const
417 /// Return manu serial number for given detElemId and manuId
419 Long_t index = AliMpExMap::GetIndex(AliMpIntPair(detElemId, manuId));
420 // cout << index << " " << fManuToSerialNbs.GetValue(index) << endl;
422 return fManuToSerialNbs.GetValue(index);
425 //______________________________________________________________________________
426 AliMpIntPair AliMpManuStore::GetDetElemIdManu(Int_t manuSerial) const
428 /// Return detElemId and manuId for given manu serial number
430 Long_t value = fSerialNbToManus.GetValue(Long_t(manuSerial));
431 // cout << manuSerial << " " << value << endl;
433 return AliMpExMap::GetPair(value);