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 Runs: (station 3)
19 Number of events needed: 400 events for each calibration run (11)
20 Input Files: mutrkcalibvalues and config_ldc-MTRK-S3-0 in path : /afs/cern.ch/user/j/jcharvet/public/DA_validation
21 Output Files: local dir (not persistent) -> MUONTRKGAINda.par FXS -> run<#>_MCH_<ldc>_GAINS
25 /**************************************************************************
26 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
28 * Author: The ALICE Off-line Project. *
29 * Contributors are mentioned in the code where appropriate. *
31 * Permission to use, copy, modify and distribute this software and its *
32 * documentation strictly for non-commercial purposes is hereby granted *
33 * without fee, provided that the above copyright notice appears in all *
34 * copies and that both the copyright notice and this permission notice *
35 * appear in the supporting documentation. The authors make no claims *
36 * about the suitability of this software for any purpose. It is *
37 * provided "as is" without express or implied warranty. *
38 **************************************************************************/
43 -------------------------------------------------------------------------
44 2012-02-29 New version: MUONTRKGAINda.cxx,v 1.7
45 -------------------------------------------------------------------------
47 Version for MUONTRKGAINda MUON tracking
48 (A. Baldisseri, J.-L. Charvet)
51 Rem: AliMUON2DMap stores all channels, even those which are not connected
52 if pedMean == -1, channel not connected to a pad
63 #include <Riostream.h>
70 #include "AliMUONRawStreamTrackerHP.h"
71 #include "AliRawReader.h"
72 #include "AliMUONVStore.h"
73 #include "AliMUON2DMap.h"
74 #include "AliMUONCalibParamND.h"
75 #include "AliMpIntPair.h"
76 #include "AliMpConstants.h"
77 #include "AliRawDataErrorLog.h"
78 #include "AliMUONTrackerIO.h"
80 #include "AliMUONDspHeader.h"
89 #include "TStopwatch.h"
91 #include "TTimeStamp.h"
92 #include "TGraphErrors.h"
95 #include "TPluginManager.h"
97 #include "TObjString.h"
98 #include "THashTable.h"
99 #include <THashList.h>
107 #include "AliMUONPedestal.h"
108 #include "AliMUONGain.h"
109 #include "AliMUONErrorCounter.h"
112 int main(Int_t argc, const char** argv)
114 Int_t status=0 , status1=0 ;
118 const char* prefixDA = "MUONTRKGAINda"; // program prefix
119 const char* prefixLDC = getenv("DATE_ROLE_NAME"); // LDC name
120 if(prefixLDC == NULL) prefixLDC ="MCH" ;
121 printf("%s : -------- Begin execution : %s -------- \n",prefixLDC,prefixDA);
123 // const char* prefixDA = "MUONTRKGAINda"; // program prefix
124 // printf(" ######## Begin execution : %s ######## \n\n",prefixDA);
127 // decode the input line
128 if (argc!=2) { printf("Wrong number of arguments\n"); return -1; }
132 // needed for streamer application
133 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
138 // needed for Minuit plugin
139 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
143 "TMinuitMinimizer(const char*)");
144 Int_t skipEvents = 0;
145 Int_t maxEvents = 1000000;
146 Int_t maxDateEvents = 1000000;
147 Int_t errorDetail = 0;
149 Int_t nDateEvents = 0;
150 Int_t nDateRejEvents = 0;
151 Int_t nGlitchErrors= 0;
152 Int_t nParityErrors= 0;
153 Int_t nPaddingErrors= 0;
154 Int_t nTokenlostErrors= 0;
155 Int_t nEventsRecovered = 0;
157 Int_t nEvthreshold = 50; //below this nb_evt the mean of the charge is not calculated and forced to 4085 (sigma)
158 Int_t statusDA = 0 ; // DA return code
160 TString logOutputFile;
162 Char_t flatFile[256]="";
165 UInt_t runNumber = 0;
170 Int_t nEntries = daqDA_ECS_getTotalIteration(); // usually = 11 = Nb of calibration runs
171 Int_t nInit=0; // = 0 all DAC values ; = 1 DAC=0 excluded (default=1)
172 Int_t nbpf1=4; // nb of points for linear fit (default=6)
173 Int_t printLevel = 0; // printout (default=0, =1 =>.ped , => .peak & .param)
174 Int_t plotLevel = 1; // plotout (default=1 => tree , =2 tree+Tgraph+fit)
178 // Reading current iteration
179 nIndex = daqDA_ECS_getCurrentIteration();
180 if(nIndex<0 || nIndex>nEntries) {printf("\n Failed: nIndex = %d\n",nIndex); return -1 ;}
183 AliMUONGain* muonGain = new AliMUONGain();
184 muonGain->SetprefixDA(prefixDA);
185 muonGain->SetprefixLDC(prefixLDC);
186 muonGain->SetAliRootDataFileName(); // MUONTRKGAINda_data.root
187 muonGain->SetStatusDA(statusDA);
189 // Output log file initialisations
190 sprintf(flatFile,"%s.log",prefixDA);
191 logOutputFile=flatFile;
192 AliLog::SetStreamOutput(&filcout); // Print details on logfile
193 filcout.open(logOutputFile.Data());
194 filcout<<"//=================================================" << endl;
195 filcout<<"//" << prefixLDC << " " << prefixDA << endl;
196 filcout<<"//=================================================" << endl;
197 filcout<<"// * Date : " << muonGain->GetDate()->AsString("l") << "\n" << endl;
199 muonGain->SetAlifilcout(&filcout);
200 cout<<prefixLDC << " : Date: " << muonGain->GetDate()->AsString("l") << "\n" << endl;
208 // DAC values stored in array vDAC (reading dbfile in DETDB)
209 // Int_t vDAC[11] = {0,200,400,800,1200,1600,2000,2500,3000,3500,4000}
210 Int_t nConfig = 1; // flag to read or not configuration ascii file in detDB
211 Int_t vDAC[11]; // DAC values
212 Char_t dbfile[256]="";
215 sprintf(dbfile,"mutrkcalibvalues");
216 status=daqDA_DB_getFile(dbfile,dbfile);
217 if(status) {printf(" Failed : input file %s is missing, status = %d\n",dbfile,status); return -1; }
219 ifstream filein(dbfile,ios::in); Int_t k=0, kk;
220 while (k<nEntries ) { filein >> kk >> vDAC[k] ; k++; }
221 injCharge=vDAC[nIndex-1];
223 filein >> line >> nInit ; cout << "mutrkcalibvalues: " << line << nInit << " " ; // = 0 all DAC values fitted ; = 1 DAC=0 excluded (default=1)
224 filein >> line >> nbpf1; cout << line << nbpf1 << " " ; // nb of points for linear fit (default=6)
225 filein >> line >> printLevel; cout << line << printLevel << " " ; // printout (default=0, =1 =>.ped /run, =2 => .peak & .param)
226 filein >> line >> plotLevel; cout << line << plotLevel << " " ; // plotout (default=1 => tree , =2 tree+Tgraph+fit)
227 filein >> line >> nConfig ; cout << line << nConfig << " " ; //nConfig (default=1 => read config in DetDB, otherwise =0)
228 filein >> line >> nEvthres ; if(nEvthres !=0)nEvthreshold=nEvthres; cout << line << nEvthreshold << " " ; // (default = 0 <=> 50) below nEvthreshold calibration not performed
229 filein >> line >> nbev ; if(nbev !=0){maxEvents=nbev; cout << line << maxEvents << " " ;} // Nb of events to read (default = 0 => reading all events)
232 muonGain->SetAliPrintLevel(printLevel);
233 muonGain->SetAliPlotLevel(plotLevel);
234 muonGain->SetconfigDA(nConfig);
235 muonGain->SetnEvthreshold(nEvthreshold);
239 // Laod configuration ascii file from DetDB
240 sprintf(dbfile,"config_%s",getenv("DATE_ROLE_NAME"));
241 status=daqDA_DB_getFile(dbfile,dbfile);
242 if(status) {printf(" Failed : Configuration file %s is missing, status = %d\n",dbfile,status); return -1; }
243 // else printf(" *** Copy ascii config file: %s from DetDB to working directory and reading ...*** \n",dbfile);
244 muonGain->LoadConfig(dbfile);
247 // Rawdeader, RawStreamHP
248 AliRawReader* rawReader = AliRawReader::Create(inputFile.Data());
249 AliMUONRawStreamTrackerHP* rawStream = new AliMUONRawStreamTrackerHP(rawReader);
250 // rawStream->DisableWarnings();
251 rawStream->EnabbleErrorLogger();
254 case 0: rawStream->SetLoggingDetailLevel(AliMUONRawStreamTrackerHP::kLowErrorDetail); break;/// Logs minimal information in the error messages.
255 case 1: rawStream->SetLoggingDetailLevel(AliMUONRawStreamTrackerHP::kMediumErrorDetail); break;/// Logs a medium level of detail in the error messages.
256 case 2: rawStream->SetLoggingDetailLevel(AliMUONRawStreamTrackerHP::kHighErrorDetail); break;/// Logs a medium level of detail in the error messages.
257 default: rawStream->SetLoggingDetailLevel(AliMUONRawStreamTrackerHP::kMediumErrorDetail); break;
260 printf("\n%s : Reading data from file %s\n",prefixLDC,inputFile.Data());
262 Int_t tabTokenError[20][14];
263 for ( Int_t i=0 ; i<20 ; i++) { for ( Int_t j=0 ; j<14 ; j++) { tabTokenError[i][j]=0;} }
265 while (rawReader->NextEvent())
267 Int_t eventType = rawReader->GetType();
268 runNumber = rawReader->GetRunNumber();
269 if(nDateEvents==0) { filcout<<"// ----> RUN = " << runNumber << "\n" << endl;}
270 if (nDateEvents >= maxDateEvents) break;
271 if (nEvents >= maxEvents) break;
272 if (nDateEvents>0 && nDateEvents % 100 == 0)
273 cout<< prefixLDC << " : DATE events = " << nDateEvents << " Used events = " << nEvents << endl;
275 // check shutdown condition
276 if (daqDA_checkShutdown())
281 rawReader->NextEvent();
285 if (eventType != PHYSICS_EVENT)
286 continue; // for the moment
288 const char* detail = "";
289 // First lopp over DDL's to find good events
290 // Error counters per event (counters in the decoding lib are for each DDL)
291 Bool_t eventIsErrorMessage = kFALSE;
292 int eventGlitchErrors = 0;
293 int eventParityErrors = 0;
294 int eventPaddingErrors = 0;
295 int eventTokenlostErrors = 0;
299 if (rawStream->IsErrorMessage()) eventIsErrorMessage = kTRUE;
300 eventGlitchErrors += rawStream->GetGlitchErrors();
301 eventParityErrors += rawStream->GetParityErrors();
302 eventPaddingErrors += rawStream->GetPaddingErrors();
303 eventTokenlostErrors += rawStream->GetTokenLostErrors();
304 if (rawStream->GetTokenLostErrors())
307 const AliMUONRawStreamTrackerHP::AliBlockHeader* blkHeader = 0x0;
308 const AliMUONRawStreamTrackerHP::AliDspHeader* dspHeader = 0x0;
309 Int_t nBlock = rawStream->GetBlockCount();
310 for(Int_t iBlock = 0; iBlock < nBlock ;iBlock++)
312 blkHeader = rawStream->GetBlockHeader(iBlock);
313 // printf("Block %d Total length %d\n",iBlock,blkHeader->GetTotalLength());
314 Int_t nDsp = rawStream->GetDspCount(iBlock);
315 // printf("Block %d DSP %d\n",iBlock,nDsp);
316 for(Int_t iDsp = 0; iDsp < nDsp ;iDsp++)
318 dspHeader = blkHeader->GetDspHeader(iDsp);
319 // printf("Dsp %d Add %X\n",iDsp,dspHeader);
320 if (dspHeader->GetErrorWord())
322 Int_t ddl = rawStream->GetDDL() ;
323 // Int_t ddl = AliDAQ::DdlID("MUONTRK", rawStream->GetDDL()) - 2560 ; // format 2560 + ddl
324 Int_t frt = (dspHeader->GetErrorWord() & 0xFFFF0000) >> 16 ; // 4*4bits right shift
325 tabTokenError[ddl][frt]++;
326 // printf(" DDL %d error word %X %d %d\n",ddl,dspHeader->GetErrorWord(),frt,tabTokenError[8][4]);
332 } while(rawStream->NextDDL());
334 AliMUONRawStreamTrackerHP::AliBusPatch* busPatch;
335 if (!eventIsErrorMessage)
337 // Good events (no error) -> compute pedestal for all channels
340 muonGain->SetAliNCurrentEvents(nEvents);
341 while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next()))
343 for(int i = 0; i < busPatch->GetLength(); ++i)
345 busPatch->GetData(i, manuId, channelId, charge);
346 muonGain->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
352 // Events with errors
353 if (eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
355 filcout << " ----------- Date Event recovered = " << nDateEvents << " ----------------" << endl;
356 // Recover parity errors -> compute pedestal for all good buspatches
357 if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
360 filcout <<"Event recovered -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
361 <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
362 <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;
366 filcout <<"Event recovered -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
367 <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
368 <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
372 muonGain->SetAliNCurrentEvents(nEvents);
373 while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next()))
375 // Check the buspatch -> if error not use it in the pedestal calculation
377 for(int i = 0; i < busPatch->GetLength(); ++i)
379 if (!busPatch->IsParityOk(i)) errorCount++;
384 for(int i = 0; i < busPatch->GetLength(); ++i)
386 busPatch->GetData(i, manuId, channelId, charge);
387 muonGain->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
393 AliMUONErrorCounter* errorCounter;
394 // Bad buspatch -> not used (just print)
395 filcout<<"bpId "<<busPatch->GetBusPatchId()<<" words "<<busPatch->GetLength()
396 <<" parity errors "<<errorCount<<endl;
397 // Number of events where this buspatch is missing
398 sprintf(bpname,"bp%d",busPatch->GetBusPatchId());
399 if (!(errorCounter = (AliMUONErrorCounter*) (muonGain->GetErrorBuspatchTable()->FindObject(bpname))))
402 errorCounter = new AliMUONErrorCounter(busPatch->GetBusPatchId());
403 errorCounter->SetName(bpname);
404 muonGain->GetErrorBuspatchTable()->Add(errorCounter);
409 errorCounter->Increment();
411 // errorCounter->Print();
412 } // end of if (!errorCount)
413 } // end of while( (busPatch = (AliMUONRawStreamTrackerHP ...
416 } //end of if (eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
419 // Fatal errors reject the event
420 detail = Form(" ----------- Date Event rejected = %d ----------------",nDateEvents);
422 filcout << detail << endl;
423 if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
426 filcout <<"Event rejected -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
427 <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
428 <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;
432 filcout <<"Event rejected -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
433 <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
434 <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
437 } // end of if (!rawStream->GetGlitchErrors() && !rawStream->GetPaddingErrors() ...
438 filcout<<"Number of errors : Glitch "<<eventGlitchErrors
439 <<" Parity "<<eventParityErrors
440 <<" Padding "<<eventPaddingErrors
441 <<" Token lost "<<eventTokenlostErrors<<endl;
443 } // end of if (!rawStream->IsErrorMessage())
445 if (eventGlitchErrors) nGlitchErrors++;
446 if (eventParityErrors) nParityErrors++;
447 if (eventPaddingErrors) nPaddingErrors++;
448 //if (eventTokenlostErrors) nTokenlostErrors++;
450 } // while (rawReader->NextEvent())
454 // process and store mean peak values in .root file
455 sprintf(flatFile,"%s.par",prefixDA);
456 if(shuttleFile.IsNull())shuttleFile=flatFile;
457 injCharge=vDAC[nIndex-1];
458 muonGain->SetAliIndex(nIndex); // fIndex
459 muonGain->SetAliInjCharge(injCharge);
460 muonGain->SetAliNEvents(nEvents);
461 muonGain->SetAliRunNumber(runNumber);
462 muonGain->MakePedStoreForGain(shuttleFile);
464 // writing some counters
465 detail=Form("\n%s : Nb of DATE events = %d",prefixLDC,nDateEvents) ; cout << detail; filcout << detail ;
466 detail=Form("\n%s : Nb of Glitch errors = %d",prefixLDC,nGlitchErrors) ; cout << detail; filcout << detail ;
467 detail=Form("\n%s : Nb of Parity errors = %d",prefixLDC,nParityErrors) ; cout << detail; filcout << detail ;
468 detail=Form("\n%s : Nb of Token lost errors = %d",prefixLDC,nTokenlostErrors) ; cout << detail; filcout << detail ;
469 detail=Form("\n%s : Nb of Rejected DATE events = %d",prefixLDC,nDateRejEvents) ; cout << detail; filcout << detail ;
470 detail=Form("\n%s : Nb of recovered events = %d",prefixLDC,nEventsRecovered) ; cout << detail; filcout << detail ;
471 detail=Form("\n%s : Nb of events without errors = %d",prefixLDC,nEvents-nEventsRecovered) ; cout << detail; filcout << detail ;
472 detail=Form("\n%s : Nb of used events = %d (threshold= %d)\n\n",prefixLDC,nEvents,nEvthreshold); cout << detail; filcout << detail ;
474 // Writing Token Error table
477 char* detail=Form("\nWarning: Token Lost occurence \n");
480 for ( Int_t i=0 ; i<20 ; i++)
482 for ( Int_t j=4 ; j<14 ; j++)
484 if(tabTokenError[i][j]>0)
486 Int_t tab=tabTokenError[i][j];
488 Int_t station = i/4 +1;
489 if( j % 2 == 0)detail=Form(" in DDL= %d (station %d) and FRT%d ( Up ) => %d Token errors (address = 0x%X0000)",2560+i,station,frt,tab,j);
490 else detail=Form(" in DDL= %d (station %d) and FRT%d (Down) => %d Token errors (address = 0x%X0000)",2560+i,station,frt,tab,j);
491 printf("%s\n",detail);
492 filcout << detail << endl;
501 muonGain->SetAliInit(nInit); // fnInit
502 muonGain->SetAliEntries(nEntries); // fnEntries
503 muonGain->SetAliNbpf1(nbpf1); // fnbpf1
504 muonGain->MakeGainStore(shuttleFile);
505 status = muonGain->GetStatusDA() ;
509 detail=Form("%s : Root data file : %s\n",prefixLDC,muonGain->GetRootDataFileName()); filcout << detail ; // cout << detail;
510 detail=Form("%s : Output logfile : %s\n",prefixLDC,logOutputFile.Data()); filcout << detail ; // cout << detail;
511 detail=Form("%s : Gain Histo file : %s\n",prefixLDC,muonGain->GetHistoFileName()); filcout << detail ; // cout << detail;
512 detail=Form("%s : Gain file (to SHUTTLE) : %s\n",prefixLDC,shuttleFile.Data()); filcout << detail ; // cout << detail;
514 // Copying files to local DB folder defined by DAQ_DETDB_LOCAL
516 unsigned int nLastVersions=50;
517 dir= getenv("DAQ_DETDB_LOCAL");
519 unsigned int nLastVersions=50;
520 printf("\n%s : --- Local DataBase: %s (Max= %d) ---\n",prefixLDC,dir,nLastVersions);
521 status1 = daqDA_localDB_storeFile(logOutputFile.Data(),nLastVersions);
525 status1 = daqDA_localDB_storeFile(muonGain->GetRootDataFileName(),nLastVersions);
526 status1 = daqDA_localDB_storeFile(muonGain->GetHistoFileName(),nLastVersions);
527 status1 = daqDA_localDB_storeFile(shuttleFile.Data(),nLastVersions); // if(status1)printf(" Store file : %s status = %d\n",shuttleFile.Data(),status1);
532 // Transferring pedestal file to FES (be sure that env variable DAQDALIB_PATH is set)
534 status1 = daqDA_FES_storeFile(shuttleFile.Data(),"GAINS");
535 if (status1) { detail=Form("%s: !!! ERROR: Failed to export calibration file : %s to FES \n",prefixLDC,shuttleFile.Data());
536 printf("%s",detail); filcout << detail ; status= -1; }
537 // else { detail=Form("%s : ---- STORE calibration FILE in FES : OK ---- \n",prefixLDC); printf("%s",detail); filcout << detail ;}
542 std::ifstream in(shuttleFile.Data());
543 ostringstream stringout;
545 while ( in.getline(line,1024) )
546 stringout << line << "\n";
549 amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
550 TObjString gaindata(stringout.str().c_str());
551 Int_t amoreStatus = amoreDA.Send("Gains",&gaindata);
553 {cout << prefixLDC << " : !!! ERROR: Failed to write Gains in the AMORE database : " << amoreStatus << endl ; status=-1 ;}
555 cout << prefixLDC << " : amoreDA.Send(Gains) ok" << endl;
557 cout << prefixLDC << " : Warning: MCH DA not compiled with AMORE support" << endl;
561 if(!status)printf("\n%s : -------- End execution : %s -------- (status= %d) \n",prefixLDC,prefixDA,status);
562 else { printf("\n%s : -------- %s ending in ERROR !!!! -------- (status= %d) \n",prefixLDC,prefixDA,status);}
565 printf("\nExecution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());