]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONTRKPEDda.cxx
Sergey: bug fix with storing cluster id's
[u/mrichter/AliRoot.git] / MUON / MUONTRKPEDda.cxx
1 /*
2   Contact: Jean-Luc Charvet <jean-luc.charvet@cern.ch>
3   Link: http://aliceinfo.cern.ch/static/Offline/dimuon/muon_html/README_mchda.html
4   Reference Run:
5   Run Type: PEDESTAL
6   DA Type: LDC
7   Number of events needed: 400 events for pedestal run
8   Input Files: mutrkpedvalues and config_ldc-MTRK-S3-0 
9   Output Files: local dir (not persistent) -> MUONTRKPEDda.ped  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-11-05 New version: MUONTRKPEDda.cxx,v 1.4
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 */
45 extern "C" {
46 #include <daqDA.h>
47 }
48
49 #include "event.h"
50 #include "monitor.h"
51
52 #include <Riostream.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <sstream>
56 #include <math.h> 
57
58 //AliRoot
59 #include "AliMUONRawStreamTrackerHP.h"
60 #include "AliRawReader.h"
61 #include "AliMUONVStore.h"
62 #include "AliMUON2DMap.h"
63 #include "AliMUONCalibParamND.h"
64 #include "AliMpIntPair.h"
65 #include "AliMpConstants.h"
66 #include "AliRawDataErrorLog.h"
67 #include "AliMUONTrackerIO.h"
68
69 //ROOT
70 #include "TFile.h"
71 #include "TSystem.h"
72 #include "TTree.h"
73 #include "TH1F.h"
74 #include "TString.h"
75 #include "TStopwatch.h"
76 #include "TMath.h"
77 #include "TTimeStamp.h"
78 #include "TGraphErrors.h"
79 #include "TF1.h"
80 #include "TROOT.h"
81 #include "TPluginManager.h"
82 #include "TFitter.h"
83 #include "TObjString.h"
84 #include "THashTable.h"
85 #include <THashList.h>
86 //
87 //AMORE
88 //
89 #ifdef ALI_AMORE
90 #include <AmoreDA.h>
91 #endif
92
93 #include "AliMUONPedestal.h"
94 #include "AliMUONErrorCounter.h"
95
96
97 // main routine
98 int main(Int_t argc, const char **argv) 
99 {
100   Int_t status=0;
101   TStopwatch timers;
102   timers.Start(kTRUE); 
103
104   const char* prefixDA = "MUONTRKPEDda"; // program prefix
105   printf(" ######## Begin execution : %s ######## \n\n",prefixDA); 
106
107   // needed for streamer application
108   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
109                                         "*",
110                                         "TStreamerInfo",
111                                         "RIO",
112                                         "TStreamerInfo()"); 
113   // needed for Minuit plugin
114   gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
115                                         "Minuit",
116                                         "TMinuitMinimizer",
117                                         "Minuit",
118                                         "TMinuitMinimizer(const char*)");
119 //   cout << argv[0];
120  
121   Int_t skipEvents = 0;
122   Int_t maxEvents  = 1000000;
123   Int_t maxDateEvents  = 1000000;
124   TString inputFile;
125
126   Int_t  nDateEvents = 0;
127   Int_t nGlitchErrors= 0;
128   Int_t nParityErrors= 0;
129   Int_t nPaddingErrors= 0;
130   Int_t recoverParityErrors = 1;
131
132   TString logOutputFile;
133
134   Char_t flatFile[256]="";
135   TString shuttleFile;
136
137   Int_t nEventsRecovered = 0;
138   Int_t nEvents = 0;
139   UInt_t runNumber   = 0;
140   Int_t nConfig = 1; 
141   ofstream filcout;
142
143   // decode the input line
144   for (Int_t i = 1; i < argc; i++) // argument 0 is the executable name
145     {
146       const char* arg = argv[i];
147
148       arg = argv[i];
149       if (arg[0] != '-') 
150         {
151           // If only one argument and no "-" => DA calling from ECS
152           if (argc == 2)
153             {
154         inputFile=argv[i];
155             }
156           continue;
157         }
158       switch (arg[1])
159         {
160         case 'f' : 
161           i++;
162       inputFile=argv[i];
163           nConfig=0;
164           break;
165         case 'a' : 
166           i++;
167           shuttleFile = argv[i];
168           break;
169         case 's' :
170           i++; 
171           skipEvents=atoi(argv[i]);
172           break;
173         case 'm' :
174           i++; 
175           sscanf(argv[i],"%d",&maxDateEvents);
176           break;
177         case 'n' :
178           i++; 
179           sscanf(argv[i],"%d",&maxEvents);
180           break;
181         case 'h' :
182           i++;
183           printf("\n******************* %s usage **********************",argv[0]);
184           printf("\nOnline (called from ECS) : %s <raw data file> (no inline options)\n",argv[0]);
185           printf("\n%s can be used locally only with options (without DiMuon configuration file)",argv[0]);
186           printf("\n%s -options, the available options are :",argv[0]);
187           printf("\n-h help                    (this screen)");
188           printf("\n");
189           printf("\n Input");
190           printf("\n-f <raw data file>         (default = %s)",inputFile.Data()); 
191           printf("\n");
192           printf("\n Output");
193           printf("\n-a <Flat ASCII file>       (default = %s)",shuttleFile.Data()); 
194           printf("\n");
195           printf("\n Options");
196           printf("\n-m <max date events>       (default = %d)",maxDateEvents);
197           printf("\n-s <skip events>           (default = %d)",skipEvents);
198           printf("\n-n <max events>            (default = %d)",maxEvents);
199
200           printf("\n\n");
201           exit(-1);
202         default :
203           printf("%s : bad argument %s (please check %s -h)\n",argv[0],argv[i],argv[0]);
204           argc = 2; exit(-1); // exit if error
205         } // end of switch  
206     } // end of for i  
207
208   // decoding the events
209
210   UShort_t manuId;  
211   UChar_t channelId;
212   UShort_t charge;
213
214   //Pedestal object
215   AliMUONPedestal* muonPedestal = new AliMUONPedestal();
216   muonPedestal->SetprefixDA(prefixDA);
217
218   Char_t dbfile[256]="";
219   // nConfig=1 : Reading configuration (or not) status via "mutrkpedvalues" file located in DetDB
220   if(nConfig)
221     { 
222   sprintf(dbfile,"mutrkpedvalues");
223   status=daqDA_DB_getFile(dbfile,dbfile);
224   if(status) {printf(" Failed  : input file %s is missing, status = %d\n",dbfile,status); return -1; } 
225   ifstream filein(dbfile,ios::in);
226   filein >> nConfig;
227   printf(" *** Copy: %s from DetDB to working directory  ***      Config= %d\n",dbfile,nConfig);
228     }
229   else
230     {
231       printf(" ***  Config= %d: no configuration ascii file is used \n",nConfig);
232     }
233   muonPedestal->SetconfigDA(nConfig);
234
235   // nConfig=1: configuration ascii file read from DetDB
236   if(nConfig)
237     {
238   // MuonTrk Configuration ascii file (initCROCUS.dat -> ascii file = config_$DATE_ROLE_NAME)
239   sprintf(dbfile,"config_%s",getenv("DATE_ROLE_NAME"));
240   status=daqDA_DB_getFile(dbfile,dbfile);
241   if(status) {printf(" Failed  : Configuration file %s is missing, status = %d\n",dbfile,status); return -1; }
242   else printf(" *** Copy ascii config file: %s from DetDB to working directory and reading ...*** \n",dbfile);
243   muonPedestal->LoadConfig(dbfile);  
244     } 
245
246   // Rawdeader, RawStreamHP
247   AliRawReader* rawReader = AliRawReader::Create(inputFile);
248   AliMUONRawStreamTrackerHP* rawStream  = new AliMUONRawStreamTrackerHP(rawReader);    
249   rawStream->DisableWarnings();
250   rawStream->EnabbleErrorLogger();
251
252   printf("\n %s : Reading data from file %s\n",prefixDA,inputFile.Data());
253
254   while (rawReader->NextEvent())
255     {
256       if (nDateEvents >= maxDateEvents) break;
257       if (nEvents >= maxEvents) break;
258       if (nDateEvents>0 &&  nDateEvents % 100 == 0)     
259         cout<<"Cumulated:  DATE events = " << nDateEvents << "   Used events = " << nEvents << endl;
260
261       // check shutdown condition 
262       if (daqDA_checkShutdown()) 
263         break;
264
265       //Skip events
266       while (skipEvents)
267         {
268           rawReader->NextEvent();
269           skipEvents--;
270         }
271
272       Int_t eventType = rawReader->GetType();
273       runNumber = rawReader->GetRunNumber();
274
275       // Output log file initialisations
276       if(nDateEvents==0)
277         {
278           sprintf(flatFile,"%s.log",prefixDA);
279           logOutputFile=flatFile;
280
281           filcout.open(logOutputFile.Data());
282           filcout<<"//=================================================" << endl;
283           filcout<<"//       " << prefixDA << " for run = " << runNumber << endl;
284           filcout<<"//=================================================" << endl;
285           filcout<<"//   * Date          : " << muonPedestal->GetDate()->AsString("l") << "\n" << endl;
286           cout<<"\n ********  " << prefixDA << " for run = " << runNumber << " ********\n" << endl;
287           cout<<" * Date          : " << muonPedestal->GetDate()->AsString("l") << "\n" << endl;
288
289         }
290
291       muonPedestal->SetAlifilcout(&filcout);
292
293       nDateEvents++;
294       if (eventType != PHYSICS_EVENT)
295         continue; // for the moment
296
297       // First lopp over DDL's to find good events
298       // Error counters per event (counters in the decoding lib are for each DDL)
299       Bool_t eventIsErrorMessage = kFALSE;
300       int eventGlitchErrors = 0;
301       int eventParityErrors = 0;
302       int eventPaddingErrors = 0;
303       rawStream->First();
304       do
305         {
306           if (rawStream->IsErrorMessage()) eventIsErrorMessage = kTRUE;
307           eventGlitchErrors += rawStream->GetGlitchErrors();
308           eventParityErrors += rawStream->GetParityErrors();
309           eventPaddingErrors += rawStream->GetPaddingErrors();
310         } while(rawStream->NextDDL()); 
311
312       AliMUONRawStreamTrackerHP::AliBusPatch* busPatch;
313       if (!eventIsErrorMessage) 
314         {
315           // Good events (no error) -> compute pedestal for all channels
316           rawStream->First(); 
317           while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next())) 
318             {
319               for(int i = 0; i < busPatch->GetLength(); ++i)
320                 {
321                   busPatch->GetData(i, manuId, channelId, charge);
322                   muonPedestal->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
323                 }
324             }
325           nEvents++;
326         }
327       else
328         {
329           // Events with errors
330           if (recoverParityErrors && eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
331             {
332               // Recover parity errors -> compute pedestal for all good buspatches
333               if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
334                                           ATTR_ORBIT_BC )) 
335                 {
336                   filcout <<"Event recovered -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
337                               <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
338                               <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;                                
339                 } 
340               else 
341                 {
342                   filcout <<"Event recovered -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
343                               <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
344                               <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
345                 }
346               rawStream->First();
347               while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next())) 
348                 {
349                   // Check the buspatch -> if error not use it in the pedestal calculation
350                   int errorCount = 0;
351                   for(int i = 0; i < busPatch->GetLength(); ++i)
352                     {
353                       if (!busPatch->IsParityOk(i)) errorCount++;
354                     }
355                   if (!errorCount) 
356                     {
357                       // Good buspatch
358                       for(int i = 0; i < busPatch->GetLength(); ++i)
359                         {
360                           busPatch->GetData(i, manuId, channelId, charge);
361                           muonPedestal->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
362                         }
363                     }
364                   else
365                     {
366                       AliMUONErrorCounter* errorCounter;
367                       // Bad buspatch -> not used (just print)
368                       filcout<<"bpId "<<busPatch->GetBusPatchId()<<" words "<<busPatch->GetLength()
369                                  <<" parity errors "<<errorCount<<endl;
370                       // Number of events where this buspatch is missing
371                       if (!(errorCounter = (AliMUONErrorCounter*) (muonPedestal->GetErrorBuspatchTable()->FindObject(busPatch->GetBusPatchId()))))
372                         {
373                           // New buspatch
374                           errorCounter = new AliMUONErrorCounter(busPatch->GetBusPatchId());
375                           muonPedestal->GetErrorBuspatchTable()->Add(errorCounter);
376                         }
377                       else
378                         {
379                           // Existing buspatch
380                           errorCounter->Increment();
381                         }       
382                       // errorCounter->Print();                                         
383                     } // end of if (!errorCount)
384                 } // end of while( (busPatch = (AliMUONRawStreamTrackerHP ...
385               nEvents++;
386               nEventsRecovered++;
387             } //end of if (recoverParityErrors && eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
388           else
389             {
390               // Fatal errors reject the event
391               if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
392                                           ATTR_ORBIT_BC )) 
393                 {
394                   filcout <<"Event rejected -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
395                               <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
396                               <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;                                
397                 } 
398               else 
399                 {
400                   filcout <<"Event rejected -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
401                               <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
402                               <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
403
404                 }
405             } // end of if (!rawStream->GetGlitchErrors() && !rawStream->GetPaddingErrors() ...
406           filcout<<"Number of errors : Glitch "<<eventGlitchErrors
407                      <<" Parity "<<eventParityErrors
408                      <<" Padding "<<eventPaddingErrors<<endl;
409           filcout<<endl;                        
410         } // end of if (!rawStream->IsErrorMessage())
411
412       if (eventGlitchErrors)  nGlitchErrors++;
413       if (eventParityErrors)  nParityErrors++;
414       if (eventPaddingErrors) nPaddingErrors++;
415
416     } // while (rawReader->NextEvent())
417   delete rawReader;
418   delete rawStream;
419
420   sprintf(flatFile,"%s.ped",prefixDA);
421   if(shuttleFile.IsNull())shuttleFile=flatFile;
422   muonPedestal->SetAliNEvents(nEvents);
423   muonPedestal->SetAliRunNumber(runNumber);
424   
425   muonPedestal->Finalize();  
426   muonPedestal->MakeControlHistos();  
427   if (!shuttleFile.IsNull())  
428   {
429     ofstream out(shuttleFile.Data());  
430     muonPedestal->MakeASCIIoutput(out);
431     out.close();
432 #ifdef ALI_AMORE
433   //
434   //Send objects to the AMORE DB
435   //
436     ostringstream stringout;
437     muonPedestal->MakeASCIIoutput(stringout);
438     
439     amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
440     TObjString peddata(stringout.str().c_str());
441     Int_t status =0;
442     status = amoreDA.Send("Pedestals",&peddata);
443     if ( status )
444       cout << "Warning: Failed to write Pedestals in the AMORE database : " << status << endl;
445     else 
446       cout << "amoreDA.Send(Pedestals) ok" << endl;  
447 #else
448     cout << "Warning: MCH DA not compiled with AMORE support" << endl;
449 #endif
450     
451   }
452
453   // writing some counters
454   cout << endl;
455   cout << prefixDA << " : Nb of DATE events           = " << nDateEvents    << endl;
456   cout << prefixDA << " : Nb of Glitch errors         = "   << nGlitchErrors  << endl;
457   cout << prefixDA << " : Nb of Parity errors         = "   << nParityErrors  << endl;
458   cout << prefixDA << " : Nb of Padding errors        = "   << nPaddingErrors << endl;          
459   cout << prefixDA << " : Nb of events recovered      = "   << nEventsRecovered<< endl;
460   cout << prefixDA << " : Nb of events without errors = "   << nEvents-nEventsRecovered<< endl;
461   cout << prefixDA << " : Nb of events used           = "   << nEvents        << endl;
462
463   filcout << endl;
464   filcout << prefixDA << " : Nb of DATE events           = " << nDateEvents    << endl;
465   filcout << prefixDA << " : Nb of Glitch errors         = "   << nGlitchErrors << endl;
466   filcout << prefixDA << " : Nb of Parity errors         = "   << nParityErrors << endl;
467   filcout << prefixDA << " : Nb of Padding errors        = "   << nPaddingErrors << endl;
468   filcout << prefixDA << " : Nb of events recovered      = "   << nEventsRecovered<< endl;      
469   filcout << prefixDA << " : Nb of events without errors = "   << nEvents-nEventsRecovered<< endl;
470   filcout << prefixDA << " : Nb of events used           = "   << nEvents        << endl;
471
472   // ouput files
473   cout << endl;
474   cout << prefixDA << " : Output logfile         : " << logOutputFile  << endl;
475   cout << prefixDA << " : Pedestal Histo file    : " << muonPedestal->GetHistoFileName() << endl;
476   cout << prefixDA << " : Ped. file (to SHUTTLE) : " << shuttleFile << endl;   
477
478   filcout << endl;
479   filcout << prefixDA << " : Output logfile         : " << logOutputFile  << endl;
480   filcout << prefixDA << " : Pedestal Histo file    : " << muonPedestal->GetHistoFileName() << endl;
481   filcout << prefixDA << " : Ped. file (to SHUTTLE) : " << shuttleFile << endl;   
482
483  // Copying files to local DB folder defined by DAQ_DETDB_LOCAL
484   Char_t *dir;
485   dir= getenv("DAQ_DETDB_LOCAL");
486   unsigned int nLastVersions=90;
487   cout << "\n ***  Local DataBase: " << dir << " (Max= " << nLastVersions << ") ***" << endl;
488   status = daqDA_localDB_storeFile(muonPedestal->GetHistoFileName(),nLastVersions);
489   if(status)printf(" Store file : %s   status = %d\n",muonPedestal->GetHistoFileName(),status);
490   status = daqDA_localDB_storeFile(shuttleFile.Data(),nLastVersions);
491   if(status)printf(" Store file : %s   status = %d\n",shuttleFile.Data(),status);
492   status = daqDA_localDB_storeFile(logOutputFile.Data(),nLastVersions);
493   if(status)printf(" Store file : %s   status = %d\n",logOutputFile.Data(),status);
494
495   filcout.close();
496
497   // Transferring to OCDB via the SHUTTLE (be sure that env variable DAQDALIB_PATH is set)
498   printf("\n *****  STORE FILE in FES ****** \n");
499   status = daqDA_FES_storeFile(shuttleFile.Data(),"PEDESTALS");
500   if (status) { printf(" Failed to export file : %s , status = %d\n",shuttleFile.Data(),status); return -1; }
501   else printf(" %s successfully exported to FES  \n",shuttleFile.Data());
502
503   printf("\n ######## End execution : %s ######## \n",prefixDA); 
504   timers.Stop();
505   printf("\nExecution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());
506   return status;
507 }