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";
82 datastring = TString(stream.str().c_str());
86 return DecodeOccupancy(datastring,occupancyMap);
90 //_____________________________________________________________________________
92 AliMUONTrackerIO::DecodeOccupancy(TString data, AliMUONVStore& occupancyMap)
94 /// Decode occupancy string created append values to the occupancyMap store.
95 /// Expected format of the file is :
96 /// busPatchId manuId sumofn nevt
98 if ( ! AliMpDDLStore::Instance(kFALSE) )
100 AliErrorClass("Mapping not loaded. Cannot work");
105 istringstream in(data.Data());
109 while ( in.getline(line,1024) )
111 AliDebugClass(3,Form("line=%s",line));
112 if ( line[0] == '/' && line[1] == '/' ) continue;
113 std::istringstream sin(line);
115 Int_t busPatchId, manuId;
116 Int_t numberOfEvents;
119 sin >> busPatchId >> manuId >> sumn >> numberOfEvents;
121 Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
123 AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
125 Int_t numberOfChannelsInManu = -1;
127 if (de) numberOfChannelsInManu = de->NofChannelsInManu(manuId);
129 if ( numberOfChannelsInManu <= 0 )
131 AliErrorClass(Form("BP %5d DE %5d MANU %5d nchannels=%d",busPatchId,detElemId,manuId,numberOfChannelsInManu));
135 AliMUONVCalibParam* occupancy =
136 static_cast<AliMUONVCalibParam*>(occupancyMap.FindObject(detElemId,manuId));
139 AliErrorClass(Form("DE %5d MANU %5d is already there ?!",detElemId,manuId));
143 occupancy = new AliMUONCalibParamND(5,1,detElemId,manuId,0);
145 occupancyMap.Add(occupancy);
147 occupancy->SetValueAsDouble(0,0,sumn);
148 occupancy->SetValueAsDouble(0,1,sumn); // with only 0 and 1s, sumw = sumw2 = sumn
149 occupancy->SetValueAsDouble(0,2,sumn);
150 occupancy->SetValueAsInt(0,3,numberOfChannelsInManu);
151 occupancy->SetValueAsInt(0,4,numberOfEvents);
158 //_____________________________________________________________________________
160 AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
162 /// Read pedestal file (produced by the MUONTRKda.exe program for instance)
163 /// and append the read values into the given VStore
164 /// To be used when the input is a file (for instance when reading data
167 TString sFilename(gSystem->ExpandPathName(filename));
169 std::ifstream in(sFilename.Data());
172 return kCannotOpenFile;
176 ostringstream stream;
178 while ( in.getline(line,1024) )
179 stream << line << "\n";
180 datastring = TString(stream.str().c_str());
184 return DecodePedestals(datastring,pedStore);
188 //_____________________________________________________________________________
190 AliMUONTrackerIO::DecodePedestals(TString data, AliMUONVStore& pedStore)
192 /// Read pedestal Data (produced by the MUONTRKda.exe program for instance)
193 /// and append the read values into the given VStore
194 /// To be used when the input is a TString (for instance when getting data
198 Int_t busPatchID, manuID, manuChannel;
199 Float_t pedMean, pedSigma;
201 istringstream in(data.Data());
203 while ( in.getline(line,1024) )
205 AliDebugClass(3,Form("line=%s",line));
206 if ( line[0] == '/' && line[1] == '/' ) continue;
207 std::istringstream sin(line);
208 sin >> busPatchID >> manuID >> manuChannel >> pedMean >> pedSigma;
209 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
211 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
213 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
214 detElemID,busPatchID,manuID));
218 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d MEAN %7.2f SIGMA %7.2f",
219 busPatchID,detElemID,manuID,manuChannel,pedMean,pedSigma));
221 AliMUONVCalibParam* ped =
222 static_cast<AliMUONVCalibParam*>(pedStore.FindObject(detElemID,manuID));
225 ped = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),
227 AliMUONVCalibParam::InvalidFloatValue());
230 ped->SetValueAsFloat(manuChannel,0,pedMean);
231 ped->SetValueAsFloat(manuChannel,1,pedSigma);
238 //_____________________________________________________________________________
240 AliMUONTrackerIO::ReadGains(const char* filename, AliMUONVStore& gainStore,
243 /// Read gain file (produced by the MUONTRKda.exe program for instance)
244 /// and append the read values into the given VStore
245 /// To be used when the input is a file (for instance when reading data
250 TString sFilename(gSystem->ExpandPathName(filename));
252 std::ifstream in(sFilename.Data());
255 return kCannotOpenFile;
259 ostringstream stream;
261 while ( in.getline(line,1024) )
262 stream << line << "\n";
263 datastring = TString(stream.str().c_str());
267 return DecodeGains(datastring,gainStore,comment);
271 //_____________________________________________________________________________
273 AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
276 /// Read gain file (produced by the MUONTRKda.exe program for instance)
277 /// and append the read values into the given VStore
278 /// To be used when the input is a TString (for instance when getting data
282 istringstream in(data.Data());
283 Int_t busPatchID, manuID, manuChannel;
287 const Int_t kSaturation(3000); // FIXME: how to get this number ?
294 while ( in.getline(line,1024) )
296 if ( strlen(line) < 10 ) continue;
297 if ( line[0] == '/' && line[1] == '/' )
300 if ( sline.Contains("DUMMY") )
302 AliDebugClass(1,"Got a dummy file here");
305 if ( sline.Contains("* Run") )
307 TObjArray* a = sline.Tokenize(":");
308 if ( a->GetLast() >= 1 )
310 TString s = static_cast<TObjString*>(a->At(1))->String();
311 runNumber = s.Atoi();
312 AliDebugClass(1,Form("runNumber is %d",runNumber));
315 if ( sline.Contains("DAC values") )
317 nDAC = TString(sline(2,sline.Length()-2)).Atoi();
318 AliDebugClass(1,Form("# of DAC values = %d",nDAC));
323 runs = new Int_t[nDAC];
324 dac = new Int_t[nDAC];
326 in.getline(line,1024);
327 in.getline(line,1024);
328 // then get run and dac values
330 for ( Int_t i = 0; i < nDAC; ++i )
332 in.getline(line,1024);
334 sscanf(line,"// %d %d",&a,&b);
337 AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
343 AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
352 sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
353 &a0,&a1,&thres,&qual);
354 AliDebugClass(3,Form("line=%s",line));
355 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
357 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
359 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
360 detElemID,busPatchID,manuID));
364 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
365 "A1 %e THRES %5d QUAL %x",
366 busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
367 if ( qual == 0 ) continue;
369 AliMUONVCalibParam* gain =
370 static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
374 gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
377 gain->SetValueAsFloat(manuChannel,0,a0);
378 gain->SetValueAsFloat(manuChannel,1,a1);
379 gain->SetValueAsInt(manuChannel,2,thres);
380 gain->SetValueAsInt(manuChannel,3,qual);
381 gain->SetValueAsInt(manuChannel,4,kSaturation);
389 comment = Form("RUN %d",runNumber);
392 for ( Int_t i = 0; i < nDAC; ++i )
394 comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
403 //_____________________________________________________________________________
405 AliMUONTrackerIO::ReadCapacitances(const char* file, AliMUONVStore& capaStore)
407 /// Read capacitance file
408 /// and append the read values into the given VStore
410 ifstream in(gSystem->ExpandPathName(file));
411 if (in.bad()) return kCannotOpenFile;
416 Int_t serialNumber(-1);
417 AliMUONVCalibParam* param(0x0);
419 while ( in.getline(line,1024,'\n') )
421 if ( isdigit(line[0]) )
423 serialNumber = atoi(line);
424 param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
427 AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
430 param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
431 Bool_t ok = capaStore.Add(param);
434 AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
441 Float_t injectionGain;
442 sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
443 AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
444 serialNumber,channel,capaValue,injectionGain));
445 param->SetValueAsFloat(channel,0,capaValue);
446 param->SetValueAsFloat(channel,1,injectionGain);
455 //_____________________________________________________________________________
457 AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore, Bool_t& changed)
459 /// Read config file (produced by the MUONTRKda.exe program for instance)
460 /// and append the read values into the given VStore
461 /// To be used when the input is a file (for instance when reading data
463 /// changed must be set to kFALSE before calling this method for the first time
464 /// (then the subsequent calls must not set it !)
467 TString sFilename(gSystem->ExpandPathName(filename));
469 std::ifstream in(sFilename.Data());
472 return kCannotOpenFile;
476 ostringstream stream;
478 while ( in.getline(line,1024) )
479 stream << line << "\n";
480 datastring = TString(stream.str().c_str());
484 return DecodeConfig(datastring,confStore,changed);
487 //_____________________________________________________________________________
489 AliMUONTrackerIO::DecodeConfig(TString data, AliMUONVStore& confStore, Bool_t& changed)
491 /// Read config data (produced by the MUONTRKda.exe program for instance)
492 /// and append the read values into the given VStore
493 /// To be used when the input is a TString (for instance when getting data
495 /// changed must be set to kFALSE before calling this method for the first time
496 /// (then the subsequent calls must not set it !)
499 Int_t busPatchID, manuID;
501 istringstream in(data.Data());
503 while ( in.getline(line,1024) )
505 AliDebugClass(3,Form("line=%s",line));
506 if ( line[0] == '#' )
510 if (sline.Contains("CHANGED") && !sline.Contains("UNCHANGED")) changed = kTRUE;
513 std::istringstream sin(line);
514 sin >> busPatchID >> manuID;
516 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
518 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
520 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
521 detElemID,busPatchID,manuID));
525 AliMUONVCalibParam* conf =
526 static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
529 conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
538 //_____________________________________________________________________________
540 AliMUONTrackerIO::WriteConfig(ofstream& out, AliMUONVStore& confStore)
542 /// Write the conf store as an ASCII file
543 /// Note that we are converting (back) the detElemId into a busPatchId
544 /// Return the number of lines written
546 if ( !AliMpDDLStore::Instance() )
548 cout << "ERROR: mapping not loaded. Cannot work" << endl;
552 TIter next(confStore.CreateIterator());
553 AliMUONVCalibParam* param;
556 while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
558 Int_t detElemId = param->ID0();
559 Int_t manuId = param->ID1();
561 Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
564 out << busPatchId << " " << manuId << endl;