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 2010-04-18 New version: MUONTRKGAINda.cxx,v 1.6
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)
118 const char* prefixDA = "MUONTRKGAINda"; // program prefix
119 printf(" ######## Begin execution : %s ######## \n\n",prefixDA);
122 // decode the input line
123 if (argc!=2) { printf("Wrong number of arguments\n"); return -1; }
127 // needed for streamer application
128 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
133 // needed for Minuit plugin
134 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
138 "TMinuitMinimizer(const char*)");
139 Int_t skipEvents = 0;
140 Int_t maxEvents = 1000000;
141 Int_t maxDateEvents = 1000000;
143 Int_t nDateEvents = 0;
144 Int_t nGlitchErrors= 0;
145 Int_t nParityErrors= 0;
146 Int_t nPaddingErrors= 0;
147 Int_t nTokenlostErrors= 0;
148 Int_t nEventsRecovered = 0;
150 Int_t nEvthreshold = 10; //below this nb_evt the mean of the charge is not calculated and forced to 4085 (sigma)
152 TString logOutputFile;
153 Char_t flatFile[256]="";
156 UInt_t runNumber = 0;
161 Int_t nEntries = daqDA_ECS_getTotalIteration(); // usually = 11 = Nb of calibration runs
162 Int_t nInit=1; // = 0 all DAC values ; = 1 DAC=0 excluded (default=1)
163 Int_t nbpf1=6; // nb of points for linear fit (default=6)
164 Int_t printLevel = 0; // printout (default=0, =1 =>.ped , => .peak & .param)
165 Int_t plotLevel = 1; // plotout (default=1 => tree , =2 tree+Tgraph+fit)
169 // Reading current iteration
170 nIndex = daqDA_ECS_getCurrentIteration();
171 if(nIndex<0 || nIndex>nEntries) {printf("\n Failed: nIndex = %d\n",nIndex); return -1 ;}
174 AliMUONGain* muonGain = new AliMUONGain();
175 muonGain->SetprefixDA(prefixDA);
176 muonGain->SetAliRootDataFileName(); // MUONTRKGAINda_data.root
182 // DAC values stored in array vDAC (reading dbfile in DETDB)
183 // Int_t vDAC[11] = {0,200,400,800,1200,1600,2000,2500,3000,3500,4000}
184 Int_t nConfig = 1; // flag to read or not configuration ascii file in detDB
185 Int_t vDAC[11]; // DAC values
186 Char_t dbfile[256]="";
187 sprintf(dbfile,"mutrkcalibvalues");
188 status=daqDA_DB_getFile(dbfile,dbfile);
189 if(status) {printf(" Failed : input file %s is missing, status = %d\n",dbfile,status); return -1; }
191 ifstream filein(dbfile,ios::in); Int_t k=0, kk;
192 while (k<nEntries ) { filein >> kk >> vDAC[k] ; k++; }
193 injCharge=vDAC[nIndex-1];
195 filein >> nInit; // = 0 all DAC values fitted ; = 1 DAC=0 excluded (default=1)
196 filein >> nbpf1; // nb of points for linear fit (default=6)
197 filein >> printLevel; // printout (default=0, =1 =>.ped /run, =2 => .peak & .param)
198 filein >> plotLevel; // plotout (default=1 => tree , =2 tree+Tgraph+fit)
199 filein >> nConfig; //nConfig (default=1 => read config in DetDB, otherwise =0)
200 filein >> nbev; // Nb of events to read (default = 0 => reading all events)
201 if(nbev>0)maxEvents=nbev;
203 // printf(" *** Copy: %s from DetDB to working directory *** Config= %d\n",dbfile,nConfig);
204 printf(" Input parameters: nInit= %d Nb linear pts= %d Print level= %d Plot Level= %d nConfig= %d",nInit,nbpf1,printLevel,plotLevel,nConfig);
205 if(nbev==0)printf("\n");
206 else printf(" Nb_max evt = %d\n",maxEvents);
208 muonGain->SetAliPrintLevel(printLevel);
209 muonGain->SetAliPlotLevel(plotLevel);
210 muonGain->SetconfigDA(nConfig);
211 muonGain->SetnEvthreshold(nEvthreshold);
215 // Laod configuration ascii file from DetDB
216 sprintf(dbfile,"config_%s",getenv("DATE_ROLE_NAME"));
217 status=daqDA_DB_getFile(dbfile,dbfile);
218 if(status) {printf(" Failed : Configuration file %s is missing, status = %d\n",dbfile,status); return -1; }
219 // else printf(" *** Copy ascii config file: %s from DetDB to working directory and reading ...*** \n",dbfile);
220 muonGain->LoadConfig(dbfile);
223 // Rawdeader, RawStreamHP
224 AliRawReader* rawReader = AliRawReader::Create(inputFile.Data());
225 AliMUONRawStreamTrackerHP* rawStream = new AliMUONRawStreamTrackerHP(rawReader);
226 // rawStream->DisableWarnings();
227 rawStream->EnabbleErrorLogger();
229 // kLowErrorDetail, /// Logs minimal information in the error messages.
230 // kMediumErrorDetail, /// Logs a medium level of detail in the error messages.
231 // kHighErrorDetail /// Logs maximum information in the error messages.
232 // rawStream->SetLoggingDetailLevel(AliMUONRawStreamTrackerHP::kLowErrorDetail);
233 rawStream->SetLoggingDetailLevel(AliMUONRawStreamTrackerHP::kMediumErrorDetail);
234 // rawStream->SetLoggingDetailLevel(AliMUONRawStreamTrackerHP::kHighErrorDetail);
236 cout << "\n" << prefixDA << " : Reading data from file " << inputFile.Data() << endl;
238 Int_t tabTokenError[20][14];
239 for ( Int_t i=0 ; i<20 ; i++) { for ( Int_t j=0 ; j<14 ; j++) { tabTokenError[i][j]=0;} }
241 while (rawReader->NextEvent())
243 if (nDateEvents >= maxDateEvents) break;
244 if (nEvents >= maxEvents) break;
245 if (nDateEvents>0 && nDateEvents % 100 == 0)
246 cout<<"Cumulated: DATE events = " << nDateEvents << " Used events = " << nEvents << endl;
248 // check shutdown condition
249 if (daqDA_checkShutdown())
254 rawReader->NextEvent();
257 Int_t eventType = rawReader->GetType();
258 runNumber = rawReader->GetRunNumber();
260 // Output log file initialisations
263 sprintf(flatFile,"%s.log",prefixDA);
264 logOutputFile=flatFile;
265 AliLog::SetStreamOutput(&filcout); // Print details on logfile
266 filcout.open(logOutputFile.Data());
267 filcout<<"//=================================================" << endl;
268 filcout<<"// " << prefixDA << " for run = " << runNumber << " (DAC=" << injCharge << ")" << endl;
269 filcout<<"//=================================================" << endl;
270 filcout<<"// * Date : " << muonGain->GetDate()->AsString("l") << "\n" << endl;
272 cout<<"\n ******** " << prefixDA << " for run = " << runNumber << " (Index= " << nIndex << "/" << nEntries << " DAC=" << injCharge << ") ********\n" << endl;
273 cout<<" * Date : " << muonGain->GetDate()->AsString("l") << "\n" << endl;
276 muonGain->SetAlifilcout(&filcout);
279 if (eventType != PHYSICS_EVENT)
280 continue; // for the moment
282 const char* detail = "";
283 // First lopp over DDL's to find good events
284 // Error counters per event (counters in the decoding lib are for each DDL)
285 Bool_t eventIsErrorMessage = kFALSE;
286 int eventGlitchErrors = 0;
287 int eventParityErrors = 0;
288 int eventPaddingErrors = 0;
289 int eventTokenlostErrors = 0;
293 if (rawStream->IsErrorMessage()) eventIsErrorMessage = kTRUE;
294 eventGlitchErrors += rawStream->GetGlitchErrors();
295 eventParityErrors += rawStream->GetParityErrors();
296 eventPaddingErrors += rawStream->GetPaddingErrors();
297 eventTokenlostErrors += rawStream->GetTokenLostErrors();
298 if (rawStream->GetTokenLostErrors())
301 const AliMUONRawStreamTrackerHP::AliBlockHeader* blkHeader = 0x0;
302 const AliMUONRawStreamTrackerHP::AliDspHeader* dspHeader = 0x0;
303 Int_t nBlock = rawStream->GetBlockCount();
304 for(Int_t iBlock = 0; iBlock < nBlock ;iBlock++)
306 blkHeader = rawStream->GetBlockHeader(iBlock);
307 // printf("Block %d Total length %d\n",iBlock,blkHeader->GetTotalLength());
308 Int_t nDsp = rawStream->GetDspCount(iBlock);
309 // printf("Block %d DSP %d\n",iBlock,nDsp);
310 for(Int_t iDsp = 0; iDsp < nDsp ;iDsp++)
312 dspHeader = blkHeader->GetDspHeader(iDsp);
313 // printf("Dsp %d Add %X\n",iDsp,dspHeader);
314 if (dspHeader->GetErrorWord())
316 Int_t ddl = rawStream->GetDDL() ;
317 // Int_t ddl = AliDAQ::DdlID("MUONTRK", rawStream->GetDDL()) - 2560 ; // format 2560 + ddl
318 Int_t frt = (dspHeader->GetErrorWord() & 0xFFFF0000) >> 16 ; // 4*4bits right shift
319 tabTokenError[ddl][frt]++;
320 // printf(" DDL %d error word %X %d %d\n",ddl,dspHeader->GetErrorWord(),frt,tabTokenError[8][4]);
326 } while(rawStream->NextDDL());
328 AliMUONRawStreamTrackerHP::AliBusPatch* busPatch;
329 if (!eventIsErrorMessage)
331 // Good events (no error) -> compute pedestal for all channels
334 muonGain->SetAliNCurrentEvents(nEvents);
335 while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next()))
337 for(int i = 0; i < busPatch->GetLength(); ++i)
339 busPatch->GetData(i, manuId, channelId, charge);
340 muonGain->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
346 // Events with errors
347 if (eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
349 filcout << " ----------- Date Event recovered = " << nDateEvents << " ----------------" << endl;
350 // Recover parity errors -> compute pedestal for all good buspatches
351 if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
354 filcout <<"Event recovered -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
355 <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
356 <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;
360 filcout <<"Event recovered -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
361 <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
362 <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
366 muonGain->SetAliNCurrentEvents(nEvents);
367 while( (busPatch = (AliMUONRawStreamTrackerHP::AliBusPatch*) rawStream->Next()))
369 // Check the buspatch -> if error not use it in the pedestal calculation
371 for(int i = 0; i < busPatch->GetLength(); ++i)
373 if (!busPatch->IsParityOk(i)) errorCount++;
378 for(int i = 0; i < busPatch->GetLength(); ++i)
380 busPatch->GetData(i, manuId, channelId, charge);
381 muonGain->MakePed(busPatch->GetBusPatchId(), (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
387 AliMUONErrorCounter* errorCounter;
388 // Bad buspatch -> not used (just print)
389 filcout<<"bpId "<<busPatch->GetBusPatchId()<<" words "<<busPatch->GetLength()
390 <<" parity errors "<<errorCount<<endl;
391 // Number of events where this buspatch is missing
392 sprintf(bpname,"bp%d",busPatch->GetBusPatchId());
393 if (!(errorCounter = (AliMUONErrorCounter*) (muonGain->GetErrorBuspatchTable()->FindObject(bpname))))
396 errorCounter = new AliMUONErrorCounter(busPatch->GetBusPatchId());
397 errorCounter->SetName(bpname);
398 muonGain->GetErrorBuspatchTable()->Add(errorCounter);
403 errorCounter->Increment();
405 // errorCounter->Print();
406 } // end of if (!errorCount)
407 } // end of while( (busPatch = (AliMUONRawStreamTrackerHP ...
410 } //end of if (eventParityErrors && !eventGlitchErrors&& !eventPaddingErrors)
413 // Fatal errors reject the event
414 detail = Form(" ----------- Date Event rejected = %d ----------------",nDateEvents);
415 filcout << detail << endl;
416 if ( TEST_SYSTEM_ATTRIBUTE( rawReader->GetAttributes(),
419 filcout <<"Event rejected -> Period:"<<EVENT_ID_GET_PERIOD( rawReader->GetEventId() )
420 <<" Orbit:"<<EVENT_ID_GET_ORBIT( rawReader->GetEventId() )
421 <<" BunchCrossing:"<<EVENT_ID_GET_BUNCH_CROSSING( rawReader->GetEventId() )<<endl;
425 filcout <<"Event rejected -> nbInRun:"<<EVENT_ID_GET_NB_IN_RUN( rawReader->GetEventId() )
426 <<" burstNb:"<<EVENT_ID_GET_BURST_NB( rawReader->GetEventId() )
427 <<" nbInBurst:"<<EVENT_ID_GET_NB_IN_BURST( rawReader->GetEventId() )<<endl;
430 } // end of if (!rawStream->GetGlitchErrors() && !rawStream->GetPaddingErrors() ...
431 filcout<<"Number of errors : Glitch "<<eventGlitchErrors
432 <<" Parity "<<eventParityErrors
433 <<" Padding "<<eventPaddingErrors
434 <<" Token lost "<<eventTokenlostErrors<<endl;
436 } // end of if (!rawStream->IsErrorMessage())
438 if (eventGlitchErrors) nGlitchErrors++;
439 if (eventParityErrors) nParityErrors++;
440 if (eventPaddingErrors) nPaddingErrors++;
441 //if (eventTokenlostErrors) nTokenlostErrors++;
443 } // while (rawReader->NextEvent())
447 // process and store mean peak values in .root file
448 sprintf(flatFile,"%s.par",prefixDA);
449 if(shuttleFile.IsNull())shuttleFile=flatFile;
450 injCharge=vDAC[nIndex-1];
451 muonGain->SetAliIndex(nIndex); // fIndex
452 muonGain->SetAliInjCharge(injCharge);
453 muonGain->SetAliNEvents(nEvents);
454 muonGain->SetAliRunNumber(runNumber);
455 muonGain->MakePedStoreForGain(shuttleFile);
458 // writing some counters
460 cout << prefixDA << " : Nb of DATE events = " << nDateEvents << endl;
461 cout << prefixDA << " : Nb of Glitch errors = " << nGlitchErrors << endl;
462 cout << prefixDA << " : Nb of Parity errors = " << nParityErrors << endl;
463 cout << prefixDA << " : Nb of Padding errors = " << nPaddingErrors << endl;
464 cout << prefixDA << " : Nb of Token lost errors = " << nTokenlostErrors << endl;
465 cout << prefixDA << " : Nb of events recovered = " << nEventsRecovered<< endl;
466 cout << prefixDA << " : Nb of events without errors = " << nEvents-nEventsRecovered<< endl;
467 cout << prefixDA << " : Nb of events used = " << nEvents << endl;
470 filcout << prefixDA << " : Nb of DATE events = " << nDateEvents << endl;
471 filcout << prefixDA << " : Nb of Glitch errors = " << nGlitchErrors << endl;
472 filcout << prefixDA << " : Nb of Parity errors = " << nParityErrors << endl;
473 filcout << prefixDA << " : Nb of Padding errors = " << nPaddingErrors << endl;
474 filcout << prefixDA << " : Nb of Token lost errors = " << nTokenlostErrors << endl;
475 filcout << prefixDA << " : Nb of events recovered = " << nEventsRecovered<< endl;
476 filcout << prefixDA << " : Nb of events without errors = " << nEvents-nEventsRecovered<< endl;
477 filcout << prefixDA << " : Nb of events used = " << nEvents << endl;
479 // Writing Token Error table
482 char* detail=Form("\nWarning: Token Lost occurence \n");
485 for ( Int_t i=0 ; i<20 ; i++)
487 for ( Int_t j=4 ; j<14 ; j++)
489 if(tabTokenError[i][j]>0)
491 Int_t tab=tabTokenError[i][j];
493 Int_t station = i/4 +1;
494 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);
495 else detail=Form(" in DDL= %d (station %d) and FRT%d (Down) => %d Token errors (address = 0x%X0000)",2560+i,station,frt,tab,j);
496 printf("%s\n",detail);
497 filcout << detail << endl;
506 muonGain->SetAliInit(nInit); // fnInit
507 muonGain->SetAliEntries(nEntries); // fnEntries
508 muonGain->SetAliNbpf1(nbpf1); // fnbpf1
509 muonGain->MakeGainStore(shuttleFile);
511 std::ifstream in(shuttleFile.Data());
512 ostringstream stringout;
514 while ( in.getline(line,1024) )
515 stringout << line << "\n";
518 amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
519 TObjString gaindata(stringout.str().c_str());
520 status = amoreDA.Send("Gains",&gaindata);
522 cout << "Warning: Failed to write Pedestals in the AMORE database : " << status << endl;
524 cout << "amoreDA.Send(Gains) ok" << endl;
526 cout << "Warning: MCH DA not compiled with AMORE support" << endl;
532 filcout << prefixDA << " : Root data file : " << muonGain->GetRootDataFileName() << endl;
533 filcout << prefixDA << " : Output logfile : " << logOutputFile << endl;
534 filcout << prefixDA << " : Gain Histo file : " << muonGain->GetHistoFileName() << endl;
535 filcout << prefixDA << " : Gain file (to SHUTTLE) : " << shuttleFile << endl;
537 // Copying files to local DB folder defined by DAQ_DETDB_LOCAL
539 dir= getenv("DAQ_DETDB_LOCAL");
540 unsigned int nLastVersions = 50;
541 printf("\n *** Local DataBase: %s (Max= %d) ***\n",dir,nLastVersions);
542 status = daqDA_localDB_storeFile(logOutputFile.Data(),nLastVersions);
543 if(status)printf(" Store file : %s status = %d\n",logOutputFile.Data(),status);
546 status = daqDA_localDB_storeFile(muonGain->GetRootDataFileName(),nLastVersions);
547 if(status)printf(" Store file : %s status = %d\n",muonGain->GetRootDataFileName(),status);
548 status = daqDA_localDB_storeFile(muonGain->GetHistoFileName(),nLastVersions);
549 if(status)printf(" Store file : %s status = %d\n",muonGain->GetHistoFileName(),status);
550 status = daqDA_localDB_storeFile(shuttleFile.Data(),nLastVersions);
551 if(status)printf(" Store file : %s status = %d\n",shuttleFile.Data(),status);
557 cout << prefixDA << " : Root data file : " << muonGain->GetRootDataFileName() << endl;
558 cout << prefixDA << " : Output logfile : " << logOutputFile << endl;
559 cout << prefixDA << " : Gain Histo file : " << muonGain->GetHistoFileName() << endl;
560 cout << prefixDA << " : Gain file (to SHUTTLE) : " << shuttleFile << endl;
564 // Transferring to calibration file to FES
565 // be sure that env variable DAQDALIB_PATH is set in script file
566 // gSystem->Setenv("DAQDALIB_PATH", "$DATE_SITE/infoLogger");
567 printf("\n ***** STORE calibration FILE to FES ****** \n");
568 status = daqDA_FES_storeFile(shuttleFile.Data(),"GAINS");
569 if (status) { printf(" Failed to export file : %s , status = %d\n",shuttleFile.Data(),status); return -1; }
570 // else printf(" %s successfully exported to FES \n",shuttleFile.Data());
572 printf("\n ######## End execution : %s ######## \n",prefixDA);
574 printf("\nExecution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());