• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

onlinemonitor/rocmonitor/TPedestalExtractor.cxx (r4864/r3724)

Go to the documentation of this file.
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         // Check argument validity:
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                                 // avoid ROOT cleanup for histograms
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         // If fHistograms should have been created in the constructor, together with fPedestals and fWidths.
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  * Check if the first line of file pedFileName matches the format kPedFileFormat of the pedestal file.
00170  * Returns kFALSE if the file can not be opened.
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  * Returns rocId which' state has changed, or -1 if none.
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  * Obsolete. Kept for compatibility.
00235  */
00236 void TPedestalExtractor::WatchPedestalMessages( roc::Message const & msg, Long64_t const fullTime )
00237 {
00238         ExtractAutocalibrStream( msg, fullTime );
00239 }
00240 
00241 
00242 /*
00243  * Identifies the calibration data among all other ROC messages, acquires it,
00244  * and invokes the calibration. Uses calibration markers, such as
00245  * roc::SYSMSG_USER_CALIBR_ON, roc::SYSMSG_USER_CALIBR_OFF and
00246  * roc::SYSMSG_USER_RECONFIGURE, to identify the calibration data.
00247  *
00248  * Data is to be passed via the msg argument. The user may pass absolutely all
00249  * ROC messages, without any preselection, to this function -- the function will
00250  * identify the calibration data itself.
00251  *
00252  * All ROCs are monitored independently. Data samples where only some of the ROCs
00253  * have calibration are also accepted (make sure then to put the ROCs without
00254  * calibration to the roc::SYSMSG_USER_RECONFIGURE state, so that the data from
00255  * them is not acquired).
00256  *
00257  * Whenever the roc::SYSMSG_USER_CALIBR_ON marker is received, all the
00258  * previously acquired calibration data is discarded. All data which comes
00259  * afterwards is acquired for the next calibration, until roc::SYSMSG_USER_RECONFIGURE
00260  * is received. Calibration is invoked on receiving a roc::SYSMSG_USER_CALIBR_OFF
00261  * message. If a roc::SYSMSG_USER_CALIBR_OFF message is received without a foregoing
00262  * roc::SYSMSG_USER_CALIBR_ON, an error is generated, and the ROC is switched to
00263  * roc::SYSMSG_USER_RECONFIGURE state.
00264  *
00265  * Return codes:
00266  *              0 - message processed successfully
00267  *              1 - roc id >= rocArrSize
00268  *              2 - CALIBR_OFF received without foregoing CALIBR_ON
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; // No need to generate an error -- it's normal to work with only some of the ROCs
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  * Extract baseline positions (pedestals) and widths (TODO: not yet implemented) from data,
00318  * where most of the hits have 0 amplitude (for instance, data taken with external trigger).
00319  * The data is divided on slices fExttrigCalibrPeriod nanoseconds long. Pedestals and
00320  * widths are extracted from data within one slice, and the result is granted to the user
00321  * for the time of the next slice. Therefore fExttrigCalibrPeriod must be set correctly.
00322  *
00323  * Data is to be passed via the msg argument. Correct fullTime is necessary for the time-slicing.
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; // => rocId >= fRocArrSize -- it's ok to work with only a fraction of the ROCs, no need to generate an error.
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                 // printf( "In TPedestalExtractor::ExtractExttrigStream: Extracting pedestals...\n" );
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                                 // Print();
00346                         }
00347                 }
00348                 else {
00349                     if( ! fPrintedDataNotTimeSorted ) {
00350                         PrintLog( "Warning in TPedestalExtractor::ExtractExttrigStream: Data is not time-sorted!", 0 );
00351                         fPrintedDataNotTimeSorted = kTRUE;
00352                         // PrintLog( Form( "FullTs: %d", fullTime ), 0 );
00353                         // msg.printData( roc::msg_print_Human );
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 //Int_t TPedestalExtractor::ExtractAutocalibrRoot( std::string rootFileName, Int_t calibrIteration )
00428 //{
00429 //
00430 //}
00431 
00432 
00433 //Int_t TPedestalExtractor::ExtractExttrigLmd( std::string lmdFileName, Float_t startTime, Float_t finishTime )
00434 //{
00435 //
00436 //}
00437 
00439 // * startTime and finishTime - w.r.t. beginning of the file
00440 // * fTreePathName must be set correctly.
00441 // */
00442 //Int_t TPedestalExtractor::ExtractExttrigRoot( std::string rootFileName, uint64_t startTime, uint64_t finishTime )
00443 //{
00444 //      uint64_t fileBeginning = 0;
00445 //      uint64_t currTime = 0;
00446 //      TFile inputFile( rootFileName.c_str(), "read" );
00447 //      TTree * tree = dynamic_cast< TTree * >( inputFile.Get( treePathName.c_str() ) );
00448 //
00449 //      TRocData* rocData= new TRocData();
00450 //      tree->SetBranchAddress("ROC00.", &rocData);
00451 //      tree->SetBranchStatus("*",0);
00452 //      tree->SetBranchStatus("ROC00.*",1);
00453 //
00454 //      Int_t nEntries = tree->GetEntries();
00455 //      for( Int_t iEntry = 0; iEntry < nEntries; ++iEntry ) {
00456 //              tree->GetEntry( iEntry );
00457 //              Int_t nMsg= rocData->fExtMessages.size();
00458 //              for( Int_t iMsg = 0; iMsg < nMsg; iMsg++ ) {
00459 //                      TRocMessageExtended msgExt = rocData->fExtMessages.at( iMsg );
00460 //                      roc::Message rocMsg = msgExt.GetRocMessage();
00461 //                      uint8_t msgType = rocMsg.getMessageType();
00462 //                      if( msgType == roc::MSG_HIT ) {
00463 //                              currTime = msgExt.GetFullTime();
00464 //                              if( fileBeginning == 0 ) {
00465 //                                      fileBeginning = currTime;
00466 //                              }
00467 //
00468 //                              if( currTime > fileBeginning + startTime ) {
00469 //                                      AddHit( rocMsg.getRocNumber(), rocMsg.getNxNumber(), rocMsg.getNxChNum(), rocMsg.getNxAdcValue() );
00470 //                              }
00471 //                      }
00472 //              }
00473 //              if( currTime > fileBeginning + finishTime ) {
00474 //                      break;
00475 //              }
00476 //      }
00477 //      Extract();
00478 //      printf("Pedestals extracted\n");
00479 //      return 0;
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                 //printf( "In TPedestalExtractor::Extract( Int_t rocId = %d )\n", rocId );
00503                 //Print( rocId );
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            // It's normal that some channels have no data (e.g. masked channels). No error message has to be written.
00520            // printf( "In TPedestalExtractor::Extract: hist contains %f entries\n", hist->GetEntries() );
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  * Reset the acquired baseline data of all ROCs.
00549  * Does not change the results of pedestal extraction.
00550  */
00551 void TPedestalExtractor::ResetCalibrData()
00552 {
00553         for( Int_t iRoc = 0; iRoc < fRocArrSize; iRoc++ ) {
00554                 ResetCalibrData( iRoc );
00555         }
00556 }
00557 
00558 
00559 /*
00560  * Reset the acquired baseline data of the specified ROCs.
00561  * Does not change the results of pedestal extraction.
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  * Reset results of pedestal and baseline width extraction on all ROCs.
00580  * Does not affect the baseline data.
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  * Reset results of pedestal and baseline width extraction on ROC rocId.
00594  * Does not affect the baseline data.
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  * Expects a text file with pedestals in format:
00722  *    roc  nxyter  channel  pedestal_position  pedestal_width
00723  * Checks if the first line of the file matches the expected format,
00724  * and if not -- tries to interpret the file as in the old format:
00725  *    pedestal_position
00726  * with all pedestals stored in the order from the first one till the last
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  * Print pedestals and widths in all channels of all n-XYTERs on all ROCs.
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  * Print pedestals and widths in all channels of all n-XYTERs on the particular ROC.
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 }

Generated on Tue Dec 10 2013 04:52:22 for ROCsoft by  doxygen 1.7.1