]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONTRKPEDda.cxx
In mapping:
[u/mrichter/AliRoot.git] / MUON / MUONTRKPEDda.cxx
1 /*
2 Contact: Jean-Luc Charvet <jean-luc.charvet@cea.fr>
3 Link: http://aliceinfo.cern.ch/static/Offline/dimuon/muon_html/README_Mchda
4 Run Type: PEDESTAL
5         DA Type: LDC
6         Number of events needed: 400 events for pedestal run
7         Input Files: Rawdata file (DATE format)
8         Output Files: local dir (not persistent) -> MUONTRKPEDda_<run#>.ped 
9         FXS -> run<#>_MCH_<ldc>_PEDESTALS
10         Trigger types used:
11 */
12
13 /**************************************************************************
14  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
15  *                                                                        *
16  * Author: The ALICE Off-line Project.                                    *
17  * Contributors are mentioned in the code where appropriate.              *
18  *                                                                        *
19  * Permission to use, copy, modify and distribute this software and its   *
20  * documentation strictly for non-commercial purposes is hereby granted   *
21  * without fee, provided that the above copyright notice appears in all   *
22  * copies and that both the copyright notice and this permission notice   *
23  * appear in the supporting documentation. The authors make no claims     *
24  * about the suitability of this software for any purpose. It is          *
25  * provided "as is" without express or implied warranty.                  *
26  **************************************************************************/
27
28 /* $Id$ */
29
30 /*
31         -------------------------------------------------------------------------
32         2009-06-18 New version: MUONTRKPEDda.cxx,v 1.2
33         -------------------------------------------------------------------------
34
35         Version for MUONTRKPEDda MUON tracking
36         (A. Baldisseri, J.-L. Charvet)
37
38
39  Rem:  AliMUON2DMap stores all channels, even those which are not connected
40  if pedMean == -1, channel not connected to a pad  
41
42
43 */
44 extern "C" {
45 #include <daqDA.h>
46 }
47
48 #include "event.h"
49 #include "monitor.h"
50
51 #include <Riostream.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <sstream>
55 #include <math.h> 
56
57 //AliRoot
58 #include "AliMUONRawStreamTrackerHP.h"
59 #include "AliRawReader.h"
60 #include "AliMUONVStore.h"
61 #include "AliMUON2DMap.h"
62 #include "AliMUONCalibParamND.h"
63 #include "AliMpIntPair.h"
64 #include "AliMpConstants.h"
65 #include "AliRawDataErrorLog.h"
66 #include "AliMUONTrackerIO.h"
67
68 //ROOT
69 #include "TFile.h"
70 #include "TSystem.h"
71 #include "TTree.h"
72 #include "TH1F.h"
73 #include "TString.h"
74 #include "TStopwatch.h"
75 #include "TMath.h"
76 #include "TTimeStamp.h"
77 #include "TGraphErrors.h"
78 #include "TF1.h"
79 #include "TROOT.h"
80 #include "TPluginManager.h"
81 #include "TFitter.h"
82 #include "TObjString.h"
83 #include "THashTable.h"
84 #include <THashList.h>
85
86 #include "AliMUONPedestal.h"
87 #include "AliMUONErrorCounter.h"
88
89
90 // main routine
91 int main(Int_t argc, Char_t **argv) 
92 {
93
94   Int_t status=0;
95   TStopwatch timers;
96   timers.Start(kTRUE); 
97
98   // needed for streamer application
99   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
100                                         "*",
101                                         "TStreamerInfo",
102                                         "RIO",
103                                         "TStreamerInfo()"); 
104
105   // needed for Minuit plugin
106   gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
107                                         "Minuit",
108                                         "TMinuitMinimizer",
109                                         "Minuit",
110                                         "TMinuitMinimizer(const char*)");
111
112   Char_t prefixDA[256]="MUONTRKPEDda"; // program prefix
113 //   cout << argv[0];
114  
115   Int_t skipEvents = 0;
116   Int_t maxEvents  = 1000000;
117   Int_t maxDateEvents  = 1000000;
118   Char_t inputFile[256]="";
119
120   Int_t  nDateEvents = 0;
121   Int_t nGlitchErrors= 0;
122   Int_t nParityErrors= 0;
123   Int_t nPaddingErrors= 0;
124   Int_t recoverParityErrors = 1;
125
126   TString logOutputFile;
127
128   Char_t flatFile[256]="";
129   TString shuttleFile;
130
131   Int_t nEventsRecovered = 0;
132   Int_t nEvents = 0;
133   UInt_t runNumber   = 0;
134   Int_t  nChannel    = 0;
135   ofstream filcout;
136 //   Int_t nIndex = -1; 
137
138   // decode the input line
139   for (Int_t i = 1; i < argc; i++) // argument 0 is the executable name
140     {
141       Char_t* arg;
142
143       arg = argv[i];
144       if (arg[0] != '-') 
145         {
146           // If only one argument and no "-" => DA calling from ECS
147           if (argc == 2)
148             {
149               sprintf(inputFile,argv[i]);
150             }
151           continue;
152         }
153       switch (arg[1])
154         {
155         case 'f' : 
156           i++;
157           sprintf(inputFile,argv[i]);
158           break;
159         case 'a' : 
160           i++;
161           shuttleFile = argv[i];
162           break;
163         case 's' :
164           i++; 
165           skipEvents=atoi(argv[i]);
166           break;
167         case 'm' :
168           i++; 
169           sscanf(argv[i],"%d",&maxDateEvents);
170           break;
171         case 'n' :
172           i++; 
173           sscanf(argv[i],"%d",&maxEvents);
174           break;
175 //      case 'p' : 
176 //        i++;
177 //        sscanf(argv[i],"%d",&recoverParityErrors);
178 //        break;
179         case 'h' :
180           i++;
181           printf("\n******************* %s usage **********************",argv[0]);
182           printf("\nOnline (called from ECS) : %s <raw data file> (no inline options)\n",argv[0]);
183           printf("\n%s -options, the available options are :",argv[0]);
184           printf("\n-h help                    (this screen)");
185           printf("\n");
186           printf("\n Input");
187           printf("\n-f <raw data file>         (default = %s)",inputFile); 
188           printf("\n");
189           printf("\n Output");
190           printf("\n-a <Flat ASCII file>       (default = %s)",shuttleFile.Data()); 
191           printf("\n");
192           printf("\n Options");
193           printf("\n-m <max date events>       (default = %d)",maxDateEvents);
194           printf("\n-s <skip events>           (default = %d)",skipEvents);
195           printf("\n-n <max events>            (default = %d)",maxEvents);
196 //        printf("\n-p <Recover parity errors> (default = %d)",recoverParityErrors);
197
198           printf("\n\n");
199           exit(-1);
200         default :
201           printf("%s : bad argument %s (please check %s -h)\n",argv[0],argv[i],argv[0]);
202           argc = 2; exit(-1); // exit if error
203         } // end of switch  
204     } // end of for i  
205
206   // decoding the events
207
208   UShort_t manuId;  
209   UChar_t channelId;
210   UShort_t charge;
211
212   //Pedestal object
213   AliMUONPedestal* muonPedestal = new AliMUONPedestal();
214   muonPedestal->SetprefixDA(prefixDA);
215
216   // Rawdeader, RawStreamHP
217   AliRawReader* rawReader = AliRawReader::Create(inputFile);
218   AliMUONRawStreamTrackerHP* rawStream  = new AliMUONRawStreamTrackerHP(rawReader);    
219   rawStream->DisableWarnings();
220   rawStream->EnabbleErrorLogger();
221
222   cout << "\n" << prefixDA << " : Reading data from file " << inputFile  << endl;
223
224   while (rawReader->NextEvent())
225     {
226       if (nDateEvents >= maxDateEvents) break;
227       if (nEvents >= maxEvents) break;
228       if (nDateEvents>0 &&  nDateEvents % 100 == 0)     
229         cout<<"Cumulated:  DATE events = " << nDateEvents << "   Used events = " << nEvents << endl;
230
231       // check shutdown condition 
232       if (daqDA_checkShutdown()) 
233         break;
234
235       //Skip events
236       while (skipEvents)
237         {
238           rawReader->NextEvent();
239           skipEvents--;
240         }
241
242       Int_t eventType = rawReader->GetType();
243       runNumber = rawReader->GetRunNumber();
244
245       // Output log file initialisations
246       if(nDateEvents==0)
247         {
248           sprintf(flatFile,"%s.log",prefixDA);
249           logOutputFile=flatFile;
250
251           filcout.open(logOutputFile.Data());
252           filcout<<"//=================================================" << endl;
253           filcout<<"//       " << prefixDA << " for run = " << runNumber << endl;
254           filcout<<"//=================================================" << endl;
255           filcout<<"//   * Date          : " << muonPedestal->GetDate()->AsString("l") << "\n" << endl;
256           cout<<"\n ********  " << prefixDA << " for run = " << runNumber << " ********\n" << endl;
257           cout<<" * Date          : " << muonPedestal->GetDate()->AsString("l") << "\n" << endl;
258
259         }
260
261       muonPedestal->SetAlifilcout(&filcout);
262
263       nDateEvents++;
264       if (eventType != PHYSICS_EVENT)
265         continue; // for the moment
266
267       // First lopp over DDL's to find good events
268       // Error counters per event (counters in the decoding lib are for each DDL)
269       Bool_t eventIsErrorMessage = kFALSE;
270       int eventGlitchErrors = 0;
271       int eventParityErrors = 0;
272       int eventPaddingErrors = 0;
273       rawStream->First();
274       do
275         {
276           if (rawStream->IsErrorMessage()) eventIsErrorMessage = kTRUE;
277           eventGlitchErrors += rawStream->GetGlitchErrors();
278           eventParityErrors += rawStream->GetParityErrors();
279           eventPaddingErrors += rawStream->GetPaddingErrors();
280         } while(rawStream->NextDDL()); 
281
282       AliMUONRawStreamTrackerHP::AliBusPatch* busPatch;
283       if (!eventIsErrorMessage) 
284         {
285           // Good events (no error) -> compute pedestal for all channels
286           rawStream->First(); 
287           while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next())) 
288             {
289               for(int i = 0; i < busPatch->GetLength(); ++i)
290                 {
291                   if (nEvents == 0) nChannel++;
292                   busPatch->GetData(i, manuId, channelId, charge);
293                   muonPedestal->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
294                 }
295             }
296           nEvents++;
297         }
298       else
299         {
300           // Events with errors
301           if (recoverParityErrors && eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
302             {
303               // Recover parity errors -> compute pedestal for all good buspatches
304               if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
305                                           ATTR_ORBIT_BC )) 
306                 {
307                   filcout <<"Event recovered -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
308                               <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
309                               <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;                                
310                 } 
311               else 
312                 {
313                   filcout <<"Event recovered -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
314                               <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
315                               <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
316                 }
317               rawStream->First();
318               while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next())) 
319                 {
320                   // Check the buspatch -> if error not use it in the pedestal calculation
321                   int errorCount = 0;
322                   for(int i = 0; i < busPatch->GetLength(); ++i)
323                     {
324                       if (!busPatch->IsParityOk(i)) errorCount++;
325                     }
326                   if (!errorCount) 
327                     {
328                       // Good buspatch
329                       for(int i = 0; i < busPatch->GetLength(); ++i)
330                         {
331                           if (nEvents == 0) nChannel++;
332                           busPatch->GetData(i, manuId, channelId, charge);
333                           // if (busPatch->GetBusPatchId()==1719 && manuId == 1 && channelId == 0) cout <<"Recovered charge "<<charge<<endl;
334                           muonPedestal->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
335                         }
336                     }
337                   else
338                     {
339                       char bpname[256];
340                       AliMUONErrorCounter* errorCounter;
341                       // Bad buspatch -> not used (just print)
342                       filcout<<"bpId "<<busPatch->GetBusPatchId()<<" words "<<busPatch->GetLength()
343                                  <<" parity errors "<<errorCount<<endl;
344                       // Number of events where this buspatch is missing
345                       sprintf(bpname,"bp%d",busPatch->GetBusPatchId());                                         
346                       if (!(errorCounter = (AliMUONErrorCounter*) (muonPedestal->GetErrorBuspatchTable()->FindObject(bpname))))
347                         {
348                           // New buspatch
349                           errorCounter = new AliMUONErrorCounter(busPatch->GetBusPatchId());
350                           errorCounter->SetName(bpname);
351                           muonPedestal->GetErrorBuspatchTable()->Add(errorCounter);
352                         }
353                       else
354                         {
355                           // Existing buspatch
356                           errorCounter->Increment();
357                         }       
358                       // errorCounter->Print();                                         
359                     } // end of if (!errorCount)
360                 } // end of while( (busPatch = (AliMUONRawStreamTrackerHP ...
361               nEvents++;
362               nEventsRecovered++;
363             } //end of if (recoverParityErrors && eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
364           else
365             {
366               // Fatal errors reject the event
367               if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
368                                           ATTR_ORBIT_BC )) 
369                 {
370                   filcout <<"Event rejected -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
371                               <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
372                               <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;                                
373                 } 
374               else 
375                 {
376                   filcout <<"Event rejected -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
377                               <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
378                               <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
379
380                 }
381             } // end of if (!rawStream->GetGlitchErrors() && !rawStream->GetPaddingErrors() ...
382           filcout<<"Number of errors : Glitch "<<eventGlitchErrors
383                      <<" Parity "<<eventParityErrors
384                      <<" Padding "<<eventPaddingErrors<<endl;
385           filcout<<endl;                        
386         } // end of if (!rawStream->IsErrorMessage())
387
388       if (eventGlitchErrors)  nGlitchErrors++;
389       if (eventParityErrors)  nParityErrors++;
390       if (eventPaddingErrors) nPaddingErrors++;
391
392     } // while (rawReader->NextEvent())
393   delete rawReader;
394   delete rawStream;
395
396   sprintf(flatFile,"%s.ped",prefixDA);
397   if(shuttleFile.IsNull())shuttleFile=flatFile;
398   muonPedestal->SetAliNEvents(nEvents);
399   muonPedestal->SetAliRunNumber(runNumber);
400   muonPedestal->SetAliNChannel(nChannel);
401   muonPedestal->MakePedStore(shuttleFile);
402
403   // writing some counters
404   cout << endl;
405   cout << prefixDA << " : Nb of DATE events           = " << nDateEvents    << endl;
406   cout << prefixDA << " : Nb of Glitch errors         = "   << nGlitchErrors  << endl;
407   cout << prefixDA << " : Nb of Parity errors         = "   << nParityErrors  << endl;
408   cout << prefixDA << " : Nb of Padding errors        = "   << nPaddingErrors << endl;          
409   cout << prefixDA << " : Nb of events recovered      = "   << nEventsRecovered<< endl;
410   cout << prefixDA << " : Nb of events without errors = "   << nEvents-nEventsRecovered<< endl;
411   cout << prefixDA << " : Nb of events used           = "   << nEvents        << endl;
412
413   filcout << endl;
414   filcout << prefixDA << " : Nb of DATE events           = " << nDateEvents    << endl;
415   filcout << prefixDA << " : Nb of Glitch errors         = "   << nGlitchErrors << endl;
416   filcout << prefixDA << " : Nb of Parity errors         = "   << nParityErrors << endl;
417   filcout << prefixDA << " : Nb of Padding errors        = "   << nPaddingErrors << endl;
418   filcout << prefixDA << " : Nb of events recovered      = "   << nEventsRecovered<< endl;      
419   filcout << prefixDA << " : Nb of events without errors = "   << nEvents-nEventsRecovered<< endl;
420   filcout << prefixDA << " : Nb of events used           = "   << nEvents        << endl;
421
422   // ouput files
423   cout << endl;
424   cout << prefixDA << " : Output logfile         : " << logOutputFile  << endl;
425   cout << prefixDA << " : Pedestal Histo file    : " << muonPedestal->GetHistoFileName() << endl;
426   cout << prefixDA << " : Ped. file (to SHUTTLE) : " << shuttleFile << endl;   
427
428   filcout << endl;
429   filcout << prefixDA << " : Output logfile         : " << logOutputFile  << endl;
430   filcout << prefixDA << " : Pedestal Histo file    : " << muonPedestal->GetHistoFileName() << endl;
431   filcout << prefixDA << " : Ped. file (to SHUTTLE) : " << shuttleFile << endl;   
432
433  // Copying files to local DB folder defined by DAQ_DETDB_LOCAL
434   Char_t *dir;
435   dir= getenv("DAQ_DETDB_LOCAL");
436   unsigned int nLastVersions=90;
437   cout << "\n ***  Local DataBase: " << dir << " (Max= " << nLastVersions << ") ***" << endl;
438   status = daqDA_localDB_storeFile(muonPedestal->GetHistoFileName(),nLastVersions);
439   //  if (!status) printf(" Failed to store file : %s   status = %d\n",muonPedestal->GetHistoFileName(),status);
440   printf(" Store file : %s   status = %d\n",muonPedestal->GetHistoFileName(),status);
441   status = daqDA_localDB_storeFile(shuttleFile.Data(),nLastVersions);
442   //  if (!status) printf(" Failed to store file : %s   status = %d\n",shuttleFile.Data(),status);
443   printf(" Store file : %s   status = %d\n",shuttleFile.Data(),status);
444   status = daqDA_localDB_storeFile(logOutputFile.Data(),nLastVersions);
445   //  if (!status) printf(" Failed to store file : %s   status = %d\n",logOutputFile.Data(),status);
446   printf(" Store file : %s   status = %d\n",logOutputFile.Data(),status);
447
448   // Transferring to OCDB via the SHUTTLE
449   printf("\n *****  STORE FILE in FES ****** \n");
450
451   // be sure that env variable DAQDALIB_PATH is set in script file
452   //       gSystem->Setenv("DAQDALIB_PATH", "$DATE_SITE/infoLogger");
453
454   status = daqDA_FES_storeFile(shuttleFile.Data(),"PEDESTALS");
455   if (status) 
456     {
457       printf(" Failed to export file : %d\n",status);
458     }
459   else printf(" %s successfully exported to FES  \n",shuttleFile.Data());
460
461   filcout.close();
462   timers.Stop();
463   printf("\nExecution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());
464   return status;
465 }