]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/TPCCEda.cxx
TPCCEda.cxx.diff update CEda for interleaved laser trigger running. Allow...
[u/mrichter/AliRoot.git] / TPC / TPCCEda.cxx
1 /*
2 TPC DA for online calibration
3
4 Contact: Haavard.Helstrup@cern.ch
5 Link: 
6 Run Type: PHYSICS STANDALONE DAQ
7 DA Type: MON
8 Number of events needed: 500
9 Input Files: 
10 Output Files: tpcCE.root, to be exported to the DAQ FXS
11 fileId:   CE
12 Trigger types used: PHYSICS_EVENT
13
14 */
15
16 /*
17
18 TPCCEda.cxx - calibration algorithm for TPC Central Electrode events
19
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.
23
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 "tpcCE.root"
33 #define FILE_ID "CE"
34 #define MAPPING_FILE "tpcMapping.root"
35 #define CONFIG_FILE "TPCCEda.conf"
36
37 #include <daqDA.h>
38 #include "event.h"
39 #include "monitor.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <pthread.h>
43 #include <vector>
44 //
45 //Root includes
46 //
47 #include <TFile.h>
48 #include "TROOT.h"
49 #include "TPluginManager.h"
50 #include "TSystem.h"
51 #include "TString.h"
52 #include "TObjString.h"
53 #include "TDatime.h"
54 #include "TMap.h"
55 #include "TGraph.h"
56 #include "TMath.h"
57 //
58 //AliRoot includes
59 //
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"
69 #include "AliLog.h"
70 #include "AliTPCConfigDA.h"
71 //
72 //AMORE
73 //
74 #include <AmoreDA.h>
75 //
76 // TPC calibration algorithm includes
77 //
78 #include "AliTPCCalibCE.h"
79
80
81 //functios, implementation below
82 void SendToAmoreDB(AliTPCCalibCE &calibCE, unsigned long32 runNb);
83 //for threaded processing
84 void *processEventBuffer(void *arg);
85
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;
92
93
94 /* Main routine
95       Arguments: list of DATE raw data files
96 */
97 int main(int argc, char **argv) {
98   /* log start of process */
99   printf("TPC CE DA started - %s\n",__FILE__);
100   
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);
110   
111   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
112                                         "*",
113                                         "TStreamerInfo",
114                                         "RIO",
115                                         "TStreamerInfo()");
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   monitorSetNowait();
125   monitorSetNoWaitNetworkTimeout(1000);
126
127   //variables
128   AliTPCmapper *mapping = 0;   // The TPC mapping
129   char localfile[255];
130   unsigned long32 runNb=0;      //run number
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
217   if (!mapping){
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);
221     if (status) {
222       printf("Failed to get mapping file (%s) from DAQdetDB, status=%d\n", MAPPING_FILE, status);
223       return -1;
224     }
225     
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;
230   }
231   
232   if (mapping == 0) {
233     printf("Failed to get mapping object from %s.  ...\n", MAPPING_FILE);
234     return -1;
235   } else {
236     printf("Got mapping object from %s\n", MAPPING_FILE);
237   }
238   
239     
240   //create calibration object
241   AliTPCCalibCE calibCE(config.GetConfigurationMap());   // central electrode calibration
242   calibCE.SetAltroMapping(mapping->GetAltroMapping()); // Use altro mapping we got from daqDetDb
243
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;
253   //===========================//
254   // loop over RAW data files //
255   //==========================//
256   int nevents=0;
257   size_t counter=0;
258   for ( i=1; i<argc; i++) {
259     
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
268     
269     /* read until EOF */
270     while (true) {
271       struct eventHeaderStruct *event;
272       
273       /* check shutdown condition */
274       if (daqDA_checkShutdown()) {break;}
275
276       //check for predefined number of events
277       if (forceNevents>0 && calibCE.GetNeventsProcessed()>=forceNevents) {
278         printf("Requested number of events reached (%d).\n",forceNevents);
279         break;
280       }
281       
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;
300       
301       /* skip start/end of run events */
302         if ( (event->eventType != physicsEvent) && (event->eventType != calibrationEvent) ){
303           free(event);
304           continue;
305         }
306
307       
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);
344     }
345   }
346
347   //
348   // Analyse CE data and write them to rootfile
349   //
350   calibCE.Analyse();
351   printf ("%d events processed, %d used\n",nevents,calibCE.GetNeventsProcessed());
352   
353   TFile * fileTPC = new TFile (RESULT_FILE,"recreate");
354   calibCE.Write("tpcCalibCE");
355   delete fileTPC;
356   printf("Wrote %s\n",RESULT_FILE);
357   
358   /* store the result file on FES */
359   
360   status=daqDA_FES_storeFile(RESULT_FILE,FILE_ID);
361   if (status) {
362     status = -2;
363   }
364   
365   SendToAmoreDB(calibCE,runNb);
366   
367   return status;
368 }
369
370 void *processEventBuffer(void *arg)
371 {
372   //
373   // event procssing thread functio
374   //
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
416 void SendToAmoreDB(AliTPCCalibCE &calibCE, unsigned long32 runNb)
417 {
418   //AMORE
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
427   //the AMORE_DA_NAME env variable is overwritten.
428   gSystem->Setenv("AMORE_DA_NAME",Form("TPC-%s",FILE_ID));
429   //
430   // end cheet
431   TGraph *grA=calibCE.MakeGraphTimeCE(-1,0,2);
432   TGraph *grC=calibCE.MakeGraphTimeCE(-2,0,2);
433   TDatime time;
434   TObjString info(Form("Run: %u; Date: %s",runNb,time.AsSQLString()));
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());
440   statusDA+=amoreDA.Send("DriftA",grA);
441   statusDA+=amoreDA.Send("DriftC",grC);
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");
445   // reset env var
446   if (amoreDANameorig) gSystem->Setenv("AMORE_DA_NAME",amoreDANameorig);
447   if (grA) delete grA;
448   if (grC) delete grC;
449 }
450
451