Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

RxThresholdBasedOnConfigRegisterAlgorithm.cpp

00001 #include "RxThresholdBasedOnConfigRegisterAlgorithm.h"
00002 #include "AnalysisAlgorithmMap.h"
00003 #include "SctData/RxThresholdBasedOnConfigRegisterTestResult.h"
00004 //#include "SctData/StandardDefects.h"
00005 //#include "SctData/FitObject.h"
00006 #include "SctData/RawScanResult.h"
00007 #include <cfloat>
00008 #include "TH2.h"
00009 #include "Sct/SctNames.h"
00010 
00011 // gcc296 needs a declaration of fabs
00012 #include <cmath>
00013 #include <set>
00014 
00015 using namespace std;
00016 using namespace boost;
00017 using namespace SctData;
00018 using namespace Sct;
00019 
00020 namespace SctAnalysis {
00021     
00022     bool RxThresholdBasedOnConfigRegisterAlgorithm::inMap = AnalysisAlgorithmMap::instance().setAlgorithm("RxThresholdBasedOnConfigRegisterTest", auto_ptr<AnalysisAlgorithm>(new RxThresholdBasedOnConfigRegisterAlgorithm()));
00023     
00024     shared_ptr<AnalysisAlgorithm> RxThresholdBasedOnConfigRegisterAlgorithm::clone(const TestData& testData, const string& moduleName) const throw() {
00025     return shared_ptr<AnalysisAlgorithm>(new RxThresholdBasedOnConfigRegisterAlgorithm(testData, moduleName, *this));
00026     }
00027 
00028     void RxThresholdBasedOnConfigRegisterAlgorithm::loadData() {
00029     loadAllRaws();
00030     }
00031 
00032     
00033     bool RxThresholdBasedOnConfigRegisterAlgorithm::canAnalyze() const{
00034     return hasAllRaws();
00035     }
00036     
00037     shared_ptr<TestResult> RxThresholdBasedOnConfigRegisterAlgorithm::createTestResult() const {
00038     return shared_ptr<RxThresholdBasedOnConfigRegisterTestResult> (new RxThresholdBasedOnConfigRegisterTestResult());
00039     }
00040      
00041   // categorise the data from any particular RxThreshold value:
00042   typedef enum {undecided,white,black,blackAndWhite,coloured} Category;
00043 
00044   std::string categoryToString(const Category & category) {
00045     return
00046       category==undecided     ? "undecided" :
00047       category==white         ? "white" :
00048       category==black         ? "black" :
00049       category==blackAndWhite ? "blackAndWhite" :
00050       category==coloured      ? "coloured" :
00051       "couldNotFigureCategoryOut";
00052   };
00053     
00054   struct Wadge {
00055     Category category;
00056     unsigned int initialThreshIndex;
00057     unsigned int width;
00058     unsigned int from() const { return initialThreshIndex; };
00059     unsigned int to() const { return initialThreshIndex+width-1; };
00060     void reset(const Category & cat, const unsigned int index) {
00061       category = cat;
00062       initialThreshIndex = index;
00063       width=1;
00064     };
00065     bool is(const Category & cat) const { return cat == category; }; 
00066     void increment() {
00067       ++width;
00068     };
00069     void makeEmpty() {
00070       width=0;
00071     };
00072     bool isEmpty() const {
00073       return width==0;
00074     };
00075     Wadge() {
00076       makeEmpty();
00077     };
00078     bool operator<(const Wadge & other) const {
00079       return (this->width)<(other.width);
00080     };
00081   };
00082     
00083     void RxThresholdBasedOnConfigRegisterAlgorithm::analyze() {
00084       RxThresholdBasedOnConfigRegisterTestResult & result =  *dynamic_pointer_cast<RxThresholdBasedOnConfigRegisterTestResult> ( getTestResult() );
00085       
00086       const RawScanResult& raw = *getRaw(0);
00087       result.setScanVariable(raw.getHeader().getVariable());    
00088 
00089       const SctData::ScanPoints& points=raw.getPoints();
00090       const unsigned int numberOfScanPoints = points.getNPoints();
00091  
00092       for (short unsigned int ilink=0; ilink<nLinkModule; ++ilink ) {
00093         const TH2& data = raw.getScanData(ilink);
00094         const unsigned nx = data.GetNbinsX();
00095         const unsigned ny = data.GetNbinsY();
00096         if(!(ny==numberOfScanPoints)) {
00097           throw IllegalStateError("This should not be!",__FILE__,__LINE__);
00098         };
00099         if(ny<=0) {
00100           throw IllegalStateError("This should not be!",__FILE__,__LINE__);
00101         };
00102         
00103         
00104        
00105         typedef unsigned int Width;
00106         typedef std::multiset<Wadge> Wadges;
00107 
00108         Wadges blackAndWhiteWadges;
00109         {
00110           Wadge currentWadge;
00111           
00112           for (unsigned int threshIndex = 0;
00113                threshIndex < ny;
00114                ++threshIndex) {
00115             const unsigned int nTriggers = points.getNEvents(threshIndex);
00116             const double rxThresh = points.getPoint(threshIndex);
00117             
00118             Category category = undecided;
00119             bool sawZero = false;
00120             bool sawSaturation = false;
00121             for (unsigned int x=0; x<nx && category==undecided; ++x) {
00122               const Stat_t binContent_t = data.GetBinContent(x+1, threshIndex+1);
00123               const unsigned int binContent = static_cast<unsigned int>(binContent_t);
00124               if (static_cast<Stat_t>(binContent) != binContent_t) {
00125                 throw IllegalStateError("This cannot be!",__FILE__,__LINE__);            
00126               }; 
00127               
00128               if (binContent != 0 && binContent != nTriggers) {
00129                 category = coloured;
00130                 break;
00131               };
00132               
00133               if (binContent == 0) {
00134                 sawZero = true;
00135                 continue;
00136               } else if (binContent == nTriggers) {
00137                 sawSaturation = true;
00138                 continue;
00139               };
00140             };
00141             
00142             if (category==undecided) {
00143               if (sawZero && !sawSaturation) {
00144                 category = white;
00145               } else if (sawSaturation && !sawZero) {
00146                 category = black;
00147               } else if (sawSaturation &&  sawZero) {
00148                 category = blackAndWhite;
00149               } else {
00150                 throw IllegalStateError("This cannot be!",__FILE__,__LINE__);
00151               };
00152             };
00153             
00154             if (category==undecided) {
00155               throw IllegalStateError("This cannot be!",__FILE__,__LINE__);
00156             };
00157             
00158             // so now we know what this line was like!
00159             
00160             {
00161               const bool debug=false;
00162               if (debug) {
00163                 const std::string lineType = 
00164                   std::string("lineType was ") + categoryToString(category);
00165                 
00166                 std::ostringstream os;
00167                 os << "Rx thresh " << rxThresh << " lineType " << lineType << " ntriggers " << nTriggers;
00168                 std::string message = os.str();
00169                 SctNames::Mrs()<< "RXTHRESHBASEDONCON" << MRS_INFORMATION<< MRS_TEXT(message.c_str())<< ENDM;
00170               };
00171             };
00172             
00173             if (currentWadge.isEmpty()) {
00174               currentWadge.reset(category, threshIndex);
00175             } else {
00176               // currentWadge is NOT empty !
00177               if (currentWadge.is(category)) {
00178                 currentWadge.increment();
00179               } else {
00180                 // the category just CHANGED !
00181 
00182                 // insert the wadge if it is blackAndWhite
00183                 if (currentWadge.is(blackAndWhite)) {
00184                   blackAndWhiteWadges.insert(currentWadge);
00185                 };
00186 
00187                 // reset the current wadge to match the new category
00188                 currentWadge.reset(category, threshIndex);
00189               };
00190             };
00191             
00192           };
00193           if (currentWadge.is(blackAndWhite)) {
00194             blackAndWhiteWadges.insert(currentWadge);
00195           };
00196         };
00197         // Now the blackAndWhiteWadges are fully set up!
00198         
00199         {
00200           const bool debug=true;
00201           if (debug) {
00202             for (Wadges::const_iterator it = blackAndWhiteWadges.begin();
00203                  it!=blackAndWhiteWadges.end();
00204                  ++it) {
00205               const Wadge & wadge = *it;              
00206               std::ostringstream os;
00207               os << categoryToString(wadge.category) << " wadge of width " << wadge.width << " from " << wadge.from() << " to " << wadge.to();
00208               const std::string message = os.str();
00209               SctNames::Mrs()<< "RXTHRESHBASEDONCON" << MRS_INFORMATION<< MRS_TEXT(message.c_str())<< ENDM;
00210             };
00211           };
00212         };
00213 
00214         {          
00215           const bool debug=true;
00216           if (debug) {
00217             if (blackAndWhiteWadges.empty()) {
00218               SctNames::Mrs()<< "RXTHRESHBASEDONCON"<< MRS_INFORMATION<< MRS_TEXT("There were no blackAndWhite wadges for this stream!") << ENDM;
00219             } else {
00220               const Wadge & largestWadge = *(blackAndWhiteWadges.rbegin());              
00221               std::ostringstream os;
00222               os << "The largest wadge was " << categoryToString(largestWadge.category) << " and had width " << largestWadge.width << " from " << largestWadge.from() << " to " << largestWadge.to();
00223               const std::string message = os.str();
00224               SctNames::Mrs()<< "RXTHRESHBASEDONCON" << MRS_INFORMATION<< MRS_TEXT(message.c_str())<< ENDM;
00225             };
00226           };
00227         };
00228 
00229         {
00230           
00231           bool pass=false;
00232           bool problem=false;
00233           
00234           // time to record the result of the test!
00235           RxThresholdBasedOnConfigRegisterTestResult::StreamResult & sr = result.getStreamResult(ilink);
00236           if (blackAndWhiteWadges.empty()) {
00237             problem=true;
00238             sr.setInvalid(); // this should be unnecessary as it already should be constructed as invalid until set otherwise ...
00239           } else {
00240             pass=true;
00241             const Wadge & largestWadge = *(blackAndWhiteWadges.rbegin());
00242             const unsigned int fromIndex = largestWadge.from();
00243             const unsigned int toIndex   = largestWadge.  to();
00244             const int fromThresh = static_cast<int>(points.getPoint( fromIndex ));
00245             const int   toThresh = static_cast<int>(points.getPoint(   toIndex ));
00246             const int bestThresh = (fromThresh+toThresh)/2;
00247             sr.setMinErrorFreeRxThresh(fromThresh);
00248             sr.setMaxErrorFreeRxThresh(  toThresh);
00249             sr.        setBestRxThresh(bestThresh);
00250           };
00251           
00252           result.setPassed(pass);
00253           result.setProblem(problem);
00254 
00255         };
00256 
00257         //HERE LESTER
00258        
00259       };
00260       
00261      /* 
00262 
00263     float min_time[nChipModule];
00264     float max_time[nChipModule];
00265     for (unsigned ichip=0; ichip<nChipModule; ++ichip){
00266         min_time[ichip]=FLT_MAX;  // large positive number
00267         max_time[ichip]=-FLT_MAX; // large negative number
00268     }
00269 
00270     for (unsigned index=0; index<result->getNScans() ; ++index){
00271         shared_ptr<const FitScanResult> fit=getFit(index);
00272 
00273         if (!fit.get()) {
00274         ostringstream os;
00275         os << "RxThresholdBasedOnConfigRegisterAlgorithm: unable to get scan at index " << index <<ends;
00276         throw IllegalStateError(os.str(), __FILE__, __LINE__);
00277         }
00278         
00279         if (debug) cout << "RxThresholdBasedOnConfigRegisterAlgorithm, scan #" << index << endl;
00280 
00281         for (unsigned ichip=0; ichip<nChipModule; ++ichip){
00282         RxThresholdBasedOnConfigRegisterTestResult::ChipTWResult& cr = result->getChipResult(ichip);
00283         FitObject& fobj =fit->getChipFit(ichip);
00284         
00285         // on last point calculate calibration factor.
00286         if (debug) cout << index << "," 
00287                 << result->getNScans()-1 << endl;
00288         if (index==result->getNScans()-1){
00289             cr.calibration = // mid-rise - mid-fall
00290             fabs(fobj.getParameter(1) - fobj.getParameter(3));
00291             if (debug) {
00292             cout << "Calibration for chip "
00293                  << ichip << " = " << cr.calibration<< endl;
00294             }
00295             
00296         }
00297         if (fobj.getParameter(3) < min_time[ichip]){
00298             min_time[ichip]=fobj.getParameter(3);
00299         }
00300         if (fobj.getParameter(3) > max_time[ichip]){
00301             max_time[ichip]=fobj.getParameter(3);
00302         }
00303         }
00304     }
00305     
00306     bool pass = true;
00307 
00308     for (unsigned ichip=0; ichip<nChipModule; ++ichip){
00309         RxThresholdBasedOnConfigRegisterTestResult::ChipTWResult& cr = result->getChipResult(ichip);
00310         cr.timewalk=max_time[ichip]-min_time[ichip];
00311         if (debug) {
00312         cout << "Time-walk for chip " << ichip
00313              << " = " <<  cr.timewalk << endl;
00314         }
00315         if (cr.timewalk<StandardDefects::TW_LO.getParameter()){
00316         result->getDefects().addDefect(Defect(StandardDefects::TW_LO, ModuleElement::Chip(ichip)));
00317         pass = false;
00318         }
00319         if (cr.timewalk>StandardDefects::TW_HI.getParameter()){
00320         result->getDefects().addDefect(Defect(StandardDefects::TW_HI, ModuleElement::Chip(ichip)));
00321         pass = false;
00322         }
00323     }
00324 
00325     result->setPassed(pass);
00326     if (debug) cout << "RxThresholdBasedOnConfigRegisterAlgorithm done" << endl;
00327       */
00328     }
00329 } // end of namespace SctAnalysis

Generated on Fri Jan 14 12:47:06 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5