RetrieveAndCompare.cpp

00001 #include "SctData/NPtGainTestResult.h"
00002 #include "SctData/ResponseCurve.h"
00003 #include "../../AnalysisTestFramework.h"
00004 #include "../../CutUtils.h"
00005 #include "../../TestFileUtil.h"
00006 
00007 #include "SctData/StandardDefects.h"
00008 #include "ipc/core.h"
00009 
00010 struct ChannelInfo {
00011     unsigned int runNumber;
00012     unsigned int scanNumber;
00013     unsigned int channel;
00014     unsigned int nScans;
00015     float SCTDAQVersion;
00016     char serial[15];    
00017 };
00018 string ChannelInfoStr = "Run/i:Scan:Channel:NScans:SCTDAQVersion/F:Serial/C";
00019 
00020 struct ChannelRCData {
00021     double gain;
00022     double noise;
00023     double offset;
00024     int code;
00025 };
00026 string ChannelDataStr = "Gain/D:Noise:Offset:Code/I";
00027 
00028 struct ChipInfo {
00029     unsigned int runNumber;
00030     unsigned int scanNumber;
00031     unsigned int chip;
00032     unsigned int nScans;
00033     float SCTDAQVersion;
00034     char serial[15];    
00035 };
00036 string ChipInfoStr = "Run/i:Scan:Chip:NScans:SCTDAQVersion/F:Serial/C";
00037 
00038 struct ChipRCData {
00039     double gain;
00040     double noise;
00041     double offset;
00042     double p0;
00043     double p1;
00044     double p2;
00045     int func;
00046     int pass;
00047 };
00048 string ChipDataStr = "Gain/D:Noise:Offset:P0:P1:P2:Func/I:Pass";
00049 
00050 //Response curve stuff
00051 string RCExt = "_rc.dat";
00052 string RCExt2 = "_ResponseCurve.dat";
00053 string RCOutput = "${SCT_DAQ_ROOT}/SystemTests/logs/ResponseCurve.root";
00054 string RCArg = "-RC";
00055 string RCArg2 = "-7";
00056 
00057 //Three-point gain stuff
00058 string GainExt = "_rc3.dat";
00059 string GainExt2 = "_3PtGain.dat";
00060 string GainOutput = "${SCT_DAQ_ROOT}/SystemTests/logs/ThreePointGain.root";
00061 string GainArg = "-3ptgain";
00062 string GainArg2 = "-5";
00063 
00064 //Globals variables:
00065 ChipRCData RodDaq;                    //Our data
00066 ChipRCData SctDaq;                    //SCTDAQ data
00067 ChipInfo info;                          //Some info
00068 
00069 ChannelRCData chRodDaq; 
00070 ChannelRCData chSctDaq; 
00071 ChannelInfo chInfo;
00072 
00073 
00074 class NPtGainCompare : public AnalysisTestFramework<NPtGainTestResult> {
00075 public:
00076     NPtGainCompare();
00077     virtual void publishData();
00078     virtual void downloadData(string serialNumber);
00079     virtual void compare(const NPtGainTestResult& t);   
00080     virtual float compareChip(const NPtGainTestResult& t);      
00081     virtual void compareChannel(const NPtGainTestResult& t, float version);     
00082     virtual void setup();
00083     virtual void printAdditionalArgs();
00084     virtual int handleArg(int argc, int i, char** argv);
00085     virtual void summaryOutput();
00086     string getFileName(string serial);
00087     string getRCFileName(string serial);
00088     
00089     bool rc;
00090     TTree* channelTree;
00091 };
00092 
00093 void NPtGainCompare::summaryOutput() {
00094     //Chip data tests
00095     if (cut(*tree, "pass", "(RodDaq.Pass-SctDaq.Pass)", 0.01, 0.01, 0.01, true) > 0) {
00096         ++exitCode;
00097         cout << "Failed pass tail check" << endl;
00098     }    
00099     if (cut(*tree, "gain", "(RodDaq.Gain-SctDaq.Gain)/SctDaq.Gain", 0.01, 0.01, 0.15, true) > 0) {
00100         ++exitCode;
00101         cout << "Failed gain tail check" << endl;
00102     }    
00103     if (cut(*tree, "noise", "(RodDaq.Noise-SctDaq.Noise)/SctDaq.Noise", 0.02, 0.02, 0.25, true) > 0) {
00104         ++exitCode;
00105         cout << "Failed noise tail check" << endl;
00106     }    
00107     if (cut(*tree, "offset", "(RodDaq.Offset-SctDaq.Offset)", 1, 10, 80, true) > 0) {
00108         ++exitCode;
00109         cout << "Failed offset tail check" << endl;
00110     }
00111     /*
00112       No checks on the output params - they are too correlated for it to mean anything
00113       Hopefully the aggregate/summary tests above and below will suffice
00114       
00115     if (cut(*tree, "p0", "(RodDaq.P0-SctDaq.P0)", 0.01, 0.01, 0.01, true) > 0) {
00116         ++exitCode;
00117         cout << "Failed p0 tail check" << endl;
00118     }    
00119     if (cut(*tree, "p1", "(RodDaq.P1-SctDaq.P1)", 0.01, 0.01, 0.01, true) > 0) {
00120         ++exitCode;
00121         cout << "Failed p1 tail check" << endl;
00122     }    
00123     if (cut(*tree, "p2", "(RodDaq.P2-SctDaq.P2)", 0.01, 0.01, 0.01, true) > 0) {
00124         ++exitCode;
00125         cout << "Failed p2 tail check" << endl;
00126     }    
00127     */
00128     
00129     //Channel data tests
00130     if (cut(*channelTree, "gain_chan", "(RodDaq.Gain-SctDaq.Gain)/SctDaq.Gain", 0.01, 0.01, 0.15, true, "RodDaq.Code==0 && SctDaq.Code==0") > 0) {
00131         ++exitCode;
00132         cout << "Failed gain tail check" << endl;
00133     }
00134     if (cut(*channelTree, "noise_chan", "(RodDaq.Noise-SctDaq.Noise)/SctDaq.Noise", 0.02, 0.02, 0.25, true, "RodDaq.Code==0 && SctDaq.Code==0") > 0) {
00135         ++exitCode;
00136         cout << "Failed noise tail check" << endl;
00137     }
00138     if (cut(*channelTree, "offset_chan", "(RodDaq.Offset-SctDaq.Offset)", 1, 10, 80, true, "RodDaq.Code==0 && SctDaq.Code==0") > 0) {
00139         ++exitCode;
00140         cout << "Failed offset tail check" << endl;
00141     }
00142     //Allow a very small %age of differences
00143     //Hopefully all these will be checked manually
00144     if (cut(*channelTree, "code", "(RodDaq.Code-SctDaq.Code)", 3, 800, 0.01, true) > 0.0015) {
00145         ++exitCode;
00146         cout << "Failed code tail check" << endl;
00147     }
00148 
00149     exitCode += errorCode;
00150 }
00151 
00152 
00153 void NPtGainCompare::compare(const NPtGainTestResult& sd) {
00154     float version = compareChip(sd);
00155     compareChannel(sd, version);
00156 }
00157 
00158 int getCode(const NPtGainTestResult& rc, unsigned int channel) {
00159     list<Defect> defects = rc.getDefects().getDefectsAffectingElement(ModuleElement::Channel(channel))->getAllDefects();
00160     
00161     int code = 0;
00162     if (defects.size() > 0) cout << "Channel: " << channel << endl;
00163     for (list<Defect>::const_iterator i=defects.begin(); i!=defects.end(); ++i) {
00164     cout << i->getPrototype().getName() << endl;
00165     if (i->getPrototype() == StandardDefects::DEAD) code += 0x1;
00166     else if (i->getPrototype() == StandardDefects::STUCKON) code += 0x2;
00167     else if (i->getPrototype() == StandardDefects::PARTBONDED) code += 0x8000;
00168     else if (i->getPrototype() == StandardDefects::UNBONDED) code += 0x1000;
00169     else if (i->getPrototype() == StandardDefects::NOISY) code += 0x4000;   
00170     else if (i->getPrototype() == StandardDefects::LO_GAIN) code += 0x100;  
00171     else if (i->getPrototype() == StandardDefects::HI_GAIN) code += 0x200;
00172     else if (i->getPrototype() == StandardDefects::LO_OFFSET) code += 0x400;
00173     else if (i->getPrototype() == StandardDefects::HI_OFFSET) code += 0x800;    
00174     //These are not SCTDAQ codes - should be ignored?
00175     else if (i->getPrototype() == StandardDefects::VLO_GAIN) code += 0x10000; 
00176     else if (i->getPrototype() == StandardDefects::UNDER) code += 0x20000; 
00177     else if (i->getPrototype() == StandardDefects::BADFIT) code += 0x30000; 
00178     else code += 100000;
00179     }
00180     return code;
00181 }
00182 
00183 string NPtGainCompare::getRCFileName(string serial) {
00184     ostringstream oss;
00185     oss << serial;
00186     if (rc) oss << RCExt;
00187     else oss << GainExt;
00188     
00189     return oss.str();
00190 }
00191 
00192 void NPtGainCompare::compareChannel(const NPtGainTestResult& rc, float version) {
00193     strncpy(chInfo.serial, rc.getModuleName().c_str(), 14);
00194     chInfo.runNumber = rc.getRunNumber();
00195     chInfo.scanNumber = rc.getScanNumberAt(0);
00196     chInfo.nScans = rc.getNScans();
00197     chInfo.SCTDAQVersion = version;
00198     
00199     string fileName = getRCFileName(rc.getModuleName());
00200     ifstream file (fileName.c_str());
00201     if (!file.good())
00202         throw IllegalStateError("Failed to open comparison file: " + fileName, __FILE__, __LINE__);
00203     //Dump first line
00204     file.ignore(256, '\n');
00205 
00206     for (unsigned int channel = 0; channel<nChannelModule; ++channel) {
00207     string comment;
00208     file >> dec >> chInfo.channel >> hex >> chSctDaq.code >> chSctDaq.gain >> chSctDaq.offset >> chSctDaq.noise >> comment;
00209     //Ignore rest of line
00210     file.ignore(256, '\n');
00211     
00212     //Handle version differences and the fact that sometimes SCTDAQ records DEAD channels as
00213     //having lots of other defects too!  
00214     
00215     //Version >= 3.42 || Version <3.38
00216     if ((chInfo.SCTDAQVersion>3.415 || chInfo.SCTDAQVersion<3.3775) && (chSctDaq.code==4353 || chSctDaq.code==5377)) chSctDaq.code = 1;
00217     //3.38<=Version<=3.41
00218     if (chInfo.SCTDAQVersion>3.375 && chInfo.SCTDAQVersion<3.415 && chSctDaq.code==4481) chSctDaq.code = 1;
00219     
00220     if (channel != chInfo.channel) 
00221         throw IllegalStateError("Read incorrect channel", __FILE__, __LINE__);
00222     
00223     const NPtGainTestResultData& data = rc.getChannelData(channel);
00224 
00225     chRodDaq.code = getCode(rc, channel);
00226     chRodDaq.gain = data.gain;
00227     chRodDaq.noise = data.noise;
00228     chRodDaq.offset = data.offset;
00229 
00230         channelTree->Fill();
00231     }
00232 }
00233 
00234 string NPtGainCompare::getFileName(string serial) {
00235     ostringstream oss;
00236     oss << serial << (rc ? RCExt2 : GainExt2);
00237     
00238     return oss.str();
00239 }
00240 
00241 //Returns the version
00242 float NPtGainCompare::compareChip(const NPtGainTestResult& sd) {
00243     strncpy(info.serial, sd.getModuleName().c_str(), 14);
00244     info.runNumber = sd.getRunNumber();
00245     info.scanNumber = sd.getScanNumberAt(0);
00246     info.nScans = sd.getNScans();
00247     
00248     string fileName = getFileName(sd.getModuleName());
00249     ifstream file (fileName.c_str());
00250     if (!file.good())
00251         throw IllegalStateError("Failed to open comparison file: " + fileName, __FILE__, __LINE__);
00252     
00253     SctDaq.pass = TestFileUtil::getPass(file);
00254     info.SCTDAQVersion = TestFileUtil::skipHeader(file);
00255 
00256     for (unsigned int chip = 0; chip<nChipModule; ++chip) {
00257     info.chip = chip;
00258     double temp;
00259     string chipStr;
00260     file >> chipStr >> temp >> temp >> SctDaq.gain >> temp >> SctDaq.offset >> temp >> temp >> SctDaq.noise >> temp >> SctDaq.func
00261          >> SctDaq.p0 >> SctDaq.p1 >> SctDaq.p2;
00262     //Ignore rest of line
00263     file.ignore(256, '\n');
00264     
00265     RodDaq.pass = sd.getPassed();
00266     const NPtGainTestResultData& data = sd.getChipData(chip);   
00267     RodDaq.gain = data.gain;
00268     RodDaq.noise = data.noise;
00269     RodDaq.offset = data.offset;
00270     RodDaq.func = data.rc->getIndex();
00271     TF1& f = *data.rc->getFunction();
00272     RodDaq.p0 = f.GetParameter(0);
00273     RodDaq.p1 = f.GetParameter(1);
00274     RodDaq.p2 = f.GetParameter(2);
00275 
00276         tree->Fill();
00277     }
00278     return info.SCTDAQVersion;
00279 }
00280 
00281 
00285 void NPtGainCompare::setup() {
00286     string name = Env::substituteVariables(rc ? RCOutput : GainOutput);
00287     file = new TFile(name.c_str(), "RECREATE");
00288     tree = new TTree("NPtChip", "NPtGain Chip Comparison Data");
00289     channelTree = new TTree("NPtChannel", "NPtGain Channel Comparison Data");
00290     tree->Branch("RodDaq", &RodDaq, ChipDataStr.c_str());
00291     tree->Branch("SctDaq", &SctDaq, ChipDataStr.c_str());
00292     tree->Branch("Info", &info, ChipInfoStr.c_str());
00293     
00294     channelTree->Branch("RodDaq", &chRodDaq, ChannelDataStr.c_str());
00295     channelTree->Branch("SctDaq", &chSctDaq, ChannelDataStr.c_str());
00296     channelTree->Branch("Info", &chInfo, ChannelInfoStr.c_str());    
00297     
00298     info.serial[14] = '\0';
00299     chInfo.serial[14] = '\0';
00300 }
00301 
00305 void NPtGainCompare::downloadData(string serialNumber) {
00306     ostringstream oss;
00307     ostringstream oss1;
00308     SctTest::TestInfo info = rc ? moduleData.getResponseCurveInfo(serialNumber) : moduleData.get3PtGainInfo(serialNumber);
00309     oss << "java -Dtdaq.ipc.init.ref=$TDAQ_IPC_INIT_REF ProdDatabase/getDBfiles " << (rc ? RCArg : GainArg) << " -d -r " << info.runNumber << " -s " << info.scanNumber << "  " << serialNumber;
00310     oss1 << "java -Dtdaq.ipc.init.ref=$TDAQ_IPC_INIT_REF ProdDatabase/getDBfiles " << (rc ? RCArg2 : GainArg2) << " -d -r " << info.runNumber << " -s " << info.scanNumber << "  " << serialNumber;
00311     system(oss.str().c_str());
00312     system(oss1.str().c_str());
00313 }
00314 
00315 void NPtGainCompare::publishData() {
00316     if (rc) highLevelApi->responseCurve();
00317     else highLevelApi->threePointGain();
00318 }
00319 
00320 NPtGainCompare::NPtGainCompare() {
00321     channelTree = 0;
00322     rc = true;
00323 }
00324 
00325 void NPtGainCompare::printAdditionalArgs() {
00326     cout << "-3:           3pt-gain rather than response curve" << endl;
00327 }
00328 
00329 int NPtGainCompare::handleArg(int argc, int i, char** argv) {
00330     if (string(argv[i]) == "-3") {
00331     rc = false;    
00332     } else {
00333     printHelp();
00334     }
00335     return i;
00336 }
00337 
00338 int main(int argc, char** argv) {
00339         IPCCore::init(argc,argv);
00340     NPtGainCompare sdc;
00341     sdc.analyzeAll(argc, argv);
00342     return sdc.getExitCode();
00343 }
00344 
00345 

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