]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONTrackerIO.cxx
Completing commit 45100 - .h file was missing
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerIO.cxx
CommitLineData
81028269 1/**************************************************************************
2* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3* *
4* Author: The ALICE Off-line Project. *
5* Contributors are mentioned in the code where appropriate. *
6* *
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**************************************************************************/
15
16// $Id$
17
b09247a2 18#include <cstdlib>
81028269 19#include "AliMUONTrackerIO.h"
20
21/// \class AliMUONTrackerIO
22///
23/// Reader class for ASCII calibration files for MUON tracker :
24/// converts those ASCII files into AliMUONVStore (suitable to e.g. feed
25/// the OCDB).
26///
27/// \author Laurent Aphecetche, Subatech
28
29/// \cond CLASSIMP
30ClassImp(AliMUONTrackerIO)
31/// \endcond
32
6c870207 33#include "AliDCSValue.h"
81028269 34#include "AliLog.h"
7eafe398 35#include "AliMUONCalibParamND.h"
81028269 36#include "AliMUONCalibParamNF.h"
37#include "AliMUONVStore.h"
38#include "AliMpConstants.h"
39#include "AliMpDDLStore.h"
6c870207 40#include "AliMpDEManager.h"
7eafe398 41#include "AliMpDetElement.h"
81028269 42#include <Riostream.h>
43#include <TClass.h>
44#include <TObjString.h>
45#include <TSystem.h>
46#include <sstream>
47
48//_____________________________________________________________________________
49AliMUONTrackerIO::AliMUONTrackerIO()
50{
51 /// ctor
52}
53
54//_____________________________________________________________________________
55AliMUONTrackerIO::~AliMUONTrackerIO()
56{
57 /// dtor
58}
59
7eafe398 60//_____________________________________________________________________________
61Int_t
62AliMUONTrackerIO::ReadOccupancy(const char* filename,AliMUONVStore& occupancyMap)
63{
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
68
69 TString sFilename(gSystem->ExpandPathName(filename));
70
71 std::ifstream in(sFilename.Data());
72 if (!in.good())
73 {
74 return kCannotOpenFile;
75 }
76
77 TString datastring;
78 ostringstream stream;
79 char line[1024];
80 while ( in.getline(line,1024) )
81 stream << line << "\n";
7eafe398 82
83 in.close();
84
ca913045 85 return DecodeOccupancy(stream.str().c_str(),occupancyMap);
7eafe398 86
87}
88
89//_____________________________________________________________________________
90Int_t
ca913045 91AliMUONTrackerIO::DecodeOccupancy(const char* data, AliMUONVStore& occupancyMap)
7eafe398 92{
93 /// Decode occupancy string created append values to the occupancyMap store.
94 /// Expected format of the file is :
95 /// busPatchId manuId sumofn nevt
96
97 if ( ! AliMpDDLStore::Instance(kFALSE) )
98 {
99 AliErrorClass("Mapping not loaded. Cannot work");
33d94e07 100 return kNoMapping;
7eafe398 101 }
102
103 char line[1024];
ca913045 104 istringstream in(data);
7eafe398 105
106 Int_t n(0);
107
108 while ( in.getline(line,1024) )
109 {
110 AliDebugClass(3,Form("line=%s",line));
111 if ( line[0] == '/' && line[1] == '/' ) continue;
112 std::istringstream sin(line);
113
114 Int_t busPatchId, manuId;
115 Int_t numberOfEvents;
116 Double_t sumn;
117
118 sin >> busPatchId >> manuId >> sumn >> numberOfEvents;
119
33d94e07 120 if ( busPatchId == -1 && manuId == -1 && sumn == 0 && numberOfEvents == 0 )
121 {
122 /// DA could not produce information (because run failed somehow).
123 /// That's OK, but let the world know about it
124 return kNoInfoFile;
125 }
126
7eafe398 127 Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
128
129 AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
130
131 Int_t numberOfChannelsInManu = -1;
132
133 if (de) numberOfChannelsInManu = de->NofChannelsInManu(manuId);
134
135 if ( numberOfChannelsInManu <= 0 )
136 {
137 AliErrorClass(Form("BP %5d DE %5d MANU %5d nchannels=%d",busPatchId,detElemId,manuId,numberOfChannelsInManu));
138 continue;
139 }
140
141 AliMUONVCalibParam* occupancy =
142 static_cast<AliMUONVCalibParam*>(occupancyMap.FindObject(detElemId,manuId));
143 if (occupancy)
144 {
145 AliErrorClass(Form("DE %5d MANU %5d is already there ?!",detElemId,manuId));
146 continue;
147 }
148
149 occupancy = new AliMUONCalibParamND(5,1,detElemId,manuId,0);
150
151 occupancyMap.Add(occupancy);
152
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);
158 ++n;
159 }
160
161 return n;
162}
163
81028269 164//_____________________________________________________________________________
165Int_t
166AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
167{
168 /// Read pedestal file (produced by the MUONTRKda.exe program for instance)
169 /// and append the read values into the given VStore
cba13f7c 170 /// To be used when the input is a file (for instance when reading data
171 /// from the OCDB).
81028269 172
173 TString sFilename(gSystem->ExpandPathName(filename));
174
175 std::ifstream in(sFilename.Data());
176 if (!in.good())
177 {
178 return kCannotOpenFile;
179 }
180
06a51226 181 ostringstream stream;
182 char line[1024];
183 while ( in.getline(line,1024) )
184 stream << line << "\n";
06a51226 185
cba13f7c 186 in.close();
187
ca913045 188 return DecodePedestals(stream.str().c_str(),pedStore);
cba13f7c 189
190}
191
192//_____________________________________________________________________________
193Int_t
ca913045 194AliMUONTrackerIO::DecodePedestals(const char* data, AliMUONVStore& pedStore)
cba13f7c 195{
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
199 /// from AMORE DB).
200
81028269 201 char line[1024];
202 Int_t busPatchID, manuID, manuChannel;
203 Float_t pedMean, pedSigma;
204 Int_t n(0);
ca913045 205 istringstream in(data);
81028269 206
207 while ( in.getline(line,1024) )
208 {
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);
6c870207 214
215 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
216 {
217 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
218 detElemID,busPatchID,manuID));
219 continue;
220 }
221
81028269 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));
cba13f7c 224
81028269 225 AliMUONVCalibParam* ped =
226 static_cast<AliMUONVCalibParam*>(pedStore.FindObject(detElemID,manuID));
81028269 227 if (!ped)
228 {
229 ped = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),
230 detElemID,manuID,
231 AliMUONVCalibParam::InvalidFloatValue());
232 pedStore.Add(ped);
233 }
234 ped->SetValueAsFloat(manuChannel,0,pedMean);
235 ped->SetValueAsFloat(manuChannel,1,pedSigma);
236 ++n;
237 }
81028269 238
239 return n;
240}
241
242//_____________________________________________________________________________
243Int_t
244AliMUONTrackerIO::ReadGains(const char* filename, AliMUONVStore& gainStore,
245 TString& comment)
246{
247 /// Read gain file (produced by the MUONTRKda.exe program for instance)
248 /// and append the read values into the given VStore
cba13f7c 249 /// To be used when the input is a file (for instance when reading data
250 /// from the OCDB).
81028269 251
252 comment = "";
253
254 TString sFilename(gSystem->ExpandPathName(filename));
255
256 std::ifstream in(sFilename.Data());
257 if (!in.good())
258 {
259 return kCannotOpenFile;
260 }
261
cba13f7c 262 ostringstream stream;
263 char line[1024];
264 while ( in.getline(line,1024) )
265 stream << line << "\n";
cba13f7c 266
267 in.close();
268
ca913045 269 return DecodeGains(stream.str().c_str(),gainStore,comment);
cba13f7c 270
271}
272
273//_____________________________________________________________________________
274Int_t
ca913045 275AliMUONTrackerIO::DecodeGains(const char* data, AliMUONVStore& gainStore,
276 TString& comment)
cba13f7c 277{
278 /// Read gain file (produced by the MUONTRKda.exe program for instance)
279 /// and append the read values into the given VStore
ca913045 280 /// To be used when the input is a string (for instance when getting data
cba13f7c 281 /// from AMORE DB).
282
81028269 283 char line[1024];
ca913045 284 istringstream in(data);
81028269 285 Int_t busPatchID, manuID, manuChannel;
286 Float_t a0, a1;
287 Int_t thres;
288 UInt_t qual;
289 const Int_t kSaturation(3000); // FIXME: how to get this number ?
290 Int_t n(0);
291 Int_t runNumber(-1);
292 Int_t* runs(0x0);
293 Int_t* dac(0x0);
294 Int_t nDAC(0);
1ccd531d 295 Int_t nInit(0),f1nbp(0),f2nbp(0);
81028269 296
297 while ( in.getline(line,1024) )
298 {
299 if ( strlen(line) < 10 ) continue;
300 if ( line[0] == '/' && line[1] == '/' )
301 {
302 TString sline(line);
303 if ( sline.Contains("DUMMY") )
304 {
305 AliDebugClass(1,"Got a dummy file here");
7ae72085 306 delete [] runs;
307 delete [] dac;
81028269 308 return kDummyFile;
309 }
310 if ( sline.Contains("* Run") )
311 {
312 TObjArray* a = sline.Tokenize(":");
313 if ( a->GetLast() >= 1 )
314 {
315 TString s = static_cast<TObjString*>(a->At(1))->String();
316 runNumber = s.Atoi();
317 AliDebugClass(1,Form("runNumber is %d",runNumber));
318 }
319 }
320 if ( sline.Contains("DAC values") )
321 {
322 nDAC = TString(sline(2,sline.Length()-2)).Atoi();
323 AliDebugClass(1,Form("# of DAC values = %d",nDAC));
324 if ( nDAC > 0 )
325 {
326 if ( nDAC < 100 )
327 {
328 runs = new Int_t[nDAC];
329 dac = new Int_t[nDAC];
330 // skip two lines
331 in.getline(line,1024);
18ec51a0 332 sline = line;
333 if (!sline.Contains("* nInit ="))
334 {
335 AliErrorClass("Improper format : was expecting nInit= line...");
336 }
337 else
338 {
339 sscanf(line,"// * nInit = %d * f1nbp = %d * f2nbp = %d",&nInit,&f1nbp,&f2nbp);
340 AliDebugClass(1,Form("nInit = %d",nInit));
341 AliDebugClass(1,Form("f1nbp = %d",f1nbp));
342 AliDebugClass(1,Form("f2nbp = %d",f2nbp));
343 }
344 in.getline(line,1024);
81028269 345 in.getline(line,1024);
346 // then get run and dac values
ca913045 347 Int_t iDAC(0);
81028269 348 for ( Int_t i = 0; i < nDAC; ++i )
349 {
350 in.getline(line,1024);
351 Int_t a,b;
352 sscanf(line,"// %d %d",&a,&b);
353 runs[iDAC] = a;
354 dac[iDAC] = b;
355 AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
356 ++iDAC;
357 }
358 }
359 else
360 {
361 AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
362 nDAC = 0;
7ae72085 363 delete [] runs;
364 delete [] dac;
81028269 365 return kFormatError;
366 }
367 }
368 }
369 continue;
370 }
371
372 sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
373 &a0,&a1,&thres,&qual);
374 AliDebugClass(3,Form("line=%s",line));
375 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
6c870207 376
377 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
378 {
379 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
380 detElemID,busPatchID,manuID));
381 continue;
382 }
383
81028269 384 AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
385 "A1 %e THRES %5d QUAL %x",
386 busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
387 if ( qual == 0 ) continue;
388
389 AliMUONVCalibParam* gain =
390 static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
391
cba13f7c 392 if (!gain)
81028269 393 {
394 gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
395 gainStore.Add(gain);
396 }
397 gain->SetValueAsFloat(manuChannel,0,a0);
398 gain->SetValueAsFloat(manuChannel,1,a1);
399 gain->SetValueAsInt(manuChannel,2,thres);
400 gain->SetValueAsInt(manuChannel,3,qual);
401 gain->SetValueAsInt(manuChannel,4,kSaturation);
402 ++n;
403 }
cba13f7c 404
81028269 405 comment = "";
406
407 if ( runNumber > 0 )
408 {
409 comment = Form("RUN %d",runNumber);
410 }
411
412 for ( Int_t i = 0; i < nDAC; ++i )
413 {
414 comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
415 }
1ccd531d 416 comment += Form(";(nDAC = %d)",nDAC);
417 comment += Form(";(nInit = %d)",nInit);
418 comment += Form(";(f1nbp = %d)",f1nbp);
419 comment += Form(";(f2nbp = %d)",f2nbp);
81028269 420
421 delete[] runs;
422 delete[] dac;
423
424 return n;
425}
426
427//_____________________________________________________________________________
428Int_t
ca913045 429AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
81028269 430{
431 /// Read capacitance file
432 /// and append the read values into the given VStore
433
ca913045 434 TString sFilename(gSystem->ExpandPathName(filename));
435
436 std::ifstream in(sFilename.Data());
437 if (!in.good())
438 {
439 return kCannotOpenFile;
440 }
441
442 ostringstream stream;
443 char line[1024];
444 while ( in.getline(line,1024) )
445 stream << line << "\n";
446
447 in.close();
448
449 return DecodeCapacitances(stream.str().c_str(),capaStore);
450}
451
452//_____________________________________________________________________________
453Int_t
454AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
455{
456 /// Read capacitances and append the read values into the given VStore
457 /// To be used when the input is a string (for instance when getting data
458 /// from AMORE DB).
81028269 459
460 Int_t ngenerated(0);
461
462 char line[1024];
463 Int_t serialNumber(-1);
464 AliMUONVCalibParam* param(0x0);
ca913045 465 istringstream in(data);
466
81028269 467 while ( in.getline(line,1024,'\n') )
468 {
469 if ( isdigit(line[0]) )
470 {
471 serialNumber = atoi(line);
472 param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
473 if (param)
474 {
475 AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
630711ed 476 continue;
81028269 477 }
478 param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
479 Bool_t ok = capaStore.Add(param);
480 if (!ok)
481 {
482 AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
483 continue;
484 }
485 continue;
486 }
9b780418 487
488 if (!param) continue;
489
81028269 490 Int_t channel;
491 Float_t capaValue;
492 Float_t injectionGain;
493 sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
494 AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
495 serialNumber,channel,capaValue,injectionGain));
496 param->SetValueAsFloat(channel,0,capaValue);
497 param->SetValueAsFloat(channel,1,injectionGain);
498 ++ngenerated;
499 }
500
81028269 501 return ngenerated;
502}
6c870207 503
504//_____________________________________________________________________________
505Int_t
042cd64e 506AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore)
6c870207 507{
508 /// Read config file (produced by the MUONTRKda.exe program for instance)
509 /// and append the read values into the given VStore
510 /// To be used when the input is a file (for instance when reading data
511 /// from the OCDB).
6c870207 512
513 TString sFilename(gSystem->ExpandPathName(filename));
514
515 std::ifstream in(sFilename.Data());
516 if (!in.good())
517 {
518 return kCannotOpenFile;
519 }
520
6c870207 521 ostringstream stream;
522 char line[1024];
523 while ( in.getline(line,1024) )
524 stream << line << "\n";
6c870207 525
526 in.close();
527
042cd64e 528 return DecodeConfig(stream.str().c_str(),confStore);
6c870207 529}
530
531//_____________________________________________________________________________
532Int_t
042cd64e 533AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore)
6c870207 534{
535 /// Read config data (produced by the MUONTRKda.exe program for instance)
536 /// and append the read values into the given VStore
537 /// To be used when the input is a TString (for instance when getting data
538 /// from AMORE DB).
6c870207 539
540 char line[1024];
541 Int_t busPatchID, manuID;
542 Int_t n(0);
ca913045 543 istringstream in(data);
6c870207 544
545 while ( in.getline(line,1024) )
546 {
547 AliDebugClass(3,Form("line=%s",line));
548 if ( line[0] == '#' )
549 {
6c870207 550 continue;
551 }
552 std::istringstream sin(line);
553 sin >> busPatchID >> manuID;
554
555 Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
556
557 if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
558 {
559 AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
560 detElemID,busPatchID,manuID));
561 continue;
562 }
563
564 AliMUONVCalibParam* conf =
565 static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
566 if (!conf)
567 {
568 conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
569 confStore.Add(conf);
570 }
571 ++n;
572 }
573
574 return n;
575}
576
577//_____________________________________________________________________________
578Int_t
ca913045 579AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
6c870207 580{
581 /// Write the conf store as an ASCII file
582 /// Note that we are converting (back) the detElemId into a busPatchId
583 /// Return the number of lines written
584
585 if ( !AliMpDDLStore::Instance() )
586 {
587 cout << "ERROR: mapping not loaded. Cannot work" << endl;
588 return 0;
589 }
590
591 TIter next(confStore.CreateIterator());
592 AliMUONVCalibParam* param;
593 Int_t n(0);
594
595 while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
596 {
597 Int_t detElemId = param->ID0();
598 Int_t manuId = param->ID1();
599
600 Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
601 ++n;
602
603 out << busPatchId << " " << manuId << endl;
604 }
605 return n;
606}
607