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