]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackerIO.cxx
Consolidation of the classes used to get the calibration data stores from OCDB and...
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerIO.cxx
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
18 #include <cstdlib>
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
30 ClassImp(AliMUONTrackerIO)
31 /// \endcond
32
33 #include "AliDCSValue.h"
34 #include "AliLog.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>
43 #include <TClass.h>
44 #include <TObjString.h>
45 #include <TSystem.h>
46 #include <sstream>
47
48 //_____________________________________________________________________________
49 AliMUONTrackerIO::AliMUONTrackerIO()
50 {
51   /// ctor
52 }
53
54 //_____________________________________________________________________________
55 AliMUONTrackerIO::~AliMUONTrackerIO()
56 {
57   /// dtor
58 }
59
60 //_____________________________________________________________________________
61 Int_t 
62 AliMUONTrackerIO::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";
82   
83   in.close();
84   
85   return DecodeOccupancy(stream.str().c_str(),occupancyMap);
86   
87 }
88
89 //_____________________________________________________________________________
90 Int_t 
91 AliMUONTrackerIO::DecodeOccupancy(const char* data, AliMUONVStore& occupancyMap)
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");
100     return 0;
101   }
102   
103   char line[1024];
104   istringstream in(data);
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     
120     Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
121
122     AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
123
124     Int_t numberOfChannelsInManu = -1;
125     
126     if (de) numberOfChannelsInManu = de->NofChannelsInManu(manuId);
127
128     if ( numberOfChannelsInManu <= 0 ) 
129     {
130       AliErrorClass(Form("BP %5d DE %5d MANU %5d nchannels=%d",busPatchId,detElemId,manuId,numberOfChannelsInManu));
131       continue;      
132     }
133     
134     AliMUONVCalibParam* occupancy = 
135     static_cast<AliMUONVCalibParam*>(occupancyMap.FindObject(detElemId,manuId));
136     if (occupancy) 
137     {
138       AliErrorClass(Form("DE %5d MANU %5d is already there ?!",detElemId,manuId));
139       continue;
140     }
141         
142     occupancy = new AliMUONCalibParamND(5,1,detElemId,manuId,0);
143
144     occupancyMap.Add(occupancy);
145     
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);
151     ++n;
152   }
153   
154   return n;
155 }
156
157 //_____________________________________________________________________________
158 Int_t 
159 AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
160 {
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 
164   /// from the OCDB).
165   
166   TString sFilename(gSystem->ExpandPathName(filename));
167   
168   std::ifstream in(sFilename.Data());
169   if (!in.good()) 
170   {
171     return kCannotOpenFile;
172   }
173   
174   ostringstream stream;
175   char line[1024];
176   while ( in.getline(line,1024) )
177         stream << line << "\n";
178   
179   in.close();
180
181   return DecodePedestals(stream.str().c_str(),pedStore);
182   
183 }
184
185 //_____________________________________________________________________________
186 Int_t 
187 AliMUONTrackerIO::DecodePedestals(const char* data, AliMUONVStore& pedStore)
188 {
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 
192   /// from AMORE DB).
193   
194   char line[1024];
195   Int_t busPatchID, manuID, manuChannel;
196   Float_t pedMean, pedSigma;
197   Int_t n(0);
198   istringstream in(data);
199   
200   while ( in.getline(line,1024) )
201   {
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);
207     
208     if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
209     {
210       AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
211                          detElemID,busPatchID,manuID));
212       continue;
213     }
214     
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));
217                     
218     AliMUONVCalibParam* ped = 
219       static_cast<AliMUONVCalibParam*>(pedStore.FindObject(detElemID,manuID));
220     if (!ped) 
221     {
222       ped = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),
223                                     detElemID,manuID,
224                                     AliMUONVCalibParam::InvalidFloatValue());  
225       pedStore.Add(ped);
226     }
227     ped->SetValueAsFloat(manuChannel,0,pedMean);
228     ped->SetValueAsFloat(manuChannel,1,pedSigma);
229     ++n;
230   }
231
232   return n;
233 }
234
235 //_____________________________________________________________________________
236 Int_t 
237 AliMUONTrackerIO::ReadGains(const char* filename, AliMUONVStore& gainStore,
238                             TString& comment)
239 {
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 
243   /// from the OCDB).
244   
245   comment = "";
246   
247   TString sFilename(gSystem->ExpandPathName(filename));
248   
249   std::ifstream in(sFilename.Data());
250   if (!in.good()) 
251   {
252     return kCannotOpenFile;
253   }
254   
255   ostringstream stream;
256   char line[1024];
257   while ( in.getline(line,1024) )
258         stream << line << "\n";
259   
260   in.close();
261   
262   return DecodeGains(stream.str().c_str(),gainStore,comment);
263
264 }
265
266 //_____________________________________________________________________________
267 Int_t 
268 AliMUONTrackerIO::DecodeGains(const char* data, AliMUONVStore& gainStore,
269                               TString& comment)
270 {
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 
274   /// from AMORE DB).
275   
276   char line[1024];
277   istringstream in(data);
278   Int_t busPatchID, manuID, manuChannel;
279   Float_t a0, a1;
280   Int_t thres;
281   UInt_t qual;
282   const Int_t kSaturation(3000); // FIXME: how to get this number ?
283   Int_t n(0);
284   Int_t runNumber(-1);
285   Int_t* runs(0x0);
286   Int_t* dac(0x0);
287   Int_t nDAC(0);
288   
289   while ( in.getline(line,1024) )
290   {
291     if ( strlen(line) < 10 ) continue;
292     if ( line[0] == '/' && line[1] == '/' ) 
293     {
294       TString sline(line);
295       if ( sline.Contains("DUMMY") )
296       {
297         AliDebugClass(1,"Got a dummy file here");
298         return kDummyFile;
299       }
300       if ( sline.Contains("* Run") )
301       {
302         TObjArray* a = sline.Tokenize(":");
303         if ( a->GetLast() >= 1 ) 
304         {
305           TString s = static_cast<TObjString*>(a->At(1))->String();
306           runNumber = s.Atoi();
307           AliDebugClass(1,Form("runNumber is %d",runNumber));
308         }            
309       }
310       if ( sline.Contains("DAC values") )
311       {
312         nDAC = TString(sline(2,sline.Length()-2)).Atoi();
313         AliDebugClass(1,Form("# of DAC values = %d",nDAC));
314         if ( nDAC > 0 )
315         {
316           if ( nDAC < 100 ) 
317           {
318             runs = new Int_t[nDAC];
319             dac = new Int_t[nDAC];
320             // skip two lines
321             in.getline(line,1024);
322             in.getline(line,1024);
323             // then get run and dac values
324             Int_t iDAC(0);
325             for ( Int_t i = 0; i < nDAC; ++i ) 
326             {
327               in.getline(line,1024);
328               Int_t a,b;
329               sscanf(line,"// %d %d",&a,&b);
330               runs[iDAC] = a;
331               dac[iDAC] = b;
332               AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
333               ++iDAC;
334             }
335           }
336           else
337           {
338             AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
339             nDAC = 0;
340             return kFormatError;
341           }
342         }
343       }
344       continue;
345     }
346     
347     sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
348            &a0,&a1,&thres,&qual); 
349     AliDebugClass(3,Form("line=%s",line));
350     Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
351     
352     if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
353     {
354       AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
355                          detElemID,busPatchID,manuID));
356       continue;
357     }
358     
359     AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
360                     "A1 %e THRES %5d QUAL %x",
361                     busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
362     if ( qual == 0 ) continue;
363     
364     AliMUONVCalibParam* gain = 
365       static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
366     
367    if (!gain) 
368     {
369       gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
370       gainStore.Add(gain);
371     }
372     gain->SetValueAsFloat(manuChannel,0,a0);
373     gain->SetValueAsFloat(manuChannel,1,a1);
374     gain->SetValueAsInt(manuChannel,2,thres);
375     gain->SetValueAsInt(manuChannel,3,qual);
376     gain->SetValueAsInt(manuChannel,4,kSaturation);
377     ++n;
378   }
379
380   comment = "";
381   
382   if ( runNumber > 0 )
383   {
384     comment = Form("RUN %d",runNumber);
385   }
386   
387   for ( Int_t i = 0; i < nDAC; ++i )
388   {
389     comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
390   }
391   
392   delete[] runs;
393   delete[] dac;
394   
395   return n;
396 }
397
398 //_____________________________________________________________________________
399 Int_t
400 AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
401 {
402   /// Read capacitance file
403   /// and append the read values into the given VStore
404   
405   TString sFilename(gSystem->ExpandPathName(filename));
406   
407   std::ifstream in(sFilename.Data());
408   if (!in.good()) 
409   {
410     return kCannotOpenFile;
411   }
412   
413   ostringstream stream;
414   char line[1024];
415   while ( in.getline(line,1024) )
416         stream << line << "\n";
417   
418   in.close();
419   
420   return DecodeCapacitances(stream.str().c_str(),capaStore);
421 }
422
423 //_____________________________________________________________________________
424 Int_t
425 AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
426 {
427   /// Read capacitances and append the read values into the given VStore
428   /// To be used when the input is a string (for instance when getting data 
429   /// from AMORE DB).
430   
431   Int_t ngenerated(0);
432   
433   char line[1024];
434   Int_t serialNumber(-1);
435   AliMUONVCalibParam* param(0x0);
436   istringstream in(data);
437
438   while ( in.getline(line,1024,'\n') )
439   {
440     if ( isdigit(line[0]) ) 
441     {
442       serialNumber = atoi(line);
443       param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
444       if (param)
445       {
446         AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
447         continue;
448       }
449       param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
450       Bool_t ok = capaStore.Add(param);
451       if (!ok)
452       {
453         AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
454         continue;
455       }      
456       continue;
457     }
458     Int_t channel;
459     Float_t capaValue;
460     Float_t injectionGain;
461     sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
462     AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
463                     serialNumber,channel,capaValue,injectionGain));
464     param->SetValueAsFloat(channel,0,capaValue);
465     param->SetValueAsFloat(channel,1,injectionGain);
466     ++ngenerated;
467   }
468   
469   return ngenerated;
470 }
471
472 //_____________________________________________________________________________
473 Int_t 
474 AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore, Bool_t& changed)
475 {
476   /// Read config file (produced by the MUONTRKda.exe program for instance)
477   /// and append the read values into the given VStore
478   /// To be used when the input is a file (for instance when reading data 
479   /// from the OCDB).
480   /// changed must be set to kFALSE before calling this method for the first time
481   /// (then the subsequent calls must not set it !)
482   ///
483   
484   TString sFilename(gSystem->ExpandPathName(filename));
485   
486   std::ifstream in(sFilename.Data());
487   if (!in.good()) 
488   {
489     return kCannotOpenFile;
490   }
491   
492   ostringstream stream;
493   char line[1024];
494   while ( in.getline(line,1024) )
495         stream << line << "\n";
496   
497   in.close();
498   
499   return DecodeConfig(stream.str().c_str(),confStore,changed);
500 }
501
502 //_____________________________________________________________________________
503 Int_t 
504 AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore, Bool_t& changed)
505 {
506   /// Read config data (produced by the MUONTRKda.exe program for instance)
507   /// and append the read values into the given VStore
508   /// To be used when the input is a TString (for instance when getting data 
509   /// from AMORE DB).
510   /// changed must be set to kFALSE before calling this method for the first time
511   /// (then the subsequent calls must not set it !)
512
513   char line[1024];
514   Int_t busPatchID, manuID;
515   Int_t n(0);
516   istringstream in(data);
517   
518   while ( in.getline(line,1024) )
519   {
520     AliDebugClass(3,Form("line=%s",line));
521     if ( line[0] == '#' ) 
522     {
523       TString sline(line);
524       sline.ToUpper();
525       if (sline.Contains("CHANGED") && !sline.Contains("UNCHANGED")) changed = kTRUE;
526       continue;
527     }
528     std::istringstream sin(line);
529     sin >> busPatchID >> manuID;
530
531     Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
532
533     if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
534     {
535       AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
536                          detElemID,busPatchID,manuID));
537       continue;
538     }
539     
540     AliMUONVCalibParam* conf = 
541     static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
542     if (!conf) 
543     {
544       conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
545       confStore.Add(conf);
546     }
547     ++n;
548   }
549   
550   return n;
551 }
552
553 //_____________________________________________________________________________
554 Int_t 
555 AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
556 {
557   /// Write the conf store as an ASCII file
558   /// Note that we are converting (back) the detElemId into a busPatchId
559   /// Return the number of lines written
560   
561   if ( !AliMpDDLStore::Instance() ) 
562   {
563     cout << "ERROR: mapping not loaded. Cannot work" << endl;
564     return 0;
565   }
566   
567   TIter next(confStore.CreateIterator());
568   AliMUONVCalibParam* param;
569   Int_t n(0);
570   
571   while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
572   {
573     Int_t detElemId = param->ID0();
574     Int_t manuId = param->ID1();
575     
576     Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
577     ++n;
578     
579     out << busPatchId << " " << manuId << endl;
580   }
581   return n;
582 }
583