]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPedestalSubprocessor.cxx
Cross library dependency removed
[u/mrichter/AliRoot.git] / MUON / AliMUONPedestalSubprocessor.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 "AliMUONPedestalSubprocessor.h"
19
20 #include "AliCDBMetaData.h"
21 #include "AliCDBEntry.h"
22 #include "AliDAQ.h"
23 #include "AliLog.h"
24 #include "AliMUON2DMap.h"
25 #include "AliMUON2DStoreValidator.h"
26 #include "AliMUONCalibParamNF.h"
27 #include "AliMUONPreprocessor.h"
28 #include "AliMUONTrackerIO.h"
29 #include "AliMpConstants.h"
30 #include "AliMpDDLStore.h"
31 #include "TObjString.h"
32 #include <Riostream.h>
33 #include <TList.h>
34 #include <TObjString.h>
35 #include <TSystem.h>
36 #include <sstream>
37
38 //-----------------------------------------------------------------------------
39 /// \class AliMUONPedestalSubprocessor
40 ///
41 /// Implementation of AliMUONVSubprocessor class to deal with MUON TRK pedestals.
42 ///
43 /// Pedestals are read in from an ascii file, with the format :               \n
44 ///---------------------------------------------------------------------------\n
45 /// BUS_PATCH MANU_ADDR CHANNEL      MEAN       SIGMA                         \n
46 ///---------------------------------------------------------------------------\n
47 ///
48 /// \author L. Aphecetche
49 //-----------------------------------------------------------------------------
50
51 /// \cond CLASSIMP
52 ClassImp(AliMUONPedestalSubprocessor)
53 /// \endcond
54
55 //_____________________________________________________________________________
56 AliMUONPedestalSubprocessor::AliMUONPedestalSubprocessor(AliMUONPreprocessor* master)
57 : AliMUONVSubprocessor(master,
58                        "Pedestals",
59                        "Upload MUON Tracker pedestals to OCDB"),
60 fPedestals(0x0),
61 fConfig(0x0),
62 fConfigChanged(kFALSE)
63 {
64   /// default ctor
65 }
66
67 //_____________________________________________________________________________
68 AliMUONPedestalSubprocessor::~AliMUONPedestalSubprocessor()
69 {
70   /// dtor
71   delete fPedestals;
72   delete fConfig;
73 }
74
75 //_____________________________________________________________________________
76 Bool_t
77 AliMUONPedestalSubprocessor::HasConfigChanged(const AliMUONVStore& newConfig) const
78 {
79   /// Check whether the config changed. 
80   /// Any error will return kTRUE to trig a config upload (safer way).
81   
82   AliCDBEntry* entry = Master()->GetFromOCDB("Calib","Config");
83   if (!entry)
84   {
85     AliError("Could not get MUON/Calib/Config entry for current run ! That's not OK !");
86     return kTRUE;
87   }
88   AliMUONVStore* oldConfig = dynamic_cast<AliMUONVStore*>(entry->GetObject());
89   if (!oldConfig)
90   {
91     AliError("Could not get MUON/Calib/Config object for current run (wrong type ?) ! That's not OK !");
92     return kTRUE;
93   }
94   
95   if ( oldConfig->GetSize() != newConfig.GetSize() ) 
96   {
97     return kTRUE;
98   }
99   
100   TIter next(oldConfig->CreateIterator());
101   AliMUONVCalibParam* old;
102   
103   while ( ( old = static_cast<AliMUONVCalibParam*>(next()) ) )
104   {
105     Int_t detElemId = old->ID0();
106     Int_t manuId = old->ID1();
107     
108     if ( ! newConfig.FindObject(detElemId,manuId) )
109     {
110       // not found in new. Configurations are different. Return right now.
111       return kTRUE;
112     }
113   }
114   
115   // all tests OK. Configuration has not changed.
116   return kFALSE;
117 }
118
119
120 //_____________________________________________________________________________
121 Bool_t 
122 AliMUONPedestalSubprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
123 {
124   /// When starting a new run, reads in the pedestals ASCII files.
125   
126   const Int_t kSystem = AliMUONPreprocessor::kDAQ;
127   const char* kId = "PEDESTALS";
128   
129   delete fPedestals;
130   fPedestals = new AliMUON2DMap(kTRUE);
131   
132   delete fConfig;
133   fConfig = new AliMUON2DMap(kTRUE);
134   
135   Master()->Log(Form("Reading pedestal files for Run %d startTime %ld endTime %ld",
136                      run,startTime,endTime));
137   
138   TList* sources = Master()->GetFileSources(kSystem,kId);
139   TIter next(sources);
140   TObjString* o(0x0);
141   Int_t n(0);
142   Int_t npedFiles(0);
143   
144   while ( ( o = static_cast<TObjString*>(next()) ) )
145   {
146     TString fileName(Master()->GetFile(kSystem,kId,o->GetName()));
147     Int_t ok = ReadPedestalFile(fileName.Data());
148     if (ok>0)
149     {
150       n += ok;
151       ++npedFiles;
152     }
153   }
154
155   delete sources;
156   
157   if (!n)
158   {
159     Master()->Log("Failed to read any pedestals");
160     delete fPedestals;
161     fPedestals = 0;
162     delete fConfig;
163     fConfig = 0;
164     return kFALSE;
165   }
166   
167   const char* kIdConf = "CONFIG";
168
169   sources = Master()->GetFileSources(kSystem,kIdConf);
170   TIter nextConf(sources);
171   Int_t nconf(0);
172   Int_t nconfFiles(0);
173   
174   while ( ( o = static_cast<TObjString*>(nextConf()) ) )
175   {
176     TString fileName(Master()->GetFile(kSystem,kIdConf,o->GetName()));
177     Int_t ok = ReadConfigFile(fileName.Data());
178     if (ok>0)
179     {
180       nconf += ok;
181       ++nconfFiles;
182     }
183   }
184   
185   delete sources;
186   
187   if ( npedFiles != nconfFiles )
188   {
189     Master()->Log(Form("ERROR : Number of config files (%d) different from number of pedestal files (%d)",nconfFiles,npedFiles));
190     delete fPedestals;
191     fPedestals = 0;
192     delete fConfig;
193     fConfig = 0;
194     return kFALSE;
195   }
196   
197   fConfigChanged = HasConfigChanged(*fConfig);
198   
199   return kTRUE;
200 }
201
202 //_____________________________________________________________________________
203 UInt_t 
204 AliMUONPedestalSubprocessor::Process(TMap* /*dcsAliasMap*/)
205 {
206   /// Store the pedestals into the CDB
207   
208   if (!fPedestals || !fConfig) 
209   {
210     // this is the only reason to fail for the moment : getting no pedestal or no config
211     // at all.
212     return 1;
213   }
214     
215   AliMUON2DStoreValidator validator;
216
217   Master()->Log("Validating");
218
219   TObjArray* chambers = validator.Validate(*fPedestals,fConfig);
220   
221   if (chambers)
222   {
223     // we hereby report what's missing, but this is not a reason to fail ;-)
224     // the only reason to fail would be if we got no pedestal at all
225     TList lines;
226     lines.SetOwner(kTRUE);
227   
228     validator.Report(lines,*chambers);
229   
230     TIter next(&lines);
231     TObjString* line;
232   
233     while ( ( line = static_cast<TObjString*>(next()) ) )
234     {
235       Master()->Log(line->String().Data());
236     }
237   }
238   
239   Master()->Log("Storing pedestals...");
240   if ( fConfigChanged ) 
241   {
242     Master()->Log("...and configuration, as it has changed");
243   }
244   
245   AliCDBMetaData metaData;
246         metaData.SetBeamPeriod(0);
247         metaData.SetResponsible("MUON TRK");
248   TString comment("Computed by AliMUONPedestalSubprocessor $Id$");
249   comment.ReplaceAll("$","");
250         metaData.SetComment(comment.Data());
251   
252   Bool_t validToInfinity = kTRUE;
253         Bool_t result = Master()->Store("Calib", "Pedestals", fPedestals, &metaData, 0, validToInfinity);
254   if ( fConfigChanged ) 
255   {
256     result = result && Master()->Store("Calib", "Config", fConfig, &metaData, 0, validToInfinity);
257   }
258   return ( result != kTRUE ); // return 0 if everything is ok.  
259 }
260
261 //_____________________________________________________________________________
262 Int_t
263 AliMUONPedestalSubprocessor::ReadPedestalFile(const char* filename)
264 {
265   /// Read the pedestals from an ASCII file.                                  \n
266   /// Format of that file is one line per channel :                           \n
267   ///-------------------------------------------------------------------------\n
268   /// BUS_PATCH MANU_ADDR CHANNEL      MEAN       SIGMA                       \n
269   ///-------------------------------------------------------------------------\n
270   ///                                                                         \n
271   /// Return kFALSE if reading was not successfull.                           \n
272   ///
273   
274   TString sFilename(gSystem->ExpandPathName(filename));
275   
276   Master()->Log(Form("Reading %s",sFilename.Data()));
277   
278   Int_t n = AliMUONTrackerIO::ReadPedestals(sFilename.Data(),*fPedestals);
279   
280   switch (n)
281   {
282     case -1:
283       Master()->Log(Form("Could not open %s",sFilename.Data()));
284       break;
285   }
286   
287   return n;
288 }
289
290 //_____________________________________________________________________________
291 Int_t
292 AliMUONPedestalSubprocessor::ReadConfigFile(const char* filename)
293 {
294   /// Read the configuration from an ASCII file.                              
295   /// Format of that file is one line per manu :                              
296   /// BUS_PATCH MANU_ADDR
297   /// Return kFALSE if reading was not successfull.                           
298   ///
299   
300   TString sFilename(gSystem->ExpandPathName(filename));
301   
302   Master()->Log(Form("Reading %s",sFilename.Data()));
303   
304   Int_t n = AliMUONTrackerIO::ReadConfig(sFilename.Data(),*fConfig);
305   
306   switch (n)
307   {
308     case -1:
309       Master()->Log(Form("Could not open %s",sFilename.Data()));
310       break;
311   }
312   
313   return n;
314 }
315
316
317 //_____________________________________________________________________________
318 void
319 AliMUONPedestalSubprocessor::Print(Option_t* opt) const
320 {
321   /// ouput to screen
322   if (fPedestals) fPedestals->Print("",opt);
323   if (fConfig) fConfig->Print("",opt);
324 }
325