00001 #include <iostream>
00002 #include <cstdlib>
00003 #include <cmath>
00004
00005 #include "BeamData/ana/Summary/BeamSummary.h"
00006 #include "MessageService/MsgService.h"
00007
00008 #include "TTree.h"
00009
00010 CVSID("$Id: BeamSummary.cxx,v 1.7 2006/05/27 07:24:02 rhatcher Exp $");
00011
00012 using namespace std;
00013
00014 BeamSummary::BeamSummary(){
00015 clear();
00016 fChain=0;
00017 }
00018
00019 BeamSummary::BeamSummary(TTree* btree)
00020 {
00021 clear();
00022 SetTree(btree);
00023 fChain->SetBranchAddress("Beaminfo",this);
00024 }
00025
00026 void BeamSummary::SetTree(TTree* btree)
00027 {
00028 fChain = btree;
00029 }
00030
00031 void BeamSummary::clear()
00032 {
00033 beamIntensity = 0.0;
00034 hBeamWidth = vBeamWidth = 0.0;
00035 hPosatTargetPM = vPosatTargetPM = 0.0;
00036 hPosatTargetBPM = vPosatTargetBPM = 0.0;
00037 tanHoriz = tanVert = 0.0;
00038 hornPeakCurrent = nuTarZ = 0.0;
00039 hadTotalPH = hadMeanX = hadMeanY = 0.0;
00040 mu1TotalPH = mu1MeanX = mu1MeanY = 0.0;
00041 mu2TotalPH = mu2MeanX = mu2MeanY = 0.0;
00042 mu3TotalPH = mu3MeanX = mu3MeanY = 0.0;
00043 timeStampD = 0.0;
00044 timeStampSec = 0;
00045 }
00046
00047 Int_t BeamSummary::MatchSpillWithTime(Int_t tsec, Int_t tnanosec=0)
00048 {
00049 static Long64_t lastindex=0;
00050 Long64_t index=0;
00051 Int_t found=0;
00052
00053
00054 Long64_t nentries = fChain->GetEntries();
00055 if (nentries<1) {
00056 MSG("BeamSummary", Msg::kWarning) << "No entries in the beam ntuple" << endl;
00057 }
00058
00059 index = lastindex;
00060
00061
00062
00063 while (index>=nentries) {
00064 --index;
00065 }
00066
00067
00068 if (index<0) index=0;
00069
00070
00071
00072
00073
00074 clear();
00075 Long64_t ientry = fChain->LoadTree(index);
00076 if (ientry < 0) {
00077 MSG("BeamSummary",Msg::kWarning) << "Error reading beam ntuple" << endl;
00078 return found;
00079 }
00080 Int_t nb = fChain->GetEntry(index);
00081 if (nb<0) {
00082 MSG("BeamSummary",Msg::kWarning) << "No byte read in" << endl;
00083 return found;
00084 }
00085 Int_t timedif = tsec-timeStampSec;
00086 if (abs(timedif)<2) {
00087
00088 found =1;
00089 }
00090 else if (timedif<=-2) {
00091
00092 index--;
00093 for (Long64_t ient=index; ient>=0; ient--) {
00094 index = ient;
00095 fChain->GetEntry(index);
00096 timedif = tsec-timeStampSec;
00097 if (abs(timedif)<2) {
00098 found = 1;
00099 break;
00100 }
00101 }
00102 }
00103 else if (timedif>=2) {
00104
00105 index++;
00106 for (Long64_t ient=index; ient<nentries; ient++) {
00107 index = ient;
00108 fChain->GetEntry(index);
00109 timedif = tsec-timeStampSec;
00110 if (abs(timedif)<2) {
00111 found = 1;
00112 break;
00113 }
00114 }
00115 }
00116 else {
00117 MSG("BeamSummary",Msg::kDebug) << "This is not supposed to happen!" << endl;
00118 }
00119
00120 if (found==0) {
00121 clear();
00122 MSG("BeamSummary",Msg::kWarning)
00123 << "No entry found in Beam ntuple corresponding to time stamp:"
00124 << " " << tsec << " seconds " << tnanosec << " nanoseconds" << endl;
00125 } else {
00126
00127
00128
00129 Double_t dtime = tsec + (tnanosec/1e9);
00130
00131 Double_t time_this = timeStampD;
00132 Double_t time_prev = 0.0;
00133 Double_t time_next = 0.0;
00134 if (index<nentries-1) {
00135 fChain->GetEntry(index+1);
00136 time_next = timeStampD;
00137 }
00138 if (index>0) {
00139 fChain->GetEntry(index-1);
00140 time_prev = timeStampD;
00141 }
00142 Double_t Deltat_this = fabs(time_this - dtime);
00143 Double_t Deltat_prev = fabs(time_prev - dtime);
00144 Double_t Deltat_next = fabs(time_next - dtime);
00145 MSG("BeamSummary",Msg::kDebug) << "Time difference i = " << Deltat_this<< endl;
00146 MSG("BeamSummary",Msg::kDebug) << "Time difference i-1 = " << Deltat_prev<< endl;
00147 MSG("BeamSummary",Msg::kDebug) << "Time difference i+1 = " << Deltat_next<< endl;
00148 if (Deltat_this>Deltat_prev) index--;
00149 else if (Deltat_this>Deltat_next) index++;
00150 fChain->GetEntry(index);
00151 }
00152 lastindex=index;
00153 return found;
00154 }