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