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