00001 #include "BeamDataUtil/BMSpillAna.h"
00002 #include "Validity/VldTimeStamp.h"
00003
00004 #include <MessageService/Msg.h>
00005 #include <MessageService/MsgService.h>
00006 CVSID("$Id: BMSpillAna.cxx,v 1.18 2008/08/15 18:28:17 loiacono Exp $");
00007
00008 #include "Conventions/Munits.h"
00009
00010 #include "TMath.h"
00011 #include <cmath>
00012
00013 #include <cstdio>
00014
00015
00016 BMSpillAna::BMSpillAna()
00017 :fSpill(0),fUserCuts(),fUseDBCuts(true),fCutsSet(0),fResPtr(),
00018 fResID(-1),fTimeDiff(-99999)
00019 {
00020 this->ChangeCutValues(this->DefaultConfig());
00021 }
00022
00023
00024 BMSpillAna::~BMSpillAna()
00025 {}
00026
00027
00028 const Registry& BMSpillAna::DefaultConfig() const
00029 {
00030 static Registry r;
00031 if (r.Size() == 0) {
00032 r.UnLockValues();
00033 r.Set("TimeDiffMax", 1.0*Munits::s);
00034
00035 r.Set("PosTgtXMin",-2.0*Munits::mm);
00036 r.Set("PosTgtXMax",-0.01*Munits::mm);
00037
00038 r.Set("PosTgtYMin",0.01*Munits::mm);
00039 r.Set("PosTgtYMax",2.0*Munits::mm);
00040
00041 r.Set("WidXMin",0.1*Munits::mm);
00042 r.Set("WidXMax",1.5*Munits::mm);
00043
00044 r.Set("WidYMin",0.1*Munits::mm);
00045 r.Set("WidYMax",2.0*Munits::mm);
00046
00047 r.Set("UseSpotSizeCut",0);
00048
00049 r.Set("UseProfMonOut",1);
00050
00051 r.Set("TorIntMin",0.50);
00052 r.Set("TorIntMax",50.0);
00053
00054 r.Set("HornCurMin",-2.0e5*Munits::ampere);
00055 r.Set("HornCurMax",-1.55e5*Munits::ampere);
00056
00057 r.Set("TargetIn",1);
00058 r.Set("BeamType",-1);
00059
00060 r.Set("FracOnTargetMin",0.0);
00061 r.Set("FracOnTargetMax",1.0);
00062
00063 r.LockValues();
00064 }
00065 return r;
00066 }
00067
00068 void BMSpillAna::Config(const Registry& r)
00069 {
00070 fUserCuts=r;
00071 this->ChangeCutValues(r);
00072 }
00073
00074 void BMSpillAna::ChangeCutValues(const Registry& r)
00075 {
00076 if (r.Size()>0){
00077 r.Get("TimeDiffMax",fTimeDiffMax);
00078
00079 r.Get("PosTgtXMin",fPosTgtXMin);
00080 r.Get("PosTgtXMax",fPosTgtXMax);
00081
00082 r.Get("PosTgtYMin",fPosTgtYMin);
00083 r.Get("PosTgtYMax",fPosTgtYMax);
00084
00085 r.Get("WidXMin",fWidXMin);
00086 r.Get("WidXMax",fWidXMax);
00087
00088 r.Get("WidYMin",fWidYMin);
00089 r.Get("WidYMax",fWidYMax);
00090
00091 r.Get("UseSpotSizeCut",fUseSpotSizeCut);
00092
00093 r.Get("UseProfMonOut",fUseProfMonOut);
00094
00095 r.Get("TorIntMin",fTorIntMin);
00096 r.Get("TorIntMax",fTorIntMax);
00097
00098 r.Get("HornCurMin",fHornCurMin);
00099 r.Get("HornCurMax",fHornCurMax);
00100
00101 r.Get("TargetIn",fTargetIn);
00102 r.Get("BeamType",fBeamType);
00103
00104 r.Get("FracOnTargetMin",fFracOnTargetMin);
00105 r.Get("FracOnTargetMax",fFracOnTargetMax);
00106 }
00107 }
00108
00109 void BMSpillAna::ApplyUserCuts()
00110 {
00111 this->ChangeCutValues(fUserCuts);
00112 }
00113
00114
00115 void BMSpillAna::SetSpill(const BeamMonSpill& spill)
00116 {
00117 fSpill = &spill;
00118
00119
00120 this->SetTimeDiff(-99999);
00121
00122 }
00123
00124 void BMSpillAna::SetSpill(const NtpBDLiteRecord& ntpbdr, BeamMonSpill& spill)
00125 {
00126 fSpill = &spill;
00127
00128 this->SetTimeDiff(ntpbdr.GetHeader().GetTimeDiffStreamSpill());
00129
00130 spill.fDaeTime = ntpbdr.GetHeader().GetEarliestTimeStamp();
00131 spill.fVmeTime = ntpbdr.GetHeader().GetEarliestTimeStamp();
00132 spill.fTor101 = ntpbdr.tor101;
00133 spill.fTr101d = ntpbdr.tr101d;
00134 spill.fTortgt = ntpbdr.tortgt;
00135 spill.fTrtgtd = ntpbdr.trtgtd;
00136 spill.fHornCur = ntpbdr.horncur;
00137 for (int i=0;i<6;++i){
00138 spill.fTargBpmX[i] = ntpbdr.bposx[i];
00139 spill.fTargBpmY[i] = ntpbdr.bposy[i];
00140 spill.fBpmInt[i] = ntpbdr.bpmint[i];
00141 }
00142 spill.fProfWidX = ntpbdr.bwidx;
00143 spill.fProfWidY = ntpbdr.bwidy;
00144 spill.fHadInt = ntpbdr.hadint;
00145 spill.fMuInt1 = ntpbdr.muint1;
00146 spill.fMuInt2 = ntpbdr.muint2;
00147 spill.fMuInt3 = ntpbdr.muint3;
00148 union {int integer; BeamMonSpill::StatusBits bits; } status;
00149 status.integer = ntpbdr.GetHeader().GetStatus();
00150 spill.SetStatusBits(status.bits);
00151
00152 }
00153
00154 void BMSpillAna::SetSnarlTime(const VldTimeStamp& vs_snarl)
00155 {
00156 if (fSpill==0){
00157 MSG("BMSpillAna", Msg::kError)<< "Set the spill object before setting this time" << endl;
00158 return;
00159 }
00160 this->SetTimeDiff(vs_snarl-fSpill->SpillTime());
00161 }
00162
00163 Bool_t BMSpillAna::SelectSpill()
00164 {
00165 if (fTimeDiff==-99999)
00166 MSG("BMSpillAna", Msg::kWarning) <<
00167 "Time difference seems not to be set correctly" << endl;
00168
00169
00170
00171
00172
00173 if (fUseDBCuts){
00174 MAXMSG("BMSpillAna", Msg::kDebug,5) <<
00175 "Using database cuts" << endl;
00176
00177
00178 VldContext vc(Detector::kNear,SimFlag::kData,fSpill->SpillTime());
00179 Int_t nrows = fResPtr.NewQuery(vc,fCutsSet);
00180 if (nrows==0){
00181 MAXMSG("BMSpillAna",Msg::kWarning,20)
00182 << "No cuts found in database. This should not happen!"
00183 << endl;
00184 }
00185 else {
00186 if (nrows>1) {
00187 MAXMSG("BMSpillAna",Msg::kWarning,20)
00188 << "More than one row found for VldContext "
00189 << vc << endl;
00190 MAXMSG("BMSpillAna",Msg::kWarning,20)
00191 << " --> Will only use first row! " << endl;
00192 }
00193
00194 MAXMSG("BMSpillAna", Msg::kDebug,5) <<
00195 "Rows found in BEAMMONCUTS table" << endl;
00196
00197
00198
00199
00200
00201 Int_t newid = fResPtr.GetResultID();
00202 if (newid != fResID){
00203 MSG("BMSpillAna", Msg::kDebug) <<
00204 "Cuts need to be updated for spill at " << fSpill->SpillTime() << endl;
00205 fResID = newid;
00206 const BeamMonCuts* bmc = fResPtr.GetRow(0);
00207 Registry newreg;
00208 bmc->FillRegistry(&newreg);
00209 this->ChangeCutValues(newreg);
00210
00211 Int_t loglevel = MsgService::Instance()->GetStream("BMSpillAna")->GetLogLevel();
00212 if (loglevel == Msg::kDebug){
00213 cout << "Database cuts" << endl;
00214 this->Print();
00215 }
00216
00217 this->ApplyUserCuts();
00218 if (loglevel == Msg::kDebug){
00219 cout << "After user cuts" << endl;
00220 this->Print();
00221 }
00222 }
00223 }
00224 }
00225
00226 if (fabs(fTimeDiff)>fTimeDiffMax) return false;
00227
00228 Double_t xmean=0;
00229 Double_t ymean=0;
00230 Double_t xrms=0;
00231 Double_t yrms=0;
00232 fSpill->BpmAtTarget(xmean,ymean,xrms,yrms);
00233 if (xmean < fPosTgtXMin || xmean > fPosTgtXMax) return false;
00234 if (ymean < fPosTgtYMin || ymean > fPosTgtYMax) return false;
00235
00236
00237
00238 if (fSpill->fProfWidX < -0.1*Munits::mm
00239 || fSpill->fProfWidY < -0.1*Munits::mm) return false;
00240
00241 if (fUseSpotSizeCut){
00242
00243
00244
00245 Double_t spot_size=0;
00246 if (fSpill->fProfWidX>0 && fSpill->fProfWidY>0)
00247 spot_size = fSpill->fProfWidX*fSpill->fProfWidY;
00248
00249
00250
00251
00252 Double_t spot_size_min = -0.01*Munits::mm*Munits::mm;
00253 if (fWidXMin>0 && fWidYMin>0)
00254 spot_size_min = fWidXMin*fWidYMin;
00255
00256
00257
00258
00259 Double_t spot_size_max = fWidXMax*fWidYMax;
00260
00261 if (!(fUseProfMonOut) && spot_size < spot_size_min ) return false;
00262 else if (spot_size > spot_size_max) return false;
00263
00264 }
00265 else {
00266 if (!(fUseProfMonOut) && fSpill->fProfWidX < fWidXMin) return false;
00267 else if (fSpill->fProfWidX > fWidXMax) return false;
00268
00269 if (!(fUseProfMonOut) && fSpill->fProfWidY < fWidYMin) return false;
00270 else if (fSpill->fProfWidY > fWidYMax) return false;
00271 }
00272
00273
00274
00275 if (fSpill->fTrtgtd < fTorIntMin || fSpill->fTrtgtd > fTorIntMax){
00276 return false;
00277 }
00278
00279
00280
00281
00282 if (fSpill->fHornCur*1e3 < fHornCurMin || fSpill->fHornCur*1e3 > fHornCurMax){
00283 return false;
00284 }
00285
00286
00287 if ( fTargetIn < 0 || (fTargetIn >= 0 && (Bool_t)fSpill->GetStatusBits().target_in != (Bool_t)fTargetIn) ){
00288 return false;
00289 }
00290
00291 if (fBeamType >=0 && (fSpill->BeamType() != fBeamType)){
00292 return false;
00293 }
00294
00295 if (fFracOnTargetMin > 0 && this->FractionOnTarget() < fFracOnTargetMin) {
00296 return false;
00297 }
00298 if (fFracOnTargetMax < 1 && this->FractionOnTarget() > fFracOnTargetMax) {
00299 return false;
00300 }
00301
00302 return true;
00303 }
00304
00305
00306 Double_t BMSpillAna::FractionOnTarget()
00307 {
00308 Double_t frac = -1;
00309
00310 Double_t bposx = 0;
00311 Double_t bwidx = 0;
00312
00313 Double_t bposy = 0;
00314 Double_t bwidy = 0;
00315
00316
00317 fSpill->BpmAtTarget(bposx,bposy,bwidx,bwidy);
00318
00319
00320 bwidx = fSpill->fProfWidX;
00321 bwidy = fSpill->fProfWidY;
00322
00323
00324 if (bwidx<=0 || bwidy<=0) return frac;
00325
00326
00327 if (bwidx>5*Munits::mm || bwidy>05*Munits::mm) return frac;
00328
00329
00330 Double_t tedgx = 3.2*Munits::mm;
00331 Double_t toffx = -1.2*Munits::mm;
00332 Double_t tedgy = 5.5*Munits::mm;
00333 Double_t toffy = 0.9*Munits::mm;
00334
00335
00336 frac = CalcFracOnTarget(bposx,bwidx,tedgx,toffx);
00337 frac *= CalcFracOnTarget(bposy,bwidy,tedgy,toffy);
00338
00339 return frac;
00340 }
00341
00342 Double_t BMSpillAna::CalcFracOnTarget(Double_t bpos, Double_t bwid, Double_t tedg, Double_t toff)
00343 {
00344 Double_t powl = tedg+bpos-toff;
00345 powl /= bwid;
00346 powl /= TMath::Sqrt(2.0);
00347 Double_t powu = tedg-bpos+toff;
00348 powu /= bwid;
00349 powu /= TMath::Sqrt(2.0);
00350 Double_t frac = TMath::Erf(powl)/2 + TMath::Erf(powu)/2;
00351 return frac;
00352 }
00353
00354 void BMSpillAna::Print()
00355 {
00356 cout << endl << "Beam Monitoring Cut Values:" << endl;
00357 cout << "===========================" << endl;
00358
00359 printf(" > Maximum time diffrence (s): %5.3f\n",fTimeDiffMax);
00360 printf(" > Spill intensity (1e12 pot): [%5.2f,%5.2f]\n",
00361 fTorIntMin, fTorIntMax);
00362 printf(" > Horn Current (kA): [%+5.1f,%+5.1f]\n",
00363 fHornCurMin/Munits::ampere/1e3, fHornCurMax/Munits::ampere/1e3);
00364 printf(" > Target in/out: %3d\n",fTargetIn);
00365 printf(" > fBeamType: %3d\n",fBeamType);
00366 printf(" > Horizontal beam position (mm): [%+5.3f,%+5.3f]\n",
00367 fPosTgtXMin/Munits::mm, fPosTgtXMax/Munits::mm);
00368 printf(" > Vertical beam position (mm): [%+5.3f,%+5.3f]\n",
00369 fPosTgtYMin/Munits::mm, fPosTgtYMax/Munits::mm);
00370 printf(" > Horizontal beam width a (mm): [%+5.3f,%+5.3f]\n",
00371 fWidXMin/Munits::mm, fWidXMax/Munits::mm);
00372 printf(" > Vertical beam width a (mm): [%+5.3f,%+5.3f]\n",
00373 fWidYMin/Munits::mm, fWidYMax/Munits::mm);
00374 printf(" > Use spill when prof mon out: %3d\n",fUseProfMonOut);
00375 printf(" > Beam fraction on target: [%5.3f,%5.3f]\n",
00376 fFracOnTargetMin, fFracOnTargetMax);
00377
00378 }
00379