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