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 **************************************************************************/
20 #include "AliMUONTrackerIO.h"
22 /// \class AliMUONTrackerIO
24 /// Reader class for ASCII calibration files for MUON tracker :
25 /// converts those ASCII files into AliMUONVStore (suitable to e.g. feed
28 /// \author Laurent Aphecetche, Subatech
30 using std::ostringstream;
31 using std::istringstream;
35 ClassImp(AliMUONTrackerIO)
38 #include "AliDCSValue.h"
40 #include "AliMUONCalibParamND.h"
41 #include "AliMUONCalibParamNF.h"
42 #include "AliMUONVStore.h"
43 #include "AliMpConstants.h"
44 #include "AliMpDDLStore.h"
45 #include "AliMpDEManager.h"
46 #include "AliMpDetElement.h"
47 #include <Riostream.h>
49 #include <TObjString.h>
53 //_____________________________________________________________________________
54 AliMUONTrackerIO::AliMUONTrackerIO()
59 //_____________________________________________________________________________
60 AliMUONTrackerIO::~AliMUONTrackerIO()
65 //_____________________________________________________________________________
67 AliMUONTrackerIO::ReadOccupancy(const char* filename,AliMUONVStore& occupancyMap)
69 /// Read occupancy file created by online DA
70 /// and append values to the occupancyMap store.
71 /// Expected format of the file is :
72 /// busPatchId manuId sumofn nevt
74 TString sFilename(gSystem->ExpandPathName(filename));
76 std::ifstream in(sFilename.Data());
79 return kCannotOpenFile;
85 while ( in.getline(line,1024) )
86 stream << line << "\n";
90 return DecodeOccupancy(stream.str().c_str(),occupancyMap);
94 //_____________________________________________________________________________
96 AliMUONTrackerIO::DecodeOccupancy(const char* data, AliMUONVStore& occupancyMap)
98 /// Decode occupancy string created append values to the occupancyMap store.
99 /// Expected format of the file is :
100 /// busPatchId manuId sumofn nevt
102 if ( ! AliMpDDLStore::Instance(kFALSE) )
104 AliErrorClass("Mapping not loaded. Cannot work");
109 istringstream in(data);
113 while ( in.getline(line,1024) )
115 AliDebugClass(3,Form("line=%s",line));
116 if ( line[0] == '/' && line[1] == '/' ) continue;
117 std::istringstream sin(line);
119 Int_t busPatchId, manuId;
120 Int_t numberOfEvents;
123 sin >> busPatchId >> manuId >> sumn >> numberOfEvents;
125 if ( busPatchId == -1 && manuId == -1 && sumn == 0 && numberOfEvents == 0 )
127 /// DA could not produce information (because run failed somehow).
128 /// That's OK, but let the world know about it
132 Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
134 AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
136 Int_t numberOfChannelsInManu = -1;
138 if (de) numberOfChannelsInManu = de->NofChannelsInManu(manuId);
140 if ( numberOfChannelsInManu <= 0 )
142 AliErrorClass(Form("BP %5d DE %5d MANU %5d nchannels=%d",busPatchId,detElemId,manuId,numberOfChannelsInManu));
146 AliMUONVCalibParam* occupancy =
147 static_cast<AliMUONVCalibParam*>(occupancyMap.FindObject(detElemId,manuId));
150 AliErrorClass(Form("DE %5d MANU %5d is already there ?!",detElemId,manuId));
154 occupancy = new AliMUONCalibParamND(5,1,detElemId,manuId,0);
156 occupancyMap.Add(occupancy);
158 occupancy->SetValueAsDouble(0,0,sumn);
159 occupancy->SetValueAsDouble(0,1,sumn); // with only 0 and 1s, sumw = sumw2 = sumn
160 occupancy->SetValueAsDouble(0,2,sumn);
161 occupancy->SetValueAsInt(0,3,numberOfChannelsInManu);
162 occupancy->SetValueAsInt(0,4,numberOfEvents);
169 //_____________________________________________________________________________
171 AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
173 /// Read pedestal file (produced by the MUONTRKda.exe program for instance)
174 /// and append the read values into the given VStore
175 /// To be used when the input is a file (for instance when reading data
178 TString sFilename(gSystem->ExpandPathName(filename));
180 std::ifstream in(sFilename.Data());
183 return kCannotOpenFile;
186 ostringstream stream;
188 while ( in.getline(line,1024) )
189 stream << line << "\n";
193 return DecodePedestals(stream.str().c_str(),pedStore);
197 //_____________________________________________________________________________
199 AliMUONTrackerIO::DecodePedestals(const char* data, AliMUONVStore& pedStore)
201 /// Read pedestal Data (produced by the MUONTRKda.exe program for instance)
202 /// and append the read values into the given VStore
203 /// To be used when the input is a TString (for instance when getting data
207 Int_t busPatchID, manuID, manuChannel;
208 Float_t pedMean, pedSigma;
210 istringstream in(data);
212 while ( in.getline(line,1024) )
214 AliDebugClass(3,Form("line=%s",line));
215 if ( line[0] == '/' && line[1] == '/' ) continue;
216 std::istringstream sin(line);
217 sin >> busPatchID >> manuID >> manuChannel >> pedMean >> pedSigma;
218 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
220 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
222 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
223 detElemID,busPatchID,manuID));
227 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d MEAN %7.2f SIGMA %7.2f",
228 busPatchID,detElemID,manuID,manuChannel,pedMean,pedSigma));
230 AliMUONVCalibParam* ped =
231 static_cast<AliMUONVCalibParam*>(pedStore.FindObject(detElemID,manuID));
234 ped = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),
236 AliMUONVCalibParam::InvalidFloatValue());
239 ped->SetValueAsFloat(manuChannel,0,pedMean);
240 ped->SetValueAsFloat(manuChannel,1,pedSigma);
247 //_____________________________________________________________________________
249 AliMUONTrackerIO::ReadGains(const char* filename, AliMUONVStore& gainStore,
252 /// Read gain file (produced by the MUONTRKda.exe program for instance)
253 /// and append the read values into the given VStore
254 /// To be used when the input is a file (for instance when reading data
259 TString sFilename(gSystem->ExpandPathName(filename));
261 std::ifstream in(sFilename.Data());
264 return kCannotOpenFile;
267 ostringstream stream;
269 while ( in.getline(line,1024) )
270 stream << line << "\n";
274 return DecodeGains(stream.str().c_str(),gainStore,comment);
278 //_____________________________________________________________________________
280 AliMUONTrackerIO::DecodeGains(const char* data, AliMUONVStore& gainStore,
283 /// Read gain file (produced by the MUONTRKda.exe program for instance)
284 /// and append the read values into the given VStore
285 /// To be used when the input is a string (for instance when getting data
289 istringstream in(data);
290 Int_t busPatchID, manuID, manuChannel;
294 const Int_t kSaturation(3000); // FIXME: how to get this number ?
300 Int_t nInit(0),f1nbp(0),f2nbp(0);
302 while ( in.getline(line,1024) )
304 if ( strlen(line) < 10 ) continue;
305 if ( line[0] == '/' && line[1] == '/' )
308 if ( sline.Contains("DUMMY") )
310 AliDebugClass(1,"Got a dummy file here");
315 if ( sline.Contains("* Run") )
317 TObjArray* a = sline.Tokenize(":");
318 if ( a->GetLast() >= 1 )
320 TString s = static_cast<TObjString*>(a->At(1))->String();
321 runNumber = s.Atoi();
322 AliDebugClass(1,Form("runNumber is %d",runNumber));
325 if ( sline.Contains("DAC values") )
327 nDAC = TString(sline(2,sline.Length()-2)).Atoi();
328 AliDebugClass(1,Form("# of DAC values = %d",nDAC));
335 runs = new Int_t[nDAC];
336 dac = new Int_t[nDAC];
338 in.getline(line,1024);
340 if (!sline.Contains("* nInit ="))
342 AliErrorClass("Improper format : was expecting nInit= line...");
346 sscanf(line,"// * nInit = %d * f1nbp = %d * f2nbp = %d",&nInit,&f1nbp,&f2nbp);
347 AliDebugClass(1,Form("nInit = %d",nInit));
348 AliDebugClass(1,Form("f1nbp = %d",f1nbp));
349 AliDebugClass(1,Form("f2nbp = %d",f2nbp));
351 in.getline(line,1024);
352 in.getline(line,1024);
353 // then get run and dac values
355 for ( Int_t i = 0; i < nDAC; ++i )
357 in.getline(line,1024);
359 sscanf(line,"// %d %d",&a,&b);
362 AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
368 AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
379 sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
380 &a0,&a1,&thres,&qual);
381 AliDebugClass(3,Form("line=%s",line));
382 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
384 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
386 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
387 detElemID,busPatchID,manuID));
391 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
392 "A1 %e THRES %5d QUAL %x",
393 busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
394 if ( qual == 0 ) continue;
396 AliMUONVCalibParam* gain =
397 static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
401 gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
404 gain->SetValueAsFloat(manuChannel,0,a0);
405 gain->SetValueAsFloat(manuChannel,1,a1);
406 gain->SetValueAsInt(manuChannel,2,thres);
407 gain->SetValueAsInt(manuChannel,3,qual);
408 gain->SetValueAsInt(manuChannel,4,kSaturation);
416 comment = Form("RUN %d",runNumber);
419 for ( Int_t i = 0; i < nDAC; ++i )
421 comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
423 comment += Form(";(nDAC = %d)",nDAC);
424 comment += Form(";(nInit = %d)",nInit);
425 comment += Form(";(f1nbp = %d)",f1nbp);
426 comment += Form(";(f2nbp = %d)",f2nbp);
434 //_____________________________________________________________________________
436 AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
438 /// Read capacitance file
439 /// and append the read values into the given VStore
441 TString sFilename(gSystem->ExpandPathName(filename));
443 std::ifstream in(sFilename.Data());
446 return kCannotOpenFile;
449 ostringstream stream;
451 while ( in.getline(line,1024) )
452 stream << line << "\n";
456 return DecodeCapacitances(stream.str().c_str(),capaStore);
459 //_____________________________________________________________________________
461 AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
463 /// Read capacitances and append the read values into the given VStore
464 /// To be used when the input is a string (for instance when getting data
470 Int_t serialNumber(-1);
471 AliMUONVCalibParam* param(0x0);
472 istringstream in(data);
474 while ( in.getline(line,1024,'\n') )
476 if ( isdigit(line[0]) )
478 serialNumber = atoi(line);
479 param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
482 AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
485 param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
486 Bool_t ok = capaStore.Add(param);
489 AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
495 if (!param) continue;
499 Float_t injectionGain;
500 sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
501 AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
502 serialNumber,channel,capaValue,injectionGain));
503 param->SetValueAsFloat(channel,0,capaValue);
504 param->SetValueAsFloat(channel,1,injectionGain);
511 //_____________________________________________________________________________
513 AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore)
515 /// Read config file (produced by the MUONTRKda.exe program for instance)
516 /// and append the read values into the given VStore
517 /// To be used when the input is a file (for instance when reading data
520 TString sFilename(gSystem->ExpandPathName(filename));
522 std::ifstream in(sFilename.Data());
525 return kCannotOpenFile;
528 ostringstream stream;
530 while ( in.getline(line,1024) )
531 stream << line << "\n";
535 return DecodeConfig(stream.str().c_str(),confStore);
538 //_____________________________________________________________________________
540 AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore)
542 /// Read config data (produced by the MUONTRKda.exe program for instance)
543 /// and append the read values into the given VStore
544 /// To be used when the input is a TString (for instance when getting data
548 Int_t busPatchID, manuID;
550 istringstream in(data);
552 while ( in.getline(line,1024) )
554 AliDebugClass(3,Form("line=%s",line));
555 if ( line[0] == '#' )
559 std::istringstream sin(line);
560 sin >> busPatchID >> manuID;
562 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
564 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
566 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
567 detElemID,busPatchID,manuID));
571 AliMUONVCalibParam* conf =
572 static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
575 conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
584 //_____________________________________________________________________________
586 AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
588 /// Write the conf store as an ASCII file
589 /// Note that we are converting (back) the detElemId into a busPatchId
590 /// Return the number of lines written
592 if ( !AliMpDDLStore::Instance() )
594 cout << "ERROR: mapping not loaded. Cannot work" << endl;
598 TIter next(confStore.CreateIterator());
599 AliMUONVCalibParam* param;
602 while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
604 Int_t detElemId = param->ID0();
605 Int_t manuId = param->ID1();
607 Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
610 out << busPatchId << " " << manuId << endl;