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