AliRoot Core  a565103 (a565103)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliMpManuStore.cxx
Go to the documentation of this file.
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 "AliMpConstants.h"
37 #include "AliMpEncodePair.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>
49 #include <cstdlib>
50 
54 
55 AliMpManuStore* AliMpManuStore::fgInstance = 0;
56 Bool_t AliMpManuStore::fgWarnIfDoublon = kFALSE;
57 
58 //
59 // static methods
60 //
61 
62 //______________________________________________________________________________
63 AliMpManuStore* AliMpManuStore::Instance(Bool_t warn)
64 {
67 
68  if ( ! fgInstance && warn ) {
69  AliWarningClass("Manu Store has not been loaded");
70  }
71 
72  return fgInstance;
73 }
74 
75 //______________________________________________________________________________
77  Bool_t warn)
78 {
81 
82  if ( fgInstance ) {
83  if ( warn )
84  AliWarningClass("Manu 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 //______________________________________________________________________________
102 : TObject(),
103  fManuToSerialNbs(),
104  fSerialNbToManus(),
105  fNofManusInDE(),
106  fNofManus(0)
107 {
109 
110  AliDebug(1,"");
111 
112  // Check if DE store is loaded
113  if ( ! AliMpDEStore::Instance(false) ) {
114  AliErrorStream()
115  << "Mapping segmentation has not be loaded. Cannont load Manu store"
116  << endl;
117  return;
118  }
119 
120  ReadManuSerial(dataStreams);
121 }
122 
123 //______________________________________________________________________________
124 AliMpManuStore::AliMpManuStore(TRootIOCtor* /*ioCtor*/)
125 : TObject(),
126  fManuToSerialNbs(),
127  fSerialNbToManus(),
128  fNofManusInDE(),
129  fNofManus(0)
130 {
132 
133  AliDebug(1,"");
134 }
135 
136 
137 //______________________________________________________________________________
139 {
141 
142  AliDebug(1,"");
143 
144  fgInstance = 0;
145 }
146 
147 //
148 // private methods
149 //
150 
151 //______________________________________________________________________________
152 Bool_t AliMpManuStore::ReadData(const AliMpDataStreams& dataStreams,
153  const AliMpDetElement* de, Int_t& nofManus)
154 {
156 
157  Int_t deId = de->GetId();
158  TString deName = de->GetDEName();
159  AliMp::StationType stationType
161  AliMq::Station12Type station12Type
163 
164  // Nothing to be done for trigger
165  if ( stationType == AliMp::kStationTrigger ) {
166  nofManus = 0;
167  return kTRUE;
168  }
169 
170  static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
171 
172  istream& in
173  = dataStreams.
174  CreateDataStream(
175  AliMpFiles::ManuToSerialPath(deName, stationType, station12Type));
176 
177  char line[80];
178 
179  nofManus = 0;
180  while ( in.getline(line,80) ) {
181 
182  if ( line[0] == '#' ) continue;
183 
184  TString tmp(AliMpHelper::Normalize(line));
185 
186  TObjArray* stringList = tmp.Tokenize(TString(" "));
187 
188  Int_t manuId = atoi( ((TObjString*)stringList->At(0))->GetName());
189  Int_t manuSerial = atoi( ((TObjString*)stringList->At(2))->GetName());
190 
191  TString sPlane = ((TObjString*)stringList->At(1))->GetString();
192 
193  // filling manuId <> manuSerial
194  if (!sPlane.CompareTo(PlaneTypeName(AliMp::kBendingPlane)))
195  AddManu(deId, manuId, manuSerial);
196  else
197  AddManu(deId, manuId + manuMask, manuSerial);
198 
199  ++nofManus;
200 
201  delete stringList;
202  }
203 
204  delete &in;
205 
206  return kTRUE;
207 }
208 
209 //______________________________________________________________________________
211 {
214 
215  Bool_t isOk = kTRUE;
216 
217  // Loop over DE
218  AliMpDEIterator it;
219  for ( it.First(); ! it.IsDone(); it.Next() ) {
220 
221  AliMpDetElement* detElement = it.CurrentDE();
222 
223  Int_t nofManus;
224  Bool_t result = ReadData(dataStreams, detElement, nofManus);
225  fNofManusInDE.Add(detElement->GetId(), nofManus);
226  fNofManus += nofManus;
227 
228  AliDebugStream(2)
229  << "Adding " << nofManus << " manus for de "
230  << detElement->GetId() << endl;
231 
232  isOk = isOk && result;
233  }
234 
235  return isOk;
236 }
237 
238 //______________________________________________________________________________
239 void AliMpManuStore::ReplaceManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
240 {
244 
245  Long_t index = AliMp::Pair(detElemId, manuId);
246 
247  TExMap newManuToSerialNbs;
248  // Loop over map
249  TExMapIter it(&fManuToSerialNbs);
250 
251 #if (defined(ROOT_VERSION_CODE) && ROOT_VERSION_CODE >= 334082) || \
252  (defined(ROOT_SVN_REVISION) && ROOT_SVN_REVISION >= 29598)
253  Long64_t key;
254  Long64_t value;
255 #else
256  Long_t key;
257  Long_t value;
258 #endif
259  while ( ( it.Next(key, value) ) ) {
260 
261  if ( key != index )
262  newManuToSerialNbs.Add(key, value);
263  else
264  newManuToSerialNbs.Add(index, Long_t(serialNb));
265  }
266 
267  TExMap newSerialNbToManus;
268  // Loop over map
269  TExMapIter it2(&fSerialNbToManus);
270  while ( ( it2.Next(key, value) ) ) {
271 
272  if ( value != index )
273  newSerialNbToManus.Add(key, value);
274  else
275  newSerialNbToManus.Add(Long_t(serialNb), index);
276  }
277 
278  // And now replace the maps
279  fManuToSerialNbs = newManuToSerialNbs;
280  fSerialNbToManus = newManuToSerialNbs;
281 }
282 
283 
284 //______________________________________________________________________________
285 Bool_t AliMpManuStore::WriteData(const TString& outDir)
286 {
290 
291  TString curDir = gSystem->pwd();
292 
293  // Create top directory
294  //
295  if ( gSystem->OpenDirectory(outDir.Data()) ) {
296  AliErrorStream()
297  << "Directory " << outDir.Data() << " already exists" << endl;
298  return kFALSE;
299  }
300  else {
301  AliDebugStream(2) << "Making directory " << outDir.Data() << endl;
302  gSystem->mkdir(outDir.Data());
303  }
304 
305  // Loop over DE
306  AliMpDEIterator it;
307  for ( it.First(); ! it.IsDone(); it.Next() ) {
308 
309  AliMpDetElement* detElement = it.CurrentDE();
310  Int_t detElemId = detElement->GetId();
311  TString deName = detElement->GetDEName();
312  AliMp::StationType stationType
313  = AliMpDEManager::GetStationType(detElemId);
314  AliMq::Station12Type station12Type
316 
317  if ( stationType == AliMp::kStationTrigger ) continue;
318 
319  // Create directory if it does not yet exist
320  //
321  TString dirPath = outDir + AliMpFiles::StationDataDir(stationType, station12Type);
322  if ( ! gSystem->OpenDirectory(dirPath.Data()) ) {
323  AliDebugStream(2) << "Making directory " << dirPath.Data() << endl;
324  gSystem->mkdir(dirPath.Data());
325  }
326 
327  // Compose output file path
328  //
329  string dataPath = AliMpFiles::ManuToSerialPath(deName, stationType, station12Type).Data();
330  string top = AliMpFiles::GetTop().Data();
331  if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
332  dataPath.erase(0,dataPath.find('/')+1);
333  TString dataPath1 = dataPath;
334  TString filePath = outDir + "/" + dataPath1;
335 
336  // Open file
337  //
338  ofstream out(filePath.Data());
339  if ( ! out.good() ) {
340  AliErrorStream()
341  << "Cannot open output file " << filePath.Data() << endl;
342  return kFALSE;
343  }
344 
345  // Loop over map
346  TExMapIter it2(&fManuToSerialNbs);
347 #if (defined(ROOT_SVN_REVISION) && ROOT_SVN_REVISION >= 29598) || \
348  (defined(ROOT_VERSION_CODE) && ROOT_VERSION_CODE >= ROOT_VERSION(5,25,02))
349  Long64_t key;
350  Long64_t value;
351 #else
352  Long_t key;
353  Long_t value;
354 #endif
355  while ( ( it2.Next(key, value) ) ) {
356  Int_t pairFirst = AliMp::PairFirst(key);
357 
358  if ( pairFirst != detElemId ) continue;
359 
360  Int_t manuId = AliMp::PairSecond(key);
361 
362  AliDebugStream(3)
363  << "Go to write " << key << " "
364  << pairFirst << " " << manuId << " " << value << endl;
365 
366  static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
367 
368  TString planeName = PlaneTypeName(AliMp::kBendingPlane);
369  if ( manuId> manuMask ) {
371  manuId -= manuMask;
372  }
373  out << manuId << " " << planeName.Data() << " " << value << endl;
374 
375  AliDebugStream(3)
376  << manuId << " " << planeName.Data() << " " << value << endl;
377  }
378  out.close();
379  }
380  gSystem->cd(curDir);
381  return kTRUE;
382 }
383 
384 //
385 // public methods
386 //
387 
388 
389 //______________________________________________________________________________
391 {
393 
394  return fNofManus;
395 }
396 
397 
398 //______________________________________________________________________________
399 Int_t AliMpManuStore::NofManus(Int_t detElemId) const
400 {
402 
403  if ( ! AliMpDEManager::IsValidDetElemId(detElemId, kTRUE) ) return 0;
404 
405  return fNofManusInDE.GetValue(detElemId);
406 }
407 
408 //______________________________________________________________________________
409 Bool_t AliMpManuStore::AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
410 {
412 
413  Long_t index = AliMp::Pair(detElemId, manuId);
414 
415  AliDebugStream(2)
416  << "Adding (" << detElemId << "," << manuId
417  << ") as index=" << index << " and serialNb=" << serialNb << endl;
418 
419  fManuToSerialNbs.Add(index, Long_t(serialNb));
420 
421  Long_t value = fSerialNbToManus.GetValue(Long_t(serialNb));
422  if ( value ) {
423  if ( fgWarnIfDoublon ) {
424  AliWarningStream()
425  << "Serial number " << serialNb
426  << " already present for (detElemId, manuId) = " ;
427  AliMp::PairPut(AliWarningStream(), (MpPair_t) value) << endl;
428  }
429  return kFALSE;
430  }
431  else {
432  fSerialNbToManus.Add(Long_t(serialNb), index);
433  return kTRUE;
434  }
435 }
436 
437 
438 //______________________________________________________________________________
439 Int_t AliMpManuStore::GetManuSerial(Int_t detElemId, Int_t manuId) const
440 {
442 
443  Long_t index = AliMp::Pair(detElemId, manuId);
444  // cout << index << " " << fManuToSerialNbs.GetValue(index) << endl;
445 
446  return fManuToSerialNbs.GetValue(index);
447 }
448 
449 //______________________________________________________________________________
451 {
454 
455  return fSerialNbToManus.GetValue(Long_t(manuSerial));
456 }
457 
static AliMq::Station12Type GetStation12Type(Int_t detElemId)
static AliMpManuStore * ReadData(const AliMpDataStreams &dataStreams, Bool_t warn=true)
The iterator over detection elements.
static AliMpDEStore * Instance(Bool_t warn=true)
AliMpManuStore()
Not implemented.
#define TObjArray
static TString Normalize(const char *line)
Bool_t IsDone() const
TString GetDEName() const
Return name.
TExMap fSerialNbToManus
Map manu serial # to manuId.
static TString ManuToSerialPath(const TString &deName, AliMp::StationType station, AliMq::Station12Type station12Type)
Definition: AliMpFiles.cxx:483
MpPair_t Pair(Int_t first, Int_t second)
Encode the pair of integers to another integer.
TExMap fNofManusInDE
Number of manus with serial nbs in DE.
AliMpDetElement * CurrentDE() const
Bool_t WriteData(const TString &outDir="data_run_out")
The class defines the electronics properties of detection element.
Int_t fNofManus
Total number of manus.
static TString StationDataDir(AliMp::StationType station, AliMq::Station12Type station12Type)
Definition: AliMpFiles.cxx:255
MpPair_t GetDetElemIdManu(Int_t manuSerial) const
ClassImp(TPCGenInfo)
Definition: AliTPCCmpNG.C:254
Int_t GetManuSerial(Int_t detElemId, Int_t manuId) const
static Bool_t fgWarnIfDoublon
Option to warn about doublons.
The container class for manu serial numbers.
static TString GetTop()
Definition: AliMpFiles.cxx:504
non-bending plane
TString PlaneTypeName(AliMp::PlaneType planeType)
Return name for given planeType.
static AliMp::StationType GetStationType(Int_t detElemId)
Bool_t ReadManuSerial(const AliMpDataStreams &dataStreams)
Bool_t AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
void ReplaceManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
virtual ~AliMpManuStore()
bending plane
Int_t MpPair_t
static AliMpManuStore * fgInstance
Singleton instance.
trigger stations (slats)
Int_t NofManus() const
Int_t PairFirst(MpPair_t pair)
Decode the first integer from encoded pair.
Int_t PairSecond(MpPair_t pair)
Decode the second integer from encoded pair.
ostream & PairPut(ostream &s, MpPair_t pair)
A special printing for encoded pair.
Int_t GetId() const
Return Id.
static Bool_t IsValidDetElemId(Int_t detElemId, Bool_t warn=false)
Bool_t GetReadFromFiles() const
TExMap fManuToSerialNbs
Map from manuId to serial #.
static Int_t ManuMask(AliMp::PlaneType planeType)
Mapping data streams provider.