DA: Put back the DA rpm description removed by the Doxygen documentation
[u/mrichter/AliRoot.git] / TPC / DA / TPCPEDESTALda.cxx
CommitLineData
65eea642 1/*
2TPC DA for online calibration
3
4Contact: Haavard.Helstrup@cern.ch
5Link:
6Run Type: PEDESTAL
7DA Type: LDC
8Number of events needed: 100
9Input Files:
10Output Files: tpcPedestal.root, to be exported to the DAQ FXS
11fileId: pedestals
12Trigger types used: CALIBRATION_EVENT
13*/
17ad8c53 14/// \file TPCPEDESTALda.cxx
15///
16/// TPC DA for online calibration
17///
18/// Contact: Haavard.Helstrup@cern.ch
19/// Link:
20/// Run Type: PEDESTAL
21/// DA Type: LDC
22/// Number of events needed: 100
23/// Input Files:
24/// Output Files: tpcPedestal.root, to be exported to the DAQ FXS
25/// fileId: pedestals
26/// Trigger types used: CALIBRATION_EVENT
27///
28/// TPCda_pedestal.cxx - calibration algorithm for TPC pedestal runs
29///
30/// 10/06/2007 sylvain.chapeland@cern.ch : first version - clean skeleton based on DAQ DA case1
31/// 19/10/2007 christian.lippmann@cern.ch : Possibility to write output to ASCII file
32/// 24/10/2007 christian.lippmann@cern.ch : Including pedestal calibration for time bins
33/// 23/11/2007 christian.lippmann@cern.ch : Fix in order to avoid streamer problems in case of
34/// invalid ROOTSTYS. The famous magic line provided by Rene.
35/// 28/11/2007 christian.lippmann@cern.ch : TPC mapping file is read from DaqDetDB
36/// 18/09/2008 christian.lippmann@cern.ch : Noisy channels are output to ASCII file. Use max noise in ALTRO.
37/// 19/09/2008 J.Wiechula@gsi.de: Added export of the calibration data to the AMORE data base.
38/// Added support for configuration files.
39/// 31/01/2011 Christian.Lippmann@cern.ch : Updates for changed setup at P2 with 2 LDCs per sector
40/// 26/09/2014 Christian.Lippmann@cern.ch : Updates for changed setup at P2 1 CRORC per LDC
41/// 26/09/2014 Jens.Wiechula@cern.ch : comment out obsolete AliTPCRawStream
42///
43/// \author marian.ivanov@cern.ch
44///
45/// This process reads RAW data from the files provided as command line arguments
46/// and save results in a file (named from RESULT_FILE define - see below).
c12208b8 47
49efac78 48#define RESULT_FILE "tpcPedestal.root"
bd955ed2 49#define FILE_ID "pedestals"
49efac78 50#define MAPPING_FILE "tpcMapping.root"
ac940b58 51#define CONFIG_FILE "TPCPEDESTALda.conf"
e9d80afd 52#define PED_FILE "tpcPedestals.data"
53#define NOISE_FILE "tpcNoise.data"
54#define PEDMEM_FILE "tpcPedestalMem.data"
55#define NOISY_FILE "tpcNoisyChannels.data"
56#define VERYNOISY_FILE "tpcVeryNoisyChannels.data"
57#define DEAD_FILE "tpcDeadChannels.data"
b401648b 58#define AliDebugLevel() -1
c12208b8 59
60extern "C" {
61#include <daqDA.h>
62}
63#include "event.h"
64#include "monitor.h"
49efac78 65
66#include "stdio.h"
67#include "stdlib.h"
68#include <fstream>
c12208b8 69
17ad8c53 70// Root includes
71
49efac78 72#include "TFile.h"
73#include "TArrayF.h"
74#include "TROOT.h"
75#include "TPluginManager.h"
ac940b58 76#include "TSystem.h"
77#include "TString.h"
78#include "TObjString.h"
79#include "TDatime.h"
c12208b8 80//
81//AliRoot includes
82//
83#include "AliRawReader.h"
84#include "AliRawReaderDate.h"
87d57858 85#include "AliTPCmapper.h"
a1b9c189 86//#include "AliTPCRawStreamV3.h"
c12208b8 87#include "AliTPCROC.h"
88#include "AliTPCCalROC.h"
89#include "AliTPCCalPad.h"
90#include "AliMathBase.h"
91#include "TTreeStream.h"
b401648b 92#include "AliLog.h"
ac940b58 93#include "AliTPCConfigDA.h"
94
95//
96//AMORE
97//
98#include <AmoreDA.h>
c12208b8 99
100//
101// TPC calibration algorithm includes
102//
103#include "AliTPCCalibPedestal.h"
104
bdf99a93 105
c12208b8 106int main(int argc, char **argv) {
17ad8c53 107 /// Main routine, TPC pedestal detector algorithm to be run on TPC LDC
108 /// Arguments: list of DATE raw data files
109 /// Main for TPC pedestal detector algorithm
110
111 // log start of process
ac940b58 112 printf("TPC DA started - %s\n",__FILE__);
113
c12208b8 114 if (argc<2) {
115 printf("Wrong number of arguments\n");
116 return -1;
117 }
e9d80afd 118
a1b9c189 119 /*
e9d80afd 120 TString daterolename(gSystem->Getenv("DATE_ROLE_NAME"));
121 if ( daterolename == "" ) {
122 printf("Error: Variable DATE_ROLE_NAME not defined! Exiting ...\n");
123 return -1;
124 }
125 bool inner;
126 if ( daterolename.EndsWith("-0") ) inner = true;
127 else if ( daterolename.EndsWith("-1") ) inner = false;
128 else {
129 printf("Error: Variable DATE_ROLE_NAME neither ends with -0 nor -1 (E.g. ldc-TPC-C12-1)! Exiting ...\n");
130 return -1;
131 }
a1b9c189 132 */
e9d80afd 133
a1b9c189 134 //AliLog::SetClassDebugLevel("AliTPCRawStreamV3",-5);
ac940b58 135 AliLog::SetClassDebugLevel("AliRawReaderDate",-5);
136 AliLog::SetClassDebugLevel("AliTPCAltroMapping",-5);
137 AliLog::SetModuleDebugLevel("RAW",-5);
c12208b8 138
49efac78 139 /* magic line */
140 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
e9d80afd 141 "*",
142 "TStreamerInfo",
143 "RIO",
144 "TStreamerInfo()");
c12208b8 145
c12208b8 146 /* declare monitoring program */
ac940b58 147 int i, status;
c12208b8 148 status=monitorDeclareMp( __FILE__ );
149 if (status!=0) {
150 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
151 return -1;
152 }
ac940b58 153
154 // variables
b401648b 155 AliTPCmapper *mapping = 0; // The TPC mapping
ac940b58 156 char localfile[255];
157 unsigned long32 runNb=0; // run number
158 // configuration options
159 Bool_t timeAnalysis = kTRUE;
160 Bool_t fastDecoding = kFALSE;
161
b401648b 162 if (!mapping){
163 /* copy locally the mapping file from daq detector config db */
ac940b58 164 sprintf(localfile,"./%s",MAPPING_FILE);
165 status = daqDA_DB_getFile(MAPPING_FILE,localfile);
b401648b 166 if (status) {
167 printf("Failed to get mapping file (%s) from DAQdetDB, status=%d\n", MAPPING_FILE, status);
ac940b58 168 return -1;
b401648b 169 }
49efac78 170
b401648b 171 /* open the mapping file and retrieve mapping object */
172 TFile *fileMapping = new TFile(MAPPING_FILE, "read");
173 mapping = (AliTPCmapper*) fileMapping->Get("tpcMapping");
49efac78 174 delete fileMapping;
b401648b 175 }
176
177 if (mapping == 0) {
178 printf("Failed to get mapping object from %s. ...\n", MAPPING_FILE);
ac940b58 179 return -1;
49efac78 180 } else {
181 printf("Got mapping object from %s\n", MAPPING_FILE);
182 }
183
ac940b58 184 //
185 // DA configuration from configuration file
186 //
187 // retrieve configuration file
188 sprintf(localfile,"./%s",CONFIG_FILE);
189 status = daqDA_DB_getFile(CONFIG_FILE,localfile);
190 if (status) {
191 printf("Failed to get configuration file (%s) from DAQdetDB, status=%d\n", CONFIG_FILE, status);
192 return -1;
193 }
194 AliTPCConfigDA config(CONFIG_FILE);
195 // check configuration
6b411ea4 196 Bool_t skipAmore=kFALSE;
197
ac940b58 198 if ( (Int_t)config.GetValue("NoTimeAnalysis") == 1 ) {
199 printf("WARNING: Time analysis was switched off in the configuration file!\n");
200 timeAnalysis=kFALSE;
201 }
6b411ea4 202
ac940b58 203 if ( (Int_t)config.GetValue("UseFastDecoder") == 1 ){
204 printf("Info: The fast decoder will be used for the processing.\n");
205 fastDecoding=kTRUE;
b401648b 206 }
6b411ea4 207
208 if ( config.GetConfigurationMap()->GetValue("SkipAmore") ) {
209 skipAmore=((TObjString*)config.GetConfigurationMap()->GetValue("SkipAmore"))->GetString().Atoi();
210 printf("TPCPEDESTALda: Skip Amore set in config\n");
211 }
212
213
ac940b58 214 // create calibration object
215 AliTPCCalibPedestal calibPedestal(config.GetConfigurationMap()); // pedestal and noise calibration
216 calibPedestal.SetAltroMapping(mapping->GetAltroMapping()); // Use altro mapping we got from daqDetDb
30255695 217 calibPedestal.SetTimeAnalysis(timeAnalysis); // pedestal(t) calibration
ac940b58 218
219 //===========================//
220 // loop over RAW data files //
221 //==========================//
c12208b8 222 int nevents=0;
bdf99a93 223 for ( i=1; i<argc; i++ ) {
ac940b58 224
c12208b8 225 /* define data source : this is argument i */
226 printf("Processing file %s\n", argv[i]);
227 status=monitorSetDataSource( argv[i] );
228 if (status!=0) {
49efac78 229 printf("monitorSetDataSource() failed. Error=%s. Exiting ...\n", monitorDecodeError(status));
c12208b8 230 return -1;
231 }
ac940b58 232
c12208b8 233 /* read until EOF */
bdf99a93 234 for ( ; ; ) {
c12208b8 235 struct eventHeaderStruct *event;
ac940b58 236
c12208b8 237 /* check shutdown condition */
238 if (daqDA_checkShutdown()) {break;}
ac940b58 239
c12208b8 240 /* get next event (blocking call until timeout) */
241 status=monitorGetEventDynamic((void **)&event);
242 if (status==MON_ERR_EOF) {
ac940b58 243 printf ("End of File %d (%s) detected\n", i, argv[i]);
244 break; /* end of monitoring file has been reached */
c12208b8 245 }
ac940b58 246
c12208b8 247 if (status!=0) {
ac940b58 248 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
249 break;
c12208b8 250 }
251
252 /* retry if got no event */
49efac78 253 if (event==NULL)
ac940b58 254 continue;
255
6e7d7dc4 256 /* skip start/end of run events */
257 if ( (event->eventType != physicsEvent) && (event->eventType != calibrationEvent) )
258 continue;
259
c12208b8 260 nevents++;
ac940b58 261 // get the run number
262 runNb = event->eventRunNb;
c12208b8 263 // Pedestal calibration
5312f439 264 calibPedestal.ProcessEvent(event);
c12208b8 265
266 /* free resources */
267 free(event);
268 }
269 }
270
49efac78 271 //
272 // Analyse pedestals and write them to rootfile
273 //
49efac78 274 calibPedestal.Analyse();
275 calibPedestal.AnalyseTime(nevents);
276 printf ("%d physics/calibration events processed.\n",nevents);
c12208b8 277
bdf99a93 278 TFile *fileTPC = new TFile(RESULT_FILE, "recreate");
e73181c9 279 calibPedestal.Write("tpcCalibPedestal");
c12208b8 280 delete fileTPC;
49efac78 281 printf("Wrote %s.\n",RESULT_FILE);
f2c72763 282
ac940b58 283 /* store the result file on FES */
284 status=daqDA_FES_storeFile(RESULT_FILE,FILE_ID);
285 if (status) {
286 status = -2;
287 }
288 //
289 //Send objects to the AMORE DB
290 //
6b411ea4 291 if (!skipAmore){
292 printf ("AMORE part\n");
293 const char *amoreDANameorig=gSystem->Getenv("AMORE_DA_NAME");
294 //cheet a little -- temporary solution (hopefully)
295 //
296 //currently amoreDA uses the environment variable AMORE_DA_NAME to create the mysql
297 //table in which the calib objects are stored. This table is dropped each time AmoreDA
298 //is initialised. This of course makes a problem if we would like to store different
299 //calibration entries in the AMORE DB. Therefore in each DA which writes to the AMORE DB
300 //the AMORE_DA_NAME env variable is overwritten.
301
302 //find processed sector
303 Char_t sideName='A';
304 Int_t sector = -1;
305 for ( Int_t roc = 0; roc < 72; roc++ ) {
306 if ( !calibPedestal.GetCalRocPedestal(roc) ) continue;
307 if (mapping->GetSideFromRoc(roc)==1) sideName='C';
308 sector = mapping->GetSectorFromRoc(roc);
309 }
310 // gSystem->Setenv("AMORE_DA_NAME",Form("TPC-%c%02d-%s",sideName,sector,FILE_ID));
311 gSystem->Setenv("AMORE_DA_NAME",Form("%s-%s",gSystem->Getenv("DATE_ROLE_NAME"),FILE_ID));
312
313 //
314 // end cheet
315 if (sector>-1){
316 TDatime time;
317 TObjString info(Form("Run: %u; Date: %s",runNb,time.AsSQLString()));
318
319 amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
320 Int_t statusDA=0;
321 statusDA+=amoreDA.Send("Pedestals",calibPedestal.GetCalPadPedestal());
322 statusDA+=amoreDA.Send("Noise",calibPedestal.GetCalPadRMS());
323 statusDA+=amoreDA.Send("Info",&info);
324 if ( statusDA )
325 printf("Warning: Failed to write one of the calib objects to the AMORE database\n");
326 } else {
327 printf("Warning: No data found!\n");
328 }
329 // reset env var
330 if (amoreDANameorig) gSystem->Setenv("AMORE_DA_NAME",amoreDANameorig);
ffcfd6fa 331 }
ffcfd6fa 332
bdf99a93 333 //
49efac78 334 // Now prepare ASCII files for local ALTRO configuration through DDL.
bdf99a93 335 //
bdf99a93 336 ofstream pedfile;
337 ofstream noisefile;
338 ofstream pedmemfile;
ac940b58 339 ofstream noisychannelfile;
e9d80afd 340 ofstream verynoisychannelfile;
341 ofstream deadchannelfile;
ac940b58 342
e9d80afd 343 pedfile.open(PED_FILE);
344 noisefile.open(NOISE_FILE);
345 pedmemfile.open(PEDMEM_FILE);
346 noisychannelfile.open(NOISY_FILE);
347 verynoisychannelfile.open(VERYNOISY_FILE);
348 deadchannelfile.open(DEAD_FILE);
bdf99a93 349
49efac78 350 TArrayF **timePed = calibPedestal.GetTimePedestals(); // pedestal values for each time bin
87d57858 351
bdf99a93 352 Int_t ctr_channel = 0;
ac940b58 353 Int_t ctr_altro = 0;
bdf99a93 354 Int_t ctr_pattern = 0;
ac940b58 355 Int_t ctr_noisy = 0;
e9d80afd 356 Int_t ctr_vnoisy = 0;
357 Int_t ctr_dead = 0;
358
359 pedfile << 10 << std::endl; // Mark file to contain PEDESTALS per channel
360 noisefile << 11 << std::endl; // Mark file to contain NOISE per altro
361 pedmemfile << 12 << std::endl; // Mark file to contain PEDESTALs per time bin
362 noisychannelfile << 14 << std::endl; // Mark file to contain NOISY or DEAD CHANNELS
363 verynoisychannelfile << 14 << std::endl; // Mark file to contain NOISY or DEAD CHANNELS
364 deadchannelfile << 14 << std::endl; // Mark file to contain NOISY or DEAD CHANNELS
365
366 // inner==true : calROC from ldc-0 contains: rcus 0,1 for IROC and rcu 2 for OROC
367 // inner==false: calROC from ldc-1 contains: nothing for IROC and rcus 3,4,5 for OROC
bdf99a93 368 for ( Int_t roc = 0; roc < 72; roc++ ) {
369 if ( !calibPedestal.GetCalRocPedestal(roc) ) continue;
e9d80afd 370 bool isIROC = mapping->IsIROC(roc);
49efac78 371 Int_t side = mapping->GetSideFromRoc(roc);
372 Int_t sector = mapping->GetSectorFromRoc(roc);
e9d80afd 373 Int_t minrcu, maxrcu;
374 if ( isIROC ) { minrcu=0; maxrcu=1; }
a1b9c189 375 //else if ( inner ) { minrcu=2; maxrcu=2; }
376 //else { minrcu=3; maxrcu=5; }
377 else { minrcu=2; maxrcu=5; }
e9d80afd 378 for ( int rcu = minrcu; rcu <= maxrcu; rcu++ ) {
379 //Int_t patch = mapping->IsIROC(roc) ? rcu : rcu+2;
bdf99a93 380 for ( int branch = 0; branch < 2; branch++ ) {
e9d80afd 381 for ( int fec = 0; fec < mapping->GetNfec(rcu, branch); fec++ ) {
ac940b58 382 for ( int altro = 0; altro < 8; altro++ ) {
383 Float_t rms = 0.;
384 Float_t maxrms = 0.;
385 Float_t ctr_altrochannel = 0.;
386 for ( int channel = 0; channel < 16; channel++ ) {
387 Int_t hwadd = mapping->CodeHWAddress(branch, fec, altro, channel);
e9d80afd 388 Int_t row = mapping->GetPadRow(rcu, hwadd); // row in a ROC
389 Int_t globalrow = mapping->GetGlobalPadRow(rcu, hwadd); // row in full sector
390 Int_t pad = mapping->GetPad(rcu, hwadd);
ac940b58 391 Float_t ped = calibPedestal.GetCalRocPedestal(roc)->GetValue(row,pad);
392 // fixed pedestal
e9d80afd 393 pedfile << ctr_channel++ << "\t" << side << "\t" << sector << "\t" << rcu << "\t"
394 << hwadd << "\t" << ped << std::endl;
395 // pedestal(t)=pedestal memories
ac940b58 396 if ( timePed && fabs(timePed[globalrow][pad].GetSum()) > 1e-10 ) {
e9d80afd 397 pedmemfile << ctr_pattern++ << "\t" << side << "\t" << sector << "\t" << rcu
ac940b58 398 << "\t" << hwadd;
399 for ( Int_t timebin = 0; timebin < 1024; timebin++ )
400 pedmemfile << "\t" << timePed[globalrow][pad].At(timebin);
401 pedmemfile << std::endl;
402 }
403 // rms=noise
404 Float_t rms2 = calibPedestal.GetCalRocRMS(roc)->GetValue(row,pad);
405 if ( fabs(ped) < 1.e-10 ) { // dead channel
e9d80afd 406 deadchannelfile << ctr_dead++ << "\t" << side << "\t" << sector << "\t"
407 << rcu << "\t" << hwadd << "\t" << rms2 << std::endl;
ac940b58 408 } else if ( (ped > 1.e-10) && (rms2 > 1.e-10) ) { // not dead
e9d80afd 409 // Find noisy channels
410 if ( rms2 > 6.0 ) { // VERY noisy
411 verynoisychannelfile << ctr_vnoisy++ << "\t" << side << "\t" << sector << "\t"
412 << rcu << "\t" << hwadd << "\t" << rms2 << std::endl;
413
414 } else if ( ((roc<36) && (rms2 > 2.0)) || // IROC
415 ((roc>35) && (row<65) && (rms2 > 2.0)) || // OROC, small pads
416 ((roc>35) && (row>64) && (rms2 > 3.0)) ) { // OROC, large pads (50% more signal)
ac940b58 417 noisychannelfile << ctr_noisy++ << "\t" << side << "\t" << sector << "\t"
e9d80afd 418 << rcu << "\t" << hwadd << "\t" << rms2 << std::endl;
419 } else {
420 // Not noisy. Get average and maximum noise in this ALTRO
421 rms += rms2;
422 ctr_altrochannel += 1.;
423 if (rms2 > maxrms) maxrms = rms2;
424 } // end if noisy
ac940b58 425 } // end if some signal
426 } // end channel for loop
427 Int_t hwadd = mapping->CodeHWAddress(branch, fec, altro, 0); // ALTRO address
e9d80afd 428 // Noise data (rms) averaged over all channels in this ALTRO.
ac940b58 429 if ( ctr_altrochannel > 1.e-10 ) {
e9d80afd 430 /*
431 // average noise of this ALTRO (excluding high-noise channels)
432 noisefile << ctr_altro << "\t" << side << "\t" << sector << "\t" << rcu << "\t"
ac940b58 433 << hwadd << "\t" << rms/ctr_altrochannel << std::endl;
e9d80afd 434 */
435 // maximum noise of this ALTRO (excluding high-noise channels)
436 noisefile << ctr_altro << "\t" << side << "\t" << sector << "\t" << rcu << "\t"
437 << hwadd << "\t" << maxrms << std::endl;
ac940b58 438 ctr_altro++;
439 }
440 } // end altro for loop
441 } // end fec for loop
bdf99a93 442 } // end branch for loop
443 } // end rcu for loop
444 } // end roc loop
ac940b58 445
bdf99a93 446 pedfile.close();
447 noisefile.close();
448 pedmemfile.close();
ac940b58 449 noisychannelfile.close();
e9d80afd 450 verynoisychannelfile.close();
451 deadchannelfile.close();
452 printf("Wrote ASCII files. Found %d noisy, %d very noisy and %d dead channels.\n", ctr_noisy, ctr_vnoisy, ctr_dead);
87d57858 453
c12208b8 454 return status;
ac940b58 455
c12208b8 456}