SCTDAQComp.cpp

00001 #include "SctData/FitScanResult.h"
00002 #include "SctData/FitObject.h"
00003 #include "SctDaqRootFileExt.h"
00004 
00005 #include "../../AnalysisTests/AnalysisTestFramework.h"
00006 #include "../../AnalysisTests/CutUtils.h"
00007 
00008 using SctData::FitScanResult;
00009 using SctData::FitObject;
00010 
00011 struct Info {
00012     unsigned int runNumber;
00013     unsigned int scanNumber;
00014     unsigned int index;
00015     char serial[15];    
00016 };
00017 string InfoStr = "Run/i:Scan:Index:Serial/C";
00018 
00019 struct FitData {
00020     double Params[5];
00021     double ChiSq;
00022     
00023     FitData() {Params[0]=Params[1]=Params[2]=Params[3]=Params[4]=ChiSq=0;}
00024 };
00025 string DataStr = "P0/D:P1:P2:P3:P4:ChiSq";
00026 
00027 
00028 //Globals variables:
00029 FitData RodDaq;
00030 FitData SctDaq;
00031 struct Info info;
00032 
00033 //Output files
00034 string SCurveOutput = "${SCT_DAQ_ROOT}/SystemTests/logs/SCurveFit.root";
00035 string StrobeOutput = "${SCT_DAQ_ROOT}/SystemTests/logs/StrobeDelayFit.root";
00036 string TWOutput = "${SCT_DAQ_ROOT}/SystemTests/logs/TimeWalkFit.root";
00037 
00038 
00039 enum Mode {
00040     SCURVE,
00041     STROBE,
00042     TIMEWALK
00043 };
00044 
00045 
00046 class FitCompare : public AnalysisTestFramework<FitScanResult> {
00047 public:
00048     FitCompare();
00049     virtual void publishData();
00050     virtual void downloadData(string serialNumber);
00051     virtual void compare(const FitScanResult& t);   
00052     virtual void setup();
00053     virtual void printAdditionalArgs();
00054     virtual int handleArg(int argc, int i, char** argv);
00055     virtual void summaryOutput();
00056     void FillTree(FitObject& ob, unsigned int index);
00057     
00058     Mode mode;
00059 };
00060 
00061 
00062 
00063 string getFileName(const TestInfo& info, unsigned int scanNumber) {
00064     ostringstream oss;
00065     oss << info.path << "/strun" << info.runNumber << "_" << scanNumber << ".root";
00066     return oss.str();
00067 }
00068 
00069 double histMean(TH1& hist, unsigned int start, unsigned int nBins) {
00070     double total = 0;
00071     for (unsigned int i=start; i<start+nBins; ++i) {
00072     total += hist.GetBinContent(i);
00073     }
00074     return total/nBins;
00075 }
00076 
00077 void FitCompare::compare(const FitScanResult& fit) {
00078     info.runNumber = fit.getHeader().getRunNumber();
00079     info.scanNumber = fit.getHeader().getScanNumber();
00080     strncpy(info.serial, fit.getHeader().getModuleName().c_str(), 14);
00081                   
00082     TestInfo testInfo;
00083     switch (mode) {
00084     case SCURVE:
00085     testInfo = moduleData.getResponseCurveInfo(fit.getHeader().getModuleName());
00086     break;
00087     case STROBE:
00088     testInfo = moduleData.getStrobeDelayInfo(fit.getHeader().getModuleName());
00089     break;
00090     case TIMEWALK:
00091     testInfo = moduleData.getTimeWalkInfo(fit.getHeader().getModuleName());
00092     break;
00093     default:
00094     cerr << "Unsupported mode" << endl;
00095     }
00096     
00097     SctDaqRootFileExt file(getFileName(testInfo, fit.getHeader().getScanNumber()));
00098     unsigned int cycle = file.getCycleNum(fit.getHeader().getModuleName());
00099     
00100     
00101     for (unsigned int link = 0; link<nLinkModule; ++link) { 
00102     if (mode == SCURVE) {
00103         auto_ptr<TH1> gMean = file.getMeanGraph(link, cycle);
00104         auto_ptr<TH1> gSigma = file.getSigmaGraph(link, cycle); 
00105         auto_ptr<TH1> gChiSq = file.getChiSqGraph(link, cycle);
00106         
00107         for (unsigned int i=0; i<nChannelLink; ++i) {
00108         SctDaq.Params[1] = gMean->GetBinContent(i);
00109         SctDaq.Params[2] = gSigma->GetBinContent(i);     
00110         SctDaq.ChiSq = gChiSq->GetBinContent(i);
00111         
00112         FitObject& fitOb = fit.getChannelFit(link, i);
00113         FillTree(fitOb, i + link*nChannelLink);     
00114         }       
00115     } else {
00116         for (unsigned int i=0; i<nChipLink; ++i) {  
00117         FitObject& fitOb = fit.getChipFit(link, i);
00118         FillTree(fitOb, i + link*nChipLink);        
00119         }
00120     }
00121     }
00122 }
00123 
00124 void FitCompare::FillTree(FitObject& fitOb, unsigned int index) {
00125     //Note SCTDAQ stores in bin 0 - the underflow bin!  
00126     for (unsigned int i=0; i<fitOb.getNPar(); ++i) {
00127     RodDaq.Params[i] = fitOb.getParameter(i);
00128     }
00129     RodDaq.ChiSq = fitOb.getChiSquared() / (fitOb.getNDF() + fitOb.getNPar());
00130         
00131     info.index = index;
00132     tree->Fill();
00133 }
00134 
00138 void FitCompare::setup() {
00139     string name;
00140     switch (mode) {
00141     case SCURVE:
00142     name = SCurveOutput;
00143     break;
00144     case STROBE:
00145     name = StrobeOutput;
00146     break;
00147     case TIMEWALK:
00148     name = TWOutput;
00149     break;
00150     default:
00151     cerr << "Ooops, unsupported mode" << endl;
00152     }    
00153     name = Env::substituteVariables(name);
00154     file = new TFile(name.c_str(), "RECREATE");
00155     tree = new TTree("FitData", "Fit Scan Comparison Data");
00156     tree->Branch("Info", &info, InfoStr.c_str());
00157     tree->Branch("SctDaq", &SctDaq, DataStr.c_str());
00158     tree->Branch("RodDaq", &RodDaq, DataStr.c_str());
00159     info.serial[14] = '\0';
00160 }
00161 
00162 
00163 
00167 void FitCompare::downloadData(string serialNumber) {
00168 }
00169 
00170 void FitCompare::publishData() {
00171     switch (mode) {
00172     case SCURVE:
00173     highLevelApi->responseCurve();
00174     break;
00175     case STROBE:
00176     highLevelApi->strobeDelay();
00177     break;
00178     case TIMEWALK:
00179     highLevelApi->timeWalk();
00180     break;
00181     default:
00182     cerr << "Ooops, unsupported mode" << endl;
00183     }
00184 }
00185 
00189 void FitCompare::summaryOutput() {
00190     if (mode != SCURVE) return;
00191     if (cut(*tree, "mean", "(RodDaq.P1-SctDaq.P1)/SctDaq.P1*100", 0.1, 0.5, 5) > 0) {
00192     ++exitCode;
00193     cout << "Failed mean tail check" << endl;
00194     }
00195     if (cut(*tree, "sigma", "(RodDaq.P2-SctDaq.P2)/SctDaq.P2*100", 1, 2, 20) > 0) {
00196     ++exitCode;
00197     cout << "Failed sigma tail check" << endl;
00198     }
00199     if (cut(*tree, "chi", "(RodDaq.ChiSq-SctDaq.ChiSq)", 0.3, 0.5, 10) > 0) {
00200     ++exitCode;
00201     cout << "Failed ChiSq tail check" << endl;
00202     }
00203     exitCode += errorCode;
00204 }
00205 
00206 FitCompare::FitCompare() : AnalysisTestFramework<FitScanResult>(".*FitScanResult", "FittedData") {
00207     mode = SCURVE;
00208     nData = 10;
00209 }
00210 
00211 void FitCompare::printAdditionalArgs() {
00212     cout << "-strobe:           Do strobe delay fits instead" << endl;
00213     cout << "-timewalk:     Do strobe delay fits using the timewalk test instead" << endl;
00214     cout << "Default is to publish a response curve and do s-curve fitting" << endl;
00215 }
00216 
00217 int FitCompare::handleArg(int argc, int i, char** argv) {
00218     if (string(argv[i]) == "-strobe") {
00219     mode = STROBE;  
00220     nData = 1;
00221     } else if (string(argv[i]) == "-timewalk") {
00222     mode = TIMEWALK;
00223     nData = 10;
00224     } else {
00225     cout << "Invalid argument " << argv[i] << endl;
00226     printHelp();
00227     }
00228     return i;
00229 }
00230 
00231 int main(int argc, char** argv) {
00232     TH1::AddDirectory(true);
00233     FitCompare sdc;
00234     sdc.analyzeAll(argc, argv);
00235     return sdc.getExitCode();
00236 }
00237 
00238 
00239 
00240 

Generated on Mon Feb 6 14:01:29 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6