2 TPC DA for online calibration
4 Contact: Haavard.Helstrup@cern.ch
6 Run Type: PHYSICS STANDALONE DAQ
8 Number of events needed: 500
10 Output Files: tpcCE.root, to be exported to the DAQ FXS
12 Trigger types used: PHYSICS_EVENT
18 TPCCEda.cxx - calibration algorithm for TPC Central Electrode events
20 10/06/2007 sylvain.chapeland@cern.ch : first version - clean skeleton based on DAQ DA case1
21 06/12/2007 haavard.helstrup@cern.ch : created CE DA based on pulser code
22 19/09/2008 J.Wiechula@gsi.de: Added support for configuration files.
24 contact: marian.ivanov@cern.ch
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).
32 #define RESULT_FILE "tpcCE.root"
34 #define MAPPING_FILE "tpcMapping.root"
35 #define CONFIG_FILE "TPCCEda.conf"
49 #include "TPluginManager.h"
52 #include "TObjString.h"
60 #include "AliRawReader.h"
61 #include "AliRawReaderDate.h"
62 #include "AliTPCmapper.h"
63 #include "AliTPCRawStream.h"
64 #include "AliTPCROC.h"
65 #include "AliTPCCalROC.h"
66 #include "AliTPCCalPad.h"
67 #include "AliMathBase.h"
68 #include "TTreeStream.h"
70 #include "AliTPCConfigDA.h"
76 // TPC calibration algorithm includes
78 #include "AliTPCCalibCE.h"
81 //functios, implementation below
82 void SendToAmoreDB(AliTPCCalibCE &calibCE, unsigned long32 runNb);
83 //for threaded processing
84 void *processEventBuffer(void *arg);
86 //common event processing variables for threaded processing
87 std::vector<eventHeaderStruct*> eventBuffer;
88 volatile int bStop = false;
89 struct timespec duree_nanosleep;
90 Int_t forceNevents=-1;
91 Bool_t forceBufferEnds=kFALSE;
95 Arguments: list of DATE raw data files
97 int main(int argc, char **argv) {
98 /* log start of process */
99 printf("TPC CE DA started - %s\n",__FILE__);
102 printf("Wrong number of arguments\n");
106 AliLog::SetClassDebugLevel("AliTPCRawStream",-5);
107 AliLog::SetClassDebugLevel("AliRawReaderDate",-5);
108 AliLog::SetClassDebugLevel("AliTPCAltroMapping",-5);
109 AliLog::SetModuleDebugLevel("RAW",-5);
111 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
117 /* declare monitoring program */
119 status=monitorDeclareMp( __FILE__ );
121 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
125 monitorSetNoWaitNetworkTimeout(1000);
128 AliTPCmapper *mapping = 0; // The TPC mapping
130 unsigned long32 runNb=0; //run number
135 duree_nanosleep.tv_sec=0;
136 duree_nanosleep.tv_nsec=1000000; //1 ms
139 // DA configuration from configuration file
141 //retrieve configuration file
142 sprintf(localfile,"./%s",CONFIG_FILE);
143 status = daqDA_DB_getFile(CONFIG_FILE,localfile);
145 printf("Failed to get configuration file (%s) from DAQdetDB, status=%d\n", CONFIG_FILE, status);
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());
161 if ( config.GetConfigurationMap()->GetValue("BufferSize") ) {
162 bufferSize=(size_t)config.GetValue("BufferSize");
163 printf("Setting event buffer size to: %d.\n",bufferSize);
166 if ( config.GetConfigurationMap()->GetValue("ForceTriggerId") ) {
167 forceTriggerId=TMath::Nint(config.GetValue("ForceTriggerId"));
168 printf("Only processing triggers with Id: %d.\n",forceTriggerId);
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");
176 if ( config.GetConfigurationMap()->GetValue("ForceNMaxEvents") ) {
177 forceNevents=TMath::Nint(config.GetValue("ForceNMaxEvents"));
178 printf("Forcing maximum number of %d events.\n",forceNeventsStandalone);
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);
188 //interleaved laser in physics runs
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;
202 printf("Unknown return value of 'daqDA_getClassIdFromName': %d\n",retClassId);
206 //see if we should force the trigger id
207 if (forceTriggerId>-1){
209 sprintf(c,"%d",forceTriggerId);
210 char *table[5] = {"PHY","Y","*",c,NULL};
211 monitorDeclareTableExtended(table);
212 // forceBufferEndsGlobal=forceBufferEndsGlobalDummy;
216 // if test setup get parameters from $DAQDA_TEST_DIR
218 /* copy locally the mapping file from daq detector config db */
219 sprintf(localfile,"./%s",MAPPING_FILE);
220 status = daqDA_DB_getFile(MAPPING_FILE,localfile);
222 printf("Failed to get mapping file (%s) from DAQdetDB, status=%d\n", MAPPING_FILE, status);
226 /* open the mapping file and retrieve mapping object */
227 TFile *fileMapping = new TFile(MAPPING_FILE, "read");
228 mapping = (AliTPCmapper*) fileMapping->Get("tpcMapping");
233 printf("Failed to get mapping object from %s. ...\n", MAPPING_FILE);
236 printf("Got mapping object from %s\n", MAPPING_FILE);
240 //create calibration object
241 AliTPCCalibCE calibCE(config.GetConfigurationMap()); // central electrode calibration
242 calibCE.SetAltroMapping(mapping->GetAltroMapping()); // Use altro mapping we got from daqDetDb
248 pthread_t threadId=0;
250 threadStatus = pthread_create( &threadId, NULL, processEventBuffer, (void*)(&calibCE));
251 eventBuffer.resize(bufferSize);
252 struct timespec duree_out;
253 //===========================//
254 // loop over RAW data files //
255 //==========================//
258 for ( i=1; i<argc; i++) {
260 /* define data source : this is argument i */
261 printf("Processing file %s\n", argv[i]);
262 status=monitorSetDataSource( argv[i] );
264 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
271 struct eventHeaderStruct *event;
273 /* check shutdown condition */
274 if (daqDA_checkShutdown()) {break;}
276 //check for predefined number of events
277 if (forceNevents>0 && calibCE.GetNeventsProcessed()>=forceNevents) {
278 printf("Requested number of events reached (%d).\n",forceNevents);
282 //buffer events, only read them if a buffer position is free
283 if (eventBuffer[counter]==0) {
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 */
293 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
297 /* retry if got no event */
301 /* skip start/end of run events */
302 if ( (event->eventType != physicsEvent) && (event->eventType != calibrationEvent) ){
308 // get the run number
309 runNb = event->eventRunNb;
311 // printf(" trigger (%05d-%03d) = %8.8x %8.8x - %02u\n",nevents, calibCE.GetNeventsProcessed(),
312 // event->eventTriggerPattern[1], event->eventTriggerPattern[0],event->eventType);
314 // printf("filling buffer %d\n",counter);
315 eventBuffer[counter]=event;
319 if (counter >= eventBuffer.size()) {counter=0;}
321 // printf("buffer already used: %d\n",counter);
322 nanosleep(&duree_nanosleep,&duree_out);
328 // wait for thread to end
330 if (!forceBufferEndsGlobal) bStop = true;
331 else forceBufferEnds=forceBufferEndsGlobal;
333 pthread_join( threadId, NULL);
334 // printf("Event Processing Thread ended with: %d\n",threadStatus);
337 // free unprocessed events
339 for (size_t i=0;i<eventBuffer.size();++i){
340 if (eventBuffer[i]) {
341 free(eventBuffer[i]);
343 // printf("freeing buffer %d\n",i);
348 // Analyse CE data and write them to rootfile
351 printf ("%d events processed, %d used\n",nevents,calibCE.GetNeventsProcessed());
353 TFile * fileTPC = new TFile (RESULT_FILE,"recreate");
354 calibCE.Write("tpcCalibCE");
356 printf("Wrote %s\n",RESULT_FILE);
358 /* store the result file on FES */
360 status=daqDA_FES_storeFile(RESULT_FILE,FILE_ID);
365 SendToAmoreDB(calibCE,runNb);
370 void *processEventBuffer(void *arg)
373 // event procssing thread functio
377 AliTPCCalibCE *ce=(AliTPCCalibCE*)arg;
378 AliTPCCalibCE &calibCE=*ce;
381 unsigned long32 runNb=0;
382 Bool_t published=kTRUE;
383 struct timespec duree_out;
385 struct eventHeaderStruct *event;
387 //wait for the first buffer to be filled
388 while (!eventBuffer[0]) nanosleep(&duree_nanosleep,&duree_out);
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);
401 //in case of empty buffer publish the results it this was not done
403 SendToAmoreDB(calibCE,runNb);
406 nanosleep(&duree_nanosleep,&duree_out);
409 if (counter >= eventBuffer.size()) {
411 if (forceBufferEnds) break;
416 void SendToAmoreDB(AliTPCCalibCE &calibCE, unsigned long32 runNb)
419 printf ("AMORE part\n");
420 const char *amoreDANameorig=gSystem->Getenv("AMORE_DA_NAME");
421 //cheet a little -- temporary solution (hopefully)
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
427 //the AMORE_DA_NAME env variable is overwritten.
428 gSystem->Setenv("AMORE_DA_NAME",Form("TPC-%s",FILE_ID));
431 TGraph *grA=calibCE.MakeGraphTimeCE(-1,0,2);
432 TGraph *grC=calibCE.MakeGraphTimeCE(-2,0,2);
434 TObjString info(Form("Run: %u; Date: %s",runNb,time.AsSQLString()));
435 amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
437 statusDA+=amoreDA.Send("CET0",calibCE.GetCalPadT0());
438 statusDA+=amoreDA.Send("CEQ",calibCE.GetCalPadQ());
439 statusDA+=amoreDA.Send("CERMS",calibCE.GetCalPadRMS());
440 statusDA+=amoreDA.Send("DriftA",grA);
441 statusDA+=amoreDA.Send("DriftC",grC);
442 statusDA+=amoreDA.Send("Info",&info);
444 printf("Waring: Failed to write one of the calib objects to the AMORE database\n");
446 if (amoreDANameorig) gSystem->Setenv("AMORE_DA_NAME",amoreDANameorig);