00001
00002
00003
00004 #include <sstream>
00005 #include <string>
00006 #include <stdio.h>
00007
00008
00009
00010 #include "TGraph.h"
00011 #include "TH2F.h"
00012 #include "TH1F.h"
00013 #include "TH1I.h"
00014 #include <TProfile.h>
00015 #include "TMath.h"
00016
00017
00018 #include "ReconDataHistogram.h"
00019
00020
00021 #include "StatisticsSvc/IStatisticsSvc.h"
00022 #include "Event/RecHeader.h"
00023 #include "Event/ReadoutHeader.h"
00024 #include "Conventions/Reconstruction.h"
00025 #include "Conventions/Calibration.h"
00026
00027 using namespace std;
00028 using namespace DayaBay;
00029 using namespace DybDaq;
00030
00031 ReconDataHistogram::ReconDataHistogram(const std::string& name,
00032 ISvcLocator* pSvcLocator)
00033 : GaudiAlgorithm(name,pSvcLocator),
00034 m_statsSvc(0),
00035 m_eventCount(0),
00036 m_firstTriggerTime(0)
00037 {
00038 }
00039
00040 ReconDataHistogram::~ReconDataHistogram()
00041 {
00042 }
00043
00044 StatusCode ReconDataHistogram::initialize()
00045 {
00046
00047 StatusCode sc = service("StatisticsSvc",m_statsSvc,true);
00048 if(sc.isFailure()){
00049 error() << "Failed to get StatisticsSvc" << endreq;
00050 return sc;
00051 }
00052 return sc;
00053 }
00054
00055 StatusCode ReconDataHistogram::execute()
00056 {
00057 verbose() << "executing: " << m_eventCount << endreq;
00058
00059 DayaBay::RecHeader* recHeader =
00060 get<DayaBay::RecHeader>("/Event/Rec/AdSimple");
00061 if(!recHeader){
00062 error() << "Failed to get reconstructed header:" << "/Event/Rec/AdSimple"
00063 << endreq;
00064 return StatusCode::FAILURE;
00065 }
00066
00067 const DayaBay::RecTrigger* recTrigger = &(recHeader->recTrigger());
00068 if(!recTrigger){
00069 error() << "Failed to get reconstructed trigger: " << "Event/Rec/AdSimple"
00070 << endreq;
00071 return StatusCode::FAILURE;
00072 }
00073
00074 if(!recTrigger->detector().isAD()){
00075
00076 return StatusCode::SUCCESS;
00077 }
00078
00079 const DayaBay::Detector& detector = recTrigger->detector();
00080 info() << "Detector = " << detector << endreq;
00081
00082 if(!m_firstTriggerTime){
00083 m_firstTriggerTime = new TimeStamp(recTrigger->triggerTime());
00084 }
00085
00086 TH2F* h_energyVsR = dynamic_cast<TH2F*>(getOrMakeHist(1, detector, kEnergyVsR));
00087 TH2F* h_energyVsZ = dynamic_cast<TH2F*>(getOrMakeHist(1, detector, kEnergyVsZ));
00088 TH1F* h_rateVsR = dynamic_cast<TH1F*>(getOrMakeHist(1, detector, kRateVsR));
00089 TH1F* h_rateVsZ = dynamic_cast<TH1F*>(getOrMakeHist(1, detector, kRateVsZ));
00090
00091 if(recTrigger->positionStatus()==ReconStatus::kGood){
00092 double x = recTrigger->position().x() / 1000;
00093 double y = recTrigger->position().y() / 1000;
00094 double z = recTrigger->position().z() / 1000;
00095 double r = TMath::Sqrt(x*x+y*y);
00096 h_rateVsR->Fill(r);
00097 h_rateVsZ->Fill(z);
00098 if(recTrigger->energyStatus()==ReconStatus::kGood){
00099 h_energyVsR->Fill(r, recTrigger->energy());
00100 h_energyVsZ->Fill(z,recTrigger->energy());
00101 }
00102 }
00103
00104 m_lastTriggerTime = recTrigger->triggerTime();
00105
00106 ++m_eventCount;
00107 if (0 == (m_eventCount % 1000)) {
00108 info() << " Processed " << m_eventCount << " events" << endreq;
00109 }
00110
00111 return StatusCode::SUCCESS;
00112 }
00113
00114 StatusCode ReconDataHistogram::finalize()
00115 {
00116 info() << "Finalizing " << endreq;
00117
00118 for(unsigned int Idx=0; Idx<m_scale.size(); Idx++){
00119 TH1* hist = m_scale[Idx];
00120 hist->Sumw2();
00121 double duration = m_lastTriggerTime.GetSeconds() - m_firstTriggerTime->GetSeconds();
00122 hist->Scale(1/duration);
00123 }
00124 m_scale.clear();
00125
00126 if( m_statsSvc ) {
00127 m_statsSvc->release();
00128 }
00129
00130 if(m_firstTriggerTime){
00131 delete m_firstTriggerTime;
00132 m_firstTriggerTime = 0;
00133 }
00134
00135 return StatusCode::SUCCESS;
00136 }
00137
00138 TH1* ReconDataHistogram::getOrMakeHist(int run, const DayaBay::Detector& detector,
00139 int histIndex)
00140 {
00141 debug() << "Detector=" << detector << "," << detector.fullPackedData() << endreq;
00142
00143 int siteInt = 0;
00144 switch(detector.site()){
00145 case Site::kUnknown:
00146 siteInt = 0; break;
00147 case Site::kDayaBay:
00148 siteInt = 1; break;
00149 case Site::kLingAo:
00150 siteInt = 2; break;
00151 case Site::kFar:
00152 siteInt = 3; break;
00153 case Site::kSAB:
00154 siteInt = 4; break;
00155 default:
00156 error() << "Unknown site: " << detector.detName() << endreq;
00157 return 0;
00158 }
00159
00160
00161 int detectorInt = int(detector.detectorId());
00162 std::map<int,TH1**>::iterator histIter = m_hist.find(detectorInt);
00163 TH1** histograms = 0;
00164 if( histIter == m_hist.end() ){
00165 info() << "Create " << MAXRECHISTS << " histograms for " << detector << endreq;
00166
00167 histograms = new TH1*[MAXRECHISTS];
00168 for(int i=0; i<MAXRECHISTS; i++) histograms[i] = 0;
00169 m_hist[detectorInt] = histograms;
00170 }else{
00171
00172 histograms = histIter->second;
00173 }
00174
00175 int iHist = siteInt * NDETECTORS * NRECHISTOGRAMS + detectorInt * NRECHISTOGRAMS + histIndex;
00176
00177 info() << "Histogram index = " << iHist << endreq;
00178 TH1* hist = histograms[siteInt * NDETECTORS * NRECHISTOGRAMS + detectorInt * NRECHISTOGRAMS + histIndex];
00179 if(!hist){
00180
00181 std::string histName;
00182 if(detector.detectorId()){
00183
00184
00185 switch(histIndex){
00186 case kEnergyVsR:
00187 hist = new TH2F("h_sum_energyVsR","h_sum_energyVsR",250, 0, 2.5, 110, -1, 10);
00188 hist->GetXaxis()->SetTitle("R_{recon}/m");
00189 hist->GetYaxis()->SetTitle("E_{recon}/MeV");
00190 break;
00191 case kEnergyVsZ:
00192 hist = new TH2F("h_sum_energyVsZ","h_sum_energyVsZ",500, -2.5, 2.5, 110, -1, 10);
00193 hist->GetXaxis()->SetTitle("Z_{recon}/m");
00194 hist->GetYaxis()->SetTitle("E_{recon}/MeV");
00195 break;
00196 case kRateVsR:
00197 hist = new TH1F("h_sum_rateVsR","h_sum_rateVsR", 250, 0, 2.5);
00198 hist->GetXaxis()->SetTitle("R_{recon}/m");
00199 hist->GetYaxis()->SetTitle("EventRate/bin(1cm)");
00200 m_scale.push_back(hist);
00201 break;
00202 case kRateVsZ:
00203 hist = new TH1F("h_sum_rateVsZ","h_sum_rateVsZ",500, -2.5, 2.5);
00204 hist->GetXaxis()->SetTitle("Z_{recon}/m");
00205 hist->GetYaxis()->SetTitle("EventRate/bin(1cm)");
00206 m_scale.push_back(hist);
00207 break;
00208 default:
00209 error() << "Unknown histogram for event: " << histIndex << endreq;
00210 return 0;
00211 }
00212
00213 }
00214 info() << "Making histogram: " << hist->GetName() << endreq;
00215 m_statsSvc->put( getPath(run, detector, hist->GetName()), hist );
00216 histograms[ siteInt * NDETECTORS * NRECHISTOGRAMS + detectorInt * NRECHISTOGRAMS + histIndex] = hist;
00217 }
00218
00219 return hist;
00220 }
00221
00222 std::string ReconDataHistogram::getPath(int run, const Detector &detector,
00223 const char* histName)
00224 {
00225
00226 std::stringstream path;
00227 path << "/file1/";
00228 path << Site::AsString(detector.site()) << "/" << DetectorId::AsString(detector.detectorId());
00229 path << "/Recon";
00230 path << "/" << histName;
00231 debug() << "Histogram path = " << path << endreq;
00232 return path.str();
00233 }