Coding conventions
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpManuStore.cxx
CommitLineData
ab167304 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>
6332274b 49#include <cstdlib>
ab167304 50
51/// \cond CLASSIMP
52ClassImp(AliMpManuStore)
53/// \endcond
54
55AliMpManuStore* AliMpManuStore::fgInstance = 0;
56Bool_t AliMpManuStore::fgWarnIfDoublon = kFALSE;
57
58//
59// static methods
60//
61
62//______________________________________________________________________________
63AliMpManuStore* 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//______________________________________________________________________________
76AliMpManuStore* 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//______________________________________________________________________________
101AliMpManuStore::AliMpManuStore(const AliMpDataStreams& dataStreams)
102: TObject(),
7d5d0cc5 103 fkDataStreams(dataStreams),
ab167304 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//______________________________________________________________________________
125AliMpManuStore::AliMpManuStore(TRootIOCtor* ioCtor)
126: TObject(),
7d5d0cc5 127 fkDataStreams(ioCtor),
ab167304 128 fManuToSerialNbs(),
129 fSerialNbToManus(),
130 fNofManusInDE(),
131 fNofManus(0)
132{
133/// Constructor for IO
134
135 AliDebug(1,"");
136}
137
138
139//______________________________________________________________________________
140AliMpManuStore::~AliMpManuStore()
141{
142/// Destructor
143
144 AliDebug(1,"");
145}
146
147//
148// private methods
149//
150
151//______________________________________________________________________________
152Bool_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());
4e51cfd2 160 AliMq::Station12Type station12Type
161 = AliMpDEManager::GetStation12Type(de->GetId());
ab167304 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
7d5d0cc5 172 = fkDataStreams.
4e51cfd2 173 CreateDataStream(
174 AliMpFiles::ManuToSerialPath(deName, stationType, station12Type));
ab167304 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//______________________________________________________________________________
207Bool_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//______________________________________________________________________________
236void 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 = AliMpExMap::GetIndex(AliMpIntPair(detElemId, manuId));
243
244 TExMap newManuToSerialNbs;
245 // Loop over map
246 TExMapIter it(&fManuToSerialNbs);
247 Long_t key;
248 Long_t value;
249 while ( ( it.Next(key, value) ) ) {
250
251 if ( key != index )
252 newManuToSerialNbs.Add(key, value);
253 else
254 newManuToSerialNbs.Add(index, Long_t(serialNb));
255 }
256
257 TExMap newSerialNbToManus;
258 // Loop over map
259 TExMapIter it2(&fSerialNbToManus);
260 while ( ( it2.Next(key, value) ) ) {
261
262 if ( value != index )
263 newSerialNbToManus.Add(key, value);
264 else
265 newSerialNbToManus.Add(Long_t(serialNb), index);
266 }
267
268 // And now replace the maps
269 fManuToSerialNbs = newManuToSerialNbs;
270 fSerialNbToManus = newManuToSerialNbs;
271}
272
273
274//______________________________________________________________________________
275Bool_t AliMpManuStore::WriteData(const TString& outDir)
276{
277/// Write data files for all detection elements.
278/// Return true if reading was successful.
279/// Not yet in use, declared private.
280
281 TString curDir = gSystem->pwd();
282
283 // Create top directory
284 //
285 if ( gSystem->OpenDirectory(outDir.Data()) ) {
286 AliErrorStream()
287 << "Directory " << outDir.Data() << " already exists" << endl;
288 return kFALSE;
289 }
290 else {
291 AliDebugStream(2) << "Making directory " << outDir.Data() << endl;
292 gSystem->mkdir(outDir.Data());
293 }
294
295 // Loop over DE
296 AliMpDEIterator it;
297 for ( it.First(); ! it.IsDone(); it.Next() ) {
298
299 AliMpDetElement* detElement = it.CurrentDE();
300 Int_t detElemId = detElement->GetId();
301 TString deName = detElement->GetDEName();
302 AliMp::StationType stationType
303 = AliMpDEManager::GetStationType(detElemId);
4e51cfd2 304 AliMq::Station12Type station12Type
305 = AliMpDEManager::GetStation12Type(detElemId);
ab167304 306
307 if ( stationType == AliMp::kStationTrigger ) continue;
308
309 // Create directory if it does not yet exist
310 //
4e51cfd2 311 TString dirPath = outDir + AliMpFiles::StationDataDir(stationType, station12Type);
ab167304 312 if ( ! gSystem->OpenDirectory(dirPath.Data()) ) {
313 AliDebugStream(2) << "Making directory " << dirPath.Data() << endl;
314 gSystem->mkdir(dirPath.Data());
315 }
316
317 // Compose output file path
318 //
4e51cfd2 319 string dataPath = AliMpFiles::ManuToSerialPath(deName, stationType, station12Type).Data();
ab167304 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);
06d8fc51 323 TString dataPath1 = dataPath;
324 TString filePath = outDir + "/" + dataPath1;
ab167304 325
326 // Open file
327 //
328 ofstream out(filePath.Data());
329 if ( ! out.good() ) {
330 AliErrorStream()
331 << "Cannot open output file " << filePath.Data() << endl;
332 return kFALSE;
333 }
334
335 // Loop over map
53bf67e2 336 TExMapIter it2(&fManuToSerialNbs);
ab167304 337 Long_t key;
338 Long_t value;
53bf67e2 339 while ( ( it2.Next(key, value) ) ) {
ab167304 340 AliMpIntPair pair = AliMpExMap::GetPair(key);
341
342 if ( pair.GetFirst() != detElemId ) continue;
343
344 AliDebugStream(3)
345 << "Go to write " << key << " " << pair << " " << value << endl;
346
347 Int_t manuId = pair.GetSecond();
348 static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
349
350 TString planeName = PlaneTypeName(AliMp::kBendingPlane);
351 if ( pair.GetSecond()> manuMask ) {
352 planeName = PlaneTypeName(AliMp::kNonBendingPlane);
353 manuId -= manuMask;
354 }
355 out << manuId << " " << planeName.Data() << " " << value << endl;
356
357 AliDebugStream(3)
358 << manuId << " " << planeName.Data() << " " << value << endl;
359 }
360 out.close();
361 }
362 gSystem->cd(curDir);
363 return kTRUE;
364}
365
366//
367// public methods
368//
369
370
371//______________________________________________________________________________
372Int_t AliMpManuStore::NofManus() const
373{
374/// Return total number of manus in the store
375
376 return fNofManus;
377}
378
379
380//______________________________________________________________________________
381Int_t AliMpManuStore::NofManus(Int_t detElemId) const
382{
383/// Return number of manus in given detection element
384
385 if ( ! AliMpDEManager::IsValidDetElemId(detElemId, kTRUE) ) return 0;
386
387 return fNofManusInDE.GetValue(detElemId);
388}
389
390//______________________________________________________________________________
391Bool_t AliMpManuStore::AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
392{
393/// Add manu to the map
394
395 Long_t index = AliMpExMap::GetIndex(AliMpIntPair(detElemId, manuId));
396
397 AliDebugStream(2)
398 << "Adding (" << detElemId << "," << manuId
399 << ") as index=" << index << " and serialNb=" << serialNb << endl;
400
401 fManuToSerialNbs.Add(index, Long_t(serialNb));
402
403 Long_t value = fSerialNbToManus.GetValue(Long_t(serialNb));
404 if ( value ) {
405 if ( fgWarnIfDoublon ) {
406 AliWarningStream()
407 << "Serial number " << serialNb
408 << " already present for (detElemId, manuId) = " << AliMpExMap::GetPair(value)
409 << ", it will nod be added for ("
410 << detElemId << "," << manuId << ")" << endl;
411 }
412 return kFALSE;
413 }
414 else {
415 fSerialNbToManus.Add(Long_t(serialNb), index);
416 return kTRUE;
417 }
418}
419
420
421//______________________________________________________________________________
422Int_t AliMpManuStore::GetManuSerial(Int_t detElemId, Int_t manuId) const
423{
424/// Return manu serial number for given detElemId and manuId
425
426 Long_t index = AliMpExMap::GetIndex(AliMpIntPair(detElemId, manuId));
427 // cout << index << " " << fManuToSerialNbs.GetValue(index) << endl;
428
429 return fManuToSerialNbs.GetValue(index);
430}
431
432//______________________________________________________________________________
433AliMpIntPair AliMpManuStore::GetDetElemIdManu(Int_t manuSerial) const
434{
435/// Return detElemId and manuId for given manu serial number
436
437 Long_t value = fSerialNbToManus.GetValue(Long_t(manuSerial));
438 // cout << manuSerial << " " << value << endl;
439
440 return AliMpExMap::GetPair(value);
441}
442