00001 #include "Sct/IS/IOManagerIS.h"
00002 #include "Sct/Exception.h"
00003 #include "Sct/LogicErrors.h"
00004 #include "Sct/SctNames.h"
00005 #include "Sct/SctParameters.h"
00006 #include "SctData/TrimRangeTestResult.h"
00007 #include <is/isinfo.h>
00008 #include <boost/shared_ptr.hpp>
00009 #include <iostream>
00010 #include <fstream>
00011
00012 #include "TGraph.h"
00013
00014 using namespace Sct;
00015 using namespace Sct::IS;
00016 using namespace boost;
00017 using namespace SctData;
00018 using namespace std;
00019
00020 string filename = "20220330200020_tr-1_20030708.trim";
00021 string testName = "TestData.SctData::TrimRangeTestResult.551.16.SCTTestAPI_PseudoModule";
00022 double compData[] = {};
00023 bool debug = false;
00024
00025 int range[12];
00026 double target[12];
00027 int trim[12][128];
00028
00029 void waitFor(string name, int nsecs) {
00030 ISInfoDictionary d(SctNames::getPartition());
00031 int tries = 0;
00032 while (!d.contains(name.c_str()) && tries < nsecs) {
00033 if (!sleep(1))
00034 ++tries;
00035 }
00036 if (tries == nsecs) {
00037 throw IllegalStateError("Didn't wait long enough", __FILE__, __LINE__);
00038 }
00039 if (debug) {
00040 cout << "Waited for " << tries << " secs" << endl;
00041 }
00042 }
00043
00044 void loadData(){
00045 cout << "Loading trim data from file " << filename << endl;
00046 ifstream f(filename.c_str());
00047 if (f.fail()) throw FileException(filename, "Error in opening Trimdata file", __FILE__, __LINE__);
00048 string dummy_string;
00049 for (unsigned i=0; i<12; ++i){
00050 f >> dummy_string >> dummy_string;
00051 f >> range[i] >> target[i];
00052 }
00053
00054 float temp_float;
00055 for (unsigned ichip=0; ichip<12; ichip++){
00056 for (unsigned ichan=0; ichan<128; ichan++){
00057 f >> temp_float;
00058 temp_float *= 15;
00059 trim[ichip][ichan]=(int)(temp_float+0.01);
00060 }
00061 }
00062 }
00063
00064 bool compare(shared_ptr<TrimRangeTestResult> result) {
00065 bool retVal = true;
00066
00067 for (unsigned ichip=0; ichip<12; ++ichip){
00068 const TrimRangeTestResult::ChipTrim& t = *result->chipTrim[ichip];
00069 if (t.range != range[ichip]){
00070 cout << "CHIP " << ichip << " range is "
00071 << t.range
00072 << " SCTDAQ: " << range[ichip] << endl;
00073 retVal=false;
00074 }
00075 if (fabs(t.target-target[ichip])>4.0){
00076 cout << "CHIP " << ichip
00077 << " target is " << t.target
00078 << " SCTDAQ: " << target[ichip] << endl;
00079 retVal=false;
00080 }
00081 }
00082
00083
00084 unsigned trimdiff[3]={0,0,0};
00085 for (unsigned ichip=0; ichip<12; ++ichip){
00086 const TrimRangeTestResult::ChipTrim& t = *result->chipTrim[ichip];
00087 for (unsigned ichan=0; ichan<128; ++ichan){
00088 short int our_trim=t.channelTrim.getAt(ichan).value.trim;
00089
00090
00091
00092 int diff = abs(our_trim - trim[ichip][ichan]);
00093
00094 if (diff!=0){
00095 cout << "CHIP " << ichip
00096 << " CHAN " << ichan
00097 << " trim is " << our_trim
00098 << " SCTDAQ: " << trim[ichip][ichan] << endl;
00099 for (int idiff=0; idiff<3; ++idiff){
00100 if (diff>idiff) trimdiff[idiff]++;
00101 }
00102 }
00103 }
00104 }
00105 for (int idiff=0; idiff<3; ++idiff){
00106 cout << "#channels with trim different by more than "
00107 << idiff << " = " << trimdiff[idiff] << endl;
00108
00109 }
00110
00111 if (trimdiff[1]>3 || trimdiff[2]>1) retVal=false;
00112 return retVal;
00113 }
00114
00115 int main(int argc, char** argv) {
00116 Sct::setExceptionHandlers(argv[0]);
00117 try {
00118 loadData();
00119 waitFor(testName, 60);
00120 shared_ptr<Serializable> ob = IOManagerIS::instance().read(testName);
00121 shared_ptr<TrimRangeTestResult> result = dynamic_pointer_cast<TrimRangeTestResult>(ob);
00122 if (!result)
00123 throw IllegalStateError("Failed to read test result: " + testName, __FILE__, __LINE__);
00124 if (compare(result)) {
00125 if (debug) cout << "Success!" << endl;
00126 return 0;
00127 }
00128 else {
00129 cout << "Failed" << endl;
00130 return 1;
00131 }
00132
00133 } catch (Throwable& e) {
00134 e.sendToMrs(MRS_FATAL);
00135 return 1;
00136 }
00137 }