]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/TPCPULSERda.cxx
Bug fix. Removed delete statement
[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 contact: marian.ivanov@cern.ch
25
26
27 This process reads RAW data from the files provided as command line arguments
28 and save results in a file (named from RESULT_FILE define - see below).
29
30 */
31
32 #define RESULT_FILE "tpcPulser.root"
33 #define FILE_ID "pulser"
34 #define MAPPING_FILE "tpcMapping.root"
35 #define CONFIG_FILE "TPCPULSERda.conf"
36 #define AliDebugLevel() -1
37
38
39 #include <daqDA.h>
40 #include "event.h"
41 #include "monitor.h"
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 //
46 //Root includes
47 //
48 #include <TFile.h>
49 #include "TROOT.h"
50 #include "TPluginManager.h"
51 #include "TString.h"
52 #include "TObjString.h"
53 #include "TDatime.h"
54 //
55 //AliRoot includes
56 //
57 #include "AliRawReader.h"
58 #include "AliRawReaderDate.h"
59 #include "AliTPCmapper.h"
60 #include "AliTPCRawStream.h"
61 #include "AliTPCROC.h"
62 #include "AliTPCCalROC.h"
63 #include "AliTPCCalPad.h"
64 #include "AliMathBase.h"
65 #include "TTreeStream.h"
66 #include "AliLog.h"
67 #include "TSystem.h"
68 #include "AliTPCConfigDA.h"
69 //
70 //AMORE
71 //
72 #include <AmoreDA.h>
73 //
74 // TPC calibration algorithm includes
75 //
76 #include "AliTPCCalibPulser.h"
77
78 /* Main routine
79       Arguments: list of DATE raw data files
80 */
81 int main(int argc, char **argv) {
82   /* log start of process */
83   printf("TPC Pulser DA started - %s\n",__FILE__);
84
85   if (argc<2) {
86     printf("Wrong number of arguments\n");
87     return -1;
88   }
89   AliLog::SetClassDebugLevel("AliTPCRawStream",-5);
90   AliLog::SetClassDebugLevel("AliRawReaderDate",-5);
91   AliLog::SetClassDebugLevel("AliTPCAltroMapping",-5);
92   AliLog::SetModuleDebugLevel("RAW",-5);
93
94   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
95                                         "*",
96                                         "TStreamerInfo",
97                                         "RIO",
98                                         "TStreamerInfo()");
99
100
101   /* declare monitoring program */
102   int i,status;
103   status=monitorDeclareMp( __FILE__ );
104   if (status!=0) {
105     printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
106     return -1;
107   }
108
109   // variables 
110   AliTPCmapper *mapping = 0;   // The TPC mapping
111   char localfile[255];
112   unsigned long32 runNb=0;      //run number
113   // configuration options 
114   Bool_t fastDecoding = kFALSE;
115   // if  test setup get parameters from $DAQDA_TEST_DIR 
116    
117   if (!mapping){
118     /* copy locally the mapping file from daq detector config db */
119     sprintf(localfile,"./%s",MAPPING_FILE);
120     status = daqDA_DB_getFile(MAPPING_FILE,localfile);
121     if (status) {
122       printf("Failed to get mapping file (%s) from DAQdetDB, status=%d\n", MAPPING_FILE, status);
123       return -1;
124     }
125
126     /* open the mapping file and retrieve mapping object */
127     TFile *fileMapping = new TFile(MAPPING_FILE, "read");
128     mapping = (AliTPCmapper*) fileMapping->Get("tpcMapping");
129     delete fileMapping;
130   }
131
132   if (mapping == 0) {
133     printf("Failed to get mapping object from %s.  ...\n", MAPPING_FILE);
134     return -1;
135   } else {
136     printf("Got mapping object from %s\n", MAPPING_FILE);
137   }
138
139   //
140   // DA configuration from configuration file
141   //
142  //retrieve configuration file
143   sprintf(localfile,"./%s",CONFIG_FILE);
144   status = daqDA_DB_getFile(CONFIG_FILE,localfile);
145   if (status) {
146     printf("Failed to get configuration file (%s) from DAQdetDB, status=%d\n", CONFIG_FILE, status);
147     return -1;
148   }
149   AliTPCConfigDA config(CONFIG_FILE);
150   // check configuration options
151   if ( (Int_t)config.GetValue("UseFastDecoder") == 1 ) {
152     printf("Info: The fast decoder will be used for the processing.\n");
153     fastDecoding=kTRUE;
154   }
155
156   // create calibration object
157   AliTPCCalibPulser calibPulser(config.GetConfigurationMap());   // pulser calibration algorithm
158   calibPulser.SetAltroMapping(mapping->GetAltroMapping()); // Use altro mapping we got from daqDetDb
159
160   //===========================//
161   // loop over RAW data files //
162   //==========================//
163   int nevents=0;
164   for(i=1;i<argc;i++) {
165
166     /* define data source : this is argument i */
167     printf("Processing file %s\n", argv[i]);
168     status=monitorSetDataSource( argv[i] );
169     if (status!=0) {
170       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
171       return -1;
172     }
173
174     /* read until EOF */
175     while (true) {
176       struct eventHeaderStruct *event;
177
178       /* check shutdown condition */
179       if (daqDA_checkShutdown()) {break;}
180
181       /* get next event (blocking call until timeout) */
182       status=monitorGetEventDynamic((void **)&event);
183       if (status==MON_ERR_EOF) {
184         printf ("End of File %d detected\n",i);
185         break; /* end of monitoring file has been reached */
186       }
187
188       if (status!=0) {
189         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
190         break;
191       }
192
193       /* retry if got no event */
194       if (event==NULL) {
195         continue;
196       }
197       nevents++;
198       // get the run number
199       runNb = event->eventRunNb;
200       //  Pulser calibration
201       AliRawReader *rawReader = new AliRawReaderDate((void*)event);
202       if ( fastDecoding ) calibPulser.ProcessEventFast(rawReader);
203       else calibPulser.ProcessEvent(rawReader);
204       delete rawReader;
205
206       /* free resources */
207       free(event);
208     }
209   }
210
211   //
212   // Analyse pulser data and write them to rootfile
213   //
214   calibPulser.Analyse();
215   printf ("%d events processed\n",nevents);
216
217   TFile * fileTPC = new TFile (RESULT_FILE,"recreate");
218   calibPulser.Write("tpcCalibPulser");
219   delete fileTPC;
220   printf("Wrote %s\n",RESULT_FILE);
221
222   /* store the result file on FES */
223
224   status=daqDA_FES_storeFile(RESULT_FILE,FILE_ID);
225   if (status) {
226     status = -2;
227   }
228   //
229   //Send objects to the AMORE DB
230   //
231   printf ("AMORE part\n");
232   const char *amoreDANameorig=gSystem->Getenv("AMORE_DA_NAME");
233   //cheet a little -- temporary solution (hopefully)
234   // 
235   //currently amoreDA uses the environment variable AMORE_DA_NAME to create the mysql
236   //table in which the calib objects are stored. This table is dropped each time AmoreDA
237   //is initialised. This of course makes a problem if we would like to store different
238   //calibration entries in the AMORE DB. Therefore in each DA which writes to the AMORE DB
239   //the AMORE_DA_NAME env variable is overwritten.  
240  
241   //find processed sector
242   Char_t sideName='A';
243   Int_t sector = 0;
244   for ( Int_t roc = 0; roc < 72; roc++ ) {
245     if ( !calibPulser.GetCalRocT0(roc) ) continue;
246     if (mapping->GetSideFromRoc(roc)==1) sideName='C';
247     sector = mapping->GetSectorFromRoc(roc);
248   }
249   gSystem->Setenv("AMORE_DA_NAME",Form("TPC-%c%02d-%s",sideName,sector,FILE_ID));
250   // 
251   // end cheet
252   TDatime time;
253   TObjString info(Form("Run: %u; Date: %s",runNb,time.AsString()));
254     
255   amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
256   Int_t statusDA=0;  
257   statusDA+=amoreDA.Send("PulserT0",calibPulser.GetCalPadT0());
258   statusDA+=amoreDA.Send("PulserQ",calibPulser.GetCalPadQ());
259   statusDA+=amoreDA.Send("PulserRMS",calibPulser.GetCalPadRMS());
260   statusDA+=amoreDA.Send("arrayTmean",calibPulser.GetMeanTimeSectorArray());
261   statusDA+=amoreDA.Send("Info",&info);
262   if ( statusDA!=0 )
263     printf("Waring: Failed to write one of the calib objects to the AMORE database\n");
264   // reset env var  
265   if (amoreDANameorig) gSystem->Setenv("AMORE_DA_NAME",amoreDANameorig);
266
267   return status;
268 }