00001 #include "TPedestalExtractor.h"
00002
00003 #include <stdio.h>
00004 #include <fstream>
00005 #include "TGo4Log.h"
00006
00007 #include "TMath.h"
00008
00009
00021
00022 const Char_t * TPedestalExtractor::kPedFileFormat = "%d%d%d%f%f";
00023
00024 TPedestalExtractor::TPedestalExtractor( Int_t rocArrSize, Int_t nxArrSize, roc::SysMessageUserTypes initialState, Int_t nChannels, Int_t nAdcBins, Double_t adcMin, Double_t adcMax )
00025 {
00026 TGo4Log::Info("Creating instance of TPedestalExtractor");
00027
00028 if( rocArrSize < 0 || nxArrSize < 0 || nChannels < 0 || nAdcBins < 0 || adcMin > adcMax ) {
00029 PrintLog( "In TPedestalExtractor::TPedestalExtractor: Invalid argument", 0 );
00030 fRocArrSize = 0;
00031 fNxArrSize = 0;
00032 fNChannels = 0;
00033 fNAdcBins = 0;
00034 fAdcMin = 0.;
00035 fAdcMax = 0.;
00036 return;
00037 }
00038
00039 fVerbose = 0;
00040 fExttrigCalibrPeriod = 10000000000LL;
00041 fRocArrSize = rocArrSize;
00042 fNxArrSize = nxArrSize;
00043 fNChannels = nChannels;
00044 fNAdcBins = nAdcBins;
00045 fAdcMin = adcMin;
00046 fAdcMax = adcMax;
00047
00048 fLastCalibrTime.resize( fRocArrSize );
00049 fLastHitTime.resize( fRocArrSize );
00050
00051 for(Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++) {
00052 fLastCalibrTime[ iRoc ] = 0;
00053 fLastHitTime[ iRoc ] = 0;
00054 }
00055
00056 fState = new roc::SysMessageUserTypes[fRocArrSize];
00057 fPrevState = new roc::SysMessageUserTypes[fRocArrSize];
00058 fIsExtracted = new Bool_t[ fRocArrSize ];
00059 for(Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++) {
00060 fState[iRoc] = initialState;
00061 fPrevState[iRoc] = roc::SYSMSG_USER_RECONFIGURE;
00062 fIsExtracted[ iRoc ] = kFALSE;
00063 }
00064
00065 fBaselines = new TH1F***[fRocArrSize];
00066 fPedestals = new Float_t**[fRocArrSize];
00067 fWidths = new Float_t**[fRocArrSize];
00068 for(Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++) {
00069 fBaselines[iRoc] = new TH1F**[fNxArrSize];
00070 fPedestals[iRoc] = new Float_t*[fNxArrSize];
00071 fWidths[iRoc] = new Float_t*[fNxArrSize];
00072 for(Int_t iNx = 0; iNx < fNxArrSize; iNx++) {
00073 fBaselines[iRoc][iNx] = new TH1F*[fNChannels];
00074 fPedestals[iRoc][iNx] = new Float_t[fNChannels];
00075 fWidths[iRoc][iNx] = new Float_t[fNChannels];
00076 for(Int_t iChannel = 0; iChannel < fNChannels; iChannel++) {
00077 fBaselines[iRoc][iNx][iChannel] = new TH1F(Form("hPedR%dN%dCh%d", iRoc, iNx, iChannel),"", nAdcBins, adcMin, adcMax );
00078
00079 fBaselines[iRoc][iNx][iChannel]->SetDirectory(0);
00080 fPedestals[iRoc][iNx][iChannel] = kInvalidPedestalValue;
00081 fWidths[iRoc][iNx][iChannel] = kInvalidWidthValue;
00082 }
00083 }
00084 }
00085 fPrintedDataNotTimeSorted = kFALSE;
00086 }
00087
00088 TPedestalExtractor::~TPedestalExtractor()
00089 {
00090
00091 if(fBaselines == 0) {
00092 TGo4Log::Error("Error in TPedestalExtractor::~TPedestalExtractor(): fHistograms is 0; Probably the default constructor was used, which is not supported");
00093 return;
00094 }
00095 for(Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++) {
00096 for(Int_t iNx = 0; iNx < fNxArrSize; iNx++) {
00097 for(Int_t iChannel = 0; iChannel < fNChannels; iChannel++) {
00098 delete fBaselines[iRoc][iNx][iChannel];
00099 fBaselines[iRoc][iNx][iChannel] = 0;
00100 }
00101 delete [] fBaselines[iRoc][iNx];
00102 delete [] fPedestals[iRoc][iNx];
00103 delete [] fWidths[iRoc][iNx];
00104 }
00105 delete [] fBaselines[iRoc];
00106 delete [] fPedestals[iRoc];
00107 delete [] fWidths[iRoc];
00108 }
00109 delete [] fBaselines;
00110 delete [] fPedestals;
00111 delete [] fWidths;
00112
00113 delete [] fState;
00114 delete [] fPrevState;
00115 delete [] fIsExtracted;
00116 }
00117
00118
00119 void TPedestalExtractor::AddHit( Int_t iRoc, Int_t iNx, Int_t iChannel, Int_t adc )
00120 {
00121 if( IsValid( iRoc, iNx, iChannel ) ) {
00122 fBaselines[iRoc][iNx][iChannel]->Fill( adc );
00123 }
00124 }
00125
00126
00127 void TPedestalExtractor::ChangeState( roc::SysMessageUserTypes const & newState )
00128 {
00129 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00130 ChangeState( iRoc, newState );
00131 }
00132 }
00133
00134
00135 void TPedestalExtractor::ChangeState( Int_t rocId, roc::SysMessageUserTypes const & newState )
00136 {
00137 fPrevState[rocId] = fState[rocId];
00138 fState[rocId] = newState;
00139 }
00140
00141
00142 void TPedestalExtractor::PrintLog(const char* message, Int_t minVerbose ) const
00143 {
00144 if( fVerbose >= minVerbose )
00145 printf( "%s\n", message );
00146 }
00147
00148
00149 uint64_t TPedestalExtractor::GetLastCalibrTime( Int_t rocId ) const
00150 {
00151 return fLastCalibrTime.at( rocId );
00152 }
00153
00154
00155 void TPedestalExtractor::SetVerbose( Int_t verbose )
00156 {
00157 fVerbose = verbose;
00158 }
00159
00160
00161 Int_t TPedestalExtractor::GetVerbose() const
00162 {
00163 return fVerbose;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 Bool_t TPedestalExtractor::IsLooksLikeNewPedestalFileFormat(const char* pedFileName ) const
00173 {
00174 if ((pedFileName==0) || (strlen(pedFileName)==0)) return kFALSE;
00175
00176 FILE * pedFile = fopen( pedFileName, "r" );
00177
00178 if (!pedFile) return kFALSE;
00179
00180 Bool_t retValue = kFALSE;
00181
00182 Int_t dummyRoc = 0, dummyNx = 0, dummyCh = 0;
00183 Float_t dummyPed = 0, dummyWidht = 0;
00184 if( 5 == fscanf( pedFile, kPedFileFormat, &dummyRoc, &dummyNx, &dummyCh, &dummyPed, &dummyWidht ) ) {
00185 retValue = kTRUE;
00186 }
00187 fclose(pedFile);
00188
00189 return retValue;
00190 }
00191
00192
00193
00194 uint64_t TPedestalExtractor::GetSelftrigCalibrPeriod() const
00195 {
00196 return fExttrigCalibrPeriod;
00197 }
00198
00199
00200
00201 void TPedestalExtractor::SetExttrigCalibrPeriod( uint64_t period )
00202 {
00203 fExttrigCalibrPeriod = period;
00204 }
00205
00206
00207
00208
00209
00210 Int_t TPedestalExtractor::WatchState( roc::Message const & msg )
00211 {
00212 uint16_t rocId = msg.getRocNumber();
00213 if( rocId >= fRocArrSize) {
00214 return -1;
00215 }
00216 uint8_t type = msg.getMessageType();
00217
00218 if( type == roc::MSG_SYS ) {
00219 if( msg.getSysMesType() == roc::SYSMSG_USER ) {
00220 uint32_t userMessageData = msg.getSysMesData();
00221 if( userMessageData == roc::SYSMSG_USER_CALIBR_ON
00222 || userMessageData == roc::SYSMSG_USER_CALIBR_OFF
00223 || userMessageData == roc::SYSMSG_USER_RECONFIGURE ) {
00224 ChangeState( rocId, ( roc::SysMessageUserTypes )userMessageData );
00225 }
00226 return rocId;
00227 }
00228 }
00229 return -1;
00230 }
00231
00232
00233
00234
00235
00236 void TPedestalExtractor::WatchPedestalMessages( roc::Message const & msg, Long64_t const fullTime )
00237 {
00238 ExtractAutocalibrStream( msg, fullTime );
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 Int_t TPedestalExtractor::ExtractAutocalibrStream( roc::Message const & msg, uint64_t fullTime )
00271 {
00272 uint16_t rocId = msg.getRocNumber();
00273 if( rocId >= fRocArrSize) {
00274 return 1;
00275 }
00276 uint8_t type = msg.getMessageType();
00277
00278 if( type == roc::MSG_SYS ) {
00279 if( msg.getSysMesType() == roc::SYSMSG_USER ) {
00280 switch( msg.getSysMesData() ) {
00281 case roc::SYSMSG_USER_CALIBR_ON: {
00282 ResetCalibrData( rocId );
00283 ChangeState( rocId, roc::SYSMSG_USER_CALIBR_ON );
00284 PrintLog( Form( "CALIBR_ON on ROC %d", rocId), 1 );
00285 return 0;
00286 }
00287 case roc::SYSMSG_USER_CALIBR_OFF: {
00288 if( fPrevState[rocId] != roc::SYSMSG_USER_CALIBR_ON ) {
00289 ChangeState( rocId, roc::SYSMSG_USER_RECONFIGURE );
00290 PrintLog( Form("WARNING in TPedestalExtractor::WatchPedestalMessages: On ROC %d : CALIBR_OFF message appeared without CALIBR_ON before. Switching to RECONFIGRE.", rocId ), 0 );
00291 return 2;
00292 }
00293 ChangeState( rocId, roc::SYSMSG_USER_CALIBR_OFF );
00294 Extract( rocId );
00295 fLastCalibrTime[ rocId ] = fLastHitTime[ rocId ];
00296 PrintLog( Form( "CALIBR_OFF on ROC %d", rocId), 1 );
00297 return 0;
00298 }
00299 case roc::SYSMSG_USER_RECONFIGURE: {
00300 ChangeState( rocId, roc::SYSMSG_USER_RECONFIGURE );
00301 PrintLog( Form( "CALIBR_RECONFIGURE on ROC %d", rocId), 1 );
00302 return 0;
00303 }
00304 }
00305 }
00306 }
00307
00308 if( type == roc::MSG_HIT && fState[rocId] == roc::SYSMSG_USER_CALIBR_ON ) {
00309 AddHit( rocId, msg.getNxNumber(), msg.getNxChNum(), msg.getNxAdcValue() );
00310 fLastHitTime[ rocId ] = fullTime;
00311 }
00312 return 0;
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 Int_t TPedestalExtractor::ExtractExttrigStream( roc::Message const & msg, uint64_t fullTime )
00326 {
00327 uint16_t rocId = msg.getRocNumber();
00328 if( ! IsRocValid( rocId ) ) {
00329 return 1;
00330 }
00331
00332 if( msg.getMessageType() == roc::MSG_HIT ) {
00333 AddHit( rocId, msg.getNxNumber(), msg.getNxChNum(), msg.getNxAdcValue() );
00334 if( fLastCalibrTime[ rocId ] == 0 ) {
00335 fLastCalibrTime[ rocId ] = fullTime;
00336 }
00337
00338 if( fullTime >= fLastCalibrTime[ rocId ] ) {
00339 if( fullTime > fLastCalibrTime[ rocId ] + fExttrigCalibrPeriod ) {
00340
00341 PrintLog( Form( "TPedestalExtractor::ExtractExttrigStream: Recalibrating ROC %d at %lu ns", rocId, (long unsigned) fullTime ), 1 );
00342 Extract( rocId );
00343 ResetCalibrData( rocId );
00344 fLastCalibrTime[ rocId ] = fullTime;
00345
00346 }
00347 }
00348 else {
00349 if( ! fPrintedDataNotTimeSorted ) {
00350 PrintLog( "Warning in TPedestalExtractor::ExtractExttrigStream: Data is not time-sorted!", 0 );
00351 fPrintedDataNotTimeSorted = kTRUE;
00352
00353
00354 }
00355 }
00356 }
00357 return 0;
00358 }
00359
00360
00361
00362 Int_t TPedestalExtractor::ExtractAutocalibrLmd( std::string lmdFileName, roc::SysMessageUserTypes initialState, Int_t skipCalibrIterations )
00363 {
00364 ResetCalibrData();
00365 ResetCalibrResults();
00366 ChangeState( roc::SYSMSG_USER_RECONFIGURE );
00367 ChangeState( initialState );
00368
00369 std::vector< Int_t > calibrIteration;
00370 calibrIteration.resize( fRocArrSize );
00371 calibrIteration.assign( fRocArrSize, 0 );
00372
00373 std::vector< Bool_t > rocCalibrated;
00374 rocCalibrated.resize( fRocArrSize );
00375 rocCalibrated.assign( fRocArrSize, ( initialState == roc::SYSMSG_USER_CALIBR_ON ) ? 1 : 0 );
00376
00377 roc::Iterator calibrIter( lmdFileName.c_str() );
00378
00379 while( calibrIter.next() ) {
00380 roc::Message const & msg = calibrIter.msg();
00381 Int_t rocId = msg.getRocNumber();
00382 uint8_t msgType = msg.getMessageType();
00383
00384 Bool_t doCheckCalibrDone = kFALSE;
00385 if( msgType == roc::MSG_SYS ) {
00386 if( msg.getSysMesType() == roc::SYSMSG_USER ) {
00387 if( msg.getSysMesData() == roc::SYSMSG_USER_CALIBR_ON ) {
00388 calibrIteration[ rocId ]++;
00389 }
00390 doCheckCalibrDone = kTRUE;
00391 }
00392 }
00393
00394 if( calibrIteration[ rocId ] == skipCalibrIterations + 1 ) {
00395 ExtractAutocalibrStream( msg, calibrIter.getMsgFullTime() );
00396 if( GetState( rocId ) == roc::SYSMSG_USER_CALIBR_OFF ) {
00397 rocCalibrated[ rocId ] = kTRUE;
00398 }
00399 }
00400
00401 if( doCheckCalibrDone ) {
00402 Bool_t allRocsCalibrated = kTRUE;
00403 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00404 allRocsCalibrated = allRocsCalibrated && rocCalibrated[ iRoc ];
00405 }
00406 if( allRocsCalibrated ) {
00407 return 0;
00408 }
00409 }
00410 }
00411
00412 if( fVerbose >= 0 ) {
00413 printf( "In TPedestalExtractor::ExtractAutocalibrLmd: Data sample is over, while not all ROCs were calibrated." );
00414 printf( "\t\t Non-calibrated ROCs: " );
00415 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00416 if( ! rocCalibrated[ iRoc ] ) {
00417 printf( " %d ", iRoc );
00418 }
00419 }
00420 printf( "\n" );
00421 }
00422 return 1;
00423 }
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484 void TPedestalExtractor::Extract()
00485 {
00486 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00487 Extract( iRoc );
00488 }
00489 }
00490
00491
00492
00493 void TPedestalExtractor::Extract( Int_t rocId )
00494 {
00495 if( IsRocValid( rocId ) ) {
00496 for(Int_t iNx = 0; iNx < fNxArrSize; iNx++) {
00497 for(Int_t iChannel = 0; iChannel < fNChannels; iChannel++) {
00498 Extract( fBaselines[rocId][iNx][iChannel], fPedestals[rocId][iNx][iChannel], fWidths[rocId][iNx][iChannel] );
00499 }
00500 }
00501 fIsExtracted[ rocId ] = kTRUE;
00502
00503
00504 }
00505 }
00506
00507
00508
00509 void TPedestalExtractor::Extract(TH1 * hist, Float_t & pedestal, Float_t & width )
00510 {
00511 pedestal = kInvalidPedestalValue;
00512 width = kInvalidWidthValue;
00513 if( ! hist ) {
00514 printf("In TPedestalExtractor::Extract: No histogram received\n");
00515 return;
00516 }
00517
00518 if( hist->GetEntries() < kEntriesMin ) {
00519
00520
00521 return;
00522 }
00523
00524 Int_t medianBin = 1;
00525
00526 Double_t* integral = hist->GetIntegral();
00527 while( integral[medianBin + 1] <= 0.5 ) {
00528 medianBin++;
00529 }
00530 pedestal = hist->GetXaxis()->GetBinCenter( medianBin );
00531
00532 Float_t sum = 0;
00533 Float_t sum2 = 0;
00534 Float_t sumCounts = 0;
00535 for( Int_t iBin = 1; iBin <= hist->GetNbinsX(); iBin++ )
00536 {
00537 Float_t nCounts = hist->GetBinContent( iBin );
00538 Float_t diff = hist->GetBinCenter( iBin ) - pedestal;
00539 sum += nCounts * diff;
00540 sum2 += nCounts * diff * diff;
00541 sumCounts += nCounts;
00542 }
00543 width = TMath::Sqrt( sum2 / sumCounts - sum / sumCounts * sum / sumCounts );
00544 }
00545
00546
00547
00548
00549
00550
00551 void TPedestalExtractor::ResetCalibrData()
00552 {
00553 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00554 ResetCalibrData( iRoc );
00555 }
00556 }
00557
00558
00559
00560
00561
00562
00563 void TPedestalExtractor::ResetCalibrData( Int_t rocId )
00564 {
00565 if( IsRocValid( rocId ) ) {
00566 for(Int_t iNx = 0; iNx < fNxArrSize; iNx++) {
00567 for(Int_t iChannel = 0; iChannel < fNChannels; iChannel++) {
00568 fBaselines[rocId][iNx][iChannel]->Reset();
00569 }
00570 }
00571 }
00572 else {
00573 PrintLog( Form("Warning in TPedestalExtractor::ResetCalibrData: rocId %d is not valid", rocId), 1 );
00574 }
00575 }
00576
00577
00578
00579
00580
00581
00582 void TPedestalExtractor::ResetCalibrResults()
00583 {
00584 {
00585 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00586 ResetCalibrResults( iRoc );
00587 }
00588 }
00589 }
00590
00591
00592
00593
00594
00595
00596 void TPedestalExtractor::ResetCalibrResults( Int_t rocId )
00597 {
00598 if( IsRocValid( rocId ) ) {
00599 for(Int_t iNx = 0; iNx < fNxArrSize; iNx++) {
00600 for(Int_t iChannel = 0; iChannel < fNChannels; iChannel++) {
00601 fPedestals[rocId][iNx][iChannel] = kInvalidPedestalValue;
00602 fWidths[rocId][iNx][iChannel] = kInvalidWidthValue;
00603 }
00604 }
00605 fIsExtracted[rocId] = kFALSE;
00606 }
00607 else {
00608 PrintLog( Form("Warning in TPedestalExtractor::ResetCalibrResults: rocId %d is not valid", rocId), 1 );
00609 }
00610 }
00611
00612
00613 void TPedestalExtractor::ResetLastCalibrTime()
00614 {
00615 {
00616 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00617 ResetLastCalibrTime( iRoc );
00618 }
00619 }
00620 }
00621
00622
00623
00624 void TPedestalExtractor::ResetLastCalibrTime( Int_t rocId )
00625 {
00626 if( IsRocValid( rocId ) ) {
00627 fLastCalibrTime[ rocId ] = 0;
00628 }
00629 else {
00630 PrintLog( Form("Warning in TPedestalExtractor::ResetLastCalibrTime: rocId %d is not valid", rocId), 1 );
00631 }
00632 }
00633
00634
00635
00636 Float_t TPedestalExtractor::GetPedestal( Int_t roc, Int_t nx, Int_t channel ) const {
00637 if( IsValid( roc, nx, channel ) ) {
00638 return fPedestals[roc][nx][channel];
00639 }
00640 else {
00641 printf("ERROR in TPedestalExtractor::GetPedestal: Invalid arguments: roc=%d nx=%d channel=%d", roc, nx, channel );
00642 return kInvalidPedestalValue;
00643 }
00644 }
00645
00646
00647 Float_t TPedestalExtractor::GetWidth( Int_t roc, Int_t nx, Int_t channel ) const {
00648 if( IsValid( roc, nx, channel ) ) {
00649 return fWidths[roc][nx][channel];
00650 }
00651 else {
00652 printf("ERROR in TPedestalExtractor::GetWidthInvalid arguments: roc=%d nx=%d channel=%d", roc, nx, channel );
00653 return kInvalidPedestalValue;
00654 }
00655 }
00656
00657
00658
00659
00660 TH1F * TPedestalExtractor::GetHistogram( Int_t roc, Int_t nx, Int_t channel )
00661 {
00662 if( IsValid( roc, nx, channel ) ) {
00663 return fBaselines[ roc ][ nx ][ channel ];
00664 }
00665 return 0;
00666 }
00667
00668
00669
00670 TH2F * TPedestalExtractor::GetHist2DCopy( Int_t roc, Int_t nx )
00671 {
00672 TH2F * hist = new TH2F( "hPedestals2D", "hPedestals2D", fNChannels, 0, fNChannels, fNAdcBins, fAdcMin, fAdcMax );
00673 Long64_t nEntries = 0;
00674 for( Int_t iCh = 0; iCh < fNChannels; iCh++ ) {
00675 for( Int_t iAdcBin = 1; iAdcBin < fNAdcBins; iAdcBin++ ) {
00676 Long64_t nEntriesBin = GetHistogram( roc, nx, iCh )->GetBinContent( iAdcBin );
00677 hist->SetBinContent( iCh + 1, iAdcBin, nEntriesBin );
00678 nEntries += nEntriesBin;
00679 }
00680 }
00681 hist->SetEntries( nEntries );
00682 return hist;
00683 }
00684
00685
00686
00687 Bool_t TPedestalExtractor::IsExtracted( Int_t roc ) const
00688 {
00689 if( IsRocValid( roc ) ) {
00690 return( fIsExtracted[ roc ] );
00691 }
00692 else {
00693 printf( "Error in TPedestalExtractor::IsExtracted: Roc %d is not valid\n", roc );
00694 }
00695 return kFALSE;
00696 }
00697
00698
00699
00700 Bool_t TPedestalExtractor::IsOneExtracted() const
00701 {
00702 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00703 if( fIsExtracted[ iRoc ] ) return kTRUE;
00704 }
00705 return kFALSE;
00706 }
00707
00708
00709
00710 Bool_t TPedestalExtractor::IsAllExtracted() const
00711 {
00712 for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00713 if( ! fIsExtracted[ iRoc ] ) return kFALSE;
00714 }
00715 return kTRUE;
00716 }
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729 void TPedestalExtractor::LoadFromFile( Char_t const * fileName )
00730 {
00731
00732 if( IsLooksLikeNewPedestalFileFormat( fileName ) ) {
00733 FILE * pedFile = fopen( fileName, "r" );
00734 if( ! pedFile ) {
00735 PrintLog( Form("Pedestal file %s not found\n", fileName) );
00736 return;
00737 }
00738 Int_t roc, nx, ch;
00739 Float_t ped, width;
00740 while( 5 == fscanf( pedFile, kPedFileFormat, &roc, &nx, &ch, &ped, &width ) ) {
00741 if( IsValid( roc, nx, ch ) ) {
00742 fPedestals[roc][nx][ch] = ped;
00743 fWidths[roc][nx][ch] = width;
00744 }
00745 else {
00746 PrintLog( Form("In TPedestalExtractor::LoadFromFile: error when reading pedestals file: roc/nx/ch combination of %d/%d/%d is not valid. Stop reading pedestals", roc, nx, ch) );
00747 return;
00748 }
00749 }
00750 fclose( pedFile );
00751 } else {
00752 printf( "Warning in TPedestalExtractor::LoadFromFile: Reading pedestals in backward-compatibility mode\n" );
00753 FILE * pedFile = fopen( fileName, "r" );
00754 if( ! pedFile ) {
00755 PrintLog( Form("Pedestal file %s not found", fileName) );
00756 return;
00757 }
00758 Int_t iRoc = 0, iNx = 0, iCh = 0;
00759 float ped;
00760 while( 1 == fscanf( pedFile, "%f", &ped ) ) {
00761 if( iRoc < fRocArrSize - 1 || iNx < fNxArrSize - 1 || iCh < fNChannels - 1 ) {
00762 fPedestals[iRoc][iNx][iCh] = static_cast<Float_t>( ped );
00763 if( iCh < fNChannels - 1 ) {
00764 iCh++;
00765 }
00766 else if( iNx < fNxArrSize - 1) {
00767 iNx++;
00768 iCh = 0;
00769 }
00770 else if( iRoc < fRocArrSize -1 ) {
00771 iRoc++;
00772 iNx = 0;
00773 iCh = 0;
00774 }
00775 }
00776 else {
00777 printf( "Error in TPedestalExtractor::LoadFromFile: Too many lines in the pedestal file\n" );
00778 break;
00779 }
00780 }
00781 if( iRoc != fRocArrSize - 1 || iNx != fNxArrSize - 1 || iCh != fNChannels - 1 ) {
00782 printf( "Error in TPedestalExtractor::LoadFromFile: The pedestal file is expected to contain more lines\n" );
00783 }
00784 fclose( pedFile );
00785 }
00786 }
00787
00788
00789
00790
00791
00792
00793 void TPedestalExtractor::Print() const
00794 {
00795 for(Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++) {
00796 Print( iRoc );
00797 }
00798 }
00799
00800
00801
00802
00803
00804 void TPedestalExtractor::Print( Int_t roc ) const
00805 {
00806 if( IsRocValid( roc ) ) {
00807 for(Int_t iNx = 0; iNx < fNxArrSize; iNx++) {
00808 for(Int_t iChannel = 0; iChannel < fNChannels; iChannel++) {
00809 printf( "roc=%2d nx=%2d ch=%3d ped=%7.1f nEnt=%10.1f\n", roc, iNx, iChannel, fPedestals[roc][iNx][iChannel], fBaselines[roc][iNx][iChannel]->GetEntries());
00810 }
00811 }
00812 }
00813 }
00814
00815
00816
00817 void TPedestalExtractor::SaveToFile( Char_t const * fileName) const
00818 {
00819 PrintLog( Form( "Storing pedestals into file %s", fileName), 1 );
00820
00821 std::ofstream pedestalfile;
00822 pedestalfile.open(fileName);
00823
00824 for( Int_t iRoc = 0; iRoc < GetRocArrSize(); iRoc++ ) {
00825 for( Int_t iNx = 0; iNx < GetNxArrSize(); iNx++ ) {
00826 for( Int_t iCh = 0; iCh < GetNChannels(); iCh++ ) {
00827 pedestalfile << Form("%4d%4d%6d%10.2f%10.2f\n", iRoc, iNx, iCh, GetPedestal(iRoc, iNx, iCh), GetWidth(iRoc, iNx, iCh) );
00828 }
00829 }
00830 }
00831 pedestalfile.close();
00832 }
00833
00834
00835
00836 roc::SysMessageUserTypes TPedestalExtractor::GetState( Int_t roc ) const
00837 {
00838 if( IsRocValid( roc ) ) {
00839 return fState[roc];
00840 }
00841 printf("ERROR in TPedestalExtractor::GetState: invalid roc numben");
00842 return roc::SYSMSG_USER_RECONFIGURE;
00843 }