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