00001
00002
00003
00004
00005
00006 #include "SctApiTest.hh"
00007 #include <is/isinfo.h>
00008 #include <iostream>
00009 #include <string>
00010 #include <sstream>
00011 #include <cstdlib>
00012 #include <ipc/object.h>
00013 #include <ipc/server.h>
00014
00015
00016 #include <TH1.h>
00017 #include <TH2.h>
00018 #include <TH3.h>
00019 #include <TKey.h>
00020 #include <TFile.h>
00021 #include <TSystem.h>
00022 #include <TRandom.h>
00023 #include <TROOT.h>
00024
00025
00026 #include "Sct/IS/IOManagerIS.h"
00027 #include "Sct/File/IOManagerFile.h"
00028 #include "Sct/ISProxy/IOManagerISProxy.h"
00029 #include "Sct/SctNames.h"
00030 #include "SctData/RawScanResult.h"
00031 #include "SctData/ModuleConfiguration.h"
00032 #include "SctData/ScanHeader.h"
00033 #include "SctData/ScanPoints.h"
00034 #include "SctData/mVThresholdVariable.h"
00035 #include "SctData/StrobeDelayVariable.h"
00036 #include "SctData/NullVariable.h"
00037 #include "SctData/DefaultVariable.h"
00038 #include "CalibrationController/IS/TestData.h"
00039
00040 #include "CommonWithDsp/ABCD/ABCDscans.h"
00041
00042
00043 #include "servernames.h"
00044
00045 enum scanType {STROBE_DELAY,THREE_POINT_GAIN};
00046
00047 using namespace SctData;
00048 using namespace Sct;
00049 using namespace Sct::IS;
00050 using namespace Sct::File;
00051 using namespace Sct::ISProxy;
00052
00053 string dataPath = "$SCT_DAQ_ROOT/installed/share/SctTestApiData/";
00054 string filePrefix = "strun";
00055
00056 class SCTAPI : public IPCObject, public virtual SctApiTest_T_sctapi {
00057 public:
00058 SCTAPI(const char* oid, IPCServer* server, IPCPartition* thepartition) : IPCObject(oid, server) {
00059 configname = "UNDEFINED";
00060 p = thepartition;
00061 }
00062 virtual ~SCTAPI() {}
00063 virtual char* ping(SctApiTestStatus* status, char* teststring) {
00064 char* c = new char[12];
00065 status->returnCode = SctApiTestReply_Success;
00066 strcpy(c, "Hello World");
00067 return c;
00068 }
00069
00070 void publishTest(TestData& t) {
00071 string name = SctNames::getControlDataName();
00072 name += ".";
00073
00074 ostringstream s;
00075 s << "TestData." << t.runNumber << "." << t.startScanNumber;
00076
00077 name += s.str();
00078 ISInfoDictionary& is = SctNames::getISDictionary();
00079 ISInfo::Status stat;
00080
00081 if (is.contains(name.c_str())) {
00082 stat = is.update(name.c_str(), t);
00083 } else {
00084 stat = is.insert(name.c_str(), t);
00085 }
00086 }
00087
00088
00089 virtual void initialise(SctApiTestStatus* status) {
00090 status->returnCode = SctApiTestReply_Success;
00091 }
00092 virtual long configure(SctApiTestStatus* status, char* newconfigname) {
00093 status->returnCode = SctApiTestReply_Success;
00094 configname=newconfigname;
00095 return 0;
00096 }
00097 virtual char* getConfigName(SctApiTestStatus* status) {
00098 status->returnCode = SctApiTestReply_Success;
00099 char *c = new char[configname.length()+1];
00100 strcpy(c,configname.c_str());
00101 return c;
00102 }
00103
00104 virtual void doStrobeDelayScan(SctApiTestStatus* status) {
00105 cout <<"DUMMY STROBE DELAY" <<endl;
00106
00107 double* testpoints = new double[1];
00108 testpoints[0]=0;
00109
00110 TestData test;
00111 test.testName="StrobeDelayTest";
00112 test.runNumber=551;
00113 test.startScanNumber=12;
00114 test.testVariable=0;
00115 test.nScans=1;
00116 test.status=test.COMPLETED;
00117 test.testPoints_size = 1;
00118 test.testPoints=testpoints;
00119
00120 try {
00121 publishTest(test);
00122 publishRawDataObjects(test, StrobeDelayVariable::instance());
00123 status->returnCode = SctApiTestReply_Success;
00124 } catch(Throwable& e) {
00125 e.sendToMrs(MRS_ERROR);
00126 cerr << e.what()<<endl;
00127 }
00128 }
00129
00130 virtual void doThreePointGainScan(SctApiTestStatus* status) {
00131 cout <<"DUMMY 3-PT GAIN" << endl;
00132
00133 double* testpoints = new double[3];
00134 testpoints[0] = 1.50;
00135 testpoints[1] = 2.00;
00136 testpoints[2] = 2.50;
00137
00138 TestData test;
00139 test.testName="NPtGainTest";
00140 test.runNumber=551;
00141 test.startScanNumber=13;
00142 test.testVariable=42;
00143 test.nScans=3;
00144 test.status=test.COMPLETED;
00145 test.testPoints_size = 3;
00146 test.testPoints=testpoints;
00147 publishTest(test);
00148
00149 status->returnCode = SctApiTestReply_Success;
00150 try {
00151 publishRawDataObjects(test, mVThresholdVariable::instance());
00152 status->returnCode = SctApiTestReply_Success;
00153 } catch (Throwable& e) {
00154 e.sendToMrs(MRS_ERROR);
00155 cerr << e.what() << endl;
00156 }
00157 }
00158 virtual void doResponseCurveScan(SctApiTestStatus* status) {
00159 cout <<"DUMMY RESPONSE CURVE" << endl;
00160 try {
00161 double* testpoints = new double[10];
00162 testpoints[0]=0.50;
00163 testpoints[1]=0.75;
00164 testpoints[2]=1.00;
00165 testpoints[3]=1.25;
00166 testpoints[4]=1.50;
00167 testpoints[5]=2.00;
00168 testpoints[6]=3.00;
00169 testpoints[7]=4.00;
00170 testpoints[8]=6.00;
00171 testpoints[9]=8.00;
00172
00173 TestData test;
00174 test.testName="NPtGainTest";
00175 test.runNumber=551;
00176 test.startScanNumber=44;
00177 test.testVariable=42;
00178 test.nScans=10;
00179 test.status=test.COMPLETED;
00180 test.testPoints_size = 10;
00181 test.testPoints=testpoints;
00182 publishTest(test);
00183
00184 publishRawDataObjects(test, mVThresholdVariable::instance());
00185 status->returnCode = SctApiTestReply_Success;
00186 } catch (Throwable& e) {
00187 e.sendToMrs(MRS_ERROR);
00188 cerr << e.what() << endl;
00189 }
00190 }
00191
00192 virtual void doTrimScan(SctApiTestStatus* status) {
00193 cout << "DUMMY TRIM SCAN"<<endl;
00194 try {
00195
00196 double* testpoints = new double[28];
00197 for(int i=0; i<28; i++) {
00198 if (i<16) {
00199 testpoints[i]=i;
00200 } else {
00201 testpoints[i]=((i%4)+1)*4-1;
00202 }
00203 }
00204 TestData test;
00205 test.testName="TrimRangeTest";
00206 test.runNumber=551;
00207 test.startScanNumber=16;
00208 test.testVariable=6;
00209 test.nScans=28;
00210 test.status=test.COMPLETED;
00211 test.testPoints_size = 28;
00212 test.testPoints=testpoints;
00213 publishTest(test);
00214
00215 for(int i=16; i<44; i++) {
00216 short pointintest=i-16;
00217 short range, point, value;
00218 if (pointintest<16) {
00219 range=0;
00220 point=pointintest;
00221 value=point;
00222 } else {
00223 range=(pointintest-16)/4+1;
00224 point=(pointintest-16)%4;
00225 value=(point+1)*4-1;
00226 }
00227 cout << "trim range: " << range << " value " << value << endl;
00228 ModuleConfiguration* m=new ModuleConfiguration();
00229 for (unsigned ichip=0; ichip<12; ++ichip) {
00230 m->getChipConfiguration(ichip).setTrimRange(range);
00231 for (unsigned ichannel=0; ichannel<128; ++ichannel) {
00232 m->getChipConfiguration(ichip).setTrim(ichannel,value);
00233 m->getChipConfiguration(ichip).setRcFunctionIndex(3);
00234 m->getChipConfiguration(ichip).setRcParam(0,1600);
00235 m->getChipConfiguration(ichip).setRcParam(1,8.0);
00236 m->getChipConfiguration(ichip).setRcParam(2,-720);
00237 }
00238 }
00239 sleep(1);
00240 publishRawDataObject(test.runNumber, i, mVThresholdVariable::instance(),m);
00241 }
00242
00243 status->returnCode = SctApiTestReply_Success;
00244 } catch (Throwable& e) {
00245 e.sendToMrs(MRS_ERROR);
00246 cerr << e.what() <<endl;
00247 }
00248 }
00249
00250 virtual void doNoiseScan(SctApiTestStatus* status) {
00251 cout <<"DUMMY NOISE SCAN"<<endl;
00252
00253 double* testpoints = new double[1];
00254 testpoints[0]=0;
00255
00256 TestData test;
00257 test.testName="NoiseOccupancyTest";
00258 test.runNumber=551;
00259 test.startScanNumber=54;
00260 test.testVariable=0;
00261 test.nScans=1;
00262 test.status=test.COMPLETED;
00263 test.testPoints_size = 1;
00264 test.testPoints=testpoints;
00265
00266 ModuleConfiguration* m=new ModuleConfiguration();
00267 double param0s[] = {1619.83, 1628.64, 1658.59, 1789.91, 1527.4, 1695.84, 1630.85, 1576.27, 1600.25, 1628.28, 1652.6, 1696.85};
00268 double param1s[] = {7.34, 7.55, 8, 8.45, 7.12, 7.93, 7.92, 7.88, 7.46, 7.36, 7.81, 7.96};
00269 double param2s[] = {-731.83, -735.28, -748.06, -814.46, -684.55, -768.05, -734.21, -704.84, -721.02, -736.24, -746.03, -768.25};
00270
00271 for (unsigned ichip=0; ichip<12; ++ichip) {
00272 m->getChipConfiguration(ichip).setRcFunctionIndex(3);
00273 m->getChipConfiguration(ichip).setRcParam(0, param0s[ichip]);
00274 m->getChipConfiguration(ichip).setRcParam(1, param1s[ichip]);
00275 m->getChipConfiguration(ichip).setRcParam(2, param2s[ichip]);
00276 }
00277
00278 try {
00279 publishTest(test);
00280 publishRawDataObjects(test, mVThresholdVariable::instance(),m);
00281 status->returnCode = SctApiTestReply_Success;
00282 } catch (Throwable& e) {
00283 e.sendToMrs(MRS_ERROR);
00284 cerr << e.what() <<endl;
00285 }
00286 }
00287
00288 virtual void doPipelineScan(SctApiTestStatus* status) {
00289 cout <<"DUMMY PIPELINE"<<endl;
00290
00291 double* testpoints = new double[2];
00292 testpoints[0]=0;
00293 testpoints[1]=0;
00294
00295 TestData test;
00296 test.testName="PipelineTest";
00297 test.runNumber=551;
00298 test.startScanNumber=9;
00299 test.testVariable=0;
00300 test.nScans=2;
00301 test.status=test.COMPLETED;
00302 test.testPoints_size = 2;
00303 test.testPoints=testpoints;
00304
00305 try {
00306 publishTest(test);
00307 ModuleConfiguration *m=new ModuleConfiguration();
00308 publishRawDataObjects(test, DefaultVariable(25),m);
00309
00310 status->returnCode = SctApiTestReply_Success;
00311 } catch (Throwable& e) {
00312 e.sendToMrs(MRS_ERROR);
00313 cerr << e.what() <<endl;
00314 }
00315 }
00316
00317 virtual void doTimeWalkScan(SctApiTestStatus* status) {
00318 cout <<"DUMMY TIMEWALK"<<endl;
00319
00320 const unsigned nscans=10;
00321 double* testpoints = new double[nscans];
00322 testpoints[0]=1.25;
00323 testpoints[1]=1.5;
00324 testpoints[2]=1.75;
00325 testpoints[3]=2.0;
00326 testpoints[4]=3.0;
00327 testpoints[5]=4.0;
00328 testpoints[6]=5.0;
00329 testpoints[7]=6.0;
00330 testpoints[8]=8.0;
00331 testpoints[9]=10.0;
00332
00333 TestData test;
00334 test.testName="TimeWalkTest";
00335 test.runNumber=551;
00336 test.startScanNumber=56;
00337 test.testVariable=0;
00338 test.nScans=nscans;
00339 test.status=test.COMPLETED;
00340 test.testPoints_size = nscans;
00341 test.testPoints=testpoints;
00342
00343 try {
00344 publishTest(test);
00345 ModuleConfiguration *m=new ModuleConfiguration();
00346 publishRawDataObjects(test,StrobeDelayVariable::instance(),m);
00347
00348 status->returnCode = SctApiTestReply_Success;
00349 } catch (Throwable& e) {
00350 e.sendToMrs(MRS_ERROR);
00351 cerr << e.what() <<endl;
00352 }
00353 }
00354
00355
00356 virtual void doFullBypassScan(SctApiTestStatus* status) {
00357 cout <<"DUMMY PIPELINE"<<endl;
00358
00359 const unsigned nscans=6;
00360
00361 double* testpoints = new double[nscans];
00362 for (unsigned index=0; index<nscans; ++index) {
00363 testpoints[index]=4.0-0.1*index;
00364 }
00365
00366 TestData test;
00367 test.testName="FullBypassTest";
00368 test.runNumber=551;
00369 test.startScanNumber=2;
00370 test.testVariable=0;
00371 test.nScans=nscans;
00372 test.status=test.COMPLETED;
00373 test.testPoints_size = nscans;
00374 test.testPoints=testpoints;
00375
00376 try {
00377 publishTest(test);
00378 ModuleConfiguration *m=new ModuleConfiguration();
00379 publishRawDataObjects(test, DefaultVariable(ST_TOKEN),m);
00380
00381 status->returnCode = SctApiTestReply_Success;
00382 } catch (Throwable& e) {
00383 e.sendToMrs(MRS_ERROR);
00384 cerr << e.what() <<endl;
00385 }
00386 }
00387
00388 virtual void publishRawDataObjects(TestData& testData, const ConfigurationVariable& scan_variable, ModuleConfiguration* moduleConfiguration=0) {
00389 for (unsigned int scanNumber = testData.startScanNumber; scanNumber < testData.startScanNumber+testData.nScans; ++scanNumber) {
00390 publishRawDataObject(testData.runNumber, scanNumber, scan_variable, moduleConfiguration);
00391 }
00392 }
00393
00394
00395 virtual void publishRawDataObject(unsigned int runNumber, unsigned int scanNumber, const ConfigurationVariable& scan_variable, ModuleConfiguration* moduleconfiguration=0) {
00396 TH1F* trighisto = 0;
00397 TH2F* scan0histo = 0;
00398 TH2F* scan1histo = 0;
00399 int histocount=0;
00400
00401 ostringstream filename;
00402 filename << dataPath << filePrefix << runNumber << "_" << scanNumber << ".root";
00403 string fname = filename.str();
00404
00405 TFile file(fname.c_str());
00406 if ( ! file.IsOpen( ) ) {
00407 cout << "Unable to open the file " << fname << "." << endl;
00408 return;
00409 }
00410
00411
00412 TKey *key;
00413 TIter it( file.GetListOfKeys() );
00414 cout << "Searching for histograms in the file..." << endl;
00415 while((key=(TKey*)it())) {
00416 string classname = key->GetClassName();
00417 string titlename = key->GetTitle();
00418 string keyname = key->GetName();
00419 short ncycle = key->GetCycle();
00420 char fullkeyname[80];
00421 sprintf(fullkeyname,"%s;%d",keyname.c_str(),ncycle);
00422 string tfullkeyname(fullkeyname);
00423
00424 if(classname.find("TH1F")!=string::npos && titlename.find("Triggers Sent")!=string::npos) {
00425 trighisto = (TH1F*)file.Get(fullkeyname);
00426 histocount++;
00427 cout << "Obtained pointer to trigger histo " << trighisto << endl;
00428 }
00429 if(tfullkeyname.find("h_scan0;1")!=string::npos) {
00430 scan0histo = (TH2F*)file.Get(fullkeyname);
00431
00432 histocount++;
00433 }
00434 if(tfullkeyname.find("h_scan1;1")!=string::npos) {
00435 scan1histo = (TH2F*)file.Get(fullkeyname);
00436
00437 histocount++;
00438 }
00439 }
00440 if(histocount!=3) {
00441 cout << "Not found all histos - object not published" << endl;
00442 return;
00443 }
00444
00445
00446
00447
00448 ScanHeader* s = new ScanHeader(scanNumber, runNumber, "SCTTestAPI_PseudoModule", scan_variable);
00449
00450 if (moduleconfiguration==0)
00451 moduleconfiguration = new ModuleConfiguration();
00452 ScanPoints* pts = new ScanPoints();
00453
00454
00455 bool bOccupancy = false;
00456 if (scan0histo->GetMaximum() <= 1)
00457 bOccupancy = true;
00458
00459
00460 for (int i=1; i<=trighisto->GetNbinsX(); ++i) {
00461
00463 if (bOccupancy || (scanNumber>1 && scanNumber<8))
00464 pts->addPoint(trighisto->GetBinCenter(i),trighisto->GetBinContent(i),0);
00465 else
00466 pts->addPoint(trighisto->GetBinCenter(i),trighisto->GetBinContent(i)/4,0);
00467 }
00468
00469
00470 gROOT->cd();
00471 TH2D* link0 = new TH2D("link0", "Link0 data", scan0histo->GetNbinsX(), 0, 100, scan0histo->GetNbinsY(), 0, 100);
00472 TH2D* link1 = new TH2D("link1", "Link1 data", scan1histo->GetNbinsX(), 0, 100, scan1histo->GetNbinsY(), 0, 100);
00473
00474 for (int i=0; i<scan0histo->GetNbinsX(); ++i) {
00475 for (int j=0; j<scan0histo->GetNbinsY(); ++j) {
00476 if (bOccupancy) {
00477 link0->SetBinContent(i+1, j+1, trighisto->GetBinContent(j+1)*scan0histo->GetBinContent(i,j+1));
00478 link1->SetBinContent(i+1, j+1, trighisto->GetBinContent(j+1)*scan1histo->GetBinContent(i,j+1));
00479 } else {
00480 link0->SetBinContent(i+1, j+1, scan0histo->GetBinContent(i,j+1));
00481 link1->SetBinContent(i+1, j+1, scan1histo->GetBinContent(i,j+1));
00482
00483 }
00484 }
00485 }
00486
00487 RawScanResult r(*s, *moduleconfiguration, *pts, *link0, *link1);
00488
00489 cout << "Created RawScanResult, now publishing: "<< endl << r.getUniqueID() << endl;
00490
00491
00492 IOParamsIS params(IS_Server_Name);
00493
00494 try {
00495
00496
00497 IOManagerISProxy::instance().write(r, ¶ms);
00498 } catch (IoException& e) {
00499 e.sendToMrs(MRS_ERROR);
00500 }
00501 cout <<"Finished publishing" << endl;
00502 }
00503
00504
00505 private:
00506 string configname;
00507 IPCPartition *p;
00508
00509 };
00510
00511
00512
00513
00514 int main(int argc, char** argv) {
00515 IPCCore::init(false);
00516 IPCPartition p(Partition_Name);
00517 IPCServer* server = new IPCServer(API_Server_Name, p);
00518
00519 SCTAPI* h = new SCTAPI(API_Object_Name, server, &p);
00520 if (!h->publish())
00521 cout << "Failed to publish " << API_Object_Name << endl;
00522 server->run();
00523 cout << "Run over" << endl;
00524
00525 h->withdraw();
00526 delete h;
00527 delete server;
00528 }