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