namespace std {} using namespace std;
#include "Rtypes.h"
#include "TRegexp.h"
#include "TFile.h"
#include "TDatime.h"
#include "TROOT.h"
#include "TObjString.h"
#include "TMrbSystem.h"
#include "TMrbLogger.h"
#include "TMrbCPTM.h"
#include "SetColor.h"
extern TMrbLogger * gMrbLog;
const SMrbNamedX kMrbCptmMaskBits[] = {
{ BIT(0), "E2", "E2" },
{ BIT(1), "T2", "T2" },
{ BIT(2), "T1", "T1" },
{ BIT(3), "Q4", "Aux detector 4" },
{ BIT(4), "Q3", "Aux detector 3" },
{ BIT(5), "Q2", "Aux detector 2" },
{ BIT(6), "Q1", "Aux detector 1" },
{ BIT(7), "Ge", "Ge-Mult" },
{ 0, NULL, NULL }
};
ClassImp(TMrbCPTM)
ClassImp(TMrbCPTMEvent)
void TMrbCPTMEvent::Reset() {
fTimeStamp = 0;
fTimeAux = 0;
fTimeStampAdjusted = 0;
fCounterT1 = 0;
fCounterT2 = 0;
fPattern = 0;
}
void TMrbCPTMEvent::SetTimeStamp(Int_t LowWord, Int_t MiddleWord, Int_t HighWord) {
fTimeStamp = HighWord;
fTimeStamp <<= 32;
fTimeStamp |= (MiddleWord << 16) | LowWord;
}
Long64_t TMrbCPTMEvent::GetTimeStampAdjusted() {
Long64_t t = fTimeStamp;
if (fPattern & 0x0F000000) {
t &= 0xFFFFFFFF0000LL;
t |= fTimeAux & 0xFFFF;
} else {
t = fTimeStamp;
}
return(t);
}
const Char_t * TMrbCPTMEvent::Pattern2Ascii(TString & PatStr) {
PatStr.Resize(0);
UInt_t pat = fPattern;
Bool_t first;
for (Int_t i = 1; i <= 24; i++) {
if (pat & 1) {
if (!first) PatStr += " + G";
first = kFALSE;
PatStr += i;
}
pat >>= 1;
}
for (Int_t i = 1; i <= 4; i++) {
if (pat & 1) {
if (!first) PatStr += " + Q";
first = kFALSE;
PatStr += i;
}
pat >>= 1;
}
for (Int_t i = 1; i <= 2; i++) {
if (pat & 1) {
if (!first) PatStr += " + T";
first = kFALSE;
PatStr += i;
}
pat >>= 1;
}
return(PatStr.Data());
}
void TMrbCPTMEvent::Print(ostream & Out) {
TString pstr;
Out << Form("%15lld %10d %10d %10d %#10x", fTimeStamp, fTimeAux, fCounterT1, fCounterT2, fPattern);
if (fPattern) Out << " = " << this->Pattern2Ascii(pstr);
Out << endl;
}
TMrbCPTM::TMrbCPTM(const Char_t * ModuleName, const Char_t * HostName, Int_t Crate, Int_t Station) : TNamed(ModuleName, "") {
TMrbEsoneCnaf cnaf;
if (gMrbLog == NULL) gMrbLog = new TMrbLogger();
if (!cnaf.CheckCnaf(TMrbEsoneCnaf::kCnafCrate, Crate) || !cnaf.CheckCnaf(TMrbEsoneCnaf::kCnafStation, Station)) {
gMrbLog->Err() << "C_PTM in " << Crate << ".N" << Station << " - illegal camac address" << endl;
gMrbLog->Flush(this->ClassName());
this->MakeZombie();
} else {
TMrbString title = "C_PTM \"";
title += this->GetName();
title += "\" in C";
title += Crate;
title += ".N";
title += Station;
this->SetTitle(title.Data());
fCamacHost = HostName;
fCrate = Crate;
fStation = Station;
this->Camac()->ConnectToHost(HostName);
}
}
TMrbCPTM::TMrbCPTM(const Char_t * ModuleName, const Char_t * HostName, const Char_t * CamacAddr) : TNamed(ModuleName, "") {
TMrbEsoneCnaf cnaf;
TMrbString title;
if (gMrbLog == NULL) gMrbLog = new TMrbLogger();
if (!cnaf.Ascii2Int(CamacAddr)) {
gMrbLog->Err() << "DGF in " << CamacAddr << " - illegal camac address" << endl;
gMrbLog->Flush(this->ClassName());
this->MakeZombie();
} else {
TMrbString title = "DGF-4C \"";
title += this->GetName();
title += "\" in ";
title += cnaf.Int2Ascii();
this->SetTitle(title.Data());
fCamacHost = HostName;
fCrate = cnaf.GetC();
fStation = cnaf.GetN();
this->Camac()->ConnectToHost(HostName);
}
}
Bool_t TMrbCPTM::Connect() {
if (this->IsConnected()) {
gMrbLog->Err() << this->GetTitle() << " - already connected to host " << fCamacHost << endl;
gMrbLog->Flush(this->ClassName(), "Connect");
return(kTRUE);
}
if (fCamac.ConnectToHost(fCamacHost) == 0) {
gMrbLog->Err() << this->GetTitle() << " - can't connect to host " << fCamacHost << endl;
gMrbLog->Flush(this->ClassName(), "Connect");
return(kFALSE);
}
return(kFALSE);
}
Bool_t TMrbCPTM::SetCamacHost(const Char_t * HostName) {
if (this->IsConnected()) {
gMrbLog->Err() << this->GetTitle() << " - already connected to host " << fCamacHost << endl;
gMrbLog->Flush(this->ClassName(), "SetCamacHost");
return(kFALSE);
}
fCamacHost = HostName;
return(kTRUE);
}
Bool_t TMrbCPTM::SetCrate(Int_t Crate) {
TMrbEsoneCnaf cnaf;
if (this->IsConnected()) {
gMrbLog->Err() << this->GetTitle() << " - already connected to host " << fCamacHost << endl;
gMrbLog->Flush(this->ClassName(), "SetCrate");
return(kFALSE);
}
if (!cnaf.CheckCnaf(TMrbEsoneCnaf::kCnafCrate, Crate)) return(kFALSE);
fCrate = Crate;
return(kTRUE);
}
Bool_t TMrbCPTM::SetStation(Int_t Station) {
TMrbEsoneCnaf cnaf;
if (IsConnected()) {
gMrbLog->Err() << this->GetTitle() << " - already connected to host " << fCamacHost << endl;
gMrbLog->Flush(this->ClassName(), "SetStation");
return(kFALSE);
}
if (!cnaf.CheckCnaf(TMrbEsoneCnaf::kCnafStation, Station)) return(kFALSE);
fStation = Station;
return(kTRUE);
}
Bool_t TMrbCPTM::ResetRead() {
if (!this->CheckConnect("ResetRead")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(1), F(8), kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A1.F8 failed" << endl;
gMrbLog->Flush(this->ClassName(), "ResetRead");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::ResetWrite() {
if (!this->CheckConnect("ResetWrite")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(2), F(8), kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A2.F8 failed" << endl;
gMrbLog->Flush(this->ClassName(), "ResetWrite");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::ResetMemory() {
if (!this->CheckConnect("ResetMemory")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(6), F(8), kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A6.F8 failed" << endl;
gMrbLog->Flush(this->ClassName(), "ResetMemory");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::ResetLAM() {
if (!this->CheckConnect("ResetLAM")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(1), F(10), kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A1.F10 failed" << endl;
gMrbLog->Flush(this->ClassName(), "ResetLAM");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::ResetDacs() {
if (!this->CheckConnect("ResetDacs")) return(kFALSE);
TArrayI dac(4);
dac.Reset(0);
return(this->WriteAllDacs(dac));
}
Bool_t TMrbCPTM::Reset() {
if (!this->CheckConnect("Reset")) return(kFALSE);
this->SetGeDGG(0, 0);
this->SetAuxDGG(0, 0);
this->SetTimeWindowAux(0);
this->SetMask(0);
this->ResetDacs();
this->ResetRead();
this->ResetWrite();
this->ResetMemory();
this->ResetLAM();
Int_t data = 10;
fCamac.ExecCnaf(fCrate, fStation, A(7), F(16), data, kTRUE);
return(kTRUE);
}
Bool_t TMrbCPTM::SetGeDGG(Int_t Delay, Int_t Width) {
if (!this->CheckConnect("SetGeDGG")) return(kFALSE);
if (!this->CheckValue(Delay, 0xFF, "delay", "SetGeDGG")) return(kFALSE);
if (!this->CheckValue(Width, 0xFF, "width", "SetGeDGG")) return(kFALSE);
if (!this->SetGeDelay(Delay)) return(kFALSE);
if (!this->SetGeWidth(Width)) return(kFALSE);
return(kTRUE);
}
Bool_t TMrbCPTM::SetGeDelay(Int_t Delay) {
if (!this->CheckConnect("SetGeDelay")) return(kFALSE);
if (!this->CheckValue(Delay, 0xFF, "delay", "SetGeDelay")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(0), F(16), Delay, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A0.F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "SetGeDelay");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::SetGeWidth(Int_t Width) {
if (!this->CheckConnect("SetGeWidth")) return(kFALSE);
if (!this->CheckValue(Width, 0xFF, "width", "SetGeWidth")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(1), F(16), Width, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A1.F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "SetGeWidth");
return(kFALSE);
}
return(kTRUE);
}
Int_t TMrbCPTM::GetGeDelay() {
if (!this->CheckConnect("GetGeDelay")) return(-1);
Int_t delay;
if (!fCamac.ExecCnaf(fCrate, fStation, A(0), F(0), delay, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A0.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetGeDelay");
return(-1);
}
return(delay);
}
Int_t TMrbCPTM::GetGeWidth() {
if (!this->CheckConnect("GetGeWidth")) return(-1);
Int_t width;
if (!fCamac.ExecCnaf(fCrate, fStation, A(1), F(0), width, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A1.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetGeWidth");
return(-1);
}
return(width);
}
Bool_t TMrbCPTM::SetAuxDGG(Int_t Delay, Int_t Width) {
if (!this->CheckConnect("SetAuxDGG")) return(kFALSE);
if (!this->CheckValue(Delay, 0xFF, "delay", "SetAuxDGG")) return(kFALSE);
if (!this->CheckValue(Width, 0xFF, "width", "SetAuxDGG")) return(kFALSE);
if (!this->SetAuxDelay(Delay)) return(kFALSE);
if (!this->SetAuxWidth(Width)) return(kFALSE);
return(kTRUE);
}
Bool_t TMrbCPTM::SetAuxDelay(Int_t Delay) {
if (!this->CheckConnect("SetAuxDelay")) return(kFALSE);
if (!this->CheckValue(Delay, 0xFF, "delay", "SetAuxDelay")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(2), F(16), Delay, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A2.F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "SetAuxDelay");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::SetAuxWidth(Int_t Width) {
if (!this->CheckConnect("SetAuxWidth")) return(kFALSE);
if (!this->CheckValue(Width, 0xFF, "width", "SetAuxWidth")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(3), F(16), Width, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A3.F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "SetAuxWidth");
return(kFALSE);
}
return(kTRUE);
}
Int_t TMrbCPTM::GetAuxDelay() {
if (!this->CheckConnect("GetAuxDelay")) return(-1);
Int_t delay;
if (!fCamac.ExecCnaf(fCrate, fStation, A(2), F(0), delay, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A2.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetAuxDelay");
return(-1);
}
return(delay);
}
Int_t TMrbCPTM::GetAuxWidth() {
if (!this->CheckConnect("GetAuxWidth")) return(-1);
Int_t width;
if (!fCamac.ExecCnaf(fCrate, fStation, A(3), F(0), width, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A3.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetAuxWidth");
return(-1);
}
return(width);
}
Bool_t TMrbCPTM::SetMask(Int_t Mask) {
if (!this->CheckConnect("SetMask")) return(kFALSE);
if (!this->CheckValue(Mask, 0xFF, "mask", "SetMask")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(5), F(16), Mask, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A5.F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "SetMask");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::SetMask(const Char_t * Mask) {
if (!this->CheckConnect("SetMask")) return(kFALSE);
TMrbLofNamedX maskBits;
maskBits.AddNamedX(kMrbCptmMaskBits);
Int_t maskValue = 0;
TMrbString mstr = Mask;
TObjArray mbits;
Int_t nb = mstr.Split(mbits, ":");
for (Int_t i = 0; i < nb; i++) {
TString mb = ((TObjString *) mbits[i])->GetString();
TMrbNamedX * mbx = (TMrbNamedX *) maskBits.FindByName(mb.Data());
if (mbx == NULL) {
gMrbLog->Err() << "Illegal mask bit - " << mb << endl;
gMrbLog->Flush(this->ClassName(), "SetMask");
return(kFALSE);
}
maskValue |= mbx->GetIndex();
}
return(this->SetMask(maskValue));
}
Int_t TMrbCPTM::GetMask() {
if (!this->CheckConnect("GetMask")) return(-1);
Int_t mask;
if (!fCamac.ExecCnaf(fCrate, fStation, A(5), F(0), mask, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A5.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetMask");
return(-1);
}
return(mask);
}
Bool_t TMrbCPTM::SetTimeWindowAux(Int_t Window) {
if (!this->CheckConnect("SetTimeWindowAux")) return(kFALSE);
if (!this->CheckValue(Window, 0xFF, "window", "SetTimeWindowAux")) return(kFALSE);
if (!fCamac.ExecCnaf(fCrate, fStation, A(4), F(16), Window, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A4.F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "SetTimeWindowAux");
return(kFALSE);
}
return(kTRUE);
}
Int_t TMrbCPTM::GetTimeWindowAux() {
if (!this->CheckConnect("GetTimeWindowAux")) return(-1);
Int_t window;
if (!fCamac.ExecCnaf(fCrate, fStation, A(4), F(0), window, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A4.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetTimeWindowAux");
return(-1);
}
return(window);
}
Bool_t TMrbCPTM::EnableSynch(Bool_t Reset) {
if (!this->CheckConnect("EnableSynch")) return(kFALSE);
Int_t enaSynch = Reset ? A(5) : A(4);
this->ResetRead();
this->ResetWrite();
this->ResetMemory();
if (!fCamac.ExecCnaf(fCrate, fStation, enaSynch, F(8), kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A4/5.F8 failed" << endl;
gMrbLog->Flush(this->ClassName(), "EnableSynch");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::SetDac(Int_t DacNo, Int_t DacValue) {
if (!this->CheckConnect("SetDac")) return(kFALSE);
if (!this->CheckValue(DacNo, 3, "dacNo", "SetDac")) return(kFALSE);
if (!this->CheckValue(DacValue, 0xFFF, "dacVal", "SetDac")) return(kFALSE);
TArrayI dac(4);
if (!this->ReadAllDacs(dac)) return(kFALSE);
dac[DacNo] = DacValue;
return(this->WriteAllDacs(dac));
}
Int_t TMrbCPTM::GetDac(Int_t DacNo) {
if (!this->CheckConnect("GetDac")) return(-1);
if (!this->CheckValue(DacNo, 3, "dacNo", "GetDac")) return(-1);
TArrayI dac(4);
if (!this->ReadAllDacs(dac)) return(-1);
return(dac[DacNo]);
}
Bool_t TMrbCPTM::SetMultiplicity(Int_t Mult) {
if (!this->CheckConnect("SetMultiplicity")) return(kFALSE);
if (!this->CheckValue(Mult, kMrbCptmMaxMult, "multVal", "SetMultiplicity")) return(kFALSE);
Int_t dacVal = kMrbCptmMultOffset + Mult * kMrbCptmMilliVoltsPerMult - kMrbCptmMultThresh;
if (dacVal < kMrbCptmMultOffset) dacVal = kMrbCptmMultOffset;
TArrayI dac(4);
if (!this->ReadAllDacs(dac)) return(kFALSE);
dac[kMrbCptmDacNoMult] = dacVal;
return(this->WriteAllDacs(dac));
}
Int_t TMrbCPTM::GetMultiplicity() {
if (!this->CheckConnect("GetMultiplicity")) return(kFALSE);
TArrayI dac(4);
if (!this->ReadAllDacs(dac)) return(kFALSE);
Int_t dacVal = dac[kMrbCptmDacNoMult];
return((dacVal - kMrbCptmMultOffset + kMrbCptmMilliVoltsPerMult - 1)/kMrbCptmMilliVoltsPerMult);
}
Int_t TMrbCPTM::GetReadAddr() {
if (!this->CheckConnect("GetReadAddr")) return(-1);
Int_t addr;
if (!fCamac.ExecCnaf(fCrate, fStation, A(12), F(0), addr, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A14.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetReadAddr");
return(-1);
}
return(addr);
}
Int_t TMrbCPTM::GetWriteAddr() {
if (!this->CheckConnect("GetWriteAddr")) return(-1);
Int_t addr;
if (!fCamac.ExecCnaf(fCrate, fStation, A(13), F(0), addr, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A13.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetWriteAddr");
return(-1);
}
return(addr);
}
Bool_t TMrbCPTM::SaveSettings(const Char_t * SaveFile) {
if (!this->CheckConnect("SaveSettings")) return(kFALSE);
ofstream sf;
TString saveFile;
TString savePath;
TDatime dt;
saveFile = SaveFile;
if (saveFile.Index("/") != 0 && saveFile.Index("./", 0) != 0) {
savePath = gEnv->GetValue("TMrbCPTM.SettingsPath", "");
if (savePath.IsNull()) savePath = gEnv->GetValue("TMrbDGF.SettingsPath", ".");
saveFile = savePath + "/";
saveFile += SaveFile;
}
if (saveFile.Index(".par") > 0) {
sf.open(saveFile, ios::out);
if (!sf.good()) {
gMrbLog->Err() << gSystem->GetError() << " - " << saveFile << endl;
gMrbLog->Flush(this->ClassName(), "SaveSettings");
return(kFALSE);
}
sf << "#----------------------------------------------------------------------------------------" << endl;
sf << "# Name : " << SaveFile << endl;
sf << "# Contents : Settings for C_PTM " << this->GetName()
<< " in C" << this->GetCrate()
<< ".N" << this->GetStation() << endl;
sf << "# Creation date : " << dt.AsString() << endl;
sf << "# Created by : " << gSystem->Getenv("USER") << endl;
sf << "#" << endl;
sf << "# Data format : <name>:<value>:<hexval>[:<trueval>:<unit>]" << endl;
sf << "#----------------------------------------------------------------------------------------" << endl;
sf << "#+FileType:par" << endl;
sf << "#+Module:" << this->GetName() << endl;
sf << "#+Crate:" << this->GetCrate() << endl;
sf << "#+Station:" << this->GetStation() << endl;
sf << "#........................................................................................" << endl;
sf << endl;
Int_t val = this->GetGeDelay();
sf << "DelayGe:" << val << ":0x" << setbase(16) << val << setbase(10);
if (val) sf << ":" << (Double_t) (val * 25. / 1000.) << ":us"; else sf << "::";
sf << endl;
val = this->GetGeWidth();
sf << "WidthGe:" << val << ":0x" << setbase(16) << val << setbase(10);
if (val) sf << ":" << (Double_t) (val * 25. / 1000.) << ":us"; else sf << "::";
sf << endl;
val = this->GetAuxDelay();
sf << "DelayAux:" << val << ":0x" << setbase(16) << val << setbase(10);
if (val) sf << ":" << (Double_t) (val * 25. / 1000.) << ":us"; else sf << "::";
sf << endl;
val = this->GetAuxWidth();
sf << "WidthAux:" << val << ":0x" << setbase(16) << val << setbase(10);
if (val) sf << ":" << (Double_t) (val * 25. / 1000.) << ":us"; else sf << "::";
sf << endl;
val = this->GetTimeWindowAux();
sf << "TimeWindowAux:" << val << ":0x" << setbase(16) << val << setbase(10);
if (val) sf << ":" << (Double_t) (val * 25. / 1000.) << ":us"; else sf << "::";
sf << endl;
val = this->GetMask();
TString mStr;
sf << "Mask:" << val << ":0x" << setbase(16) << val << setbase(10) << ":" << this->ConvertMask(mStr, val, kTRUE) << ":" << endl;
val = this->GetDac(0);
sf << "Dac0:" << val << ":0x" << setbase(16) << val << setbase(10) << "::" << endl;
val = this->GetDac(1);
sf << "Dac1:" << val << ":0x" << setbase(16) << val << setbase(10) << "::" << endl;
val = this->GetDac(2);
sf << "Dac2:" << val << ":0x" << setbase(16) << val << setbase(10) << "::" << endl;
val = this->GetDac(3);
sf << "Dac3:" << val << ":0x" << setbase(16) << val << setbase(10) << "::" << endl;
sf.close();
} else {
gMrbLog->Err() << "Wrong file extension - " << SaveFile << " (should be .par)" << endl;
gMrbLog->Flush(this->ClassName(), "SaveSettings");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::RestoreSettings(const Char_t * RestoreFile) {
if (!this->CheckConnect("RestoreSettings")) return(kFALSE);
ifstream rf;
TString restFile;
if (gSystem->AccessPathName(RestoreFile, kFileExists) == 0) {
restFile = RestoreFile;
} else {
gMrbLog->Err() << "Settings file not found - " << RestoreFile << endl;
gMrbLog->Flush(this->ClassName(), "RestoreSettings");
return(kFALSE);
}
if (restFile.Index(".par") > 0) {
rf.open(restFile, ios::out);
if (!rf.good()) {
gMrbLog->Err() << gSystem->GetError() << " - " << restFile << endl;
gMrbLog->Flush(this->ClassName(), "RestoreSettings");
return(kFALSE);
}
Int_t lineCnt = 0;
TMrbString rLine;
for (;;) {
rLine.ReadLine(rf, kFALSE);
lineCnt++;
if (rf.eof()) break;
rLine = rLine.Strip(TString::kBoth);
if (rLine.Length() == 0) continue;
if (rLine(0) == '#') continue;
TObjArray rFields;
rFields.Delete();
Int_t nFields = rLine.Split(rFields, ":");
if (nFields < 3) {
gMrbLog->Err() << restFile << " (line " << lineCnt << "): Wrong number of fields - " << nFields << endl;
gMrbLog->Flush(this->ClassName(), "RestoreSettings");
continue;
}
TString rName = ((TObjString *) rFields[0])->GetString();
TMrbString rVal = (((TObjString *) rFields[1])->GetString()).Data();
Int_t val;
rVal.ToInteger(val);
if (rName.CompareTo("DelayGe") == 0) this->SetGeDelay(val);
else if (rName.CompareTo("WidthGe") == 0) this->SetGeWidth(val);
else if (rName.CompareTo("DelayAux") == 0) this->SetAuxDelay(val);
else if (rName.CompareTo("WidthAux") == 0) this->SetAuxWidth(val);
else if (rName.CompareTo("TimeWindowAux") == 0) this->SetTimeWindowAux(val);
else if (rName.CompareTo("Mask") == 0) this->SetMask(val);
else if (rName.CompareTo("Dac0") == 0) this->SetDac(0, val);
else if (rName.CompareTo("Dac1") == 0) this->SetDac(1, val);
else if (rName.CompareTo("Dac2") == 0) this->SetDac(2, val);
else if (rName.CompareTo("Dac3") == 0) this->SetDac(3, val);
else {
gMrbLog->Err() << restFile << " (line " << lineCnt << "): No such key word - " << rName << " (ignored)" << endl;
gMrbLog->Flush(this->ClassName(), "RestoreSettings");
continue;
}
}
rf.close();
} else {
gMrbLog->Err() << "Wrong file extension - " << restFile << " (should be .par)" << endl;
gMrbLog->Flush(this->ClassName(), "RestoreSettings");
return(kFALSE);
}
return(kTRUE);
}
Bool_t TMrbCPTM::DownloadAlteraCode(const Char_t * CodeFile) {
if (!this->CheckConnect("DownloadAlteraCode")) return(kFALSE);
ifstream cf;
TString codeFile;
Bool_t ok = kTRUE;
if (gSystem->AccessPathName(CodeFile, kFileExists) == 0) {
codeFile = CodeFile;
} else {
gMrbLog->Err() << "Codefile not found - " << CodeFile << endl;
gMrbLog->Flush(this->ClassName(), "DownloadAlteraCode");
return(kFALSE);
}
if (codeFile.Index(".rbf") > 0) {
cf.open(codeFile, ios::out);
if (!cf.good()) {
gMrbLog->Err() << gSystem->GetError() << " - " << codeFile << endl;
gMrbLog->Flush(this->ClassName(), "DownloadAlteraCode");
return(kFALSE);
}
this->SaveSettings(".cptm.par");
fCamac.ExecCnaf(fCrate, fStation, A(0), F(28), kTRUE);
fCamac.ExecCnaf(fCrate, fStation, A(0), F(30), kTRUE);
Char_t byte;
Int_t nofBytes = 0;
TArrayI codeData;
codeData.Set(kMrbCptmMaxCodeSize);
codeData.Reset(0);
for (;;) {
cf.get(byte);
if (cf.eof()) break;
codeData[nofBytes++] = byte;
}
cf.close();
if (fCamac.BlockXfer(fCrate, fStation, A(0), F(16), codeData, 0, nofBytes, kTRUE) == -1) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A0.F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "DownloadAlteraCode");
ok = kFALSE;
}
if (!fCamac.GetQ()) {
gMrbLog->Err() << "Q error afer F16 (write code): Q=" << (fCamac.GetQ() ? "1" : "0") << endl;
gMrbLog->Flush(this->ClassName(), "DownloadAlteraCode");
ok = kFALSE;
}
if (!fCamac.ExecCnaf(fCrate, fStation, A(0), F(29), kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A0.F29 failed" << endl;
gMrbLog->Flush(this->ClassName(), "DownloadAlteraCode");
ok = kFALSE;
}
if (ok) {
gMrbLog->Out() << fName << " in C" << fCrate << ".N" << fStation
<< ": Code successfully loaded from file " << CodeFile
<< " (" << nofBytes << " bytes)" << endl;
gMrbLog->Flush(this->ClassName(), "DownloadAlteraCode", setblue);
} else {
gMrbLog->Wrn() << fName << " in C" << fCrate << ".N" << fStation
<< ": Code loaded *WITH ERRORS* from file " << CodeFile
<< " (" << nofBytes << " bytes)" << endl;
gMrbLog->Flush(this->ClassName(), "DownloadAlteraCode");
}
this->Reset();
this->RestoreSettings(".cptm.par");
} else {
gMrbLog->Err() << "Wrong file extension - " << CodeFile << " (should be \".rbf\")" << endl;
gMrbLog->Flush(this->ClassName(), "DownloadAlteraCode");
}
return(ok);
}
Bool_t TMrbCPTM::PrintBuffer(ostream & Out) {
if (!this->CheckConnect("PrintBuffer")) return(kFALSE);
Int_t wc = this->CheckWordCount("PrintBuffer");
if (wc < 0) return(kFALSE);
Out << Form("%15s %10s %10s %10s %s", "TimeStamp", "TsAux", "T1", "T2", "Pattern");
Out << "---------------------------------------------------------------------------------------" << endl;
TMrbCPTMEvent e;
for (; wc; wc -= 10) {
if (!this->ReadEvent(e)) return(kFALSE);
e.Print();
}
return(kTRUE);
}
void TMrbCPTM::Print(ostream & Out) {
Out << endl << "C_PTM - Clock and Programmable Trigger Module:" << endl;
Out << "----------------------------------------------" << endl;
Out << "Name : " << this->GetName() << endl;
Out << "Camac address : C" << fCrate << ".N" << fStation << endl;
Out << "DGG (Ge) delay : " << (Double_t) (this->GetGeDelay() * 25.)/1000. << " us (" << this->GetGeDelay() << ")" << endl;
Out << " width : " << (Double_t) (this->GetGeWidth() * 25.)/1000. << " us (" << this->GetGeWidth() << ")" << endl;
Out << "DGG (Aux) delay : " << (Double_t) (this->GetAuxDelay() * 25.)/1000. << " us (" << this->GetAuxDelay() << ")" << endl;
Out << " width : " << (Double_t) (this->GetAuxWidth() * 25.)/1000. << " us (" << this->GetAuxWidth() << ")" << endl;
Out << "Time window (Aux) : " << (Double_t) (this->GetTimeWindowAux() * 25.)/1000. << " us (" << this->GetTimeWindowAux() << ")" << endl;
Out << "DAC settings dac0 : " << this->GetDac(0) << endl;
Out << " dac1 : " << this->GetDac(1) << endl;
Out << " dac2 : " << this->GetDac(2) << endl;
Out << " dac3 : " << this->GetDac(3) << endl;
Out << "Multiplicity (dac" << kMrbCptmDacNoMult << "): " << this->GetMultiplicity()
<< " (1 mult = " << kMrbCptmMilliVoltsPerMult << "mV)" << endl;
TString mStr;
Out << "Mask register : " << this->ConvertMask(mStr, this->GetMask()) << endl;
Out << "SRAM pointers (read) : " << this->GetReadAddr() << endl;
Out << " (write) : " << this->GetWriteAddr() << endl << endl;
}
const Char_t * TMrbCPTM::ConvertMask(TString & Mask, Int_t MaskValue, Bool_t TextOnly) {
TMrbString mstr;
if (!TextOnly) mstr.FromInteger(MaskValue, 0, 16);
TMrbLofNamedX maskBits;
maskBits.AddNamedX(kMrbCptmMaskBits);
Bool_t first = kTRUE;
TString eq = TextOnly ? "" : " = ";
for (Int_t i = 0; i <= 7; i++) {
if (MaskValue & 1) {
mstr += first ? eq.Data() : " + ";
first = kFALSE;
mstr += ((TObjString *) maskBits[i])->GetString();
}
MaskValue >>= 1;
}
Mask = mstr;
return(Mask.Data());
}
Bool_t TMrbCPTM::CheckValue(Int_t Value, Int_t MaxValue, const Char_t * ArgName, const Char_t * Method) {
if (Value < 0 || Value > MaxValue) {
gMrbLog->Err() << "Illegal input value: " << ArgName << "=" << Value << " - should be in [0, " << MaxValue << "]" << endl;
gMrbLog->Flush(this->ClassName(), Method);
return(kFALSE);
}
return(kTRUE);
}
Int_t TMrbCPTM::ReadNext() {
if (!this->CheckConnect("ReadNext")) return(-1);
#if 0
Int_t wc = this->GetWordCount();
if (wc == -1) return(-1);
if (wc == 0) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": wc = 0" << endl;
gMrbLog->Flush(this->ClassName(), "ReadNext");
return(-1);
}
#endif
Int_t data;
if (!fCamac.ExecCnaf(fCrate, fStation, A(6), F(0), data, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A6.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "ReadNext");
return(-1);
}
return(data);
}
Bool_t TMrbCPTM::ReadEvent(TMrbCPTMEvent & Event) {
if (!this->CheckConnect("ReadEvent")) return(kFALSE);
Event.Reset();
Int_t wc = this->CheckWordCount("ReadEvent");
if (wc <= 0) return(kFALSE);
TArrayI data(10);
if (fCamac.BlockXfer(fCrate, fStation, A(6), F(0), data, 0, 10, kTRUE) == -1) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A0.F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "ReadEvent");
return(kFALSE);
}
Event.SetTimeStamp(data[0], data[1], data[2]);
Event.SetTimeAux(data[3]);
Event.SetCounterT1(data[5] << 16 | data[4]);
Event.SetCounterT2(data[7] << 16 | data[6]);
Event.SetPattern(data[9] << 16 | data[8]);
return(kTRUE);
}
Int_t TMrbCPTM::GetWordCount() {
if (!this->CheckConnect("GetWordCount")) return(-1);
Int_t wc;
if (!fCamac.ExecCnaf(fCrate, fStation, A(13), F(0), wc, kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A13.F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "GetWordCount");
return(-1);
}
return(wc);
}
Int_t TMrbCPTM::CheckWordCount(const Char_t * Method) {
if (!this->CheckConnect("CheckWordCount")) return(kFALSE);
Int_t wc = this->GetWordCount();
if (wc % 10) {
gMrbLog->Wrn() << fName << " in C" << fCrate << ".N" << fStation
<< ": Out of phase (wc = " << wc << ")" << endl;
gMrbLog->Flush(this->ClassName(), "ReadEvent");
}
if (wc < 10) {
gMrbLog->Wrn() << fName << " in C" << fCrate << ".N" << fStation
<< ": Too few data (wc = " << wc << ")" << endl;
gMrbLog->Flush(this->ClassName(), "ReadEvent");
}
return(wc);
}
Bool_t TMrbCPTM::CheckConnect(const Char_t * Method) {
if (this->IsConnected()) return(kTRUE);
gMrbLog->Err() << this->GetTitle() << " - assigned to host " << fCamacHost << ", but NOT connected" << endl;
gMrbLog->Flush(this->ClassName(), Method);
return(kFALSE);
}
Bool_t TMrbCPTM::ReadAllDacs(TArrayI & Dac) {
if (!this->CheckConnect("ReadAllDacs")) return(kFALSE);
Int_t j = 0;
for (Int_t i = 8; i <= 11; i++, j++) {
if (!fCamac.ExecCnaf(fCrate, fStation, A(i), F(0), Dac[j], kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A" << i << ".F0 failed" << endl;
gMrbLog->Flush(this->ClassName(), "ReadAllDacs");
return(kFALSE);
}
}
return(kTRUE);
}
Bool_t TMrbCPTM::WriteAllDacs(TArrayI & Dac) {
if (!this->CheckConnect("WriteAllDacs")) return(kFALSE);
Long64_t dacBits = 0;
for (Int_t i = 3; i >= 0; i--) {
dacBits <<= 12;
dacBits |= Dac[i];
}
TArrayI dac8(6);
for (Int_t i = 0; i < 6; i++) {
dac8[i] = dacBits & 0xFF;
dacBits >>= 8;
}
Int_t j = 0;
for (Int_t i = 8; i <= 13; i++, j++) {
if (!fCamac.ExecCnaf(fCrate, fStation, A(i), F(16), dac8[j], kTRUE)) {
gMrbLog->Err() << fName << " in C" << fCrate << ".N" << fStation << ": A" << i << ".F16 failed" << endl;
gMrbLog->Flush(this->ClassName(), "ReadAllDacs");
return(kFALSE);
}
}
return(kTRUE);
}