00001 #include "TTree.h"
00002 #include "TFile.h"
00003 #include "TDirectory.h"
00004 #include "TROOT.h"
00005 #include "NueAna/Module/NueBeamMonModule.h"
00006 #include "NueAna/NueRecord.h"
00007 #include "NueAna/NuePOT.h"
00008
00009 #include "MessageService/MsgService.h"
00010 #include "MinosObjectMap/MomNavigator.h"
00011 #include "JobControl/JobCModuleRegistry.h"
00012 #include "Validity/VldContext.h"
00013 #include "Conventions/Detector.h"
00014
00015 #include "BeamDataUtil/BDSpillAccessor.h"
00016 #include "BeamDataUtil/BeamMonSpill.h"
00017 #include "BeamDataUtil/BMSpillAna.h"
00018 #include "SpillTiming/SpillTimeFinder.h"
00019
00020 #include "DataUtil/MCInfo.h"
00021 #include "DcsUser/CoilTools.h"
00022 #include "DataUtil/GetTempTags.h"
00023
00024 #include "NueAna/NueStandard.h"
00025 #include "TSystem.h"
00026
00027 #include <fstream>
00028 #include <iomanip>
00029 #include <vector>
00030
00031 JOBMODULE(NueBeamMonModule, "NueBeamMon",
00032 "Fill the beam monitoring data in the NueAna ntuples");
00033 CVSID("$Id: NueBeamMonModule.cxx,v 1.16 2008/11/19 18:22:51 rhatcher Exp $");
00034
00035
00036 NueBeamMonModule::NueBeamMonModule()
00037 :fBMSpillAna(),firstevent(true), zbeamtype(0), lastrun(-1)
00038 {
00039 beamtype = BeamType::kUnknown;
00040 nuepot = new NuePOT();
00041 fBMSpillAna.UseDatabaseCuts();
00042 }
00043
00044 NueBeamMonModule::~NueBeamMonModule()
00045 {
00046 if(nuepot){
00047 delete nuepot;
00048 nuepot=0;
00049 }
00050 }
00051
00052 const Registry& NueBeamMonModule::DefaultConfig() const
00053 {
00054 static Registry r;
00055 r.UnLockValues();
00056 r.Set("ZarkoBeamType",0);
00057 r.Set("POTTreeFileName","pottree.root");
00058 r.Set("BeamString", "");
00059 r.LockValues();
00060
00061 return r;
00062 }
00063
00064 void NueBeamMonModule::Config(const Registry& r)
00065 {
00066 fBMSpillAna.Config(r);
00067 const char* tmps;
00068 if(r.Get("POTTreeFileName",tmps)){kPOTTreeName=tmps;}
00069
00070
00071
00072 }
00073
00074 JobCResult NueBeamMonModule::Reco(MomNavigator* mom)
00075 {
00076
00077
00078
00079 BDSpillAccessor& sa = BDSpillAccessor::Get();
00080
00081
00082 SpillTimeFinder &stf = SpillTimeFinder::Instance();
00083
00084
00085
00086 TObject *obj=0;
00087 vector<NueRecord *> records;
00088
00089 int nnuerec=0;
00090 bool contains_mc=false;
00091 TIter objiter = mom->FragmentIter();
00092 while(( obj=objiter.Next() )){
00093 NueRecord *nr = dynamic_cast<NueRecord *>(obj);
00094
00095 if (!nr){
00096 MSG("NueBeamMon",Msg::kDebug)<<"Didn't find a NueRecord in MOM"<<endl;
00097 continue;
00098 }
00099 MSG("NueBeamMon",Msg::kDebug)<<"Found a NueRecord in MOM"
00100 <<" Snarl "<<nr->GetHeader().GetSnarl()
00101 <<" Event "<<nr->GetHeader().GetEventNo()<<endl;
00102
00103 VldContext evt_vldc = nr->GetHeader().GetVldContext();
00104 ReleaseType::Release_t rel = nr->GetHeader().GetRelease();
00105 beamtype = nr->GetHeader().GetBeamType();
00106
00107 if(firstevent){
00108 firstevent=false;
00109 nuepot->beamtype=beamtype;
00110 zbeamtype = BeamType::ToZarko(beamtype);
00111 }
00112
00113 if(nr->GetHeader().GetRun()!=lastrun){
00114 nuepot->nruns++;
00115 lastrun = nr->GetHeader().GetRun();
00116 if(evt_vldc.GetSimFlag() != SimFlag::kData &&
00117 evt_vldc.GetDetector()==Detector::kFar){
00118 double temp = MCInfo::GetMCPoT(Detector::kFar, beamtype, rel);
00119 nuepot->pot += temp;
00120 nuepot->pot_nocut += temp;
00121 }
00122 }
00123 if(nnuerec==0){
00124 nuepot->nsnarls++;
00125 }
00126
00127
00128 if (evt_vldc.GetSimFlag() != SimFlag::kData){
00129 contains_mc=true;
00130 if(nnuerec==0&&evt_vldc.GetDetector()==Detector::kNear){
00131 nuepot->pot+=MCInfo::GetMCPoT(Detector::kNear, beamtype, rel);
00132 }
00133 nnuerec++;
00134 continue;
00135 }
00136
00137
00138 const BeamMonSpill* bms = sa.LoadSpill(evt_vldc.GetTimeStamp());
00139 VldTimeStamp bms_vts;
00140 if (!bms) {
00141 MSG("NueBeamMon",Msg::kError) << "No BeamMonSpill found for " << evt_vldc << endl;
00142 bms_vts = VldTimeStamp::GetEOT();
00143 }
00144 else {
00145 bms_vts = bms->SpillTime();
00146 }
00147
00148 fBMSpillAna.SetSpill(*bms);
00149
00150
00151 fBMSpillAna.SetSnarlTime(evt_vldc.GetTimeStamp());
00152
00153
00154 nr->bmon.Reset();
00155
00156 if (fBMSpillAna.SelectSpill())
00157 nr->bmon.goodBeamMon=1;
00158 else
00159 nr->bmon.goodBeamMon=0;
00160
00161 nr->bmon.tortgt = bms->fTortgt;
00162 nr->bmon.trtgtd = bms->fTrtgtd;
00163 nr->bmon.tor101 = bms->fTor101;
00164 nr->bmon.tr101d = bms->fTr101d;
00165
00166
00167 nr->bmon.bI = nr->bmon.GetPot();
00168
00169 double xbpm,ybpm,xrms,yrms;
00170 bms->BpmAtTarget(xbpm,ybpm,xrms,yrms);
00171 nr->bmon.hpos2 = xbpm;
00172 nr->bmon.vpos2 = ybpm;
00173
00174 for (Int_t i=0;i<6;++i){
00175 nr->bmon.batchposx[i]=bms->fTargBpmX[i];
00176 nr->bmon.batchposy[i]=bms->fTargBpmY[i];
00177 nr->bmon.batchint[i]=bms->fBpmInt[i];
00178 }
00179
00180 nr->bmon.hpos1 = bms->fTargProfX;
00181 nr->bmon.vpos1 = bms->fTargProfY;
00182
00183 nr->bmon.hbw = bms->fProfWidX;
00184 nr->bmon.vbw = bms->fProfWidY;
00185
00186 nr->bmon.hornI = bms->fHornCur;
00187
00188 nr->bmon.bmst_vts = bms->SpillTime();
00189
00190 VldTimeStamp vtsdif = nr->bmon.bmst_vts-evt_vldc.GetTimeStamp();
00191 nr->bmon.dt_bmst=vtsdif.GetSeconds();
00192
00193 nr->bmon.stnd_vts=stf.GetTimeOfNearestSpill(evt_vldc);
00194 nr->bmon.dt_stnd=stf.GetTimeToNearestSpill(evt_vldc);
00195
00196
00197
00198 bool goodCoil = CoilTools::IsOK(evt_vldc) && !CoilTools::IsReverse(evt_vldc);
00199 goodCoil = goodCoil || (evt_vldc.GetDetector()==Detector::kFar);
00200
00201 nr->bmon.goodDataQual = (int) NueStandard::PassesPOTStandards(nr);
00202
00203
00204 if (nnuerec==0 && nr->bmon.goodDataQual == 1){
00205
00206 nuepot->pot+=nr->bmon.bI;
00207 }
00208 if(nnuerec==0) nuepot->pot_nocut +=nr->bmon.bI;
00209 ++nnuerec;
00210 }
00211 if (!contains_mc && nnuerec==0)
00212 MSG("NueBeamMon",Msg::kWarning)<<"No NueRecord found in MOM"<<endl;
00213
00214 return JobCResult::kPassed;
00215 }
00216
00217 void NueBeamMonModule::EndJob(){
00218 MSG("NueBeamMon",Msg::kInfo)<<"Number of POT in this job: "<<nuepot->pot<<endl;
00219
00220 TDirectory *savedir = gDirectory;
00221
00222 TFile *fpf = dynamic_cast<TFile *>(gROOT->GetListOfFiles()->FindObject(kPOTTreeName.c_str()));
00223 if(fpf){
00224 fpf->cd();
00225 TTree *pottree = new TTree("pottree","pottree");
00226 pottree->Branch("NuePOT",&nuepot);
00227 pottree->Fill();
00228 pottree->Write();
00229 savedir->cd();
00230 }
00231 else{
00232 MSG("NueBeamMon",Msg::kError)<<"Could not find the file to write the pottree to, there will be no pottree"<<endl;
00233 }
00234
00235 }