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 Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
122 AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
124 Int_t numberOfChannelsInManu = -1;
126 if (de) numberOfChannelsInManu = de->NofChannelsInManu(manuId);
128 if ( numberOfChannelsInManu <= 0 )
130 AliErrorClass(Form("BP %5d DE %5d MANU %5d nchannels=%d",busPatchId,detElemId,manuId,numberOfChannelsInManu));
134 AliMUONVCalibParam* occupancy =
135 static_cast<AliMUONVCalibParam*>(occupancyMap.FindObject(detElemId,manuId));
138 AliErrorClass(Form("DE %5d MANU %5d is already there ?!",detElemId,manuId));
142 occupancy = new AliMUONCalibParamND(5,1,detElemId,manuId,0);
144 occupancyMap.Add(occupancy);
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);
157 //_____________________________________________________________________________
159 AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
161 /// Read pedestal file (produced by the MUONTRKda.exe program for instance)
162 /// and append the read values into the given VStore
163 /// To be used when the input is a file (for instance when reading data
166 TString sFilename(gSystem->ExpandPathName(filename));
168 std::ifstream in(sFilename.Data());
171 return kCannotOpenFile;
174 ostringstream stream;
176 while ( in.getline(line,1024) )
177 stream << line << "\n";
181 return DecodePedestals(stream.str().c_str(),pedStore);
185 //_____________________________________________________________________________
187 AliMUONTrackerIO::DecodePedestals(const char* data, AliMUONVStore& pedStore)
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
195 Int_t busPatchID, manuID, manuChannel;
196 Float_t pedMean, pedSigma;
198 istringstream in(data);
200 while ( in.getline(line,1024) )
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);
208 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
210 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
211 detElemID,busPatchID,manuID));
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));
218 AliMUONVCalibParam* ped =
219 static_cast<AliMUONVCalibParam*>(pedStore.FindObject(detElemID,manuID));
222 ped = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),
224 AliMUONVCalibParam::InvalidFloatValue());
227 ped->SetValueAsFloat(manuChannel,0,pedMean);
228 ped->SetValueAsFloat(manuChannel,1,pedSigma);
235 //_____________________________________________________________________________
237 AliMUONTrackerIO::ReadGains(const char* filename, AliMUONVStore& gainStore,
240 /// Read gain file (produced by the MUONTRKda.exe program for instance)
241 /// and append the read values into the given VStore
242 /// To be used when the input is a file (for instance when reading data
247 TString sFilename(gSystem->ExpandPathName(filename));
249 std::ifstream in(sFilename.Data());
252 return kCannotOpenFile;
255 ostringstream stream;
257 while ( in.getline(line,1024) )
258 stream << line << "\n";
262 return DecodeGains(stream.str().c_str(),gainStore,comment);
266 //_____________________________________________________________________________
268 AliMUONTrackerIO::DecodeGains(const char* data, AliMUONVStore& gainStore,
271 /// Read gain file (produced by the MUONTRKda.exe program for instance)
272 /// and append the read values into the given VStore
273 /// To be used when the input is a string (for instance when getting data
277 istringstream in(data);
278 Int_t busPatchID, manuID, manuChannel;
282 const Int_t kSaturation(3000); // FIXME: how to get this number ?
288 Int_t nInit(0),f1nbp(0),f2nbp(0);
290 while ( in.getline(line,1024) )
292 if ( strlen(line) < 10 ) continue;
293 if ( line[0] == '/' && line[1] == '/' )
296 if ( sline.Contains("DUMMY") )
298 AliDebugClass(1,"Got a dummy file here");
301 if ( sline.Contains("* Run") )
303 TObjArray* a = sline.Tokenize(":");
304 if ( a->GetLast() >= 1 )
306 TString s = static_cast<TObjString*>(a->At(1))->String();
307 runNumber = s.Atoi();
308 AliDebugClass(1,Form("runNumber is %d",runNumber));
311 if ( sline.Contains("DAC values") )
313 nDAC = TString(sline(2,sline.Length()-2)).Atoi();
314 AliDebugClass(1,Form("# of DAC values = %d",nDAC));
319 runs = new Int_t[nDAC];
320 dac = new Int_t[nDAC];
322 in.getline(line,1024);
324 if (!sline.Contains("* nInit ="))
326 AliErrorClass("Improper format : was expecting nInit= line...");
330 sscanf(line,"// * nInit = %d * f1nbp = %d * f2nbp = %d",&nInit,&f1nbp,&f2nbp);
331 AliDebugClass(1,Form("nInit = %d",nInit));
332 AliDebugClass(1,Form("f1nbp = %d",f1nbp));
333 AliDebugClass(1,Form("f2nbp = %d",f2nbp));
335 in.getline(line,1024);
336 in.getline(line,1024);
337 // then get run and dac values
339 for ( Int_t i = 0; i < nDAC; ++i )
341 in.getline(line,1024);
343 sscanf(line,"// %d %d",&a,&b);
346 AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
352 AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
361 sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
362 &a0,&a1,&thres,&qual);
363 AliDebugClass(3,Form("line=%s",line));
364 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
366 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
368 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
369 detElemID,busPatchID,manuID));
373 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
374 "A1 %e THRES %5d QUAL %x",
375 busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
376 if ( qual == 0 ) continue;
378 AliMUONVCalibParam* gain =
379 static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
383 gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
386 gain->SetValueAsFloat(manuChannel,0,a0);
387 gain->SetValueAsFloat(manuChannel,1,a1);
388 gain->SetValueAsInt(manuChannel,2,thres);
389 gain->SetValueAsInt(manuChannel,3,qual);
390 gain->SetValueAsInt(manuChannel,4,kSaturation);
398 comment = Form("RUN %d",runNumber);
401 for ( Int_t i = 0; i < nDAC; ++i )
403 comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
405 comment += Form(";(nDAC = %d)",nDAC);
406 comment += Form(";(nInit = %d)",nInit);
407 comment += Form(";(f1nbp = %d)",f1nbp);
408 comment += Form(";(f2nbp = %d)",f2nbp);
416 //_____________________________________________________________________________
418 AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
420 /// Read capacitance file
421 /// and append the read values into the given VStore
423 TString sFilename(gSystem->ExpandPathName(filename));
425 std::ifstream in(sFilename.Data());
428 return kCannotOpenFile;
431 ostringstream stream;
433 while ( in.getline(line,1024) )
434 stream << line << "\n";
438 return DecodeCapacitances(stream.str().c_str(),capaStore);
441 //_____________________________________________________________________________
443 AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
445 /// Read capacitances and append the read values into the given VStore
446 /// To be used when the input is a string (for instance when getting data
452 Int_t serialNumber(-1);
453 AliMUONVCalibParam* param(0x0);
454 istringstream in(data);
456 while ( in.getline(line,1024,'\n') )
458 if ( isdigit(line[0]) )
460 serialNumber = atoi(line);
461 param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
464 AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
467 param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
468 Bool_t ok = capaStore.Add(param);
471 AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
478 Float_t injectionGain;
479 sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
480 AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
481 serialNumber,channel,capaValue,injectionGain));
482 param->SetValueAsFloat(channel,0,capaValue);
483 param->SetValueAsFloat(channel,1,injectionGain);
490 //_____________________________________________________________________________
492 AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore)
494 /// Read config file (produced by the MUONTRKda.exe program for instance)
495 /// and append the read values into the given VStore
496 /// To be used when the input is a file (for instance when reading data
499 TString sFilename(gSystem->ExpandPathName(filename));
501 std::ifstream in(sFilename.Data());
504 return kCannotOpenFile;
507 ostringstream stream;
509 while ( in.getline(line,1024) )
510 stream << line << "\n";
514 return DecodeConfig(stream.str().c_str(),confStore);
517 //_____________________________________________________________________________
519 AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore)
521 /// Read config data (produced by the MUONTRKda.exe program for instance)
522 /// and append the read values into the given VStore
523 /// To be used when the input is a TString (for instance when getting data
527 Int_t busPatchID, manuID;
529 istringstream in(data);
531 while ( in.getline(line,1024) )
533 AliDebugClass(3,Form("line=%s",line));
534 if ( line[0] == '#' )
538 std::istringstream sin(line);
539 sin >> busPatchID >> manuID;
541 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
543 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
545 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
546 detElemID,busPatchID,manuID));
550 AliMUONVCalibParam* conf =
551 static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
554 conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
563 //_____________________________________________________________________________
565 AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
567 /// Write the conf store as an ASCII file
568 /// Note that we are converting (back) the detElemId into a busPatchId
569 /// Return the number of lines written
571 if ( !AliMpDDLStore::Instance() )
573 cout << "ERROR: mapping not loaded. Cannot work" << endl;
577 TIter next(confStore.CreateIterator());
578 AliMUONVCalibParam* param;
581 while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
583 Int_t detElemId = param->ID0();
584 Int_t manuId = param->ID1();
586 Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
589 out << busPatchId << " " << manuId << endl;