AliRoot Core  3dc7879 (3dc7879)
TPCPULSERda.cxx
Go to the documentation of this file.
1 /*
2 TPC DA for online calibration
3 
4 Contact: Haavard.Helstrup@cern.ch
5 Link:
6 Run Type: CALIBRATION_PULSER
7 DA Type: LDC
8 Number of events needed: 100
9 Input Files:
10 Output Files: tpcPulser.root, to be exported to the DAQ FXS
11 fileId: pulser
12 Trigger types used: CALIBRATION_EVENT
13 */
41 
42 #define RESULT_FILE "tpcPulser.root"
43 #define FILE_ID "pulser"
44 #define MAPPING_FILE "tpcMapping.root"
45 #define CONFIG_FILE "TPCPULSERda.conf"
46 #define Q_FILE "tpcPulserQ.data"
47 #define DEAD_FILE "tpcDeadChannelsPulser.data"
48 #define AliDebugLevel() -1
49 
50 
51 #include <daqDA.h>
52 #include "event.h"
53 #include "monitor.h"
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <fstream>
57 
58 // Root includes
59 
60 #include <TFile.h>
61 #include "TROOT.h"
62 #include "TPluginManager.h"
63 #include "TString.h"
64 #include "TObjString.h"
65 #include "TDatime.h"
66 //
67 //AliRoot includes
68 //
69 #include "AliRawReader.h"
70 #include "AliRawReaderDate.h"
71 #include "AliTPCmapper.h"
72 //#include "AliTPCRawStreamV3.h"
73 #include "AliTPCROC.h"
74 #include "AliTPCCalROC.h"
75 #include "AliTPCCalPad.h"
76 #include "AliMathBase.h"
77 #include "TTreeStream.h"
78 #include "AliLog.h"
79 #include "TSystem.h"
80 #include "AliTPCConfigDA.h"
81 //
82 //AMORE
83 //
84 #include <AmoreDA.h>
85 //
86 // TPC calibration algorithm includes
87 //
88 #include "AliTPCCalibPulser.h"
89 
90 int main(int argc, char **argv) {
93 
94  // log start of process
95  printf("TPC Pulser DA started - %s\n",__FILE__);
96 
97  if (argc<2) {
98  printf("Wrong number of arguments\n");
99  return -1;
100  }
101  //AliLog::SetClassDebugLevel("AliTPCRawStreamV3",-5);
102  AliLog::SetClassDebugLevel("AliRawReaderDate",-5);
103  AliLog::SetClassDebugLevel("AliTPCAltroMapping",-5);
104  AliLog::SetModuleDebugLevel("RAW",-5);
105 
106  gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
107  "*",
108  "TStreamerInfo",
109  "RIO",
110  "TStreamerInfo()");
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 
127  /* declare monitoring program */
128  int i,status;
129  status=monitorDeclareMp( __FILE__ );
130  if (status!=0) {
131  printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
132  return -1;
133  }
134 
135  // variables
136  AliTPCmapper *mapping = 0; // The TPC mapping
137  char localfile[255];
138  unsigned long32 runNb=0; //run number
139  // configuration options
140  // if test setup get parameters from $DAQDA_TEST_DIR
141 
142  if (!mapping){
143  /* copy locally the mapping file from daq detector config db */
144  sprintf(localfile,"./%s",MAPPING_FILE);
145  status = daqDA_DB_getFile(MAPPING_FILE,localfile);
146  if (status) {
147  printf("Failed to get mapping file (%s) from DAQdetDB, status=%d\n", MAPPING_FILE, status);
148  return -1;
149  }
150 
151  /* open the mapping file and retrieve mapping object */
152  TFile *fileMapping = new TFile(MAPPING_FILE, "read");
153  mapping = (AliTPCmapper*) fileMapping->Get("tpcMapping");
154  delete fileMapping;
155  }
156 
157  if (mapping == 0) {
158  printf("Failed to get mapping object from %s. ...\n", MAPPING_FILE);
159  return -1;
160  } else {
161  printf("Got mapping object from %s\n", MAPPING_FILE);
162  }
163 
164  //
165  // DA configuration from configuration file
166  //
167  //retrieve configuration file
168  sprintf(localfile,"./%s",CONFIG_FILE);
169  status = daqDA_DB_getFile(CONFIG_FILE,localfile);
170  if (status) {
171  printf("Failed to get configuration file (%s) from DAQdetDB, status=%d\n", CONFIG_FILE, status);
172  return -1;
173  }
174  AliTPCConfigDA config(CONFIG_FILE);
175  // check configuration options
176  Bool_t skipAmore=kFALSE;
177 
178  //skip the amore part
179  if ( config.GetConfigurationMap()->GetValue("SkipAmore") ) {
180  skipAmore=((TObjString*)config.GetConfigurationMap()->GetValue("SkipAmore"))->GetString().Atoi();
181  printf("TPCPULSERda: Skip Amore set in config\n");
182  }
183 
184  // create calibration object
185  AliTPCCalibPulser calibPulser(config.GetConfigurationMap()); // pulser calibration algorithm
186  calibPulser.SetAltroMapping(mapping->GetAltroMapping()); // Use altro mapping we got from daqDetDb
187 
188  //===========================//
189  // loop over RAW data files //
190  //==========================//
191  int nevents=0;
192  for(i=1;i<argc;i++) {
193 
194  /* define data source : this is argument i */
195  printf("Processing file %s\n", argv[i]);
196  status=monitorSetDataSource( argv[i] );
197  if (status!=0) {
198  printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
199  return -1;
200  }
201 
202  /* read until EOF */
203  while (true) {
204  struct eventHeaderStruct *event;
205 
206  /* check shutdown condition */
207  if (daqDA_checkShutdown()) {break;}
208 
209  /* get next event (blocking call until timeout) */
210  status=monitorGetEventDynamic((void **)&event);
211  if (status==MON_ERR_EOF) {
212  printf ("End of File %d detected\n",i);
213  break; /* end of monitoring file has been reached */
214  }
215 
216  if (status!=0) {
217  printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
218  break;
219  }
220 
221  /* retry if got no event */
222  if (event==NULL) {
223  continue;
224  }
225  nevents++;
226  // get the run number
227  runNb = event->eventRunNb;
228  // Pulser calibration
229  calibPulser.ProcessEvent(event);
230 
231  /* free resources */
232  free(event);
233  }
234  }
235 
236  //
237  // Analyse pulser data and write them to rootfile
238  //
239  calibPulser.Analyse();
240  printf ("%d events processed\n",nevents);
241 
242  TFile * fileTPC = new TFile (RESULT_FILE,"recreate");
243  calibPulser.Write("tpcCalibPulser");
244  delete fileTPC;
245  printf("Wrote %s\n",RESULT_FILE);
246 
247  /* store the result file on FES */
248 
249  status=daqDA_FES_storeFile(RESULT_FILE,FILE_ID);
250  if (status) {
251  status = -2;
252  }
253  //
254  //Send objects to the AMORE DB
255  //
256  if (!skipAmore){
257  printf ("AMORE part\n");
258  const char *amoreDANameorig=gSystem->Getenv("AMORE_DA_NAME");
259  //cheet a little -- temporary solution (hopefully)
260  //
261  //currently amoreDA uses the environment variable AMORE_DA_NAME to create the mysql
262  //table in which the calib objects are stored. This table is dropped each time AmoreDA
263  //is initialised. This of course makes a problem if we would like to store different
264  //calibration entries in the AMORE DB. Therefore in each DA which writes to the AMORE DB
265  //the AMORE_DA_NAME env variable is overwritten.
266 
267  //find processed sector
268  Char_t sideName='A';
269  Int_t sector = -1;
270  for ( Int_t roc = 0; roc < 72; roc++ ) {
271  if ( !calibPulser.GetCalRocT0(roc) ) continue;
272  if (mapping->GetSideFromRoc(roc)==1) sideName='C';
273  sector = mapping->GetSectorFromRoc(roc);
274  }
275  //gSystem->Setenv("AMORE_DA_NAME",Form("TPC-%c%02d-%s",sideName,sector,FILE_ID));
276  gSystem->Setenv("AMORE_DA_NAME",Form("%s-%s", gSystem->Getenv("DATE_ROLE_NAME"), FILE_ID));
277  //
278  // end cheet
279  if (sector>-1){
280  TDatime time;
281  TObjString info(Form("Run: %u; Date: %s",runNb,time.AsSQLString()));
282 
283  amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
284  Int_t statusDA=0;
285  statusDA+=amoreDA.Send("PulserT0",calibPulser.GetCalPadT0());
286  statusDA+=amoreDA.Send("PulserQ",calibPulser.GetCalPadQ());
287  statusDA+=amoreDA.Send("PulserRMS",calibPulser.GetCalPadRMS());
288  statusDA+=amoreDA.Send("arrayTmean",calibPulser.GetMeanTimeSectorArray());
289  statusDA+=amoreDA.Send("Info",&info);
290  if ( statusDA!=0 )
291  printf("Waring: Failed to write one of the calib objects to the AMORE database\n");
292  } else {
293  printf("Waring: No data found!\n");
294  }
295  // reset env var
296  if (amoreDANameorig) gSystem->Setenv("AMORE_DA_NAME",amoreDANameorig);
297  }
298 
299  //
300  // Now prepare ASCII files for local ALTRO configuration through DDL.
301  //
302  ofstream deadchannelfile;
303  ofstream qfile;
304 
305  qfile.open(Q_FILE);
306  deadchannelfile.open(DEAD_FILE);
307 
308  qfile << 19 << std::endl; // Pulser Q
309  deadchannelfile << 14 << std::endl; // Mark file to contain NOISY or DEAD CHANNELS
310 
311  Int_t ctr_channel = 0;
312  Int_t ctr_dead= 0;
313 
314  // inner==true : calROC from ldc-0 contains: rcus 0,1 for IROC and rcu 2 for OROC
315  // inner==false: calROC from ldc-1 contains: nothingfor IROC and rcus 3,4,5 for OROC
316  for ( Int_t roc = 0; roc < 72; roc++ ) {
317  if ( !calibPulser.GetCalRocQ(roc) ) continue;
318  bool isIROC= mapping->IsIROC(roc);
319  Int_t side = mapping->GetSideFromRoc(roc);
320  Int_t sec= mapping->GetSectorFromRoc(roc);
321  Int_t minrcu, maxrcu;
322  if( isIROC ) { minrcu=0; maxrcu=1; }
323  //else if ( inner) { minrcu=2; maxrcu=2; }
324  //else { minrcu=3; maxrcu=5; }
325  else { minrcu=2; maxrcu=5; }
326  for ( int rcu = minrcu; rcu <= maxrcu; rcu++ ) {
327  //Int_t patch = mapping->IsIROC(roc) ? rcu : rcu+2;
328  for ( int branch = 0; branch < 2; branch++ ) {
329  for ( int fec = 0; fec < mapping->GetNfec(rcu, branch); fec++ ) {
330  for ( int altro = 0; altro < 8; altro++ ) {
331  for ( int channel = 0; channel < 16; channel++ ) {
332  Int_t hwadd = mapping->CodeHWAddress(branch, fec, altro, channel);
333  Int_t row = mapping->GetPadRow(rcu, hwadd); // row in a ROC
334  Int_t globalrow = mapping->GetGlobalPadRow(rcu, hwadd); // row in full sector
335  Int_t pad = mapping->GetPad(rcu, hwadd);
336  // skip edge pads
337  if ( (pad<1) || (pad>mapping->GetNpads(globalrow)-2) ) continue;
338  Float_t Q = calibPulser.GetCalRocQ(roc)->GetValue(row,pad);
339  qfile << ctr_channel++ << "\t" << side << "\t" << sec << "\t" << rcu << "\t" << hwadd << "\t" << Q << std::endl;
340  if ( fabs(Q) < 2) { // dead channel
341  deadchannelfile << ctr_dead++ << "\t" << side << "\t" << sec << "\t"
342  << rcu << "\t" << hwadd << "\t" << Q << std::endl;
343  }
344  } // end channel for loop
345  } // end altro for loop
346  } // end fec for loop
347  } // end branch for loop
348  } // end rcu for loop
349  } // end roc loop
350 
351  qfile.close();
352  deadchannelfile.close();
353 
354  printf("Wrote ASCII file. Found %d dead channels.\n", ctr_dead);
355 
356 return status;
357 }
printf("Chi2/npoints = %f\n", TMath::Sqrt(chi2/npoints))
#define Q_FILE
Definition: TPCPULSERda.cxx:46
Bool_t IsIROC(Int_t roc) const
Int_t GetGlobalPadRow(Int_t patch, Int_t hwAddress) const
Int_t GetNpads(Int_t roc, Int_t padrow) const
static void SetClassDebugLevel(const char *className, Int_t level)
Definition: AliLog.cxx:513
const TMap * GetConfigurationMap() const
AliTPCmapper.
Definition: AliTPCmapper.h:15
#define RESULT_FILE
Definition: TPCPULSERda.cxx:42
TROOT * gROOT
Int_t GetPad(Int_t patch, Int_t hwAddress) const
#define FILE_ID
Definition: TPCPULSERda.cxx:43
#define DEAD_FILE
Definition: TPCPULSERda.cxx:47
Int_t GetNfec(Int_t patch, Int_t branch) const
Int_t CodeHWAddress(Int_t branch, Int_t fec, Int_t chip, Int_t channel) const
AliExternalInfo info
Implementation of the TPC pulser calibration.
Int_t GetPadRow(Int_t patch, Int_t hwAddress) const
static void SetModuleDebugLevel(const char *module, Int_t level)
Definition: AliLog.cxx:485
int main(int argc, char **argv)
Definition: TPCPULSERda.cxx:90
Int_t GetSectorFromRoc(Int_t roc) const
#define MAPPING_FILE
Definition: TPCPULSERda.cxx:44
Class for Parsing simple text configuration files.
#define CONFIG_FILE
Definition: TPCPULSERda.cxx:45
Int_t GetSideFromRoc(Int_t roc) const
AliTPCAltroMapping ** GetAltroMapping()
Definition: AliTPCmapper.h:28
void SetAltroMapping(AliTPCAltroMapping **mapp)