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