New MUON Tracking DA splitted in two programs:
[u/mrichter/AliRoot.git] / MUON / MUONTRKPEDda.cxx
CommitLineData
dc928fbf 1/*
2Contact: Jean-Luc Charvet <jean-luc.charvet@cea.fr>
3Link: http://aliceinfo.cern.ch/static/Offline/dimuon/muon_html/README_Mchda
4Run Type: PEDESTAL
5 DA Type: LDC
6 Number of events needed: 400 events for pedestal run
7 Input Files: Rawdata file (DATE format)
8 Output Files: local dir (not persistent) -> MUONTRKPEDda_<run#>.ped
9 FXS -> run<#>_MCH_<ldc>_PEDESTALS
10 Trigger types used:
11*/
12
13/**************************************************************************
14 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
15 * *
16 * Author: The ALICE Off-line Project. *
17 * Contributors are mentioned in the code where appropriate. *
18 * *
19 * Permission to use, copy, modify and distribute this software and its *
20 * documentation strictly for non-commercial purposes is hereby granted *
21 * without fee, provided that the above copyright notice appears in all *
22 * copies and that both the copyright notice and this permission notice *
23 * appear in the supporting documentation. The authors make no claims *
24 * about the suitability of this software for any purpose. It is *
25 * provided "as is" without express or implied warranty. *
26 **************************************************************************/
27
28/* $Id$ */
29
30/*
31 -------------------------------------------------------------------------
32 2009-05-11 New version: MUONTRKPEDda.cxx,v 1.0
33 -------------------------------------------------------------------------
34
35 Version for MUONTRKPEDda MUON tracking
36 (A. Baldisseri, J.-L. Charvet)
37
38
39 Rem: AliMUON2DMap stores all channels, even those which are not connected
40 if pedMean == -1, channel not connected to a pad
41
42
43*/
44extern "C" {
45#include <daqDA.h>
46}
47
48#include "event.h"
49#include "monitor.h"
50
51#include <Riostream.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <sstream>
55#include <math.h>
56
57//AliRoot
58#include "AliMUONRawStreamTrackerHP.h"
59#include "AliRawReader.h"
60#include "AliMUONVStore.h"
61#include "AliMUON2DMap.h"
62#include "AliMUONCalibParamND.h"
63#include "AliMpIntPair.h"
64#include "AliMpConstants.h"
65#include "AliRawDataErrorLog.h"
66#include "AliMUONTrackerIO.h"
67
68//ROOT
69#include "TFile.h"
70#include "TSystem.h"
71#include "TTree.h"
72#include "TH1F.h"
73#include "TString.h"
74#include "TStopwatch.h"
75#include "TMath.h"
76#include "TTimeStamp.h"
77#include "TGraphErrors.h"
78#include "TF1.h"
79#include "TROOT.h"
80#include "TPluginManager.h"
81#include "TFitter.h"
82#include "TObjString.h"
83#include "THashTable.h"
84#include <THashList.h>
85//
86//AMORE
87//
88#ifdef ALI_AMORE
89#include <AmoreDA.h>
90#endif
91
92#include "AliMUONPedestal.h"
93#include "AliMUONErrorCounter.h"
94
95
96// main routine
97int main(Int_t argc, Char_t **argv)
98{
99
100 Int_t status=0;
101 TStopwatch timers;
102 timers.Start(kTRUE);
103
104 // needed for streamer application
105 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
106 "*",
107 "TStreamerInfo",
108 "RIO",
109 "TStreamerInfo()");
110
111 // needed for Minuit plugin
112 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
113 "Minuit",
114 "TMinuitMinimizer",
115 "Minuit",
116 "TMinuitMinimizer(const char*)");
117
118 Char_t prefixDA[256]="MUONTRKPEDda"; // program prefix
119// cout << argv[0];
120
121 Int_t skipEvents = 0;
122 Int_t maxEvents = 1000000;
123 Int_t maxDateEvents = 1000000;
124 Char_t inputFile[256]="";
125
126 Int_t nDateEvents = 0;
127 Int_t nGlitchErrors= 0;
128 Int_t nParityErrors= 0;
129 Int_t nPaddingErrors= 0;
130 Int_t recoverParityErrors = 1;
131
132 TString logOutputFile;
133
134 Char_t flatFile[256]="";
135 TString shuttleFile;
136
137 Int_t nEventsRecovered = 0;
138 Int_t nEvents = 0;
139 UInt_t runNumber = 0;
140 Int_t nChannel = 0;
141 ofstream filcout;
142// Int_t nIndex = -1;
143
144 // decode the input line
145 for (Int_t i = 1; i < argc; i++) // argument 0 is the executable name
146 {
147 Char_t* arg;
148
149 arg = argv[i];
150 if (arg[0] != '-')
151 {
152 // If only one argument and no "-" => DA calling from ECS
153 if (argc == 2)
154 {
155 sprintf(inputFile,argv[i]);
156 }
157 continue;
158 }
159 switch (arg[1])
160 {
161 case 'f' :
162 i++;
163 sprintf(inputFile,argv[i]);
164 break;
165 case 'a' :
166 i++;
167 shuttleFile = argv[i];
168 break;
169 case 's' :
170 i++;
171 skipEvents=atoi(argv[i]);
172 break;
173 case 'm' :
174 i++;
175 sscanf(argv[i],"%d",&maxDateEvents);
176 break;
177 case 'n' :
178 i++;
179 sscanf(argv[i],"%d",&maxEvents);
180 break;
181 case 'p' :
182 i++;
183 sscanf(argv[i],"%d",&recoverParityErrors);
184 break;
185 case 'h' :
186 i++;
187 printf("\n******************* %s usage **********************",argv[0]);
188 printf("\nOnline (called from ECS) : %s <raw data file> (no inline options)\n",argv[0]);
189 printf("\n%s -options, the available options are :",argv[0]);
190 printf("\n-h help (this screen)");
191 printf("\n");
192 printf("\n Input");
193 printf("\n-f <raw data file> (default = %s)",inputFile);
194 printf("\n");
195 printf("\n Output");
196 printf("\n-a <Flat ASCII file> (default = %s)",shuttleFile.Data());
197 printf("\n");
198 printf("\n Options");
199 printf("\n-m <max date events> (default = %d)",maxDateEvents);
200 printf("\n-s <skip events> (default = %d)",skipEvents);
201 printf("\n-n <max events> (default = %d)",maxEvents);
202 printf("\n-p <Recover parity errors> (default = %d)",recoverParityErrors);
203
204 printf("\n\n");
205 exit(-1);
206 default :
207 printf("%s : bad argument %s (please check %s -h)\n",argv[0],argv[i],argv[0]);
208 argc = 2; exit(-1); // exit if error
209 } // end of switch
210 } // end of for i
211
212 // decoding the events
213
214 UShort_t manuId;
215 UChar_t channelId;
216 UShort_t charge;
217
218 //Pedestal object
219 AliMUONPedestal* muonPedestal = new AliMUONPedestal();
220 muonPedestal->SetprefixDA(prefixDA);
221
222 // Rawdeader, RawStreamHP
223 AliRawReader* rawReader = AliRawReader::Create(inputFile);
224 AliMUONRawStreamTrackerHP* rawStream = new AliMUONRawStreamTrackerHP(rawReader);
225 rawStream->DisableWarnings();
226 rawStream->EnabbleErrorLogger();
227
228 cout << "\n" << prefixDA << " : Reading data from file " << inputFile << endl;
229
230 while (rawReader->NextEvent())
231 {
232 if (nDateEvents >= maxDateEvents) break;
233 if (nEvents >= maxEvents) break;
234 if (nDateEvents>0 && nDateEvents % 100 == 0)
235 cout<<"Cumulated: DATE events = " << nDateEvents << " Used events = " << nEvents << endl;
236
237 // check shutdown condition
238 if (daqDA_checkShutdown())
239 break;
240
241 //Skip events
242 while (skipEvents)
243 {
244 rawReader->NextEvent();
245 skipEvents--;
246 }
247
248 Int_t eventType = rawReader->GetType();
249 runNumber = rawReader->GetRunNumber();
250
251 // Output log file initialisations
252 if(nDateEvents==0)
253 {
254 sprintf(flatFile,"%s_%d.log",prefixDA,runNumber);
255 logOutputFile=flatFile;
256
257 filcout.open(logOutputFile.Data());
258 filcout<<"//=================================================" << endl;
259 filcout<<"// " << prefixDA << " for run = " << runNumber << endl;
260 filcout<<"//=================================================" << endl;
261 filcout<<"// * Date : " << muonPedestal->GetDate()->AsString("l") << "\n" << endl;
262 cout<<"\n ******** " << prefixDA << " for run = " << runNumber << " ********\n" << endl;
263 cout<<" * Date : " << muonPedestal->GetDate()->AsString("l") << "\n" << endl;
264
265 }
266
267 muonPedestal->SetAlifilcout(&filcout);
268
269 nDateEvents++;
270 if (eventType != PHYSICS_EVENT)
271 continue; // for the moment
272
273 // First lopp over DDL's to find good events
274 // Error counters per event (counters in the decoding lib are for each DDL)
275 Bool_t eventIsErrorMessage = kFALSE;
276 int eventGlitchErrors = 0;
277 int eventParityErrors = 0;
278 int eventPaddingErrors = 0;
279 rawStream->First();
280 do
281 {
282 if (rawStream->IsErrorMessage()) eventIsErrorMessage = kTRUE;
283 eventGlitchErrors += rawStream->GetGlitchErrors();
284 eventParityErrors += rawStream->GetParityErrors();
285 eventPaddingErrors += rawStream->GetPaddingErrors();
286 } while(rawStream->NextDDL());
287
288 AliMUONRawStreamTrackerHP::AliBusPatch* busPatch;
289 if (!eventIsErrorMessage)
290 {
291 // Good events (no error) -> compute pedestal for all channels
292 rawStream->First();
293 while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next()))
294 {
295 for(int i = 0; i < busPatch->GetLength(); ++i)
296 {
297 if (nEvents == 0) nChannel++;
298 busPatch->GetData(i, manuId, channelId, charge);
299 muonPedestal->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
300 }
301 }
302 nEvents++;
303 }
304 else
305 {
306 // Events with errors
307 if (recoverParityErrors && eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
308 {
309 // Recover parity errors -> compute pedestal for all good buspatches
310 if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
311 ATTR_ORBIT_BC ))
312 {
313 filcout <<"Event recovered -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
314 <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
315 <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;
316 }
317 else
318 {
319 filcout <<"Event recovered -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
320 <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
321 <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
322 }
323 rawStream->First();
324 while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next()))
325 {
326 // Check the buspatch -> if error not use it in the pedestal calculation
327 int errorCount = 0;
328 for(int i = 0; i < busPatch->GetLength(); ++i)
329 {
330 if (!busPatch->IsParityOk(i)) errorCount++;
331 }
332 if (!errorCount)
333 {
334 // Good buspatch
335 for(int i = 0; i < busPatch->GetLength(); ++i)
336 {
337 if (nEvents == 0) nChannel++;
338 busPatch->GetData(i, manuId, channelId, charge);
339 // if (busPatch->GetBusPatchId()==1719 && manuId == 1 && channelId == 0) cout <<"Recovered charge "<<charge<<endl;
340 muonPedestal->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
341 }
342 }
343 else
344 {
345 char bpname[256];
346 AliMUONErrorCounter* errorCounter;
347 // Bad buspatch -> not used (just print)
348 filcout<<"bpId "<<busPatch->GetBusPatchId()<<" words "<<busPatch->GetLength()
349 <<" parity errors "<<errorCount<<endl;
350 // Number of events where this buspatch is missing
351 sprintf(bpname,"bp%d",busPatch->GetBusPatchId());
352 if (!(errorCounter = (AliMUONErrorCounter*) (muonPedestal->GetErrorBuspatchTable()->FindObject(bpname))))
353 {
354 // New buspatch
355 errorCounter = new AliMUONErrorCounter(busPatch->GetBusPatchId());
356 errorCounter->SetName(bpname);
357 muonPedestal->GetErrorBuspatchTable()->Add(errorCounter);
358 }
359 else
360 {
361 // Existing buspatch
362 errorCounter->Increment();
363 }
364 // errorCounter->Print();
365 } // end of if (!errorCount)
366 } // end of while( (busPatch = (AliMUONRawStreamTrackerHP ...
367 nEvents++;
368 nEventsRecovered++;
369 } //end of if (recoverParityErrors && eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
370 else
371 {
372 // Fatal errors reject the event
373 if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
374 ATTR_ORBIT_BC ))
375 {
376 filcout <<"Event rejected -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
377 <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
378 <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;
379 }
380 else
381 {
382 filcout <<"Event rejected -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
383 <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
384 <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
385
386 }
387 } // end of if (!rawStream->GetGlitchErrors() && !rawStream->GetPaddingErrors() ...
388 filcout<<"Number of errors : Glitch "<<eventGlitchErrors
389 <<" Parity "<<eventParityErrors
390 <<" Padding "<<eventPaddingErrors<<endl;
391 filcout<<endl;
392 } // end of if (!rawStream->IsErrorMessage())
393
394 if (eventGlitchErrors) nGlitchErrors++;
395 if (eventParityErrors) nParityErrors++;
396 if (eventPaddingErrors) nPaddingErrors++;
397
398 } // while (rawReader->NextEvent())
399 delete rawReader;
400 delete rawStream;
401
402 sprintf(flatFile,"%s_%d.ped",prefixDA,runNumber);
403 if(shuttleFile.IsNull())shuttleFile=flatFile;
404 muonPedestal->SetAliNEvents(nEvents);
405 muonPedestal->SetAliRunNumber(runNumber);
406 muonPedestal->SetAliNChannel(nChannel);
407 muonPedestal->MakePedStore(shuttleFile);
408
409 // writing some counters
410 cout << endl;
411 cout << prefixDA << " : Nb of DATE events = " << nDateEvents << endl;
412 cout << prefixDA << " : Nb of Glitch errors = " << nGlitchErrors << endl;
413 cout << prefixDA << " : Nb of Parity errors = " << nParityErrors << endl;
414 cout << prefixDA << " : Nb of Padding errors = " << nPaddingErrors << endl;
415 cout << prefixDA << " : Nb of events recovered = " << nEventsRecovered<< endl;
416 cout << prefixDA << " : Nb of events without errors = " << nEvents-nEventsRecovered<< endl;
417 cout << prefixDA << " : Nb of events used = " << nEvents << endl;
418
419 filcout << endl;
420 filcout << prefixDA << " : Nb of DATE events = " << nDateEvents << endl;
421 filcout << prefixDA << " : Nb of Glitch errors = " << nGlitchErrors << endl;
422 filcout << prefixDA << " : Nb of Parity errors = " << nParityErrors << endl;
423 filcout << prefixDA << " : Nb of Padding errors = " << nPaddingErrors << endl;
424 filcout << prefixDA << " : Nb of events recovered = " << nEventsRecovered<< endl;
425 filcout << prefixDA << " : Nb of events without errors = " << nEvents-nEventsRecovered<< endl;
426 filcout << prefixDA << " : Nb of events used = " << nEvents << endl;
427
428
429 // Copying files to local DB folder defined by DAQ_DETDB_LOCAL
430 Char_t *dir;
431 dir= getenv("DAQ_DETDB_LOCAL");
432 unsigned int nLastVersions = 2;
433 cout << "\n *** Output files stored locally in " << dir << " (nb of previous versions = " << nLastVersions << ") ***" << endl;
434 filcout << "\n *** Output files stored locally in " << dir << " (nb of previous versions = " << nLastVersions << ") ***" << endl;
435
436 status = daqDA_localDB_storeFile(logOutputFile.Data(),nLastVersions);
437 status = daqDA_localDB_storeFile(muonPedestal->GetHistoFileName(),nLastVersions);
438 status = daqDA_localDB_storeFile(shuttleFile.Data(),nLastVersions);
439
440 // ouput files
441 cout << endl;
442 cout << prefixDA << " : Output logfile : " << logOutputFile << endl;
443 cout << prefixDA << " : Gain Histo file : " << muonPedestal->GetHistoFileName() << endl;
444 cout << prefixDA << " : Gain file (to SHUTTLE) : " << shuttleFile << endl;
445
446 cout << endl;
447 filcout << prefixDA << " : Output logfile : " << logOutputFile << endl;
448 filcout << prefixDA << " : Gain Histo file : " << muonPedestal->GetHistoFileName() << endl;
449 filcout << prefixDA << " : Gain file (to SHUTTLE) : " << shuttleFile << endl;
450
451
452 // Transferring to OCDB via the SHUTTLE
453 printf("\n ***** STORE FILE in FES ****** \n");
454
455 // be sure that env variable DAQDALIB_PATH is set in script file
456 // gSystem->Setenv("DAQDALIB_PATH", "$DATE_SITE/infoLogger");
457
458 status = daqDA_FES_storeFile(shuttleFile.Data(),"PEDESTALS");
459 if (status)
460 {
461 printf(" Failed to export file : %d\n",status);
462 }
463 else printf(" %s successfully exported to FES \n",shuttleFile.Data());
464
465 filcout.close();
466 timers.Stop();
467 printf("\nExecution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());
468 return status;
469}