new module for hadron correlations
[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 <pthread.h>
43#include <vector>
5a3a40fa 44//
45//Root includes
46//
47#include <TFile.h>
48#include "TROOT.h"
49#include "TPluginManager.h"
ac940b58 50#include "TSystem.h"
51#include "TString.h"
52#include "TObjString.h"
53#include "TDatime.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
82void SendToAmoreDB(AliTPCCalibCE &calibCE, unsigned long32 runNb);
83//for threaded processing
84void *processEventBuffer(void *arg);
85
86//common event processing variables for threaded processing
87std::vector<eventHeaderStruct*> eventBuffer;
88volatile int bStop = false;
89struct timespec duree_nanosleep;
90Int_t forceNevents=-1;
91Bool_t forceBufferEnds=kFALSE;
5a3a40fa 92
93
94/* Main routine
95 Arguments: list of DATE raw data files
96*/
97int main(int argc, char **argv) {
ac940b58 98 /* log start of process */
99 printf("TPC CE DA started - %s\n",__FILE__);
db7038d0 100
ac940b58 101 if (argc<2) {
102 printf("Wrong number of arguments\n");
103 return -1;
104 }
105
106 AliLog::SetClassDebugLevel("AliTPCRawStream",-5);
107 AliLog::SetClassDebugLevel("AliRawReaderDate",-5);
108 AliLog::SetClassDebugLevel("AliTPCAltroMapping",-5);
109 AliLog::SetModuleDebugLevel("RAW",-5);
db7038d0 110
111 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
112 "*",
113 "TStreamerInfo",
114 "RIO",
115 "TStreamerInfo()");
116
ac940b58 117 /* declare monitoring program */
db7038d0 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 monitorSetNowait();
125 monitorSetNoWaitNetworkTimeout(1000);
126
127 //variables
b401648b 128 AliTPCmapper *mapping = 0; // The TPC mapping
ac940b58 129 char localfile[255];
130 unsigned long32 runNb=0; //run number
db7038d0 131
132 //
133 // thread wait time
134 //
135 duree_nanosleep.tv_sec=0;
136 duree_nanosleep.tv_nsec=1000000; //1 ms
137
138 //
139 // DA configuration from configuration file
140 //
141 //retrieve configuration file
142 sprintf(localfile,"./%s",CONFIG_FILE);
143 status = daqDA_DB_getFile(CONFIG_FILE,localfile);
144 if (status) {
145 printf("Failed to get configuration file (%s) from DAQdetDB, status=%d\n", CONFIG_FILE, status);
146 return -1;
147 }
148 AliTPCConfigDA config(CONFIG_FILE);
149 // check configuration options
150 TString laserTriggerName("C0LSR-ABCE-NOPF-CENT");
151 size_t bufferSize=30;
152 Int_t forceTriggerId=-1;
153 Int_t forceNeventsStandalone=-1;
154 Bool_t forceBufferEndsGlobal=kTRUE;
155// Bool_t forceBufferEndsGlobalDummy=kFALSE;
156 if ( config.GetConfigurationMap()->GetValue("LaserTriggerName") ) {
157 laserTriggerName=config.GetConfigurationMap()->GetValue("LaserTriggerName")->GetName();
158 printf("Laser trigger class name set to: %s.\n",laserTriggerName.Data());
159 }
160
161 if ( config.GetConfigurationMap()->GetValue("BufferSize") ) {
162 bufferSize=(size_t)config.GetValue("BufferSize");
163 printf("Setting event buffer size to: %d.\n",bufferSize);
164 }
165
166 if ( config.GetConfigurationMap()->GetValue("ForceTriggerId") ) {
167 forceTriggerId=TMath::Nint(config.GetValue("ForceTriggerId"));
168 printf("Only processing triggers with Id: %d.\n",forceTriggerId);
169 }
170
171 if ( config.GetConfigurationMap()->GetValue("ForceBufferEndsGlobal") ) {
172 forceBufferEndsGlobal=config.GetValue("ForceBufferEndsGlobal")!=0.;
173 printf("Process all buffered events in global partition: %s.\n",forceBufferEndsGlobal?"yes":"no");
174 }
175
176 if ( config.GetConfigurationMap()->GetValue("ForceNMaxEvents") ) {
177 forceNevents=TMath::Nint(config.GetValue("ForceNMaxEvents"));
178 printf("Forcing maximum number of %d events.\n",forceNeventsStandalone);
179 }
180
181 //subsribe to laser triggers only in physics partition
182 //if the trigger class is not available the return value is -1
183 //in this case we are most probably running as a standalone
184 // laser run and should request all events
185 unsigned char *classIdptr=0;
186 int retClassId=daqDA_getClassIdFromName(laserTriggerName.Data(),classIdptr);
187 if (retClassId==0){
188 //interleaved laser in physics runs
189 char c[5];
190 sprintf(c,"%u",*classIdptr);
191 char *table[5] = {"PHY","Y","*",c,NULL};
192 monitorDeclareTableExtended(table);
193 printf("Using trigger class Id: %s\n",c);
194// forceBufferEndsGlobal=forceBufferEndsGlobalDummy;
195 } else if (retClassId==-1){
196 //defaul case, accept all physics events
197 char *table[3] = {"PHY","Y",NULL};
198 monitorDeclareTableExtended(table);
199 printf("Using all trigger class Ids\n");
200// forceNevents=forceNeventsStandalone;
201 } else {
202 printf("Unknown return value of 'daqDA_getClassIdFromName': %d\n",retClassId);
203 return -2;
204 }
205
206 //see if we should force the trigger id
207 if (forceTriggerId>-1){
208 char c[5];
209 sprintf(c,"%d",forceTriggerId);
210 char *table[5] = {"PHY","Y","*",c,NULL};
211 monitorDeclareTableExtended(table);
212// forceBufferEndsGlobal=forceBufferEndsGlobalDummy;
213 }
214
215
216 // if test setup get parameters from $DAQDA_TEST_DIR
b401648b 217 if (!mapping){
218 /* copy locally the mapping file from daq detector config db */
ac940b58 219 sprintf(localfile,"./%s",MAPPING_FILE);
220 status = daqDA_DB_getFile(MAPPING_FILE,localfile);
b401648b 221 if (status) {
222 printf("Failed to get mapping file (%s) from DAQdetDB, status=%d\n", MAPPING_FILE, status);
ac940b58 223 return -1;
b401648b 224 }
db7038d0 225
b401648b 226 /* open the mapping file and retrieve mapping object */
227 TFile *fileMapping = new TFile(MAPPING_FILE, "read");
228 mapping = (AliTPCmapper*) fileMapping->Get("tpcMapping");
229 delete fileMapping;
04049c81 230 }
db7038d0 231
04049c81 232 if (mapping == 0) {
b401648b 233 printf("Failed to get mapping object from %s. ...\n", MAPPING_FILE);
ac940b58 234 return -1;
04049c81 235 } else {
236 printf("Got mapping object from %s\n", MAPPING_FILE);
237 }
ac940b58 238
db7038d0 239
240 //create calibration object
ac940b58 241 AliTPCCalibCE calibCE(config.GetConfigurationMap()); // central electrode calibration
242 calibCE.SetAltroMapping(mapping->GetAltroMapping()); // Use altro mapping we got from daqDetDb
5a3a40fa 243
db7038d0 244 //
245 // start thread
246 //
247// sleep(5);
248 pthread_t threadId=0;
249 int threadStatus=0;
250 threadStatus = pthread_create( &threadId, NULL, processEventBuffer, (void*)(&calibCE));
251 eventBuffer.resize(bufferSize);
252 struct timespec duree_out;
ac940b58 253 //===========================//
254 // loop over RAW data files //
255 //==========================//
5a3a40fa 256 int nevents=0;
db7038d0 257 size_t counter=0;
ac940b58 258 for ( i=1; i<argc; i++) {
db7038d0 259
5a3a40fa 260 /* define data source : this is argument i */
261 printf("Processing file %s\n", argv[i]);
262 status=monitorSetDataSource( argv[i] );
263 if (status!=0) {
264 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
265 return -1;
266 }
267
db7038d0 268
5a3a40fa 269 /* read until EOF */
270 while (true) {
271 struct eventHeaderStruct *event;
db7038d0 272
5a3a40fa 273 /* check shutdown condition */
274 if (daqDA_checkShutdown()) {break;}
275
db7038d0 276 //check for predefined number of events
277 if (forceNevents>0 && calibCE.GetNeventsProcessed()>=forceNevents) {
278 printf("Requested number of events reached (%d).\n",forceNevents);
5a3a40fa 279 break;
280 }
ac940b58 281
db7038d0 282 //buffer events, only read them if a buffer position is free
283 if (eventBuffer[counter]==0) {
284
285 /* get next event (blocking call until timeout) */
286 status=monitorGetEventDynamic((void **)&event);
287 if (status==MON_ERR_EOF) {
288 printf ("End of File %d detected\n",i);
289 break; /* end of monitoring file has been reached */
290 }
291
292 if (status!=0) {
293 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
294 break;
295 }
296
297 /* retry if got no event */
298 if (event==NULL)
299 continue;
6e7d7dc4 300
ac940b58 301 /* skip start/end of run events */
db7038d0 302 if ( (event->eventType != physicsEvent) && (event->eventType != calibrationEvent) ){
303 free(event);
304 continue;
305 }
5a3a40fa 306
ac940b58 307
db7038d0 308 // get the run number
309 runNb = event->eventRunNb;
310
311// printf(" trigger (%05d-%03d) = %8.8x %8.8x - %02u\n",nevents, calibCE.GetNeventsProcessed(),
312// event->eventTriggerPattern[1], event->eventTriggerPattern[0],event->eventType);
313
314// printf("filling buffer %d\n",counter);
315 eventBuffer[counter]=event;
316
317 ++nevents;
318 ++counter;
319 if (counter >= eventBuffer.size()) {counter=0;}
320 }else{
321// printf("buffer already used: %d\n",counter);
322 nanosleep(&duree_nanosleep,&duree_out);
323 }
324 }
325 }
326
327 //
328 // wait for thread to end
329 //
330 if (!forceBufferEndsGlobal) bStop = true;
331 else forceBufferEnds=forceBufferEndsGlobal;
332
333 pthread_join( threadId, NULL);
334// printf("Event Processing Thread ended with: %d\n",threadStatus);
335
336 //
337 // free unprocessed events
338 //
339 for (size_t i=0;i<eventBuffer.size();++i){
340 if (eventBuffer[i]) {
341 free(eventBuffer[i]);
342 eventBuffer[i]=0;
343// printf("freeing buffer %d\n",i);
5a3a40fa 344 }
345 }
346
ac940b58 347 //
348 // Analyse CE data and write them to rootfile
349 //
350 calibCE.Analyse();
db7038d0 351 printf ("%d events processed, %d used\n",nevents,calibCE.GetNeventsProcessed());
352
5a3a40fa 353 TFile * fileTPC = new TFile (RESULT_FILE,"recreate");
e73181c9 354 calibCE.Write("tpcCalibCE");
5a3a40fa 355 delete fileTPC;
356 printf("Wrote %s\n",RESULT_FILE);
db7038d0 357
f2c72763 358 /* store the result file on FES */
db7038d0 359
bd955ed2 360 status=daqDA_FES_storeFile(RESULT_FILE,FILE_ID);
f2c72763 361 if (status) {
362 status = -2;
363 }
db7038d0 364
365 SendToAmoreDB(calibCE,runNb);
366
367 return status;
368}
f2c72763 369
db7038d0 370void *processEventBuffer(void *arg)
371{
ac940b58 372 //
db7038d0 373 // event procssing thread functio
ac940b58 374 //
db7038d0 375
376 //cast argument
377 AliTPCCalibCE *ce=(AliTPCCalibCE*)arg;
378 AliTPCCalibCE &calibCE=*ce;
379
380 size_t counter=0;
381 unsigned long32 runNb=0;
382 Bool_t published=kTRUE;
383 struct timespec duree_out;
384
385 struct eventHeaderStruct *event;
386
387 //wait for the first buffer to be filled
388 while (!eventBuffer[0]) nanosleep(&duree_nanosleep,&duree_out);
389 //loop over buffer
390 while (!bStop){
391// printf("testing buffer: %d\n",counter);
392 if (eventBuffer[counter]) {
393 event=eventBuffer[counter];
394 runNb = event->eventRunNb;
395// printf("processing buffer: %d\n",counter);
396 eventBuffer[counter]=0;
397 calibCE.ProcessEvent(event);
398 free(event);
399 published=kFALSE;
400 } else {
401 //in case of empty buffer publish the results it this was not done
402 if (!published) {
403 SendToAmoreDB(calibCE,runNb);
404 published=kTRUE;
405 }
406 nanosleep(&duree_nanosleep,&duree_out);
407 }
408 ++counter;
409 if (counter >= eventBuffer.size()) {
410 counter=0;
411 if (forceBufferEnds) break;
412 }
413 }
414}
415
416void SendToAmoreDB(AliTPCCalibCE &calibCE, unsigned long32 runNb)
417{
418 //AMORE
ac940b58 419 printf ("AMORE part\n");
420 const char *amoreDANameorig=gSystem->Getenv("AMORE_DA_NAME");
421 //cheet a little -- temporary solution (hopefully)
422 //
423 //currently amoreDA uses the environment variable AMORE_DA_NAME to create the mysql
424 //table in which the calib objects are stored. This table is dropped each time AmoreDA
425 //is initialised. This of course makes a problem if we would like to store different
426 //calibration entries in the AMORE DB. Therefore in each DA which writes to the AMORE DB
db7038d0 427 //the AMORE_DA_NAME env variable is overwritten.
6a02fd57 428 gSystem->Setenv("AMORE_DA_NAME",Form("TPC-%s",FILE_ID));
ac940b58 429 //
430 // end cheet
db7038d0 431 TGraph *grA=calibCE.MakeGraphTimeCE(-1,0,2);
432 TGraph *grC=calibCE.MakeGraphTimeCE(-2,0,2);
6a02fd57 433 TDatime time;
6e7d7dc4 434 TObjString info(Form("Run: %u; Date: %s",runNb,time.AsSQLString()));
6a02fd57 435 amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
436 Int_t statusDA=0;
437 statusDA+=amoreDA.Send("CET0",calibCE.GetCalPadT0());
438 statusDA+=amoreDA.Send("CEQ",calibCE.GetCalPadQ());
439 statusDA+=amoreDA.Send("CERMS",calibCE.GetCalPadRMS());
db7038d0 440 statusDA+=amoreDA.Send("DriftA",grA);
441 statusDA+=amoreDA.Send("DriftC",grC);
6a02fd57 442 statusDA+=amoreDA.Send("Info",&info);
443 if ( statusDA!=0 )
444 printf("Waring: Failed to write one of the calib objects to the AMORE database\n");
db7038d0 445 // reset env var
6a02fd57 446 if (amoreDANameorig) gSystem->Setenv("AMORE_DA_NAME",amoreDANameorig);
db7038d0 447 if (grA) delete grA;
448 if (grC) delete grC;
5a3a40fa 449}
db7038d0 450
451