]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackerIO.cxx
code cleanup: renaming functions; adding prototype code for later development; no...
[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       }
325       if ( sline.Contains("DAC values") )
326       {
327         nDAC = TString(sline(2,sline.Length()-2)).Atoi();
328         AliDebugClass(1,Form("# of DAC values = %d",nDAC));
329         if ( nDAC > 0 )
330         {
331           if ( nDAC < 100 ) 
332           {
333             delete[] runs;
334             delete[] dac;
335             runs = new Int_t[nDAC];
336             dac = new Int_t[nDAC];
337             // skip two lines
338             in.getline(line,1024);
339             sline = line;
340             if (!sline.Contains("*  nInit ="))
341             {
342               AliErrorClass("Improper format : was expecting nInit= line...");              
343             }
344             else
345             {
346               sscanf(line,"//   *  nInit = %d  *  f1nbp = %d  *  f2nbp = %d",&nInit,&f1nbp,&f2nbp);
347               AliDebugClass(1,Form("nInit = %d",nInit));
348               AliDebugClass(1,Form("f1nbp = %d",f1nbp));
349               AliDebugClass(1,Form("f2nbp = %d",f2nbp));
350             }
351             in.getline(line,1024);
352             in.getline(line,1024);
353             // then get run and dac values
354             Int_t iDAC(0);
355             for ( Int_t i = 0; i < nDAC; ++i ) 
356             {
357               in.getline(line,1024);
358               Int_t a,b;
359               sscanf(line,"// %d %d",&a,&b);
360               runs[iDAC] = a;
361               dac[iDAC] = b;
362               AliDebugClass(1,Form("RUN %d is DAC %d",runs[iDAC],dac[iDAC]));
363               ++iDAC;
364             }
365           }
366           else
367           {
368             AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
369             nDAC = 0;
370             delete [] runs;
371             delete [] dac;
372             return kFormatError;
373           }
374         }
375       }
376       continue;
377     }
378     
379     sscanf(line,"%d %d %d %f %f %d %x",&busPatchID,&manuID,&manuChannel,
380            &a0,&a1,&thres,&qual); 
381     AliDebugClass(3,Form("line=%s",line));
382     Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
383     
384     if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
385     {
386       AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
387                          detElemID,busPatchID,manuID));
388       continue;
389     }
390     
391     AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
392                     "A1 %e THRES %5d QUAL %x",
393                     busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
394     if ( qual == 0 ) continue;
395     
396     AliMUONVCalibParam* gain = 
397       static_cast<AliMUONVCalibParam*>(gainStore.FindObject(detElemID,manuID));
398     
399    if (!gain) 
400     {
401       gain = new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),detElemID,manuID,0);
402       gainStore.Add(gain);
403     }
404     gain->SetValueAsFloat(manuChannel,0,a0);
405     gain->SetValueAsFloat(manuChannel,1,a1);
406     gain->SetValueAsInt(manuChannel,2,thres);
407     gain->SetValueAsInt(manuChannel,3,qual);
408     gain->SetValueAsInt(manuChannel,4,kSaturation);
409     ++n;
410   }
411
412   comment = "";
413   
414   if ( runNumber > 0 )
415   {
416     comment = Form("RUN %d",runNumber);
417   }
418   
419   for ( Int_t i = 0; i < nDAC; ++i )
420   {
421     comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
422   }
423   comment += Form(";(nDAC = %d)",nDAC);
424   comment += Form(";(nInit = %d)",nInit);
425   comment += Form(";(f1nbp = %d)",f1nbp);
426   comment += Form(";(f2nbp = %d)",f2nbp);
427   
428   delete[] runs;
429   delete[] dac;
430   
431   return n;
432 }
433
434 //_____________________________________________________________________________
435 Int_t
436 AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
437 {
438   /// Read capacitance file
439   /// and append the read values into the given VStore
440   
441   TString sFilename(gSystem->ExpandPathName(filename));
442   
443   std::ifstream in(sFilename.Data());
444   if (!in.good()) 
445   {
446     return kCannotOpenFile;
447   }
448   
449   ostringstream stream;
450   char line[1024];
451   while ( in.getline(line,1024) )
452         stream << line << "\n";
453   
454   in.close();
455   
456   return DecodeCapacitances(stream.str().c_str(),capaStore);
457 }
458
459 //_____________________________________________________________________________
460 Int_t
461 AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
462 {
463   /// Read capacitances and append the read values into the given VStore
464   /// To be used when the input is a string (for instance when getting data 
465   /// from AMORE DB).
466   
467   Int_t ngenerated(0);
468   
469   char line[1024];
470   Int_t serialNumber(-1);
471   AliMUONVCalibParam* param(0x0);
472   istringstream in(data);
473
474   while ( in.getline(line,1024,'\n') )
475   {
476     if ( isdigit(line[0]) ) 
477     {
478       serialNumber = atoi(line);
479       param = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
480       if (param)
481       {
482         AliErrorClass(Form("serialNumber %d appears several times !",serialNumber));
483         continue;
484       }
485       param = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
486       Bool_t ok = capaStore.Add(param);
487       if (!ok)
488       {
489         AliErrorClass(Form("Could not set serialNumber=%d",serialNumber));
490         continue;
491       }      
492       continue;
493     }
494     
495     if (!param) continue;
496     
497     Int_t channel;
498     Float_t capaValue;
499     Float_t injectionGain;
500     sscanf(line,"%d %f %f",&channel,&capaValue,&injectionGain);
501     AliDebugClass(1,Form("SerialNumber %10d Channel %3d Capa %f injectionGain %f",
502                     serialNumber,channel,capaValue,injectionGain));
503     param->SetValueAsFloat(channel,0,capaValue);
504     param->SetValueAsFloat(channel,1,injectionGain);
505     ++ngenerated;
506   }
507   
508   return ngenerated;
509 }
510
511 //_____________________________________________________________________________
512 Int_t 
513 AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore)
514 {
515   /// Read config file (produced by the MUONTRKda.exe program for instance)
516   /// and append the read values into the given VStore
517   /// To be used when the input is a file (for instance when reading data 
518   /// from the OCDB).
519   
520   TString sFilename(gSystem->ExpandPathName(filename));
521   
522   std::ifstream in(sFilename.Data());
523   if (!in.good()) 
524   {
525     return kCannotOpenFile;
526   }
527   
528   ostringstream stream;
529   char line[1024];
530   while ( in.getline(line,1024) )
531         stream << line << "\n";
532   
533   in.close();
534   
535   return DecodeConfig(stream.str().c_str(),confStore);
536 }
537
538 //_____________________________________________________________________________
539 Int_t 
540 AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore)
541 {
542   /// Read config data (produced by the MUONTRKda.exe program for instance)
543   /// and append the read values into the given VStore
544   /// To be used when the input is a TString (for instance when getting data 
545   /// from AMORE DB).
546
547   char line[1024];
548   Int_t busPatchID, manuID;
549   Int_t n(0);
550   istringstream in(data);
551   
552   while ( in.getline(line,1024) )
553   {
554     AliDebugClass(3,Form("line=%s",line));
555     if ( line[0] == '#' ) 
556     {
557       continue;
558     }
559     std::istringstream sin(line);
560     sin >> busPatchID >> manuID;
561
562     Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
563
564     if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
565     {
566       AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
567                          detElemID,busPatchID,manuID));
568       continue;
569     }
570     
571     AliMUONVCalibParam* conf = 
572     static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
573     if (!conf) 
574     {
575       conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
576       confStore.Add(conf);
577     }
578     ++n;
579   }
580   
581   return n;
582 }
583
584 //_____________________________________________________________________________
585 Int_t 
586 AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
587 {
588   /// Write the conf store as an ASCII file
589   /// Note that we are converting (back) the detElemId into a busPatchId
590   /// Return the number of lines written
591   
592   if ( !AliMpDDLStore::Instance() ) 
593   {
594     cout << "ERROR: mapping not loaded. Cannot work" << endl;
595     return 0;
596   }
597   
598   TIter next(confStore.CreateIterator());
599   AliMUONVCalibParam* param;
600   Int_t n(0);
601   
602   while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
603   {
604     Int_t detElemId = param->ID0();
605     Int_t manuId = param->ID1();
606     
607     Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
608     ++n;
609     
610     out << busPatchId << " " << manuId << endl;
611   }
612   return n;
613 }
614