00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00019 #include <dirent.h>
00020 #include <vector>
00021 #include <string>
00022 #include <iostream>
00023 #include <cmath>
00024 #include <cstdlib>
00025
00026 #include "TFile.h"
00027 #include "TTree.h"
00028 #include "Mad/BeamMonMap.h"
00029
00030 BeamMonTV::BeamMonTV():
00031 bI(0.),
00032 hbw(0.),
00033 vbw(0.),
00034 hpos1(0.),
00035 vpos1(0.),
00036 hpos2(0.),
00037 vpos2(0.),
00038 htan(0.),
00039 vtan(0.),
00040 hornI(0.),
00041 nuTarZ(0.),
00042 timens(0.),
00043 closestspill(-99999.),
00044 time(0)
00045 {}
00046
00047 BeamMonTV::~BeamMonTV()
00048 {}
00049
00050 BeamMonTV BeamMonMap::FillBeamMonTV(struct bmon1 bmn)
00051 {
00052 BeamMonTV b;
00053
00054 b.bI=bmn.beamIntensity;
00055 b.hbw=bmn.hBeamWidth;
00056 b.vbw=bmn.vBeamWidth;
00057 b.hpos1=bmn.hPosatTargetPM;
00058 b.vpos1=bmn.vPosatTargetPM;
00059 b.hpos2=bmn.hPosatTargetBPM;
00060 b.vpos2=bmn.vPosatTargetBPM;
00061 b.htan=bmn.tanHoriz;
00062 b.vtan=bmn.tanVert;
00063 b.hornI=bmn.hornPeakCurrent;
00064 b.nuTarZ=bmn.nuTarZ;
00065 b.timens=bmn.timeStampD;
00066 b.time=bmn.timeStampSec;
00067
00068 return b;
00069 }
00070
00071
00072 BeamMonTV BeamMonMap::FillBeamMonTV(struct bmon2 bmn)
00073 {
00074 BeamMonTV b;
00075
00076 b.bI=bmn.beamIntensity;
00077 b.hbw=bmn.hBeamWidth;
00078 b.vbw=bmn.vBeamWidth;
00079 b.hpos1=bmn.hPosatTargetPM;
00080 b.vpos1=bmn.vPosatTargetPM;
00081 b.hpos2=bmn.hPosatTargetBPM;
00082 b.vpos2=bmn.vPosatTargetBPM;
00083 b.htan=bmn.tanHoriz;
00084 b.vtan=bmn.tanVert;
00085 b.hornI=bmn.hornPeakCurrent;
00086 b.nuTarZ=bmn.nuTarZ;
00087 b.timens=bmn.timeStampD;
00088 b.time=bmn.timeStampSec;
00089
00090 return b;
00091 }
00092
00093
00094 std::map<VldTimeStamp, BeamMonTV> BeamMonMap::MakeBeamMonMap(const char* path)
00095 {
00096
00097 std::map<VldTimeStamp, BeamMonTV> m;
00098 if(path==NULL){
00099 std::cout<<"Didn't specify a path for beam mon files, wont get em in your tree"<<std::endl;
00100 return m;
00101 }
00102 std::cout<<"opening dir "<<path<<std::endl;
00103
00104 DIR *dfd;
00105 dirent *dp;
00106 if(!(dfd = opendir(path))){
00107 std::cout<<" path "<<path<<" "<<dfd<<std::endl;
00108 return m;
00109 }
00110
00111 std::vector<std::string> names;
00112 while((dp=readdir(dfd))!=NULL){
00113 if(strstr(dp->d_name,"beamsummary_")!=NULL){
00114 names.push_back((std::string)(dp->d_name));
00115 }
00116 }
00117
00118 std::cout<<"Found "<<names.size()<<" names "<<std::endl;
00119 for(unsigned int i=0;i<names.size();i++){
00120
00121 std::string n=names[i].substr(names[i].find_last_of("_")+1,
00122 names[i].find_last_of(".")-names[i].find_last_of("_")-1);
00123
00124
00125
00126 std::string fname = path+names[i];
00127 TFile f(fname.c_str());
00128 TTree *b = (TTree *)(f.Get("btree"));
00129 if(atoi(n.c_str())<=7){
00130 struct bmon1 bs;
00131 b->SetBranchAddress("Beaminfo",&bs.beamIntensity);
00132
00133
00134 for(int z=0;z<b->GetEntries();z++){
00135 b->GetEntry(z);
00136 BeamMonTV bmtv = FillBeamMonTV(bs);
00137 VldTimeStamp vts(bs.timeStampSec,0);
00138 m[vts]=bmtv;
00139 }
00140 }
00141 else{
00142
00143 struct bmon2 bs;
00144 b->SetBranchAddress("Beaminfo",&bs.beamIntensity);
00145
00146 for(int z=0;z<b->GetEntries();z++){
00147 b->GetEntry(z);
00148 BeamMonTV bmtv = FillBeamMonTV(bs);
00149 VldTimeStamp vts(bs.timeStampSec,0);
00150 m[vts]=bmtv;
00151 }
00152 }
00153 f.Close();
00154 }
00155 return m;
00156 }
00157
00158 BeamMonTV BeamMonMap::FindClosestSpill(const std::map<VldTimeStamp, BeamMonTV>& m, VldTimeStamp vts)
00159 {
00160 if(m.size()==0){
00161 BeamMonTV b;
00162 return b;
00163 }
00164
00165 std::map<VldTimeStamp, BeamMonTV>::const_iterator lb = m.lower_bound(vts);
00166 if(lb==m.begin()){ return lb->second; }
00167 std::map<VldTimeStamp, BeamMonTV>::const_iterator prev=lb;
00168 prev--;
00169 if(lb==m.end()){ return prev->second; }
00170
00171 double early = fabs(vts-prev->first);
00172 double late = fabs(lb->first-vts);
00173
00174
00175
00176 if(early<late){
00177 BeamMonTV bmtv = prev->second;
00178 bmtv.closestspill=early;
00179 return bmtv;
00180 }
00181 BeamMonTV bmtv2 = lb->second;
00182 bmtv2.closestspill=late;
00183 return bmtv2;
00184 }
00185
00186 bool BeamMonMap::IsGoodSpill(BeamMonTV b)
00187 {
00188
00189 if(b.bI>0.1&&
00190 b.hbw<2&&b.vbw<2&&
00191 b.hpos2<-1.5&&b.hpos2>-2.0&&
00192 b.vpos2>.2&&b.hpos2<1.8&&b.closestspill<2){
00193 return true;
00194 }
00195 return false;
00196 }