]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONTrackerIO.cxx
updated vertex selection
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerIO.cxx
CommitLineData
81028269 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
b09247a2 18#include <cstdlib>
81028269 19#include "AliMUONTrackerIO.h"
20
21/// \class AliMUONTrackerIO
22///
23/// Reader class for ASCII calibration files for MUON tracker :
24/// converts those ASCII files into AliMUONVStore (suitable to e.g. feed
25/// the OCDB).
26///
27/// \author Laurent Aphecetche, Subatech
28
29/// \cond CLASSIMP
30ClassImp(AliMUONTrackerIO)
31/// \endcond
32
6c870207 33#include "AliDCSValue.h"
81028269 34#include "AliLog.h"
7eafe398 35#include "AliMUONCalibParamND.h"
81028269 36#include "AliMUONCalibParamNF.h"
37#include "AliMUONVStore.h"
38#include "AliMpConstants.h"
39#include "AliMpDDLStore.h"
6c870207 40#include "AliMpDEManager.h"
7eafe398 41#include "AliMpDetElement.h"
81028269 42#include <Riostream.h>
43#include <TClass.h>
44#include <TObjString.h>
45#include <TSystem.h>
46#include <sstream>
47
48//_____________________________________________________________________________
49AliMUONTrackerIO::AliMUONTrackerIO()
50{
51 /// ctor
52}
53
54//_____________________________________________________________________________
55AliMUONTrackerIO::~AliMUONTrackerIO()
56{
57 /// dtor
58}
59
7eafe398 60//_____________________________________________________________________________
61Int_t
62AliMUONTrackerIO::ReadOccupancy(const char* filename,AliMUONVStore& occupancyMap)
63{
64 /// Read occupancy file created by online DA
65 /// and append values to the occupancyMap store.
66 /// Expected format of the file is :
67 /// busPatchId manuId sumofn nevt
68
69 TString sFilename(gSystem->ExpandPathName(filename));
70
71 std::ifstream in(sFilename.Data());
72 if (!in.good())
73 {
74 return kCannotOpenFile;
75 }
76
77 TString datastring;
78 ostringstream stream;
79 char line[1024];
80 while ( in.getline(line,1024) )
81 stream << line << "\n";
7eafe398 82
83 in.close();
84
ca913045 85 return DecodeOccupancy(stream.str().c_str(),occupancyMap);
7eafe398 86
87}
88
89//_____________________________________________________________________________
90Int_t
ca913045 91AliMUONTrackerIO::DecodeOccupancy(const char* data, AliMUONVStore& occupancyMap)
7eafe398 92{
93 /// Decode occupancy string created append values to the occupancyMap store.
94 /// Expected format of the file is :
95 /// busPatchId manuId sumofn nevt
96
97 if ( ! AliMpDDLStore::Instance(kFALSE) )
98 {
99 AliErrorClass("Mapping not loaded. Cannot work");
100 return 0;
101 }
102
103 char line[1024];
ca913045 104 istringstream in(data);
7eafe398 105
106 Int_t n(0);
107
108 while ( in.getline(line,1024) )
109 {
110 AliDebugClass(3,Form("line=%s",line));
111 if ( line[0] == '/' && line[1] == '/' ) continue;
112 std::istringstream sin(line);
113
114 Int_t busPatchId, manuId;
115 Int_t numberOfEvents;
116 Double_t sumn;
117
118 sin >> busPatchId >> manuId >> sumn >> numberOfEvents;
119
120 Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
121
122 AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
123
124 Int_t numberOfChannelsInManu = -1;
125
126 if (de) numberOfChannelsInManu = de->NofChannelsInManu(manuId);
127
128 if ( numberOfChannelsInManu <= 0 )
129 {
130 AliErrorClass(Form("BP %5d DE %5d MANU %5d nchannels=%d",busPatchId,detElemId,manuId,numberOfChannelsInManu));
131 continue;
132 }
133
134 AliMUONVCalibParam* occupancy =
135 static_cast<AliMUONVCalibParam*>(occupancyMap.FindObject(detElemId,manuId));
136 if (occupancy)
137 {
138 AliErrorClass(Form("DE %5d MANU %5d is already there ?!",detElemId,manuId));
139 continue;
140 }
141
142 occupancy = new AliMUONCalibParamND(5,1,detElemId,manuId,0);
143
144 occupancyMap.Add(occupancy);
145
146 occupancy->SetValueAsDouble(0,0,sumn);
147 occupancy->SetValueAsDouble(0,1,sumn); // with only 0 and 1s, sumw = sumw2 = sumn
148 occupancy->SetValueAsDouble(0,2,sumn);
149 occupancy->SetValueAsInt(0,3,numberOfChannelsInManu);
150 occupancy->SetValueAsInt(0,4,numberOfEvents);
151 ++n;
152 }
153
154 return n;
155}
156
81028269 157//_____________________________________________________________________________
158Int_t
159AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
160{
161 /// Read pedestal file (produced by the MUONTRKda.exe program for instance)
162 /// and append the read values into the given VStore
cba13f7c 163 /// To be used when the input is a file (for instance when reading data
164 /// from the OCDB).
81028269 165
166 TString sFilename(gSystem->ExpandPathName(filename));
167
168 std::ifstream in(sFilename.Data());
169 if (!in.good())
170 {
171 return kCannotOpenFile;
172 }
173
06a51226 174 ostringstream stream;
175 char line[1024];
176 while ( in.getline(line,1024) )
177 stream << line << "\n";
06a51226 178
cba13f7c 179 in.close();
180
ca913045 181 return DecodePedestals(stream.str().c_str(),pedStore);
cba13f7c 182
183}
184
185//_____________________________________________________________________________
186Int_t
ca913045 187AliMUONTrackerIO::DecodePedestals(const char* data, AliMUONVStore& pedStore)
cba13f7c 188{
189 /// Read pedestal Data (produced by the MUONTRKda.exe program for instance)
190 /// and append the read values into the given VStore
191 /// To be used when the input is a TString (for instance when getting data
192 /// from AMORE DB).
193
81028269 194 char line[1024];
195 Int_t busPatchID, manuID, manuChannel;
196 Float_t pedMean, pedSigma;
197 Int_t n(0);
ca913045 198 istringstream in(data);
81028269 199
200 while ( in.getline(line,1024) )
201 {
202 AliDebugClass(3,Form("line=%s",line));
203 if ( line[0] == '/' && line[1] == '/' ) continue;
204 std::istringstream sin(line);
205 sin >> busPatchID >> manuID >> manuChannel >> pedMean >> pedSigma;
206 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
6c870207 207
208 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
209 {
210 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
211 detElemID,busPatchID,manuID));
212 continue;
213 }
214
81028269 215 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d MEAN %7.2f SIGMA %7.2f",
216 busPatchID,detElemID,manuID,manuChannel,pedMean,pedSigma));
cba13f7c 217
81028269 218 AliMUONVCalibParam* ped =
219 static_cast<AliMUONVCalibParam*>(pedStore.FindObject(detElemID,manuID));
81028269 220 if (!ped)
221 {
222 ped = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),
223 detElemID,manuID,
224 AliMUONVCalibParam::InvalidFloatValue());
225 pedStore.Add(ped);
226 }
227 ped->SetValueAsFloat(manuChannel,0,pedMean);
228 ped->SetValueAsFloat(manuChannel,1,pedSigma);
229 ++n;
230 }
81028269 231
232 return n;
233}
234
235//_____________________________________________________________________________
236Int_t
237AliMUONTrackerIO::ReadGains(const char* filename, AliMUONVStore& gainStore,
238 TString& comment)
239{
240 /// Read gain file (produced by the MUONTRKda.exe program for instance)
241 /// and append the read values into the given VStore
cba13f7c 242 /// To be used when the input is a file (for instance when reading data
243 /// from the OCDB).
81028269 244
245 comment = "";
246
247 TString sFilename(gSystem->ExpandPathName(filename));
248
249 std::ifstream in(sFilename.Data());
250 if (!in.good())
251 {
252 return kCannotOpenFile;
253 }
254
cba13f7c 255 ostringstream stream;
256 char line[1024];
257 while ( in.getline(line,1024) )
258 stream << line << "\n";
cba13f7c 259
260 in.close();
261
ca913045 262 return DecodeGains(stream.str().c_str(),gainStore,comment);
cba13f7c 263
264}
265
266//_____________________________________________________________________________
267Int_t
ca913045 268AliMUONTrackerIO::DecodeGains(const char* data, AliMUONVStore& gainStore,
269 TString& comment)
cba13f7c 270{
271 /// Read gain file (produced by the MUONTRKda.exe program for instance)
272 /// and append the read values into the given VStore
ca913045 273 /// To be used when the input is a string (for instance when getting data
cba13f7c 274 /// from AMORE DB).
275
81028269 276 char line[1024];
ca913045 277 istringstream in(data);
81028269 278 Int_t busPatchID, manuID, manuChannel;
279 Float_t a0, a1;
280 Int_t thres;
281 UInt_t qual;
282 const Int_t kSaturation(3000); // FIXME: how to get this number ?
283 Int_t n(0);
284 Int_t runNumber(-1);
285 Int_t* runs(0x0);
286 Int_t* dac(0x0);
287 Int_t nDAC(0);
1ccd531d 288 Int_t nInit(0),f1nbp(0),f2nbp(0);
81028269 289
290 while ( in.getline(line,1024) )
291 {
292 if ( strlen(line) < 10 ) continue;
293 if ( line[0] == '/' && line[1] == '/' )
294 {
295 TString sline(line);
296 if ( sline.Contains("DUMMY") )
297 {
298 AliDebugClass(1,"Got a dummy file here");
299 return kDummyFile;
300 }
301 if ( sline.Contains("* Run") )
302 {
303 TObjArray* a = sline.Tokenize(":");
304 if ( a->GetLast() >= 1 )
305 {
306 TString s = static_cast<TObjString*>(a->At(1))->String();
307 runNumber = s.Atoi();
308 AliDebugClass(1,Form("runNumber is %d",runNumber));
309 }
310 }
1ccd531d 311 if ( sline.Contains("* nInit =") )
312 {
313 sscanf(line,"// * nInit = %d * f1nbp = %d * f2nbp = %d",&nInit,&f1nbp,&f2nbp);
314 AliDebugClass(1,Form("nInit = %d",nInit));
315 AliDebugClass(1,Form("f1nbp = %d",f1nbp));
316 AliDebugClass(1,Form("f2nbp = %d",f2nbp));
317 }
81028269 318 if ( sline.Contains("DAC values") )
319 {
320 nDAC = TString(sline(2,sline.Length()-2)).Atoi();
321 AliDebugClass(1,Form("# of DAC values = %d",nDAC));
322 if ( nDAC > 0 )
323 {
324 if ( nDAC < 100 )
325 {
326 runs = new Int_t[nDAC];
327 dac = new Int_t[nDAC];
328 // skip two lines
329 in.getline(line,1024);
330 in.getline(line,1024);
331 // then get run and dac values
ca913045 332 Int_t iDAC(0);
81028269 333 for ( Int_t i = 0; i < nDAC; ++i )
334 {
335 in.getline(line,1024);
336 Int_t a,b;
337 sscanf(line,"// %d %d",&a,&b);
338 runs[iDAC] = a;
339 dac[iDAC] = b;
340 AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
341 ++iDAC;
342 }
343 }
344 else
345 {
346 AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
347 nDAC = 0;
348 return kFormatError;
349 }
350 }
351 }
352 continue;
353 }
354
355 sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
356 &a0,&a1,&thres,&qual);
357 AliDebugClass(3,Form("line=%s",line));
358 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
6c870207 359
360 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
361 {
362 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
363 detElemID,busPatchID,manuID));
364 continue;
365 }
366
81028269 367 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
368 "A1 %e THRES %5d QUAL %x",
369 busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
370 if ( qual == 0 ) continue;
371
372 AliMUONVCalibParam* gain =
373 static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
374
cba13f7c 375 if (!gain)
81028269 376 {
377 gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
378 gainStore.Add(gain);
379 }
380 gain->SetValueAsFloat(manuChannel,0,a0);
381 gain->SetValueAsFloat(manuChannel,1,a1);
382 gain->SetValueAsInt(manuChannel,2,thres);
383 gain->SetValueAsInt(manuChannel,3,qual);
384 gain->SetValueAsInt(manuChannel,4,kSaturation);
385 ++n;
386 }
cba13f7c 387
81028269 388 comment = "";
389
390 if ( runNumber > 0 )
391 {
392 comment = Form("RUN %d",runNumber);
393 }
394
395 for ( Int_t i = 0; i < nDAC; ++i )
396 {
397 comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
398 }
1ccd531d 399 comment += Form(";(nDAC = %d)",nDAC);
400 comment += Form(";(nInit = %d)",nInit);
401 comment += Form(";(f1nbp = %d)",f1nbp);
402 comment += Form(";(f2nbp = %d)",f2nbp);
81028269 403
404 delete[] runs;
405 delete[] dac;
406
407 return n;
408}
409
410//_____________________________________________________________________________
411Int_t
ca913045 412AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
81028269 413{
414 /// Read capacitance file
415 /// and append the read values into the given VStore
416
ca913045 417 TString sFilename(gSystem->ExpandPathName(filename));
418
419 std::ifstream in(sFilename.Data());
420 if (!in.good())
421 {
422 return kCannotOpenFile;
423 }
424
425 ostringstream stream;
426 char line[1024];
427 while ( in.getline(line,1024) )
428 stream << line << "\n";
429
430 in.close();
431
432 return DecodeCapacitances(stream.str().c_str(),capaStore);
433}
434
435//_____________________________________________________________________________
436Int_t
437AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
438{
439 /// Read capacitances and append the read values into the given VStore
440 /// To be used when the input is a string (for instance when getting data
441 /// from AMORE DB).
81028269 442
443 Int_t ngenerated(0);
444
445 char line[1024];
446 Int_t serialNumber(-1);
447 AliMUONVCalibParam* param(0x0);
ca913045 448 istringstream in(data);
449
81028269 450 while ( in.getline(line,1024,'\n') )
451 {
452 if ( isdigit(line[0]) )
453 {
454 serialNumber = atoi(line);
455 param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
456 if (param)
457 {
458 AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
630711ed 459 continue;
81028269 460 }
461 param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
462 Bool_t ok = capaStore.Add(param);
463 if (!ok)
464 {
465 AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
466 continue;
467 }
468 continue;
469 }
470 Int_t channel;
471 Float_t capaValue;
472 Float_t injectionGain;
473 sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
474 AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
475 serialNumber,channel,capaValue,injectionGain));
476 param->SetValueAsFloat(channel,0,capaValue);
477 param->SetValueAsFloat(channel,1,injectionGain);
478 ++ngenerated;
479 }
480
81028269 481 return ngenerated;
482}
6c870207 483
484//_____________________________________________________________________________
485Int_t
486AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore, Bool_t& changed)
487{
488 /// Read config file (produced by the MUONTRKda.exe program for instance)
489 /// and append the read values into the given VStore
490 /// To be used when the input is a file (for instance when reading data
491 /// from the OCDB).
492 /// changed must be set to kFALSE before calling this method for the first time
493 /// (then the subsequent calls must not set it !)
494 ///
495
496 TString sFilename(gSystem->ExpandPathName(filename));
497
498 std::ifstream in(sFilename.Data());
499 if (!in.good())
500 {
501 return kCannotOpenFile;
502 }
503
6c870207 504 ostringstream stream;
505 char line[1024];
506 while ( in.getline(line,1024) )
507 stream << line << "\n";
6c870207 508
509 in.close();
510
ca913045 511 return DecodeConfig(stream.str().c_str(),confStore,changed);
6c870207 512}
513
514//_____________________________________________________________________________
515Int_t
ca913045 516AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore, Bool_t& changed)
6c870207 517{
518 /// Read config data (produced by the MUONTRKda.exe program for instance)
519 /// and append the read values into the given VStore
520 /// To be used when the input is a TString (for instance when getting data
521 /// from AMORE DB).
522 /// changed must be set to kFALSE before calling this method for the first time
523 /// (then the subsequent calls must not set it !)
524
525 char line[1024];
526 Int_t busPatchID, manuID;
527 Int_t n(0);
ca913045 528 istringstream in(data);
6c870207 529
530 while ( in.getline(line,1024) )
531 {
532 AliDebugClass(3,Form("line=%s",line));
533 if ( line[0] == '#' )
534 {
535 TString sline(line);
536 sline.ToUpper();
537 if (sline.Contains("CHANGED") && !sline.Contains("UNCHANGED")) changed = kTRUE;
538 continue;
539 }
540 std::istringstream sin(line);
541 sin >> busPatchID >> manuID;
542
543 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
544
545 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
546 {
547 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
548 detElemID,busPatchID,manuID));
549 continue;
550 }
551
552 AliMUONVCalibParam* conf =
553 static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
554 if (!conf)
555 {
556 conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
557 confStore.Add(conf);
558 }
559 ++n;
560 }
561
562 return n;
563}
564
565//_____________________________________________________________________________
566Int_t
ca913045 567AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
6c870207 568{
569 /// Write the conf store as an ASCII file
570 /// Note that we are converting (back) the detElemId into a busPatchId
571 /// Return the number of lines written
572
573 if ( !AliMpDDLStore::Instance() )
574 {
575 cout << "ERROR: mapping not loaded. Cannot work" << endl;
576 return 0;
577 }
578
579 TIter next(confStore.CreateIterator());
580 AliMUONVCalibParam* param;
581 Int_t n(0);
582
583 while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
584 {
585 Int_t detElemId = param->ID0();
586 Int_t manuId = param->ID1();
587
588 Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
589 ++n;
590
591 out << busPatchId << " " << manuId << endl;
592 }
593 return n;
594}
595