27 #include "THaGlobals.h"
42 #include "THaVarList.h"
60 : THaEvtTypeHandler(name,description),
62 fUseFirstEvent(
kTRUE), fDelayedType(-1),
63 fBCM_Gain(0), fBCM_Offset(0), fBCM_SatOffset(0), fBCM_SatQuadratic(0), fBCM_delta_charge(0),
64 evcount(0), evcountR(0.0), ifound(0),
65 fNormIdx(-1), fNormSlot(-1),
66 dvars(0), dvarsFirst(0),
67 fScalerTree(0), fOnlyBanks(
kFALSE),
68 fClockChan(-1), fLastClock(0)
197 std::map<std::string, Int_t> bcmindex;
198 bcmindex[
"BCM1_Hel.scal"] = 0;
199 bcmindex[
"BCM2_Hel.scal"] = 2;
200 bcmindex[
"Unser_Hel.scal"] = 6;
201 bcmindex[
"BCM4A_Hel.scal"] = 10;
202 bcmindex[
"BCM4B_Hel.scal"] = 4;
203 bcmindex[
"BCM4C_Hel.scal"] = 12;
285 {
"BCM_Names", &bcm_namelist, kString},
298 vector<string> bcm_names = Podd::vsplit(bcm_namelist);
300 fBCM_Name.push_back(bcm_names[i]+
"_Hel.scal");
345 if ( !IsMyEvent(evdata->
GetEvType()) )
return -1;
348 *fDebugFile << endl <<
"---------------------------------- "<<endl<<endl;
349 *fDebugFile <<
"\nEnter THcHelicityScaler for fName = "<<fName<<endl;
360 TString sname2 = sname1 + fName;
361 TString sname3 = fName +
" Scaler Data";
364 *fDebugFile <<
"\nAnalyze 1st time for fName = "<<fName<<endl;
365 *fDebugFile << sname2 <<
" " <<sname3<<endl;
383 name =
"actualHelicity";
388 name =
"quartetPhase";
392 for (
size_t i = 0; i <
scalerloc.size(); i++) {
407 memcpy(datacopy,rdata,evlen*
sizeof(
UInt_t));
412 if (fDebugFile) *fDebugFile<<
"\n\nTHcHelicityScaler :: Debugging event type "<<dec<<evdata->
GetEvType()<<
" event num = " << evdata->
GetEvNum() << endl<<endl;
416 if (fDebugFile) *fDebugFile <<
"scaler tree ptr 1 "<<
fScalerTree<<endl;
417 if (fDebugFile) *fDebugFile <<
"ret = "<< ret <<endl;
449 *fDebugFile <<
"Bank: " << hex << *p << dec <<
" len: " << *(p-1) << endl;
451 if((*p & 0xff00) == 0x1000) {
452 if(evlen-*(p-1) > 1) {
453 roc = (*p>>16) & 0xf;
454 if(fDebugFile) *fDebugFile <<
"ROC: " << roc <<
" " << evlen <<
" " << *(p-1) << hex <<
" " << *p << dec << endl;
461 }
else if (((*p & 0xff00) == 0x100) && (*p != 0xC0000100)) {
469 UInt_t tag = (*p>>16) & 0xffff;
471 UInt_t *pnext = p+banklen;
488 cout <<
"Error! Beam off for too long" << endl;
494 for (
UInt_t iev = 0; iev < nevents; iev++) {
506 *fDebugFile <<
"Scaler Header: " << hex << *p << dec;
525 *fDebugFile <<
"Finished with decoding. "<<endl;
526 *fDebugFile <<
" Found flag = "<<ifound<<endl;
529 if (!ifound)
return 0;
565 Int_t hbits = (p[0]>>30) & 0x3;
566 Bool_t isquartet = (hbits&2) != 0;
567 Int_t ispos = hbits&1;
568 Int_t actualhelicity = 0;
584 if(quartetphase == 0) {
590 cout <<
"THcHelicityScaler: Helicity Prediction Failed" << endl;
592 cout <<
"Predicted " << bitset<32>(predicted) << endl;
599 }
else if (quartetphase == 3) {
601 cout <<
"THcHelicityScaler: Quartet bit expected but not set (" <<
636 if(quartetphase == 3) {
641 if(quartetphase == 0 || quartetphase == 1) {
642 actualhelicity = -actualhelicity;
645 quartetphase = (quartetphase+1)%4;
648 if(quartetphase == 1 || quartetphase == 2) {
649 actualhelicity = -actualhelicity;
672 Int_t count = p[i]&0xFFFFFF;
680 if(actualhelicity!=0) {
683 Int_t hindex = (actualhelicity>0)?0:1;
691 Int_t count = p[i]&0xFFFFFF;
706 cout <<
" ******************* Severe Warning ****************************" << endl;
707 cout <<
" In THcHelicityScaler have found fDeltaTime is zero !! " << endl;
708 cout <<
" ******************* Alert DAQ experts ***************************" << endl;
709 if (fDebugFile) *fDebugFile <<
" In THcHelicityScaler have found fDeltaTime is zero !! " << endl;
719 for (
size_t i = 0; i <
scalerloc.size(); i++) {
761 if (match!=string::npos)
777 dvars[ivar]=cur_temp;
823 if (match!=string::npos)
856 cout <<
"THcHelicityScaler:: ERROR:: incorrect index "<<ivar<<
" "<<idx<<
" "<<ichan<<endl;
885 if (match!=string::npos)
898 dvars[ivar]=cur_temp;
920 cout <<
"THcHelicityScaler:: ERROR:: incorrect index "<<ivar<<
" "<<idx<<
" "<<ichan<<endl;
928 for (
size_t i = 0; i <
scalerloc.size(); i++) {
943 if (match!=string::npos)
965 if(actualhelicity!=0) {
968 if(quartetphase==0) {
1080 UInt_t bit7 = (ranseed & 0x00000040) != 0;
1081 UInt_t bit28 = (ranseed & 0x08000000) != 0;
1082 UInt_t bit29 = (ranseed & 0x10000000) != 0;
1083 UInt_t bit30 = (ranseed & 0x20000000) != 0;
1085 UInt_t newbit = (bit30 ^ bit29 ^ bit28 ^ bit7) & 0x1;
1087 ranseed = ( (ranseed<<1) | newbit ) & 0x3FFFFFFF;
1097 const int LEN = 200;
1108 cout <<
"Howdy ! We are initializing THcHelicityScaler !! name = "
1111 if(eventtypes.size()==0) {
1112 eventtypes.push_back(0);
1122 dfile = fName +
"scaler.txt";
1126 sname =
"hel"+fName+sname0;
1129 FILE *fi = Podd::OpenDBFile(sname.
Data(), date);
1131 cout <<
"Cannot find db file for "<<fName<<
" scaler event handler"<<endl;
1137 string scomment =
"#";
1138 string svariable =
"variable";
1139 string smap =
"map";
1140 vector<string> dbline;
1142 while( fgets(cbuf, LEN, fi) != NULL) {
1143 std::string
sin(cbuf);
1144 std::string sinput(sin.substr(0,sin.find_first_of(
"#")));
1145 if (fDebugFile) *fDebugFile <<
"string input "<<sinput<<endl;
1146 dbline = Podd::vsplit(sinput);
1147 if (dbline.size() > 0) {
1149 if (pos1 != minus1)
continue;
1152 if (pos1 != minus1 && dbline.size()>4) {
1154 for (
size_t j=5; j<dbline.size(); j++) sdesc = sdesc+
" "+dbline[j];
1155 UInt_t islot = atoi(dbline[1].c_str());
1156 UInt_t ichan = atoi(dbline[2].c_str());
1157 UInt_t ikind = atoi(dbline[3].c_str());
1159 *fDebugFile <<
"add var "<<dbline[1]<<
" desc = "<<sdesc<<
" islot= "<<islot<<
" "<<ichan<<
" "<<ikind<<endl;
1160 TString tsname(dbline[4].c_str());
1161 TString tsdesc(sdesc.c_str());
1162 AddVars(tsname,tsdesc,islot,ichan,ikind);
1165 tsname=tsname+
"Cut";
1171 if (fDebugFile) *fDebugFile <<
"map ? "<<dbline[0]<<
" "<<smap<<
" "<<pos1<<
" "<<dbline.size()<<endl;
1172 if (pos1 != minus1 && dbline.size()>6) {
1173 Int_t imodel, icrate, islot, inorm;
1176 sscanf(sinput.c_str(),
"%s %d %d %d %x %x %d \n",cdum,&imodel,&icrate,&islot, &header, &mask, &inorm);
1177 if ((
fNormSlot >= 0) && (
fNormSlot != inorm)) cout <<
"THcHelicityScaler::WARN: contradictory norm slot "<<
fNormSlot<<
" "<<inorm<<endl;
1181 if (dbline.size()>8) {
1182 clkchan = atoi(dbline[7].c_str());
1183 clkfreq = 1.0*atoi(dbline[8].c_str());
1188 *fDebugFile <<
"map line "<<dec<<imodel<<
" "<<icrate<<
" "<<islot<<endl;
1189 *fDebugFile <<
" header 0x"<<hex<<header<<
" 0x"<<mask<<dec<<
" "<<inorm<<
" "<<clkchan<<
" "<<clkfreq<<endl;
1204 scalers[idx]->SetHeader(header, mask);
1210 cout <<
"Setting scaler clock ... channel = "<<clkchan<<
" ... freq = "<<clkfreq<<endl;
1211 if (fDebugFile) *fDebugFile <<
"Setting scaler clock ... channel = "<<clkchan<<
" ... freq = "<<clkfreq<<endl;
1213 if (islot !=
fNormSlot) cout <<
"THcHelicityScaler:: WARN: contradictory norm slot ! "<<islot<<endl;
1227 if (fDebugFile) *fDebugFile <<
"fNormIdx = "<<
fNormIdx<<endl;
1242 cout <<
"THcHelicityScaler:: WARN: same slot defined twice"<<endl;
1253 if(fDebugFile) *fDebugFile <<
"THcHelicityScaler:: Name of scaler bank "<<fName<<endl;
1254 for (
size_t i=0; i<
scalers.size(); i++) {
1256 *fDebugFile <<
"Scaler # "<<i<<endl;
1257 scalers[i]->SetDebugFile(fDebugFile);
1258 scalers[i]->DebugPrint(fDebugFile);
1292 for(
Int_t i=0; i<4; i++) {
1367 "Plus Helcity Scalers",*(
fHScalers[0]));
1370 "Minus Helcity Scalers",*(
fHScalers[1]));
1375 gHcParms->Define(
Form(
"g%s_hscaler_triggers",fName.Data()),
1376 "Total Helicity Scaler Triggers",
fNTriggers);
1378 gHcParms->Define(
Form(
"g%s_hscaler_triggers_plus",fName.Data()),
1381 gHcParms->Define(
Form(
"g%s_hscaler_triggers_minus",fName.Data()),
1384 gHcParms->Define(
Form(
"g%s_hscaler_trigger_asy",fName.Data()),
1412 gHcParms->Define(
Form(
"g%s_hscaler_time",fName.Data()),
1413 "Helicity Gated Time (sec)",
fTimeSum);
1415 gHcParms->Define(
Form(
"g%s_hscaler_time_asy",fName.Data()),
1418 gHcParms->Define(
Form(
"g%s_hscaler_time_asyerr",fName.Data()),
1432 if (fDebugFile) *fDebugFile <<
"C.Y. | Calling THcHelicityScaler::AddVars() "<<endl;
1444 if (fDebugFile) *fDebugFile <<
"C.Y. | Calling THcHelicityScaler::DefVars() "<<endl;
1447 if (
Nvars == 0)
return;
1453 if(fDebugFile) *fDebugFile <<
"THcScalerEVtHandler:: Have gHaVars "<<gHaVars<<endl;
1455 cout <<
"No gHaVars ?! Well, that's a problem !!"<<endl;
1458 if(fDebugFile) *fDebugFile <<
"THcHelicityScaler:: scalerloc size "<<
scalerloc.size()<<endl;
1459 const Int_t* count = 0;
1460 for (
size_t i = 0; i <
scalerloc.size(); i++) {
1462 &
dvars[i], kDouble, count);
1469 string sdatalc, skeylc;
1470 sdatalc =
""; skeylc =
"";
1471 for (string::const_iterator p =
1472 sdata.begin(); p != sdata.end(); ++p) {
1473 sdatalc += tolower(*p);
1475 for (string::const_iterator p =
1476 skey.begin(); p != skey.end(); ++p) {
1477 skeylc += tolower(*p);
1479 if (sdatalc.find(skeylc,0) == string::npos)
return -1;
1480 return sdatalc.find(skeylc,0);
Double_t * fBCM_SatQuadratic
Double_t * fChargeAsymSum2
THcHelicityScaler(const char *, const char *)
Double_t fbcm_Current_Threshold
Int_t AnalyzeBuffer(UInt_t *rdata)
Double_t fTriggerAsymmetry
Double_t * fBCM_delta_charge
Event handler for Hall C helicity scalers.
Double_t * fChargeCycle[4]
Int_t fHelicityHistory[200]
std::vector< std::string > fBCM_Name
virtual Int_t End(THaRunBase *r=0)
virtual void SetAutoSave(Long64_t autos=-300000000)
Double_t fTimeAsymmetryError
std::vector< UInt_t * > fDelayedEvents
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
static const UInt_t defaultDT
const char * Data() const
static Bool_t Initialized()
Double_t * fBCM_SatOffset
static const UInt_t MAXCHAN
UInt_t GetEvLength() const
const UInt_t * GetRawDataBuffer() const
Double_t * fChargeAsymmetry
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
static const UInt_t ITIME
Int_t AnalyzeHelicityScaler(UInt_t *p)
void AddVars(TString name, TString desc, UInt_t iscal, UInt_t ichan, UInt_t ikind)
Double_t * fChargeAsymSum
char * Form(const char *fmt,...)
std::vector< Decoder::GenScaler * > scalers
static const UInt_t ICOUNT
Int_t Analyze(THaEvData *evdata)
static size_t FindNoCase(const std::string &sdata, const std::string &skey)
static const UInt_t IRATE
Double_t * fScalAsymmetry
Int_t LoadParmValues(const DBRequest *list, const char *prefix="")
Retrieve parameter values from the parameter cache.
virtual ~THcHelicityScaler()
virtual Int_t ReadDatabase(const TDatime &date)
std::set< UInt_t > fRocSet
Int_t fbcm_Current_Threshold_Index
ClassImp(THcDCLookupTTDConv) THcDCLookupTTDConv
std::vector< HCScalerLoc * > scalerloc
Double_t * fChargeAsymmetryError
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
virtual EStatus Init(const TDatime &run_time)
Short_t Max(Short_t a, Short_t b)
static const UInt_t ICURRENT
R__EXTERN class THcParmList * gHcParms
Int_t RanBit30(Int_t ranseed)
Double_t Sqrt(Double_t x)
std::set< UInt_t > fModuleSet
virtual void SetDelayedType(int evtype)
static const UInt_t ICHARGE
Double_t * fScalAsymmetryError