]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/mapping/AliMpManuStore.cxx
Fix for ROOT Git transition
[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"
ab167304 36#include "AliMpConstants.h"
168e9c4d 37#include "AliMpEncodePair.h"
ab167304 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,
d2d10ee1 77 Bool_t warn)
ab167304 78{
79 /// Load the DDL store from ASCII data files
80 /// and return its instance
81
82 if ( fgInstance ) {
83 if ( warn )
f05cce50 84 AliWarningClass("Manu Store has been already loaded");
ab167304 85 return fgInstance;
86 }
87
d2d10ee1 88 if ( dataStreams.GetReadFromFiles() )
89 AliInfoClass("Reading Manu Store from ASCII files.");
ab167304 90
d2d10ee1 91 fgInstance = new AliMpManuStore(dataStreams);
ab167304 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 }
ab167304 120
d2d10ee1 121 ReadManuSerial();
877f7c6e 122}
123
ab167304 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,"");
f05cce50 145
146 fgInstance = 0;
ab167304 147}
148
149//
150// private methods
151//
152
153//______________________________________________________________________________
154Bool_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());
4e51cfd2 162 AliMq::Station12Type station12Type
163 = AliMpDEManager::GetStation12Type(de->GetId());
ab167304 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
7d5d0cc5 174 = fkDataStreams.
4e51cfd2 175 CreateDataStream(
176 AliMpFiles::ManuToSerialPath(deName, stationType, station12Type));
ab167304 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 }
b0cb5ffd 204
205 delete &in;
ab167304 206
207 return kTRUE;
208}
209
210//______________________________________________________________________________
211Bool_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//______________________________________________________________________________
240void 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
168e9c4d 246 Long_t index = AliMp::Pair(detElemId, manuId);
ab167304 247
248 TExMap newManuToSerialNbs;
249 // Loop over map
250 TExMapIter it(&fManuToSerialNbs);
82280315 251
8b8f8fd4 252#if (defined(ROOT_VERSION_CODE) && ROOT_VERSION_CODE >= 334082) || \
253 (defined(ROOT_SVN_REVISION) && ROOT_SVN_REVISION >= 29598)
82280315 254 Long64_t key;
255 Long64_t value;
256#else
ab167304 257 Long_t key;
258 Long_t value;
82280315 259#endif
ab167304 260 while ( ( it.Next(key, value) ) ) {
261
262 if ( key != index )
263 newManuToSerialNbs.Add(key, value);
264 else
265 newManuToSerialNbs.Add(index, Long_t(serialNb));
266 }
267
268 TExMap newSerialNbToManus;
269 // Loop over map
270 TExMapIter it2(&fSerialNbToManus);
271 while ( ( it2.Next(key, value) ) ) {
272
273 if ( value != index )
274 newSerialNbToManus.Add(key, value);
275 else
276 newSerialNbToManus.Add(Long_t(serialNb), index);
277 }
278
279 // And now replace the maps
280 fManuToSerialNbs = newManuToSerialNbs;
281 fSerialNbToManus = newManuToSerialNbs;
282}
283
284
285//______________________________________________________________________________
286Bool_t AliMpManuStore::WriteData(const TString& outDir)
287{
288/// Write data files for all detection elements.
289/// Return true if reading was successful.
290/// Not yet in use, declared private.
291
292 TString curDir = gSystem->pwd();
293
294 // Create top directory
295 //
296 if ( gSystem->OpenDirectory(outDir.Data()) ) {
297 AliErrorStream()
298 << "Directory " << outDir.Data() << " already exists" << endl;
299 return kFALSE;
300 }
301 else {
302 AliDebugStream(2) << "Making directory " << outDir.Data() << endl;
303 gSystem->mkdir(outDir.Data());
304 }
305
306 // Loop over DE
307 AliMpDEIterator it;
308 for ( it.First(); ! it.IsDone(); it.Next() ) {
309
310 AliMpDetElement* detElement = it.CurrentDE();
311 Int_t detElemId = detElement->GetId();
312 TString deName = detElement->GetDEName();
313 AliMp::StationType stationType
314 = AliMpDEManager::GetStationType(detElemId);
4e51cfd2 315 AliMq::Station12Type station12Type
316 = AliMpDEManager::GetStation12Type(detElemId);
ab167304 317
318 if ( stationType == AliMp::kStationTrigger ) continue;
319
320 // Create directory if it does not yet exist
321 //
4e51cfd2 322 TString dirPath = outDir + AliMpFiles::StationDataDir(stationType, station12Type);
ab167304 323 if ( ! gSystem->OpenDirectory(dirPath.Data()) ) {
324 AliDebugStream(2) << "Making directory " << dirPath.Data() << endl;
325 gSystem->mkdir(dirPath.Data());
326 }
327
328 // Compose output file path
329 //
4e51cfd2 330 string dataPath = AliMpFiles::ManuToSerialPath(deName, stationType, station12Type).Data();
ab167304 331 string top = AliMpFiles::GetTop().Data();
332 if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
333 dataPath.erase(0,dataPath.find('/')+1);
06d8fc51 334 TString dataPath1 = dataPath;
335 TString filePath = outDir + "/" + dataPath1;
ab167304 336
337 // Open file
338 //
339 ofstream out(filePath.Data());
340 if ( ! out.good() ) {
341 AliErrorStream()
342 << "Cannot open output file " << filePath.Data() << endl;
343 return kFALSE;
344 }
345
346 // Loop over map
53bf67e2 347 TExMapIter it2(&fManuToSerialNbs);
8b8f8fd4 348#if (defined(ROOT_SVN_REVISION) && ROOT_SVN_REVISION >= 29598) || \
349 (defined(ROOT_VERSION_CODE) && ROOT_VERSION_CODE >= ROOT_VERSION(5,25,02))
82280315 350 Long64_t key;
351 Long64_t value;
352#else
ab167304 353 Long_t key;
354 Long_t value;
82280315 355#endif
53bf67e2 356 while ( ( it2.Next(key, value) ) ) {
168e9c4d 357 Int_t pairFirst = AliMp::PairFirst(key);
ab167304 358
168e9c4d 359 if ( pairFirst != detElemId ) continue;
ab167304 360
168e9c4d 361 Int_t manuId = AliMp::PairSecond(key);
362
ab167304 363 AliDebugStream(3)
168e9c4d 364 << "Go to write " << key << " "
365 << pairFirst << " " << manuId << " " << value << endl;
ab167304 366
ab167304 367 static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
368
369 TString planeName = PlaneTypeName(AliMp::kBendingPlane);
168e9c4d 370 if ( manuId> manuMask ) {
ab167304 371 planeName = PlaneTypeName(AliMp::kNonBendingPlane);
372 manuId -= manuMask;
373 }
374 out << manuId << " " << planeName.Data() << " " << value << endl;
375
376 AliDebugStream(3)
377 << manuId << " " << planeName.Data() << " " << value << endl;
378 }
379 out.close();
380 }
381 gSystem->cd(curDir);
382 return kTRUE;
383}
384
385//
386// public methods
387//
388
389
390//______________________________________________________________________________
391Int_t AliMpManuStore::NofManus() const
392{
393/// Return total number of manus in the store
394
395 return fNofManus;
396}
397
398
399//______________________________________________________________________________
400Int_t AliMpManuStore::NofManus(Int_t detElemId) const
401{
402/// Return number of manus in given detection element
403
404 if ( ! AliMpDEManager::IsValidDetElemId(detElemId, kTRUE) ) return 0;
405
406 return fNofManusInDE.GetValue(detElemId);
407}
408
409//______________________________________________________________________________
410Bool_t AliMpManuStore::AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
411{
412/// Add manu to the map
413
168e9c4d 414 Long_t index = AliMp::Pair(detElemId, manuId);
ab167304 415
416 AliDebugStream(2)
417 << "Adding (" << detElemId << "," << manuId
418 << ") as index=" << index << " and serialNb=" << serialNb << endl;
419
420 fManuToSerialNbs.Add(index, Long_t(serialNb));
421
422 Long_t value = fSerialNbToManus.GetValue(Long_t(serialNb));
423 if ( value ) {
424 if ( fgWarnIfDoublon ) {
425 AliWarningStream()
426 << "Serial number " << serialNb
168e9c4d 427 << " already present for (detElemId, manuId) = " ;
428 AliMp::PairPut(AliWarningStream(), (MpPair_t) value) << endl;
ab167304 429 }
430 return kFALSE;
431 }
432 else {
433 fSerialNbToManus.Add(Long_t(serialNb), index);
434 return kTRUE;
435 }
436}
437
438
439//______________________________________________________________________________
440Int_t AliMpManuStore::GetManuSerial(Int_t detElemId, Int_t manuId) const
441{
442/// Return manu serial number for given detElemId and manuId
443
168e9c4d 444 Long_t index = AliMp::Pair(detElemId, manuId);
ab167304 445 // cout << index << " " << fManuToSerialNbs.GetValue(index) << endl;
446
447 return fManuToSerialNbs.GetValue(index);
448}
449
450//______________________________________________________________________________
168e9c4d 451MpPair_t AliMpManuStore::GetDetElemIdManu(Int_t manuSerial) const
ab167304 452{
453/// Return detElemId and manuId for given manu serial number
168e9c4d 454/// as encoded pair
ab167304 455
168e9c4d 456 return fSerialNbToManus.GetValue(Long_t(manuSerial));
ab167304 457}
458