HighLevelApi.cpp

00001 #include "HighLevelApi.h"
00002 #include "XmlSummaryFile.h"
00003 #include "ModuleData.h"
00004 #include "SctDaqRootFile.h"
00005 #include "Exceptions.h"
00006 #include "Globals.h"
00007 
00008 #include "Sct/Env.h"
00009 #include "Sct/IoExceptions.h"
00010 #include "Sct/OmniMarshalling.h"
00011 #include "Sct/SctNames.h"
00012 #include "Sct/ISProxy/IOManagerISProxy.h"
00013 #include "Sct_SctApi/SctApiServer.h"
00014 
00015 #include "SctData/ModuleConfiguration.h"
00016 #include "SctData/mVThresholdVariable.h"
00017 #include "SctData/mVfromTrimTargetThresholdVariable.h"
00018 #include "SctData/StrobeDelayVariable.h"
00019 #include "SctData/DefaultVariable.h"
00020 #include "SctData/NullVariable.h"
00021 #include "SctData/RawScanResult.h"
00022 #include "SctData/ScanPoints.h"
00023 
00024 #include "CalibrationController/IS/TestData.h"
00025 #include "Sct/AbcdScans.h"
00026 
00027 #include <TH1.h>
00028 #include <TH2.h>
00029 
00030 #include <sstream>
00031 #include <is/info.h>
00032 #include <is/infodictionary.h>
00033 #include <is/infoiterator.h>
00034 #include <is/inforeceiver.h>
00035 #include <is/infoT.h>
00036 
00037 using namespace SctData;
00038 using namespace Sct;
00039 using namespace Sct::IS;
00040 using namespace Sct::ISProxy;
00041 using namespace std;
00042 using namespace boost;
00043 using namespace SctConfiguration;
00044 
00045 namespace SctTest {
00046 
00047 HighLevelApi::HighLevelApi() : IPCNamedObject<POA_SctTestApi::HighLevelApi>(Sct::SctNames::getPartition(),SctTestApi::HighLevelApi::instanceName){
00048     try {
00049     handy_setDataFile("${SCT_DAQ_ROOT}/installed/config/TestConfig/ModuleData.txt");
00050     } catch (Sct::FileException& fe) {
00051         fe.sendToMrs(MRS_INFORMATION);
00052     throw SctTestApi::NoSuchFileException();
00053     }
00054 
00055     try {
00056     handy_setXmlSummaryFile("${SCT_DAQ_ROOT}/installed/config/TestConfig/XmlSummary.txt");
00057     } catch (Sct::FileException& fe) {
00058         fe.sendToMrs(MRS_INFORMATION);
00059     throw SctTestApi::NoSuchFileException();
00060     }
00061 
00062     try {
00063         XmlSummaryFile file(handy_getXmlSummaryFile());
00064         XmlSummaryRecord record = file.getNextRecord();
00065         handy_setModuleSerial(record.serialNumber);
00066     } catch (Exception& e) {
00067         e.sendToMrs(MRS_INFORMATION);
00068     throw SctTestApi::NoSuchFileException();
00069     }
00070 }
00071 
00072 HighLevelApi& HighLevelApi::instance() {
00073     static HighLevelApi api;
00074     return api;
00075 }
00076 
00077 void HighLevelApi::handy_setModuleSerial(const string & serial) {
00078     getConfig(serial);
00079     this->serial = serial;
00080     if (!data.dataExists(serial)) throw NoDataException("No data for module " + serial, __FILE__, __LINE__);
00081 }
00082 
00083 string HighLevelApi::handy_getModuleSerial() const {
00084     return serial;
00085 }
00086 
00087 void HighLevelApi::handy_setDataFile(const string & file) {
00088     data.load(file);
00089 }
00090 
00091 string HighLevelApi::handy_getDataFile() const {
00092     return data.getFileName();
00093 }
00094 
00095 void HighLevelApi::handy_setXmlSummaryFile(const string & file) {
00096     xmlFile = Env::substituteVariables(file);;
00097 }
00098 
00099 string HighLevelApi::handy_getXmlSummaryFile() const {
00100     return xmlFile;
00101 }
00102 
00103 
00104 void HighLevelApi::getConfig(string serial) {
00105     try {
00106         ModuleConfiguration* c = new ModuleConfiguration();
00107         if (!config) config = shared_ptr<ModuleConfiguration>(c);
00108         config->getABCDModule() = configServer->getModuleConfig(serial);
00109     } catch (ConfigurationException& e) {
00110         addAndGetConfig(serial);
00111     }
00112 }
00113 
00114 void HighLevelApi::addAndGetConfig(string serial) {
00115     XmlSummaryFile file(handy_getXmlSummaryFile());
00116     string xmlFile = "";
00117 
00118     while (xmlFile.length() == 0 && file.hasMoreRecords()) {
00119         XmlSummaryRecord xsr = file.getNextRecord();
00120         if (xsr.serialNumber == serial) xmlFile = xsr.xmlFile;
00121     }
00122 
00123     if (xmlFile.length() == 0) {
00124         throw NoDataException("No data for module " + serial, __FILE__, __LINE__);
00125     }
00126 
00127     try {
00128         configServer->configureModuleFromFile(xmlFile);
00129         config->getABCDModule() = configServer->getModuleConfig(serial);
00130     } catch (ConfigurationException& e) {
00131         throw NoDataException("Could not configure module " + serial, __FILE__, __LINE__);
00132     }
00133 }
00134 
00135 void HighLevelApi::doTest(const TestInfo& info, TestData& data, const SctData::ConfigurationVariable& var) const {
00136     if (!config) return;
00137     data.nScans = data.testPoints_size;
00138     data.runNumber = info.runNumber;
00139     data.startScanNumber = info.scanNumber;
00140     data.status = TestData::COMPLETED;
00141 
00142     publishTest(data);
00143     for (unsigned int i=0; i<data.nScans; ++i) {
00144         publishScan(info, data.startScanNumber + i, var, *config);        
00145     }
00146 }
00147 
00148 void HighLevelApi::publishTest(TestData& t) const {
00149     string name = SctNames::getControlDataName();
00150     name += ".";
00151 
00152     ostringstream s;
00153     s << "TestData." << t.runNumber << "." << t.startScanNumber;
00154 
00155     name += s.str();
00156     ISInfoDictionary& is = SctNames::getISDictionary();
00157     ISInfo::Status stat = is.contains(name);
00158     if (stat == ISInfo::Success) {
00159         stat = is.update(name, t);
00160     } else if (stat==ISInfo::NotFound) {
00161         stat = is.insert(name, t);
00162     }
00163     if (stat!=ISInfo::Success) throw IsException(stat, string("could not publish ")+name, __FILE__, __LINE__);
00164 }
00165 
00166 string HighLevelApi::getFileName(const TestInfo& info, unsigned int scanNumber) const{
00167     ostringstream filename;
00168     filename << info.path << "/strun" << info.runNumber << "_" << scanNumber << ".root";
00169     return filename.str();
00170 }
00171 
00172 void HighLevelApi::publishScan(const TestInfo& info, unsigned int scanNumber, const ConfigurationVariable& scan_variable, const SctData::ModuleConfiguration& config) const {
00173     cout << getFileName(info, scanNumber) << endl;
00174     SctDaqRootFile file(getFileName(info, scanNumber));
00175 
00176     int cycleNum = file.getCycleNum(serial);
00177     auto_ptr<TH1F> hist0Data = file.getHistData(cycleNum, 0);
00178     auto_ptr<TH1F> hist1Data = file.getHistData(cycleNum, 1);
00179     auto_ptr<TH1F> triggerData = file.getTriggerData();
00180 
00181     file.dealWithOccupancy(*hist0Data, *hist1Data, *triggerData, ConfigurationVariableIOHelper::getTypeRep(scan_variable), false);
00182 
00183     auto_ptr<TH2D> link0Data = file.fillData(*hist0Data);
00184     auto_ptr<TH2D> link1Data = file.fillData(*hist1Data);
00185 
00186     ResultHeader* s = new ResultHeader(scanNumber, info.runNumber, serial, scan_variable);
00187     ScanPoints* pts = new ScanPoints();
00188 
00189     for (int i=1; i<=triggerData->GetNbinsX(); ++i) {
00190         pts->addPoint(triggerData->GetBinCenter(i),triggerData->GetBinContent(i),0);
00191     }
00192 
00193     RawScanResult r(*s, config, *pts, *link0Data.release(), *link1Data.release());
00194 
00195     // Publish
00196     IOParamsIS params(SctNames::getEventDataName());
00197     IOManagerISProxy::instance().write(r, &params);    
00198 }
00199 
00200 
00201 //Corba  methods
00202 void HighLevelApi::setDataFile (const char * file) {
00203     try {
00204         handy_setDataFile(file);
00205     } catch (Sct::FileException& e) {
00206         throw SctTestApi::NoSuchFileException();
00207     }
00208 }
00209 
00210 char * HighLevelApi::getDataFile () {
00211     return copyStringToCorba(handy_getDataFile());
00212 }
00213 
00214 void HighLevelApi::setXmlSummaryFile (const char * file) {
00215     try {
00216         handy_setXmlSummaryFile(file);
00217     } catch (Sct::FileException& e) {
00218         throw SctTestApi::NoSuchFileException();
00219     }
00220 }
00221 
00222 char * HighLevelApi::getXmlSummaryFile () {
00223     return copyStringToCorba(handy_getXmlSummaryFile());
00224 }
00225 
00226 
00227 void HighLevelApi::setModuleSerial (const char * serial) {
00228     try {
00229         handy_setModuleSerial(serial);
00230     } catch(Sct::FileException& e) {
00231         throw SctTestApi::NoSuchModuleException();
00232     }
00233 }
00234 
00235 char * HighLevelApi::getModuleSerial () {
00236     return copyStringToCorba(handy_getModuleSerial());
00237 }
00238 
00239 void HighLevelApi::fullBypass() {
00240     TestData test;
00241     test.testName="FullBypassTest";
00242     test.testPoints_size = 6;
00243     test.testPoints = new double[test.testPoints_size];
00244     test.testVariable = 0;
00245     for (unsigned index=0; index<test.testPoints_size; ++index) {
00246         test.testPoints[index]=4.0-0.1*index;
00247     }
00248     try {
00249     doTest(data.getFullBypassInfo(handy_getModuleSerial()), test, *DefaultVariable::instance(ST_TOKEN));
00250     } catch (Exception& e) {
00251     e.sendToMrs(MRS_ERROR);
00252     std::cout << "LESTERDEBUGWARNING 4532 in " << __FILE__ << std::endl;
00253     throw SctTestApi::NoDataException();
00254     }
00255 }
00256 
00257 void HighLevelApi::timeWalk() {
00258     TestData test;
00259     test.testName="TimeWalkTest";
00260     test.testPoints_size = 10;
00261     test.testPoints = new double[test.testPoints_size];
00262     test.testVariable = ST_QCAL;
00263     test.testPoints[0]=1.25;
00264     test.testPoints[1]=1.5;
00265     test.testPoints[2]=1.75;
00266     test.testPoints[3]=2.0;
00267     test.testPoints[4]=3.0;
00268     test.testPoints[5]=4.0;
00269     test.testPoints[6]=5.0;
00270     test.testPoints[7]=6.0;
00271     test.testPoints[8]=8.0;
00272     test.testPoints[9]=10.0;
00273     try {
00274     doTest(data.getTimeWalkInfo(handy_getModuleSerial()), test, StrobeDelayVariable::instance());
00275     } catch (Exception& e) {
00276     e.sendToMrs(MRS_ERROR);
00277     throw SctTestApi::NoDataException();
00278     }
00279     
00280 }
00281 
00282 void HighLevelApi::strobeDelay() {
00283     TestData test;
00284     test.testName="StrobeDelayTest";
00285     test.testPoints_size = 1;
00286     test.testPoints = new double[test.testPoints_size];
00287     test.testVariable = 0;
00288     test.testPoints[0] = 0;
00289     try {
00290     doTest(data.getStrobeDelayInfo(handy_getModuleSerial()), test, StrobeDelayVariable::instance());
00291     } catch (Exception& e) {
00292     e.sendToMrs(MRS_ERROR);
00293     throw SctTestApi::NoDataException();
00294     }
00295 
00296 }
00297 
00298 void HighLevelApi::threePointGain() {
00299     TestData test;
00300     test.testName="NPtGainTest";
00301     test.testPoints_size = 3;
00302     test.testPoints = new double[test.testPoints_size];
00303     test.testVariable = ST_QCAL;
00304     test.testPoints[0] = 1.50;
00305     test.testPoints[1] = 2.00;
00306     test.testPoints[2] = 2.50;
00307     try {
00308     doTest(data.get3PtGainInfo(handy_getModuleSerial()), test, mVThresholdVariable::instance());
00309     } catch (Exception& e) {
00310     e.sendToMrs(MRS_ERROR);
00311     throw SctTestApi::NoDataException();
00312     }
00313 }
00314 
00315 void HighLevelApi::responseCurve() {
00316     TestData test;
00317     test.testName="NPtGainTest";
00318     test.testPoints_size = 10;
00319     test.testPoints = new double[test.testPoints_size];
00320     test.testVariable = ST_QCAL;
00321     test.testPoints[0]=0.50;
00322     test.testPoints[1]=0.75;
00323     test.testPoints[2]=1.00;
00324     test.testPoints[3]=1.25;
00325     test.testPoints[4]=1.50;
00326     test.testPoints[5]=2.00;
00327     test.testPoints[6]=3.00;
00328     test.testPoints[7]=4.00;
00329     test.testPoints[8]=6.00;
00330     test.testPoints[9]=8.00;
00331     try {
00332     doTest(data.getResponseCurveInfo(handy_getModuleSerial()), test, mVThresholdVariable::instance());
00333     } catch (Exception& e) {
00334     e.sendToMrs(MRS_ERROR);
00335     throw SctTestApi::NoDataException();
00336     }
00337 }
00338 
00339 void HighLevelApi::noiseOccupancy() {
00340     TestData test;
00341     test.testName="NoiseOccupancyTest";
00342     test.testPoints_size = 1;
00343     test.testPoints = new double[test.testPoints_size];
00344     test.testVariable = 0;
00345     test.testPoints[0] = 0;
00346     
00347     try {
00348     TestInfo info = data.getNoiseOccupancyInfo(getModuleSerial());
00349     //Check if it is relative to trim target or not
00350     SctDaqRootFile file(getFileName(info, info.scanNumber));
00351     int cycleNum = file.getCycleNum(serial);
00352     auto_ptr<TH1F> hist0Data = file.getHistData(cycleNum, 0);
00353     double firstBin = hist0Data->GetYaxis()->GetBinCenter(1);
00354     
00355     if (firstBin < 0) 
00356         doTest(info, test, mVfromTrimTargetThresholdVariable::instance());
00357     else 
00358         doTest(info, test, mVThresholdVariable::instance());
00359     } catch (Exception& e) {
00360     e.sendToMrs(MRS_ERROR);
00361     throw SctTestApi::NoDataException();
00362     }
00363 }
00364 
00365 void HighLevelApi::trim() {
00366     if (!config) return;
00367  
00368     try {
00369     TestInfo info = data.getTrimInfo(handy_getModuleSerial());
00370 
00371     TestData test;
00372     test.testName="TrimRangeTest";
00373     test.testPoints_size = 28;
00374     test.testPoints = new double[28];
00375     test.testVariable = ST_TRIM;
00376     test.nScans = test.testPoints_size;
00377     for(unsigned int i=0; i<test.testPoints_size; i++) {
00378         if (i<16) {
00379         test.testPoints[i]=i;
00380         } else {
00381         test.testPoints[i]=((i%4)+1)*4-1;
00382         }
00383     }
00384     
00385     //unsigned int index = 4;
00386     test.runNumber = info.runNumber;
00387     test.startScanNumber = info.scanNumber;
00388     test.status = TestData::COMPLETED;
00389     
00390     publishTest(test);
00391     ModuleConfiguration tempConfig(*config);
00392     
00393     for(unsigned int i=0; i<test.nScans; i++) {
00394         short range, point, value;
00395         if (i<16) {
00396         range=0;
00397         point=i;
00398         value=point;
00399         } else {
00400         range=(i-16)/4+1;
00401         point=(i-16)%4;
00402         value=(point+1)*4-1;
00403         }
00404         
00405         for (unsigned ichip=0; ichip<12; ++ichip) {
00406         tempConfig.getChipConfiguration(ichip).setTrimRange(range);
00407         for (unsigned ichannel=0; ichannel<128; ++ichannel) {
00408             tempConfig.getChipConfiguration(ichip).setTrim(ichannel,value);
00409         }
00410         }
00411         
00412         try {
00413         publishScan(info, test.startScanNumber + i, mVThresholdVariable::instance(), tempConfig);
00414         } catch (SctTestException& e) {
00415         e.sendToMrs(MRS_DIAGNOSTIC);
00416         throw SctTestApi::NoDataException();
00417         }
00418     }
00419     } catch (Exception& e) {
00420     e.sendToMrs(MRS_ERROR);
00421     throw SctTestApi::NoDataException();
00422     return;
00423     }
00424 }
00425 
00426 void HighLevelApi::pipeline() {
00427     TestData test;
00428     test.testName="PipelineTest";
00429     test.testPoints_size = 2;
00430     test.testPoints = new double[test.testPoints_size];
00431     test.testVariable = 0;
00432     test.testPoints[0] = 0;
00433     test.testPoints[1] = 0;
00434     try {
00435     doTest(data.getPipelineInfo(handy_getModuleSerial()), test, *DefaultVariable::instance(ST_MASK));
00436     } catch (Exception& e) {
00437     e.sendToMrs(MRS_ERROR);
00438     throw SctTestApi::NoDataException();
00439     }
00440 }
00441 
00442 }
00443 

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