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