Script supersceeded by AliForwarddNdetaTask.C and
[u/mrichter/AliRoot.git] / TPC / TPCCEda.cxx
CommitLineData
5a3a40fa 1/*
9c754ce7 2TPC DA for online calibration
3
4Contact: Haavard.Helstrup@cern.ch
5Link:
1b37fc95 6Run Type: PHYSICS STANDALONE DAQ
9c754ce7 7DA Type: MON
8Number of events needed: 500
9Input Files:
10Output Files: tpcCE.root, to be exported to the DAQ FXS
bd955ed2 11fileId: CE
9c754ce7 12Trigger types used: PHYSICS_EVENT
13
14*/
15
16/*
5a3a40fa 17
18TPCCEda.cxx - calibration algorithm for TPC Central Electrode events
19
2010/06/2007 sylvain.chapeland@cern.ch : first version - clean skeleton based on DAQ DA case1
2106/12/2007 haavard.helstrup@cern.ch : created CE DA based on pulser code
ac940b58 2219/09/2008 J.Wiechula@gsi.de: Added support for configuration files.
5a3a40fa 23
24contact: marian.ivanov@cern.ch
25
26
27This process reads RAW data from the files provided as command line arguments
28and save results in a file (named from RESULT_FILE define - see below).
29
30*/
31
9c754ce7 32#define RESULT_FILE "tpcCE.root"
bd955ed2 33#define FILE_ID "CE"
04049c81 34#define MAPPING_FILE "tpcMapping.root"
ac940b58 35#define CONFIG_FILE "TPCCEda.conf"
5a3a40fa 36
5a3a40fa 37#include <daqDA.h>
38#include "event.h"
39#include "monitor.h"
40#include <stdio.h>
41#include <stdlib.h>
db7038d0 42#include <vector>
5a3a40fa 43//
44//Root includes
45//
46#include <TFile.h>
47#include "TROOT.h"
48#include "TPluginManager.h"
ac940b58 49#include "TSystem.h"
50#include "TString.h"
51#include "TObjString.h"
52#include "TDatime.h"
96bf9029 53#include "TStopwatch.h"
db7038d0 54#include "TMap.h"
55#include "TGraph.h"
56#include "TMath.h"
5a3a40fa 57//
58//AliRoot includes
59//
60#include "AliRawReader.h"
61#include "AliRawReaderDate.h"
97b609ee 62#include "AliTPCmapper.h"
5a3a40fa 63#include "AliTPCRawStream.h"
64#include "AliTPCROC.h"
65#include "AliTPCCalROC.h"
66#include "AliTPCCalPad.h"
67#include "AliMathBase.h"
68#include "TTreeStream.h"
b401648b 69#include "AliLog.h"
ac940b58 70#include "AliTPCConfigDA.h"
71//
72//AMORE
73//
74#include <AmoreDA.h>
5a3a40fa 75//
76// TPC calibration algorithm includes
77//
78#include "AliTPCCalibCE.h"
79
80
db7038d0 81//functios, implementation below
9f02b62b 82void SendToAmoreDB(AliTPCCalibCE *calibCE, unsigned long32 runNb);
db7038d0 83//for threaded processing
5a3a40fa 84
85
86/* Main routine
87 Arguments: list of DATE raw data files
88*/
89int main(int argc, char **argv) {
ac940b58 90 /* log start of process */
23fcceca 91 printf("TPCCEda: DA started - %s\n",__FILE__);
db7038d0 92
ac940b58 93 if (argc<2) {
23fcceca 94 printf("TPCCEda: Wrong number of arguments\n");
ac940b58 95 return -1;
96 }
97
98 AliLog::SetClassDebugLevel("AliTPCRawStream",-5);
99 AliLog::SetClassDebugLevel("AliRawReaderDate",-5);
100 AliLog::SetClassDebugLevel("AliTPCAltroMapping",-5);
101 AliLog::SetModuleDebugLevel("RAW",-5);
db7038d0 102
103 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
104 "*",
105 "TStreamerInfo",
106 "RIO",
107 "TStreamerInfo()");
108
ac940b58 109 /* declare monitoring program */
db7038d0 110 int i,status;
111 status=monitorDeclareMp( __FILE__ );
112 if (status!=0) {
23fcceca 113 printf("TPCCEda: monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
db7038d0 114 return -1;
115 }
116 monitorSetNowait();
117 monitorSetNoWaitNetworkTimeout(1000);
118
119 //variables
b401648b 120 AliTPCmapper *mapping = 0; // The TPC mapping
ac940b58 121 char localfile[255];
122 unsigned long32 runNb=0; //run number
db7038d0 123
96bf9029 124
db7038d0 125 //
126 // DA configuration from configuration file
127 //
128 //retrieve configuration file
129 sprintf(localfile,"./%s",CONFIG_FILE);
130 status = daqDA_DB_getFile(CONFIG_FILE,localfile);
131 if (status) {
23fcceca 132 printf("TPCCEda: Failed to get configuration file (%s) from DAQdetDB, status=%d\n", CONFIG_FILE, status);
db7038d0 133 return -1;
134 }
135 AliTPCConfigDA config(CONFIG_FILE);
136 // check configuration options
137 TString laserTriggerName("C0LSR-ABCE-NOPF-CENT");
23fcceca 138 TString monitoringType("YES");
db7038d0 139 Int_t forceTriggerId=-1;
9f02b62b 140 Int_t saveOption=2; // how to store the object. See AliTPCCalibCE::DumpToFile
23fcceca 141
db7038d0 142 if ( config.GetConfigurationMap()->GetValue("LaserTriggerName") ) {
143 laserTriggerName=config.GetConfigurationMap()->GetValue("LaserTriggerName")->GetName();
23fcceca 144 printf("TPCCEda: Laser trigger class name set to: %s.\n",laserTriggerName.Data());
db7038d0 145 }
23fcceca 146
147 if ( config.GetConfigurationMap()->GetValue("MonitoringType") ) {
148 monitoringType=config.GetConfigurationMap()->GetValue("MonitoringType")->GetName();
149 printf("TPCCEda: Monitoring type set to: %s.\n",monitoringType.Data());
150 }
151
152 if ( config.GetConfigurationMap()->GetValue("ForceLaserTriggerId") ) {
153 forceTriggerId=TMath::Nint(config.GetValue("ForceLaserTriggerId"));
154 printf("TPCCEda: Only processing triggers with Id: %d.\n",forceTriggerId);
db7038d0 155 }
156
9f02b62b 157 if ( config.GetConfigurationMap()->GetValue("SaveOption") ) {
158 saveOption=TMath::Nint(config.GetValue("SaveOption"));
159 printf("TPCCEda: Saving option set to: %d.\n",saveOption);
160 }
db7038d0 161
162 //subsribe to laser triggers only in physics partition
163 //if the trigger class is not available the return value is -1
164 //in this case we are most probably running as a standalone
165 // laser run and should request all events
96bf9029 166 unsigned char classIdptr=0;
167 int retClassId=daqDA_getClassIdFromName(laserTriggerName.Data(),&classIdptr);
db7038d0 168 if (retClassId==0){
169 //interleaved laser in physics runs
96bf9029 170 //select proper trigger class id
db7038d0 171 char c[5];
96bf9029 172 snprintf(c,sizeof(c),"%u",(unsigned int)classIdptr);
23fcceca 173 char *table[5] = {"PHY",const_cast<char*>(monitoringType.Data()),"*",c,NULL};
db7038d0 174 monitorDeclareTableExtended(table);
23fcceca 175 printf("TPCCEda: Using monitoring table: (PHY, %s, *, %s)\n",monitoringType.Data(),c);
db7038d0 176 } else if (retClassId==-1){
96bf9029 177 //global partition without laser triggered events
178 //the DA should exit properly without processing
23fcceca 179 printf("TPCCEda: Laser trigger class '%s' was not found among trigger class names. Will stop processing.\n",laserTriggerName.Data());
96bf9029 180 return 0;
181 } else if (retClassId==-2){
182 //standalone case, accept all physics events
183 char *table[5] = {"PHY","Y","*","*",NULL};
db7038d0 184 monitorDeclareTableExtended(table);
23fcceca 185 printf("TPCCEda: Using all trigger class Ids\n");
db7038d0 186 } else {
23fcceca 187 printf("TPCCEda: Unknown return value of 'daqDA_getClassIdFromName': %d\n",retClassId);
db7038d0 188 return -2;
189 }
190
191 //see if we should force the trigger id
192 if (forceTriggerId>-1){
193 char c[5];
194 sprintf(c,"%d",forceTriggerId);
195 char *table[5] = {"PHY","Y","*",c,NULL};
196 monitorDeclareTableExtended(table);
db7038d0 197 }
198
199
200 // if test setup get parameters from $DAQDA_TEST_DIR
b401648b 201 if (!mapping){
202 /* copy locally the mapping file from daq detector config db */
ac940b58 203 sprintf(localfile,"./%s",MAPPING_FILE);
204 status = daqDA_DB_getFile(MAPPING_FILE,localfile);
b401648b 205 if (status) {
23fcceca 206 printf("TPCCEda: Failed to get mapping file (%s) from DAQdetDB, status=%d\n", MAPPING_FILE, status);
ac940b58 207 return -1;
b401648b 208 }
db7038d0 209
b401648b 210 /* open the mapping file and retrieve mapping object */
211 TFile *fileMapping = new TFile(MAPPING_FILE, "read");
212 mapping = (AliTPCmapper*) fileMapping->Get("tpcMapping");
213 delete fileMapping;
04049c81 214 }
db7038d0 215
04049c81 216 if (mapping == 0) {
23fcceca 217 printf("TPCCEda: Failed to get mapping object from %s. ...\n", MAPPING_FILE);
ac940b58 218 return -1;
04049c81 219 } else {
23fcceca 220 printf("TPCCEda: Got mapping object from %s\n", MAPPING_FILE);
04049c81 221 }
ac940b58 222
db7038d0 223
224 //create calibration object
9f02b62b 225 AliTPCCalibCE *calibCE=new AliTPCCalibCE(config.GetConfigurationMap()); // central electrode calibration
226 calibCE->SetAltroMapping(mapping->GetAltroMapping()); // Use altro mapping we got from daqDetDb
5a3a40fa 227
96bf9029 228 //amore update interval
229 Double_t updateInterval=300; //seconds
230 Double_t valConf=config.GetValue("AmoreUpdateInterval");
231 if ( valConf>0 ) updateInterval=valConf;
232 //timer
233 TStopwatch stopWatch;
234
ac940b58 235 //===========================//
236 // loop over RAW data files //
237 //==========================//
5a3a40fa 238 int nevents=0;
23fcceca 239 int neventsOld=0;
db7038d0 240 size_t counter=0;
ac940b58 241 for ( i=1; i<argc; i++) {
db7038d0 242
5a3a40fa 243 /* define data source : this is argument i */
23fcceca 244 printf("TPCCEda: Processing file %s\n", argv[i]);
5a3a40fa 245 status=monitorSetDataSource( argv[i] );
246 if (status!=0) {
23fcceca 247 printf("TPCCEda: monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
5a3a40fa 248 return -1;
249 }
250
db7038d0 251
5a3a40fa 252 /* read until EOF */
253 while (true) {
254 struct eventHeaderStruct *event;
db7038d0 255
5a3a40fa 256 /* check shutdown condition */
257 if (daqDA_checkShutdown()) {break;}
96bf9029 258
259 /* get next event (blocking call until timeout) */
260 status=monitorGetEventDynamic((void **)&event);
261 if (status==MON_ERR_EOF) {
23fcceca 262 printf ("TPCCEda: End of File %d detected\n",i);
96bf9029 263 break; /* end of monitoring file has been reached */
5a3a40fa 264 }
ac940b58 265
96bf9029 266 if (status!=0) {
23fcceca 267 printf("TPCCEda: monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
96bf9029 268 break;
269 }
db7038d0 270
271 /* retry if got no event */
23fcceca 272 if (event==NULL){
273 //use time in between bursts to
274 // send the data to AMOREdb
275 if (stopWatch.RealTime()>updateInterval){
9f02b62b 276 calibCE->Analyse();
23fcceca 277 SendToAmoreDB(calibCE,runNb);
278 stopWatch.Start();
279 } else {
280 stopWatch.Continue();
281 }
282 //debug output
283 if (nevents>neventsOld){
9f02b62b 284 printf ("TPCCEda: %d events processed, %d used\n",nevents,calibCE->GetNeventsProcessed());
23fcceca 285 neventsOld=nevents;
286 }
287
96bf9029 288 continue;
23fcceca 289 }
6e7d7dc4 290
ac940b58 291 /* skip start/end of run events */
96bf9029 292 if ( (event->eventType != physicsEvent) && (event->eventType != calibrationEvent) ){
293 free(event);
294 continue;
295 }
ac940b58 296
96bf9029 297
298 // get the run number
299 runNb = event->eventRunNb;
300
301 // CE calibration
9f02b62b 302 calibCE->ProcessEvent(event);
96bf9029 303
96bf9029 304 /* free resources */
305 free(event);
306 ++nevents;
db7038d0 307 }
308 }
db7038d0 309
310 //
ac940b58 311 // Analyse CE data and write them to rootfile
312 //
9f02b62b 313 printf ("TPCCEda: %d events processed, %d used\n",nevents,calibCE->GetNeventsProcessed());
db7038d0 314
9f02b62b 315// TFile * fileTPC = new TFile (RESULT_FILE,"recreate");
316// calibCE->Write("tpcCalibCE");
317// delete fileTPC;
e9d80afd 318
9f02b62b 319 calibCE->DumpToFile(RESULT_FILE,Form("name=tpcCalibCE,type=%d",saveOption));
23fcceca 320 printf("TPCCEda: Wrote %s\n",RESULT_FILE);
db7038d0 321
f2c72763 322 /* store the result file on FES */
db7038d0 323
bd955ed2 324 status=daqDA_FES_storeFile(RESULT_FILE,FILE_ID);
f2c72763 325 if (status) {
326 status = -2;
327 }
9f02b62b 328
e9d80afd 329 printf("TPCCEda: Amore part\n");
330 calibCE->Analyse();
331 SendToAmoreDB(calibCE,runNb);
332
9f02b62b 333 delete calibCE;
db7038d0 334 return status;
335}
f2c72763 336
db7038d0 337
9f02b62b 338void SendToAmoreDB(AliTPCCalibCE *calibCE, unsigned long32 runNb)
db7038d0 339{
340 //AMORE
96bf9029 341// printf ("AMORE part\n");
ac940b58 342 const char *amoreDANameorig=gSystem->Getenv("AMORE_DA_NAME");
343 //cheet a little -- temporary solution (hopefully)
344 //
345 //currently amoreDA uses the environment variable AMORE_DA_NAME to create the mysql
346 //table in which the calib objects are stored. This table is dropped each time AmoreDA
347 //is initialised. This of course makes a problem if we would like to store different
348 //calibration entries in the AMORE DB. Therefore in each DA which writes to the AMORE DB
db7038d0 349 //the AMORE_DA_NAME env variable is overwritten.
6a02fd57 350 gSystem->Setenv("AMORE_DA_NAME",Form("TPC-%s",FILE_ID));
ac940b58 351 //
352 // end cheet
9f02b62b 353 TGraph *grA=calibCE->MakeGraphTimeCE(-1,0,2);
354 TGraph *grC=calibCE->MakeGraphTimeCE(-2,0,2);
6a02fd57 355 TDatime time;
6e7d7dc4 356 TObjString info(Form("Run: %u; Date: %s",runNb,time.AsSQLString()));
6a02fd57 357 amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
358 Int_t statusDA=0;
9f02b62b 359 statusDA+=amoreDA.Send("CET0",calibCE->GetCalPadT0());
360 statusDA+=amoreDA.Send("CEQ",calibCE->GetCalPadQ());
361 statusDA+=amoreDA.Send("CERMS",calibCE->GetCalPadRMS());
db7038d0 362 statusDA+=amoreDA.Send("DriftA",grA);
363 statusDA+=amoreDA.Send("DriftC",grC);
6a02fd57 364 statusDA+=amoreDA.Send("Info",&info);
365 if ( statusDA!=0 )
23fcceca 366 printf("TPCCEda: Waring: Failed to write one of the calib objects to the AMORE database\n");
db7038d0 367 // reset env var
6a02fd57 368 if (amoreDANameorig) gSystem->Setenv("AMORE_DA_NAME",amoreDANameorig);
db7038d0 369 if (grA) delete grA;
370 if (grC) delete grC;
5a3a40fa 371}
db7038d0 372
373