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