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