]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/ITSSPDFOda.cxx
A more elaborate approach for associating the reconstructed and MC pileup vertices
[u/mrichter/AliRoot.git] / ITS / ITSSPDFOda.cxx
1 /*
2 Contact: annalisa.mastroserio@cern.ch
3 Link: tydes.home.cern.ch/tydes/doc/CalibrationOverview/CalibrationAlgorithms/
4 Run Type: DAQ_FO_UNIF_SCAN
5 DA Type: LDC
6 Number of events needed: Depending on scan type
7 Input Files: spd_focalib_params, raw data
8 Output Files: ./calibResults/ScanDCSconfigToFXS/* 
9 Trigger types used: PHYSICS
10 */
11
12 ////////////////////////////////////////////////////////////////////////////////
13 // This program can be compiled in two modes.                                 //
14 //                                                                            //
15 // 1. With the DAQ DA framework on. This is the default operating mode.       //
16 // Call this program with the name of the executable followed by the          //
17 // data files to process.                                                     //
18 //                                                                            //
19 // 2. Without the DAQ DA framework on. Define the SPD_DA_OFF environment var. //
20 // Call this program with the name of the executable followed by the          //
21 // runNr and the data files to process.                                       //
22 //                                                                            //
23 ////////////////////////////////////////////////////////////////////////////////
24
25 #ifndef SPD_DA_OFF
26 extern "C" {
27 #include <daqDA.h>
28 }
29 #endif
30 #include "event.h"
31 #include "monitor.h"
32 #include "AliRawReaderDate.h"
33 #include "AliITSRawStreamSPD.h"
34 #include "AliITSOnlineSPDfoChip.h"
35 #include "AliITSOnlineSPDfoInfo.h"
36 #include "AliITSOnlineSPDfo.h"
37 #include "AliITSOnlineSPDfoAnalyzer.h"
38 #include "AliLog.h"
39 #include <Riostream.h>
40 #include <fstream>
41 #include <TROOT.h>
42 #include <TStopwatch.h>
43 #include <TPluginManager.h>
44 #include <TObjArray.h>
45 #include <TClonesArray.h>
46 #include <TObjString.h>
47 #include <TString.h>
48 #include <TFitter.h>
49 #include <TFile.h>
50 #include <cstdlib>
51
52 int main(int argc, char **argv) {
53   if (argc<2) {
54     printf("Wrong number of arguments\n");
55     return -1;
56   }
57   
58   
59   TStopwatch timer;
60   timer.Start();
61   
62   // directory structure, hard coded
63   char *saveDirDCSconfigToFXS= "./calibResults/ScanDCSconfigToFXS"; //     may delete content
64   char *configFilesDir       = "./configFiles";                     //     may delete content
65   char *saveDirIdsToFXS      = "./calibResults/IdsToFXS"; 
66
67   // make sure the directory structure is correct:
68   system("mkdir ./calibResults >& /dev/null");
69   system("mkdir ./calibResults/ScanDCSconfigToFXS >& /dev/null");
70   system("mkdir ./calibResults/IdsToFXS >& /dev/null");
71   system("mkdir ./configFiles >& /dev/null");
72
73
74   // parameters config files
75   TString thresholdsFileName = Form("%s/focalib_params.txt",configFilesDir); 
76   
77   TFitter *fitter = new TFitter(3);
78   TVirtualFitter::SetFitter(fitter);
79   
80   // This line is needed in case of a stand-alone application w/o
81   // $ROOTSYS/etc/system.rootrc file
82   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
83                                         "*",
84                                         "TStreamerInfo",
85                                         "RIO",
86                                         "TStreamerInfo()");
87   
88   // turn off annoying warning messages
89   // NB: Should not be handled here 
90   AliLog* logger = AliLog::GetRootLogger();
91   logger->SetGlobalDebugLevel(-20);
92   
93 // ********* STEP 0: Get configuration files from db (if there are any) , then read parameters*********  
94   
95   //chip efficiency selection parameters (needed afterwards for the data analysis)
96   Int_t status = 0;
97 #ifndef SPD_DA_OFF
98   TString idp = "spd_focalib_params";
99   status=daqDA_DB_getFile(idp.Data(),thresholdsFileName.Data());
100   if (status) {
101     printf("Failed to get config file %s: status=%d. Using default tuning parameters.\n",idp.Data(),status);
102     TString rmCmd = Form("rm -f %s",thresholdsFileName.Data());
103     system(rmCmd.Data());
104   }
105 #endif
106    
107   
108   
109   // ********* STEP 1: Produce FO scan container files (Reference Data). ***********************************
110   int startSeg = 1;
111   
112 #ifndef SPD_DA_OFF
113   if (getenv("DATE_RUN_NUMBER")==0) {
114     printf("DATE_RUN_NUMBER not properly set.\n");
115     return -1;
116   }
117   int runNr = atoi(getenv("DATE_RUN_NUMBER"));
118 #else
119   int runNr = atoi(argv[1]);
120   startSeg = 2;
121 #endif
122   
123   Int_t evType =-1;
124   AliITSOnlineSPDfoInfo *info[20]; Int_t ntriggers[20]; Int_t vDB[20]; Bool_t iseq[20];
125   AliITSOnlineSPDfo *fomanager[20];
126   TString s = "focalib";
127   
128   for(Int_t equip =0; equip < 20; equip++ ) {        
129     info[equip] = new AliITSOnlineSPDfoInfo();
130     info[equip]->SetRunNumber(runNr);
131     info[equip]->SetRouter(equip);
132     ntriggers[equip] = 0;
133     vDB[equip] =0;  
134     iseq[equip]=kFALSE;
135     fomanager[equip] = new AliITSOnlineSPDfo(s,runNr,equip);
136   }
137   
138   
139   // loop over run segments
140   for (int segNr=startSeg; segNr<argc; segNr++) {
141     
142     int status;
143     
144     // define data source : this is argument 1   
145     status=monitorSetDataSource( argv[segNr] );
146     if (status!=0) {
147       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
148       return -1;
149     }
150     // declare monitoring program 
151     status=monitorDeclareMp("ITS_SPD_CAL");
152     if (status!=0) {
153       printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
154       return -1;
155     }
156     // define wait event timeout - 1s max 
157     monitorSetNowait();
158     monitorSetNoWaitNetworkTimeout(1000);
159     
160     Int_t eventType;
161     UInt_t eventNr=0;
162       
163     // main loop (infinite) 
164     for(;;) {
165       
166       struct eventHeaderStruct *event;
167       eventTypeType eventT;
168       
169       /* check shutdown condition */
170 #ifndef SPD_DA_OFF
171       if (daqDA_checkShutdown()) {break;}
172 #endif
173       /* get next event (blocking call until timeout) */
174       status=monitorGetEventDynamic((void **)&event);
175       if (status==MON_ERR_EOF) {
176         printf ("End of File detected\n");
177         break; /* end of monitoring file has been reached */
178       }
179       if (status!=0) {
180         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
181         break;
182       }
183       /* retry if got no event */
184       if (event==NULL) {
185         continue;
186       }
187       
188       eventT=event->eventType;
189       eventType = (Int_t) eventT;
190       
191       if (eventT == PHYSICS_EVENT) {
192         
193         eventNr++;
194         //if(eventNr%5000 == 0 )printf(" eventNr %d\n",eventNr);
195         
196         AliRawReader *reader = new AliRawReaderDate((void*)event);
197         AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
198         
199         for (UInt_t eqId=0; eqId<20; eqId++) {
200           
201           reader->Reset();
202           reader->Select("ITSSPD",eqId,eqId);
203           
204           if (str->ReadCalibHeader()>0) {
205         
206             if(evType<0) evType = str->GetFOHtype();
207                
208             if(!iseq[eqId]){ // create output files           
209              fomanager[eqId]->CreateOutputFile();
210              fomanager[eqId]->SetFOscanParams(info[eqId]);
211              iseq[eqId]=kTRUE;
212             }
213                      
214             if(info[eqId]->GetNumDACindex()<1) {
215               Int_t ind =0;
216               while(str->GetFOHdacIndex(ind)>0) {
217                 info[eqId]->AddDACindex(str->GetFOHdacIndex(ind));
218                 ind++;
219               }
220             }
221                     
222             if(!ntriggers[eqId]) {
223               ntriggers[eqId] = str->GetFOHtriggers();
224               info[eqId]->SetNumTriggers(str->GetFOHtriggers()); 
225             }
226             if(!vDB[eqId])      {
227               vDB[eqId]       = str->GetFOHglobalDBversion();
228               info[eqId]->SetDBversion(str->GetFOHglobalDBversion());
229             }       
230            
231             if(!fomanager[eqId]->GetNdacs()) fomanager[eqId]->SetNdacs(str->GetFOHnumDacs());
232            
233             TArrayS dacvalues(str->GetFOHnumDacs());
234             for(Int_t n = 0; n<(Int_t)str->GetFOHnumDacs(); n++) dacvalues.AddAt(str->GetFOHdacValue(n),n);
235             
236             TArrayS dacs = fomanager[eqId]->CreateDACArray(dacvalues, info[eqId]->GetDACIndexArray());
237             
238             for(Int_t ihs =0; ihs < 6; ihs++) { // needed in the header to access the HS and ChipId info (in data it is different)
239               for(Int_t ich =0; ich < 10; ich++){
240                 if(!str->GetFOHchipPresent(ihs, ich)) continue;
241                   info[eqId]->SetActiveChipsAndHS(ihs,ich);
242                   Short_t measure[4] = {str->GetFOHMatrixID(),str->GetFOHpixelRow(), str->GetFOHpixelCol(), str->GetFOHchipCount(ihs,ich)}; 
243                   fomanager[eqId]->AddMeasurement(dacs,measure,ihs,ich);                                            
244               } // chip loop      
245             }// HS loop
246           }// if str->ReadHeader()>0;          
247         }//if eqId      
248         delete str;
249         delete reader;
250       }
251       
252       // free resources 
253       free(event);   
254     }// infinite loop
255     
256     
257  
258     
259 #ifndef SPD_DA_OFF
260     daqDA_progressReport((unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
261 #else
262     printf("progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
263 #endif
264     
265   }// loop over run segments
266   
267    
268      TString id[20], files[20];
269     for(Int_t ifile =0; ifile < 20; ifile++) {
270       if(iseq[ifile]){
271       id[ifile] = Form("SPD_ref_fo%02i",ifile);
272       files[ifile] = fomanager[ifile]->GetFile()->GetName();
273       fomanager[ifile]->WriteToFile();
274     }
275       delete fomanager[ifile];
276     }
277     
278       
279     // ANALYSIS part
280     
281     for(Int_t iff =0; iff<20 ; iff++){
282      if(!iseq[iff]) continue;
283           
284      AliITSOnlineSPDfoAnalyzer * analyzer = new AliITSOnlineSPDfoAnalyzer(Form("%i_%s%02i.root",runNr,s.Data(),iff));      
285      analyzer->ReadParamsFromLocation(configFilesDir);
286      analyzer->Process();
287      
288      TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,iff);
289      ofstream dcsfile;
290      dcsfile.open(dcsConfigFileName.Data());
291      dcsfile << "[SPD SCAN]\n"; 
292      dcsfile << "RunNumber=" << runNr << "\n";
293      dcsfile << "Type="<< evType <<"\n";
294      dcsfile << "Router=" << iff << "\n";
295      dcsfile << "ActualDetConfiguration=" << vDB[iff]<<"\n\n";
296      dcsfile << "[DACvalues]\n"; 
297      
298        for(Int_t hs =0; hs<6; hs++){
299          for(Int_t ichip =0; ichip < 10; ichip++){
300            TArrayI dacs = analyzer->ChooseDACValues(hs,ichip);
301            
302            if(dacs.GetSize() == 0) continue;
303            for(Int_t idac =0; idac < dacs.GetSize() - 1; idac++) { // -1 (the last one is the quality flag)
304              if(dacs.At(idac) >=0 ) {
305              
306                dcsfile << ((analyzer->GetFOHandler())->GetFOscanInfo())->GetDACindex(idac) << ",";
307                dcsfile << iff << ",";
308                dcsfile << hs << ",";
309                dcsfile << ichip << "=" ;
310                dcsfile << dacs.At(idac) << ",";
311                dcsfile << dacs.At(dacs.GetSize() - 1) << "\n";
312              }
313            }
314          }
315        }
316        dcsfile.close();
317     }
318     
319     
320     printf("Preparing DCS config files\n");
321     // send a tared file of all the dcsConfig text files
322     TString command = Form("cd %s; tar -cf dcsConfig.tar *",saveDirDCSconfigToFXS);
323     //printf("\n\n%s\n\n",command.Data());
324     system(command.Data());
325     TString fileName = Form("%s/dcsConfig.tar",saveDirDCSconfigToFXS);
326     TString iddcs = "SPD_dcsConfig";
327     
328 #ifndef SPD_DA_OFF
329     status = daqDA_FES_storeFile(fileName.Data(),iddcs.Data());
330     if (status!=0) {
331       printf("Failed to export file %s , status %d\n",fileName.Data(),status);
332       return -1;
333     }
334 #endif
335     
336     
337     printf("Opening id list file\n");
338     TString idsFXSFileName = Form("%s/FXSids_run_%d.txt",saveDirIdsToFXS,runNr);
339     ofstream idsFXSfile;
340     idsFXSfile.open(idsFXSFileName.Data());
341     
342     // send reference data to FXS
343     for (UInt_t eqId=0; eqId<20; eqId++) {
344       if(!iseq[eqId]) continue;   
345       //printf("Preparing reference data for eq %d\n",eqId);
346       
347       TString idf = Form("SPD_ref_fo_%d",eqId);
348 #ifndef SPD_DA_OFF
349       status = daqDA_FES_storeFile(files[eqId].Data(),idf.Data());
350       if (status!=0) {
351         printf("Failed to export file %s , status %d\n",files[eqId].Data(),status);
352         return -1;
353       }
354 #endif
355       idsFXSfile << Form("%s\n",idf.Data());   
356     }
357     
358
359
360   printf("Preparing id list file\n");
361   idsFXSfile.close();
362   TString idlist = "SPD_id_list";
363 #ifndef SPD_DA_OFF
364   status = daqDA_FES_storeFile(idsFXSFileName.Data(),idlist.Data());
365   if (status!=0) {
366     printf("Failed to export file %s , status %d\n",idsFXSFileName.Data(),status);
367     return -1;
368   }
369 #endif
370
371
372
373     timer.Stop();
374     timer.Print();   
375     printf("DA finished.\n");
376     return 0;  
377 }