]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/DA/TPCPULSERda.cxx
doxy: TPC/DA
[u/mrichter/AliRoot.git] / TPC / DA / TPCPULSERda.cxx
1 /// \file TPCPULSERda.cxx
2 /// \brief 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 /// 
14 /// TPCda_pulser.cxx - calibration algorithm for TPC pulser events
15 /// 
16 /// 10/06/2007  sylvain.chapeland@cern.ch  :  first version - clean skeleton based on DAQ DA case1
17 /// 30/09/2007  haavard.helstrup@cern.ch   :  created pulser DA based on pedestal code
18 /// 19/09/2008  J.Wiechula@gsi.de:            Added export of the calibration data to the AMORE data base.
19 ///                                           Added support for configuration files.
20 /// 23/04/2011  Christian.Lippmann@cern.ch :  Added output of acsii files for online
21 /// 26/09/2014  Christian.Lippmann@cern.ch :  CHange for new DAQ setup with one CRORC per LDC
22 /// 26/09/2014  Jens.Wiechula@cern.ch      :  comment out obsolete AliTPCRawStream
23 /// 
24 /// \author marian.ivanov@cern.ch
25 /// 
26 /// This process reads RAW data from the files provided as command line arguments
27 /// and save results in a file (named from RESULT_FILE define - see below).
28
29 #define RESULT_FILE "tpcPulser.root"
30 #define FILE_ID "pulser"
31 #define MAPPING_FILE "tpcMapping.root"
32 #define CONFIG_FILE "TPCPULSERda.conf"
33 #define Q_FILE "tpcPulserQ.data"
34 #define DEAD_FILE "tpcDeadChannelsPulser.data"
35 #define AliDebugLevel() -1
36
37
38 #include <daqDA.h>
39 #include "event.h"
40 #include "monitor.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <fstream>
44
45 // Root includes
46
47 #include <TFile.h>
48 #include "TROOT.h"
49 #include "TPluginManager.h"
50 #include "TString.h"
51 #include "TObjString.h"
52 #include "TDatime.h"
53 //
54 //AliRoot includes
55 //
56 #include "AliRawReader.h"
57 #include "AliRawReaderDate.h"
58 #include "AliTPCmapper.h"
59 //#include "AliTPCRawStreamV3.h"
60 #include "AliTPCROC.h"
61 #include "AliTPCCalROC.h"
62 #include "AliTPCCalPad.h"
63 #include "AliMathBase.h"
64 #include "TTreeStream.h"
65 #include "AliLog.h"
66 #include "TSystem.h"
67 #include "AliTPCConfigDA.h"
68 //
69 //AMORE
70 //
71 #include <AmoreDA.h>
72 //
73 // TPC calibration algorithm includes
74 //
75 #include "AliTPCCalibPulser.h"
76
77 int main(int argc, char **argv) {
78   /// Main routine
79   /// Arguments: list of DATE raw data files
80
81   // log start of process
82   printf("TPC Pulser DA started - %s\n",__FILE__);
83
84   if (argc<2) {
85     printf("Wrong number of arguments\n");
86     return -1;
87   }
88   //AliLog::SetClassDebugLevel("AliTPCRawStreamV3",-5);
89   AliLog::SetClassDebugLevel("AliRawReaderDate",-5);
90   AliLog::SetClassDebugLevel("AliTPCAltroMapping",-5);
91   AliLog::SetModuleDebugLevel("RAW",-5);
92
93   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
94                                         "*",
95                                         "TStreamerInfo",
96                                         "RIO",
97                                         "TStreamerInfo()");
98
99   /*
100   TString daterolename(gSystem->Getenv("DATE_ROLE_NAME"));
101   if ( daterolename == "" ) {
102     printf("Error: Variable DATE_ROLE_NAME not defined! Exiting ...\n");
103     return -1;
104   }
105   bool inner;
106   if      ( daterolename.EndsWith("-0") ) inner = true;
107   else if ( daterolename.EndsWith("-1") ) inner = false;
108   else {
109     printf("Error: Variable DATE_ROLE_NAME neither ends with -0 nor -1 (E.g. ldc-TPC-C12-1)! Exiting ...\n");
110     return -1;
111   }
112   */
113
114   /* declare monitoring program */
115   int i,status;
116   status=monitorDeclareMp( __FILE__ );
117   if (status!=0) {
118     printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
119     return -1;
120   }
121
122   // variables 
123   AliTPCmapper *mapping = 0;   // The TPC mapping
124   char localfile[255];
125   unsigned long32 runNb=0;      //run number
126   // configuration options 
127   // if  test setup get parameters from $DAQDA_TEST_DIR 
128    
129   if (!mapping){
130     /* copy locally the mapping file from daq detector config db */
131     sprintf(localfile,"./%s",MAPPING_FILE);
132     status = daqDA_DB_getFile(MAPPING_FILE,localfile);
133     if (status) {
134       printf("Failed to get mapping file (%s) from DAQdetDB, status=%d\n", MAPPING_FILE, status);
135       return -1;
136     }
137
138     /* open the mapping file and retrieve mapping object */
139     TFile *fileMapping = new TFile(MAPPING_FILE, "read");
140     mapping = (AliTPCmapper*) fileMapping->Get("tpcMapping");
141     delete fileMapping;
142   }
143
144   if (mapping == 0) {
145     printf("Failed to get mapping object from %s.  ...\n", MAPPING_FILE);
146     return -1;
147   } else {
148     printf("Got mapping object from %s\n", MAPPING_FILE);
149   }
150
151   //
152   // DA configuration from configuration file
153   //
154  //retrieve configuration file
155   sprintf(localfile,"./%s",CONFIG_FILE);
156   status = daqDA_DB_getFile(CONFIG_FILE,localfile);
157   if (status) {
158     printf("Failed to get configuration file (%s) from DAQdetDB, status=%d\n", CONFIG_FILE, status);
159     return -1;
160   }
161   AliTPCConfigDA config(CONFIG_FILE);
162   // check configuration options
163   Bool_t  skipAmore=kFALSE;
164
165   //skip the amore part
166   if ( config.GetConfigurationMap()->GetValue("SkipAmore") ) {
167     skipAmore=((TObjString*)config.GetConfigurationMap()->GetValue("SkipAmore"))->GetString().Atoi();
168     printf("TPCPULSERda: Skip Amore set in config\n");
169   }
170
171   // create calibration object
172   AliTPCCalibPulser calibPulser(config.GetConfigurationMap());   // pulser calibration algorithm
173   calibPulser.SetAltroMapping(mapping->GetAltroMapping()); // Use altro mapping we got from daqDetDb
174
175   //===========================//
176   // loop over RAW data files //
177   //==========================//
178   int nevents=0;
179   for(i=1;i<argc;i++) {
180
181     /* define data source : this is argument i */
182     printf("Processing file %s\n", argv[i]);
183     status=monitorSetDataSource( argv[i] );
184     if (status!=0) {
185       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
186       return -1;
187     }
188
189     /* read until EOF */
190     while (true) {
191       struct eventHeaderStruct *event;
192
193       /* check shutdown condition */
194       if (daqDA_checkShutdown()) {break;}
195
196       /* get next event (blocking call until timeout) */
197       status=monitorGetEventDynamic((void **)&event);
198       if (status==MON_ERR_EOF) {
199         printf ("End of File %d detected\n",i);
200         break; /* end of monitoring file has been reached */
201       }
202
203       if (status!=0) {
204         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
205         break;
206       }
207
208       /* retry if got no event */
209       if (event==NULL) {
210         continue;
211       }
212       nevents++;
213       // get the run number
214       runNb = event->eventRunNb;
215       //  Pulser calibration
216       calibPulser.ProcessEvent(event);
217
218       /* free resources */
219       free(event);
220     }
221   }
222
223   //
224   // Analyse pulser data and write them to rootfile
225   //
226   calibPulser.Analyse();
227   printf ("%d events processed\n",nevents);
228
229   TFile * fileTPC = new TFile (RESULT_FILE,"recreate");
230   calibPulser.Write("tpcCalibPulser");
231   delete fileTPC;
232   printf("Wrote %s\n",RESULT_FILE);
233
234   /* store the result file on FES */
235
236   status=daqDA_FES_storeFile(RESULT_FILE,FILE_ID);
237   if (status) {
238     status = -2;
239   }
240   //
241   //Send objects to the AMORE DB
242   //
243   if (!skipAmore){
244     printf ("AMORE part\n");
245     const char *amoreDANameorig=gSystem->Getenv("AMORE_DA_NAME");
246     //cheet a little -- temporary solution (hopefully)
247     //
248     //currently amoreDA uses the environment variable AMORE_DA_NAME to create the mysql
249     //table in which the calib objects are stored. This table is dropped each time AmoreDA
250     //is initialised. This of course makes a problem if we would like to store different
251     //calibration entries in the AMORE DB. Therefore in each DA which writes to the AMORE DB
252     //the AMORE_DA_NAME env variable is overwritten.
253
254     //find processed sector
255     Char_t sideName='A';
256     Int_t sector = -1;
257     for ( Int_t roc = 0; roc < 72; roc++ ) {
258       if ( !calibPulser.GetCalRocT0(roc) ) continue;
259       if (mapping->GetSideFromRoc(roc)==1) sideName='C';
260       sector = mapping->GetSectorFromRoc(roc);
261     }
262     //gSystem->Setenv("AMORE_DA_NAME",Form("TPC-%c%02d-%s",sideName,sector,FILE_ID));
263     gSystem->Setenv("AMORE_DA_NAME",Form("%s-%s", gSystem->Getenv("DATE_ROLE_NAME"), FILE_ID));
264     //
265     // end cheet
266     if (sector>-1){
267       TDatime time;
268       TObjString info(Form("Run: %u; Date: %s",runNb,time.AsSQLString()));
269
270       amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
271       Int_t statusDA=0;
272       statusDA+=amoreDA.Send("PulserT0",calibPulser.GetCalPadT0());
273       statusDA+=amoreDA.Send("PulserQ",calibPulser.GetCalPadQ());
274       statusDA+=amoreDA.Send("PulserRMS",calibPulser.GetCalPadRMS());
275       statusDA+=amoreDA.Send("arrayTmean",calibPulser.GetMeanTimeSectorArray());
276       statusDA+=amoreDA.Send("Info",&info);
277       if ( statusDA!=0 )
278         printf("Waring: Failed to write one of the calib objects to the AMORE database\n");
279     } else {
280       printf("Waring: No data found!\n");
281     }
282     // reset env var
283     if (amoreDANameorig) gSystem->Setenv("AMORE_DA_NAME",amoreDANameorig);
284   }
285   
286   //
287   // Now prepare ASCII files for local ALTRO configuration through DDL. 
288   //
289   ofstream deadchannelfile;
290   ofstream qfile;
291
292   qfile.open(Q_FILE);
293   deadchannelfile.open(DEAD_FILE);
294
295   qfile           << 19 << std::endl; // Pulser Q
296   deadchannelfile << 14 << std::endl; // Mark file to contain NOISY or DEAD CHANNELS 
297
298   Int_t ctr_channel = 0;
299   Int_t ctr_dead= 0;
300
301   // inner==true : calROC from ldc-0 contains: rcus 0,1 for IROC and rcu 2 for OROC 
302   // inner==false: calROC from ldc-1 contains: nothingfor IROC and rcus 3,4,5 for OROC
303   for ( Int_t roc = 0; roc < 72; roc++ ) {
304     if ( !calibPulser.GetCalRocQ(roc) ) continue;
305     bool isIROC= mapping->IsIROC(roc);
306     Int_t side = mapping->GetSideFromRoc(roc);
307     Int_t sec= mapping->GetSectorFromRoc(roc);
308     Int_t minrcu, maxrcu;
309     if( isIROC )     { minrcu=0; maxrcu=1; }
310     //else if ( inner) { minrcu=2; maxrcu=2; }
311     //else             { minrcu=3; maxrcu=5; }
312     else             { minrcu=2; maxrcu=5; }
313     for ( int rcu = minrcu; rcu <= maxrcu; rcu++ ) {
314       //Int_t patch = mapping->IsIROC(roc) ? rcu : rcu+2; 
315       for ( int branch = 0; branch < 2; branch++ ) {
316         for ( int fec = 0; fec < mapping->GetNfec(rcu, branch); fec++ ) {
317           for ( int altro = 0; altro < 8; altro++ ) {
318             for ( int channel = 0; channel < 16; channel++ ) {
319               Int_t hwadd = mapping->CodeHWAddress(branch, fec, altro, channel);
320               Int_t row = mapping->GetPadRow(rcu, hwadd);              // row in a ROC
321               Int_t globalrow = mapping->GetGlobalPadRow(rcu, hwadd);  // row in full sector
322               Int_t pad = mapping->GetPad(rcu, hwadd);
323               // skip edge pads
324               if ( (pad<1) || (pad>mapping->GetNpads(globalrow)-2) ) continue;
325               Float_t Q = calibPulser.GetCalRocQ(roc)->GetValue(row,pad);
326               qfile << ctr_channel++ << "\t" << side << "\t" << sec << "\t" << rcu << "\t" << hwadd << "\t" << Q << std::endl;
327               if ( fabs(Q) < 2) { // dead channel
328                 deadchannelfile << ctr_dead++ << "\t" << side << "\t" << sec << "\t"
329                                 << rcu << "\t" << hwadd << "\t" << Q << std::endl;
330               }
331             } // end channel for loop 
332           } // end altro for loop 
333         } // end fec for loop 
334       } // end branch for loop
335     } // end rcu for loop 
336   } // end roc loop 
337   
338   qfile.close();
339   deadchannelfile.close();
340
341   printf("Wrote ASCII file. Found %d dead channels.\n", ctr_dead);
342
343 return status;
344 }