1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
19 #include "AliMUONTrackerIO.h"
21 /// \class AliMUONTrackerIO
23 /// Reader class for ASCII calibration files for MUON tracker :
24 /// converts those ASCII files into AliMUONVStore (suitable to e.g. feed
27 /// \author Laurent Aphecetche, Subatech
30 ClassImp(AliMUONTrackerIO)
33 #include "AliDCSValue.h"
35 #include "AliMUONCalibParamND.h"
36 #include "AliMUONCalibParamNF.h"
37 #include "AliMUONVStore.h"
38 #include "AliMpConstants.h"
39 #include "AliMpDDLStore.h"
40 #include "AliMpDEManager.h"
41 #include "AliMpDetElement.h"
42 #include <Riostream.h>
44 #include <TObjString.h>
48 //_____________________________________________________________________________
49 AliMUONTrackerIO::AliMUONTrackerIO()
54 //_____________________________________________________________________________
55 AliMUONTrackerIO::~AliMUONTrackerIO()
60 //_____________________________________________________________________________
62 AliMUONTrackerIO::ReadOccupancy(const char* filename,AliMUONVStore& occupancyMap)
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
69 TString sFilename(gSystem->ExpandPathName(filename));
71 std::ifstream in(sFilename.Data());
74 return kCannotOpenFile;
80 while ( in.getline(line,1024) )
81 stream << line << "\n";
85 return DecodeOccupancy(stream.str().c_str(),occupancyMap);
89 //_____________________________________________________________________________
91 AliMUONTrackerIO::DecodeOccupancy(const char* data, AliMUONVStore& occupancyMap)
93 /// Decode occupancy string created append values to the occupancyMap store.
94 /// Expected format of the file is :
95 /// busPatchId manuId sumofn nevt
97 if ( ! AliMpDDLStore::Instance(kFALSE) )
99 AliErrorClass("Mapping not loaded. Cannot work");
104 istringstream in(data);
108 while ( in.getline(line,1024) )
110 AliDebugClass(3,Form("line=%s",line));
111 if ( line[0] == '/' && line[1] == '/' ) continue;
112 std::istringstream sin(line);
114 Int_t busPatchId, manuId;
115 Int_t numberOfEvents;
118 sin >> busPatchId >> manuId >> sumn >> numberOfEvents;
120 if ( busPatchId == -1 && manuId == -1 && sumn == 0 && numberOfEvents == 0 )
122 /// DA could not produce information (because run failed somehow).
123 /// That's OK, but let the world know about it
127 Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
129 AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
131 Int_t numberOfChannelsInManu = -1;
133 if (de) numberOfChannelsInManu = de->NofChannelsInManu(manuId);
135 if ( numberOfChannelsInManu <= 0 )
137 AliErrorClass(Form("BP %5d DE %5d MANU %5d nchannels=%d",busPatchId,detElemId,manuId,numberOfChannelsInManu));
141 AliMUONVCalibParam* occupancy =
142 static_cast<AliMUONVCalibParam*>(occupancyMap.FindObject(detElemId,manuId));
145 AliErrorClass(Form("DE %5d MANU %5d is already there ?!",detElemId,manuId));
149 occupancy = new AliMUONCalibParamND(5,1,detElemId,manuId,0);
151 occupancyMap.Add(occupancy);
153 occupancy->SetValueAsDouble(0,0,sumn);
154 occupancy->SetValueAsDouble(0,1,sumn); // with only 0 and 1s, sumw = sumw2 = sumn
155 occupancy->SetValueAsDouble(0,2,sumn);
156 occupancy->SetValueAsInt(0,3,numberOfChannelsInManu);
157 occupancy->SetValueAsInt(0,4,numberOfEvents);
164 //_____________________________________________________________________________
166 AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
168 /// Read pedestal file (produced by the MUONTRKda.exe program for instance)
169 /// and append the read values into the given VStore
170 /// To be used when the input is a file (for instance when reading data
173 TString sFilename(gSystem->ExpandPathName(filename));
175 std::ifstream in(sFilename.Data());
178 return kCannotOpenFile;
181 ostringstream stream;
183 while ( in.getline(line,1024) )
184 stream << line << "\n";
188 return DecodePedestals(stream.str().c_str(),pedStore);
192 //_____________________________________________________________________________
194 AliMUONTrackerIO::DecodePedestals(const char* data, AliMUONVStore& pedStore)
196 /// Read pedestal Data (produced by the MUONTRKda.exe program for instance)
197 /// and append the read values into the given VStore
198 /// To be used when the input is a TString (for instance when getting data
202 Int_t busPatchID, manuID, manuChannel;
203 Float_t pedMean, pedSigma;
205 istringstream in(data);
207 while ( in.getline(line,1024) )
209 AliDebugClass(3,Form("line=%s",line));
210 if ( line[0] == '/' && line[1] == '/' ) continue;
211 std::istringstream sin(line);
212 sin >> busPatchID >> manuID >> manuChannel >> pedMean >> pedSigma;
213 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
215 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
217 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
218 detElemID,busPatchID,manuID));
222 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d MEAN %7.2f SIGMA %7.2f",
223 busPatchID,detElemID,manuID,manuChannel,pedMean,pedSigma));
225 AliMUONVCalibParam* ped =
226 static_cast<AliMUONVCalibParam*>(pedStore.FindObject(detElemID,manuID));
229 ped = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),
231 AliMUONVCalibParam::InvalidFloatValue());
234 ped->SetValueAsFloat(manuChannel,0,pedMean);
235 ped->SetValueAsFloat(manuChannel,1,pedSigma);
242 //_____________________________________________________________________________
244 AliMUONTrackerIO::ReadGains(const char* filename, AliMUONVStore& gainStore,
247 /// Read gain file (produced by the MUONTRKda.exe program for instance)
248 /// and append the read values into the given VStore
249 /// To be used when the input is a file (for instance when reading data
254 TString sFilename(gSystem->ExpandPathName(filename));
256 std::ifstream in(sFilename.Data());
259 return kCannotOpenFile;
262 ostringstream stream;
264 while ( in.getline(line,1024) )
265 stream << line << "\n";
269 return DecodeGains(stream.str().c_str(),gainStore,comment);
273 //_____________________________________________________________________________
275 AliMUONTrackerIO::DecodeGains(const char* data, AliMUONVStore& gainStore,
278 /// Read gain file (produced by the MUONTRKda.exe program for instance)
279 /// and append the read values into the given VStore
280 /// To be used when the input is a string (for instance when getting data
284 istringstream in(data);
285 Int_t busPatchID, manuID, manuChannel;
289 const Int_t kSaturation(3000); // FIXME: how to get this number ?
295 Int_t nInit(0),f1nbp(0),f2nbp(0);
297 while ( in.getline(line,1024) )
299 if ( strlen(line) < 10 ) continue;
300 if ( line[0] == '/' && line[1] == '/' )
303 if ( sline.Contains("DUMMY") )
305 AliDebugClass(1,"Got a dummy file here");
310 if ( sline.Contains("* Run") )
312 TObjArray* a = sline.Tokenize(":");
313 if ( a->GetLast() >= 1 )
315 TString s = static_cast<TObjString*>(a->At(1))->String();
316 runNumber = s.Atoi();
317 AliDebugClass(1,Form("runNumber is %d",runNumber));
320 if ( sline.Contains("DAC values") )
322 nDAC = TString(sline(2,sline.Length()-2)).Atoi();
323 AliDebugClass(1,Form("# of DAC values = %d",nDAC));
330 runs = new Int_t[nDAC];
331 dac = new Int_t[nDAC];
333 in.getline(line,1024);
335 if (!sline.Contains("* nInit ="))
337 AliErrorClass("Improper format : was expecting nInit= line...");
341 sscanf(line,"// * nInit = %d * f1nbp = %d * f2nbp = %d",&nInit,&f1nbp,&f2nbp);
342 AliDebugClass(1,Form("nInit = %d",nInit));
343 AliDebugClass(1,Form("f1nbp = %d",f1nbp));
344 AliDebugClass(1,Form("f2nbp = %d",f2nbp));
346 in.getline(line,1024);
347 in.getline(line,1024);
348 // then get run and dac values
350 for ( Int_t i = 0; i < nDAC; ++i )
352 in.getline(line,1024);
354 sscanf(line,"// %d %d",&a,&b);
357 AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
363 AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
374 sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
375 &a0,&a1,&thres,&qual);
376 AliDebugClass(3,Form("line=%s",line));
377 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
379 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
381 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
382 detElemID,busPatchID,manuID));
386 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
387 "A1 %e THRES %5d QUAL %x",
388 busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
389 if ( qual == 0 ) continue;
391 AliMUONVCalibParam* gain =
392 static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
396 gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
399 gain->SetValueAsFloat(manuChannel,0,a0);
400 gain->SetValueAsFloat(manuChannel,1,a1);
401 gain->SetValueAsInt(manuChannel,2,thres);
402 gain->SetValueAsInt(manuChannel,3,qual);
403 gain->SetValueAsInt(manuChannel,4,kSaturation);
411 comment = Form("RUN %d",runNumber);
414 for ( Int_t i = 0; i < nDAC; ++i )
416 comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
418 comment += Form(";(nDAC = %d)",nDAC);
419 comment += Form(";(nInit = %d)",nInit);
420 comment += Form(";(f1nbp = %d)",f1nbp);
421 comment += Form(";(f2nbp = %d)",f2nbp);
429 //_____________________________________________________________________________
431 AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
433 /// Read capacitance file
434 /// and append the read values into the given VStore
436 TString sFilename(gSystem->ExpandPathName(filename));
438 std::ifstream in(sFilename.Data());
441 return kCannotOpenFile;
444 ostringstream stream;
446 while ( in.getline(line,1024) )
447 stream << line << "\n";
451 return DecodeCapacitances(stream.str().c_str(),capaStore);
454 //_____________________________________________________________________________
456 AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
458 /// Read capacitances and append the read values into the given VStore
459 /// To be used when the input is a string (for instance when getting data
465 Int_t serialNumber(-1);
466 AliMUONVCalibParam* param(0x0);
467 istringstream in(data);
469 while ( in.getline(line,1024,'\n') )
471 if ( isdigit(line[0]) )
473 serialNumber = atoi(line);
474 param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
477 AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
480 param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
481 Bool_t ok = capaStore.Add(param);
484 AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
490 if (!param) continue;
494 Float_t injectionGain;
495 sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
496 AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
497 serialNumber,channel,capaValue,injectionGain));
498 param->SetValueAsFloat(channel,0,capaValue);
499 param->SetValueAsFloat(channel,1,injectionGain);
506 //_____________________________________________________________________________
508 AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore)
510 /// Read config file (produced by the MUONTRKda.exe program for instance)
511 /// and append the read values into the given VStore
512 /// To be used when the input is a file (for instance when reading data
515 TString sFilename(gSystem->ExpandPathName(filename));
517 std::ifstream in(sFilename.Data());
520 return kCannotOpenFile;
523 ostringstream stream;
525 while ( in.getline(line,1024) )
526 stream << line << "\n";
530 return DecodeConfig(stream.str().c_str(),confStore);
533 //_____________________________________________________________________________
535 AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore)
537 /// Read config data (produced by the MUONTRKda.exe program for instance)
538 /// and append the read values into the given VStore
539 /// To be used when the input is a TString (for instance when getting data
543 Int_t busPatchID, manuID;
545 istringstream in(data);
547 while ( in.getline(line,1024) )
549 AliDebugClass(3,Form("line=%s",line));
550 if ( line[0] == '#' )
554 std::istringstream sin(line);
555 sin >> busPatchID >> manuID;
557 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
559 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
561 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
562 detElemID,busPatchID,manuID));
566 AliMUONVCalibParam* conf =
567 static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
570 conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
579 //_____________________________________________________________________________
581 AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
583 /// Write the conf store as an ASCII file
584 /// Note that we are converting (back) the detElemId into a busPatchId
585 /// Return the number of lines written
587 if ( !AliMpDDLStore::Instance() )
589 cout << "ERROR: mapping not loaded. Cannot work" << endl;
593 TIter next(confStore.CreateIterator());
594 AliMUONVCalibParam* param;
597 while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
599 Int_t detElemId = param->ID0();
600 Int_t manuId = param->ID1();
602 Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
605 out << busPatchId << " " << manuId << endl;