Reading muon trigger scalers with the DA of the muon trigger and transfer
[u/mrichter/AliRoot.git] / MUON / MUONshuttle / AliMUONTriggerSubprocessor.cxx
CommitLineData
2ab3623b 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 "AliMUONTriggerSubprocessor.h"
19
20#include "AliCDBMetaData.h"
21#include "AliLog.h"
22#include "AliMUON1DArray.h"
92c23b09 23#include "AliMUONGlobalCrateConfig.h"
24#include "AliMUONRegionalTriggerConfig.h"
2ab3623b 25#include "AliMUONCalibParamNI.h"
26#include "AliMUONPreprocessor.h"
27#include "AliMUONTriggerIO.h"
28#include "AliMUONTriggerLut.h"
29#include "AliMpConstants.h"
30#include <Riostream.h>
31#include <TList.h>
32#include <TObjString.h>
33#include <TSystem.h>
87f615d8 34#include <TClonesArray.h>
2ab3623b 35
36/// \class AliMUONTriggerSubprocessor
37///
38/// Implementation of AliMUONVSubprocessor for MUON trigger system
39///
40/// Reads masks and LUT online files to feed the OCDB
41///
42/// \author L. Aphecetche
43
cdd730d0 44using std::ifstream;
2ab3623b 45/// \cond CLASSIMP
46ClassImp(AliMUONTriggerSubprocessor)
47/// \endcond
48
49//_____________________________________________________________________________
50AliMUONTriggerSubprocessor::AliMUONTriggerSubprocessor(AliMUONPreprocessor* master)
51: AliMUONVSubprocessor(master,
52 "Triggers",
53 "Upload MUON Trigger masks and LUT to OCDB"),
92c23b09 54fRegionalConfig(0x0),
2ab3623b 55fLocalMasks(0x0),
92c23b09 56fGlobalConfig(0x0),
87f615d8 57fLUT(0x0),
58fTrigScalers(0x0)
2ab3623b 59{
60 /// default ctor
61}
62
63//_____________________________________________________________________________
64AliMUONTriggerSubprocessor::~AliMUONTriggerSubprocessor()
65{
66 /// dtor
92c23b09 67 delete fRegionalConfig;
2ab3623b 68 delete fLocalMasks;
92c23b09 69 delete fGlobalConfig;
2ab3623b 70 delete fLUT;
87f615d8 71 delete [] fTrigScalers;
2ab3623b 72}
73
74//_____________________________________________________________________________
75TString
76AliMUONTriggerSubprocessor::GetFileName(const char* fid) const
77{
78 /// Get filename for a given id
79
80 const Int_t kSystem = AliMUONPreprocessor::kDAQ;
81
82 TList* sources = Master()->GetFileSources(kSystem,fid);
83 if ( sources && sources->GetSize() == 1 )
84 {
85 return Master()->GetFile(kSystem,fid,static_cast<TObjString*>(sources->First())->GetName());
86 }
87 return "";
88}
89
90//_____________________________________________________________________________
6c870207 91Bool_t
2ab3623b 92AliMUONTriggerSubprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
93{
94 /// When starting a new run, reads in the trigger online files.
95
ca6cee23 96 // First thing to do (after cleanup, that is), is to check whether the DA
97 // was alive or not. If it was, it should have put the "MtgCurrent.dat" file
98 // on the FXS.
99 //
100
101 Bool_t da(kTRUE);
102
c2d0667a 103 TString exportedFiles(gSystem->ExpandPathName(GetFileName("EXPORTED").Data()));
ca6cee23 104
c2d0667a 105 if ( exportedFiles == "" )
ca6cee23 106 {
c2d0667a 107 Master()->Log("exported files not specified !");
ca6cee23 108 da = kFALSE;
109 }
110
c2d0667a 111 if ( gSystem->AccessPathName(exportedFiles.Data(),kFileExists) ) // mind the strange return value convention of that method !
ca6cee23 112 {
c2d0667a 113 Master()->Log(Form("%s is not there !",exportedFiles.Data()));
ca6cee23 114 da = kFALSE;
115 }
116
117 if (!da)
118 {
119 Master()->Log("FATAL ERROR : DA does not seem to have been run !!!");
120 Master()->Invalidate();
6c870207 121 return kFALSE;
ca6cee23 122 }
123
c2d0667a 124 // OK. We have an exportedFiles.dat file at hand.
125 // Read it to know what other files should be there.
126
127 Bool_t regionalFile(kFALSE);
128 Bool_t globalFile(kFALSE);
129 Bool_t localFile(kFALSE);
130 Bool_t lutFile(kFALSE);
87f615d8 131 Bool_t trigScalFile(kFALSE);
c2d0667a 132
133 WhichFilesToRead(GetFileName("EXPORTED").Data(),
87f615d8 134 globalFile,regionalFile,localFile,lutFile,trigScalFile);
c2d0667a 135
87f615d8 136 if ((globalFile+regionalFile+localFile+lutFile+trigScalFile) == 0) {
04ad734f 137 Master()->Log("No file(s) to be processed for this run. Exiting.");
6c870207 138 return kTRUE;
04ad734f 139 }
140
92c23b09 141 delete fRegionalConfig; fRegionalConfig = 0x0;
ca6cee23 142 delete fLocalMasks; fLocalMasks = 0x0;
92c23b09 143 delete fGlobalConfig; fGlobalConfig = 0x0;
ca6cee23 144 delete fLUT; fLUT = 0x0;
87f615d8 145 delete fTrigScalers; fTrigScalers = 0x0;
ca6cee23 146
1130977f 147 Master()->Log(Form("Reading trigger masks for Run %d startTime %u endTime %u",
2ab3623b 148 run,startTime,endTime));
149
c2d0667a 150 Int_t check =
151 TestFile("REGIONAL",regionalFile) +
152 TestFile("LOCAL",localFile) +
153 TestFile("GLOBAL",globalFile) +
87f615d8 154 TestFile("LUT",lutFile) +
155 TestFile("TRIGSCAL",trigScalFile);
c2d0667a 156
157 if ( check )
158 {
9ee1d6ff 159 Master()->Log("Could not read some input file(s). Exiting");
c2d0667a 160 Master()->Invalidate();
6c870207 161 return kFALSE;
c2d0667a 162 }
163
5a26af27 164 if ( regionalFile ) globalFile = kTRUE;
165
92c23b09 166 if ( regionalFile ) fRegionalConfig = new AliMUONRegionalTriggerConfig();
c2d0667a 167 if ( localFile ) fLocalMasks = new AliMUON1DArray(AliMpConstants::TotalNofLocalBoards()+1);
92c23b09 168 if ( globalFile ) fGlobalConfig = new AliMUONGlobalCrateConfig();
2ab3623b 169
170 AliMUONTriggerIO tio;
171
11ee2085 172 Bool_t ok = tio.ReadConfig(localFile ? GetFileName("LOCAL").Data() : "",
173 regionalFile ? GetFileName("REGIONAL").Data() : "",
174 globalFile ? GetFileName("GLOBAL").Data() : "",
5cf57a25 175 fLocalMasks,fRegionalConfig,fGlobalConfig);
890cc210 176
177 if (!ok)
178 {
5cf57a25 179 Master()->Log("ERROR : ReadConfig failed");
890cc210 180 delete fLocalMasks;
92c23b09 181 delete fRegionalConfig;
182 delete fGlobalConfig;
890cc210 183 fLocalMasks = 0x0;
92c23b09 184 fRegionalConfig = 0x0;
185 fGlobalConfig = 0x0;
890cc210 186 }
2ab3623b 187
c2d0667a 188 if ( lutFile )
189 {
190 fLUT = new AliMUONTriggerLut;
2ab3623b 191
1130977f 192 Master()->Log(Form("Reading trigger LUT for Run %d startTime %u endTime %u",
c2d0667a 193 run,startTime,endTime));
890cc210 194
c2d0667a 195 ok = tio.ReadLUT(GetFileName("LUT").Data(),*fLUT);
890cc210 196
c2d0667a 197 if (!ok)
198 {
199 Master()->Log("ERROR : ReadLUT failed");
200 delete fLUT;
201 fLUT = 0x0;
202 }
890cc210 203 }
87f615d8 204
205 if ( trigScalFile ) {
206 fTrigScalers = new TClonesArray("AliMUONTriggerScalers",10);
207 ok = tio.ReadTrigScalers(GetFileName("TRIGSCAL").Data(),*fTrigScalers);
208 if (!ok)
209 {
210 Master()->Log("ERROR : ReadTrigScalers failed");
211 delete fTrigScalers;
212 fTrigScalers = 0x0;
213 }
214 }
215
6c870207 216 return kTRUE;
87f615d8 217
2ab3623b 218}
219
220//_____________________________________________________________________________
221UInt_t
222AliMUONTriggerSubprocessor::Process(TMap* /*dcsAliasMap*/)
223{
224 /// Store the trigger masks into the CDB
225
92c23b09 226 if ( !fGlobalConfig && !fRegionalConfig && !fLocalMasks && !fLUT )
2ab3623b 227 {
228 // nothing to do
229 return 0;
230 }
231
d08fea67 232 Master()->Log(Form("N global = %d N regional = %d N local %d N lut %d",
92c23b09 233 (fGlobalConfig ? 1 : 0 ),
234 (fRegionalConfig ? fRegionalConfig->GetNofTriggerCrates() : 0 ),
d08fea67 235 (fLocalMasks ? fLocalMasks->GetSize() : 0 ),
236 (fLUT ? 1 : 0)));
2ab3623b 237
238 AliCDBMetaData metaData;
239 metaData.SetBeamPeriod(0);
240 metaData.SetResponsible("MUON TRG");
241 metaData.SetComment("Computed by AliMUONTriggerSubprocessor $Id$");
242
243 Bool_t validToInfinity = kTRUE;
244
87f615d8 245 Bool_t result1(kTRUE);
2ab3623b 246 Bool_t result2(kTRUE);
247 Bool_t result3(kTRUE);
248 Bool_t result4(kTRUE);
87f615d8 249 Bool_t result5(kTRUE);
250
92c23b09 251 if ( fGlobalConfig )
2ab3623b 252 {
ea6d7574 253 result1 = Master()->Store("Calib", "GlobalTriggerCrateConfig", fGlobalConfig,
2ab3623b 254 &metaData, 0, validToInfinity);
255 }
256
92c23b09 257 if ( fRegionalConfig && fRegionalConfig->GetNofTriggerCrates() > 0 )
2ab3623b 258 {
ea6d7574 259 result2 = Master()->Store("Calib", "RegionalTriggerConfig", fRegionalConfig,
2ab3623b 260 &metaData, 0, validToInfinity);
261 }
262
263 if ( fLocalMasks && fLocalMasks->GetSize() > 0 )
264 {
265 result3 = Master()->Store("Calib", "LocalTriggerBoardMasks", fLocalMasks,
266 &metaData, 0, validToInfinity);
267 }
268
269 if ( fLUT )
270 {
271 result4 = Master()->Store("Calib", "TriggerLut", fLUT,
272 &metaData, 0, validToInfinity);
273 }
87f615d8 274
275 if ( fTrigScalers )
276 {
277 result5 = Master()->Store("Calib","TriggerScalers", fTrigScalers,
278 &metaData, 0, validToInfinity);
279 }
280
281 return ( result1 != kTRUE || result2 != kTRUE || result3 != kTRUE || result4 != kTRUE || result5 != kTRUE); // return 0 if everything is ok.
2ab3623b 282}
c2d0667a 283
284//_____________________________________________________________________________
285Int_t
286AliMUONTriggerSubprocessor::TestFile(const char* baseName, Bool_t shouldBeThere) const
287{
288 /// Check if required file can be accessed
289 if (!shouldBeThere) return 0; // all is ok
290
291 TString fileName(gSystem->ExpandPathName(GetFileName(baseName).Data()));
292
293 if ( gSystem->AccessPathName(fileName.Data(),kFileExists) )
294 {
295 // mind the strange return value convention of that method !
296 Master()->Log(Form("File %s does not exist",fileName.Data()));
297 return 1; // this is an error
298 }
299 return 0; // all is ok.
300}
301
302//_____________________________________________________________________________
303void
304AliMUONTriggerSubprocessor::WhichFilesToRead(const char* exportedFiles,
305 Bool_t& globalFile,
306 Bool_t& regionalFile,
307 Bool_t& localFile,
87f615d8 308 Bool_t& lutFile,
309 Bool_t& trigScalFile) const
c2d0667a 310{
311 /// From the exportedFiles file, determine which other files will need
312 /// to be read in
313 ifstream in(gSystem->ExpandPathName(exportedFiles));
314
315 globalFile = kFALSE;
316 regionalFile = kFALSE;
317 localFile = kFALSE;
318 lutFile = kFALSE;
87f615d8 319 trigScalFile = kFALSE;
c2d0667a 320
321 char line[1024];
322
323 while ( in.getline(line,1024,'\n') )
324 {
325 TString sline(line);
326 sline.ToUpper();
327
328 if ( sline.Contains("REGIONAL") ) regionalFile = kTRUE;
329 if ( sline.Contains("LUT") ) lutFile = kTRUE;
330 if ( sline.Contains("LOCAL") && sline.Contains("MASK") ) localFile = kTRUE;
331 if ( sline.Contains("GLOBAL") ) globalFile = kTRUE;
87f615d8 332 if ( sline.Contains("TRIGSCAL") ) trigScalFile = kTRUE;
c2d0667a 333 }
334
87f615d8 335 if ( regionalFile || localFile || globalFile || lutFile || trigScalFile)
c2d0667a 336 {
337 Master()->Log(Form("Will have to read the following file types:"));
338 if ( globalFile) Master()->Log("GLOBAL");
339 if ( regionalFile ) Master()->Log("REGIONAL");
340 if ( localFile) Master()->Log("LOCAL");
341 if ( lutFile ) Master()->Log("LUT");
87f615d8 342 if ( trigScalFile ) Master()->Log("TRIGSCAL");
c2d0667a 343 }
344}
345