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