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));
326 if ( sline.Contains("DAC values") )
328 nDAC = TString(sline(2,sline.Length()-2)).Atoi();
329 AliDebugClass(1,Form("# of DAC values = %d",nDAC));
336 runs = new Int_t[nDAC];
337 dac = new Int_t[nDAC];
339 in.getline(line,1024);
341 if (!sline.Contains("* nInit ="))
343 AliErrorClass("Improper format : was expecting nInit= line...");
347 sscanf(line,"// * nInit = %d * f1nbp = %d * f2nbp = %d",&nInit,&f1nbp,&f2nbp);
348 AliDebugClass(1,Form("nInit = %d",nInit));
349 AliDebugClass(1,Form("f1nbp = %d",f1nbp));
350 AliDebugClass(1,Form("f2nbp = %d",f2nbp));
352 in.getline(line,1024);
353 in.getline(line,1024);
354 // then get run and dac values
356 for ( Int_t i = 0; i < nDAC; ++i )
358 in.getline(line,1024);
360 sscanf(line,"// %d %d",&a,&b);
363 AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
369 AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
380 sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
381 &a0,&a1,&thres,&qual);
382 AliDebugClass(3,Form("line=%s",line));
383 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
385 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
387 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
388 detElemID,busPatchID,manuID));
392 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
393 "A1 %e THRES %5d QUAL %x",
394 busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
395 if ( qual == 0 ) continue;
397 AliMUONVCalibParam* gain =
398 static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
402 gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
405 gain->SetValueAsFloat(manuChannel,0,a0);
406 gain->SetValueAsFloat(manuChannel,1,a1);
407 gain->SetValueAsInt(manuChannel,2,thres);
408 gain->SetValueAsInt(manuChannel,3,qual);
409 gain->SetValueAsInt(manuChannel,4,kSaturation);
417 comment = Form("RUN %d",runNumber);
420 for ( Int_t i = 0; i < nDAC; ++i )
422 comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
424 comment += Form(";(nDAC = %d)",nDAC);
425 comment += Form(";(nInit = %d)",nInit);
426 comment += Form(";(f1nbp = %d)",f1nbp);
427 comment += Form(";(f2nbp = %d)",f2nbp);
435 //_____________________________________________________________________________
437 AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
439 /// Read capacitance file
440 /// and append the read values into the given VStore
442 TString sFilename(gSystem->ExpandPathName(filename));
444 std::ifstream in(sFilename.Data());
447 return kCannotOpenFile;
450 ostringstream stream;
452 while ( in.getline(line,1024) )
453 stream << line << "\n";
457 return DecodeCapacitances(stream.str().c_str(),capaStore);
460 //_____________________________________________________________________________
462 AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
464 /// Read capacitances and append the read values into the given VStore
465 /// To be used when the input is a string (for instance when getting data
471 Int_t serialNumber(-1);
472 AliMUONVCalibParam* param(0x0);
473 istringstream in(data);
475 while ( in.getline(line,1024,'\n') )
477 if ( isdigit(line[0]) )
479 serialNumber = atoi(line);
480 param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
483 AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
486 param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
487 Bool_t ok = capaStore.Add(param);
490 AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
496 if (!param) continue;
500 Float_t injectionGain;
501 sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
502 AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
503 serialNumber,channel,capaValue,injectionGain));
504 param->SetValueAsFloat(channel,0,capaValue);
505 param->SetValueAsFloat(channel,1,injectionGain);
512 //_____________________________________________________________________________
514 AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore)
516 /// Read config file (produced by the MUONTRKda.exe program for instance)
517 /// and append the read values into the given VStore
518 /// To be used when the input is a file (for instance when reading data
521 TString sFilename(gSystem->ExpandPathName(filename));
523 std::ifstream in(sFilename.Data());
526 return kCannotOpenFile;
529 ostringstream stream;
531 while ( in.getline(line,1024) )
532 stream << line << "\n";
536 return DecodeConfig(stream.str().c_str(),confStore);
539 //_____________________________________________________________________________
541 AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore)
543 /// Read config data (produced by the MUONTRKda.exe program for instance)
544 /// and append the read values into the given VStore
545 /// To be used when the input is a TString (for instance when getting data
549 Int_t busPatchID, manuID;
551 istringstream in(data);
553 while ( in.getline(line,1024) )
555 AliDebugClass(3,Form("line=%s",line));
556 if ( line[0] == '#' )
560 std::istringstream sin(line);
561 sin >> busPatchID >> manuID;
563 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
565 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
567 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
568 detElemID,busPatchID,manuID));
572 AliMUONVCalibParam* conf =
573 static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
576 conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
585 //_____________________________________________________________________________
587 AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
589 /// Write the conf store as an ASCII file
590 /// Note that we are converting (back) the detElemId into a busPatchId
591 /// Return the number of lines written
593 if ( !AliMpDDLStore::Instance() )
595 cout << "ERROR: mapping not loaded. Cannot work" << endl;
599 TIter next(confStore.CreateIterator());
600 AliMUONVCalibParam* param;
603 while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
605 Int_t detElemId = param->ID0();
606 Int_t manuId = param->ID1();
608 Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
611 out << busPatchId << " " << manuId << endl;