| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

AdReconFigs.cc

Go to the documentation of this file.
00001 #include "AdReconFigs.h"
00002 
00003 #include "StatisticsSvc/IStatisticsSvc.h"
00004 #include "Event/RecHeader.h"
00005 #include "Event/ReadoutHeader.h"
00006 #include "Conventions/Reconstruction.h"
00007 #include "Conventions/Calibration.h"
00008 
00009 #include "TH2.h"
00010 #include "TMath.h"
00011 #include "TList.h"
00012 
00013 #include <sstream>
00014 
00015 AdReconFigs::AdReconFigs(const std::string& name, 
00016                          ISvcLocator* pSvcLocator)
00017   : GaudiAlgorithm(name,pSvcLocator),
00018     m_statsSvc(0),
00019     m_firstTriggerTime(0)
00020 {
00021   declareProperty("ReconLocation", m_reconLocation="/Event/Rec/AdSimple",
00022                   "Generate histograms for this reconstruction TES path");
00023 }
00024 
00025 AdReconFigs::~AdReconFigs()
00026 {
00027 }
00028 
00029 StatusCode AdReconFigs::initialize()
00030 {
00031   // Initialize the necessary services
00032   StatusCode sc = this->service("StatisticsSvc",m_statsSvc,true);
00033   if(sc.isFailure()){
00034     error() << "Failed to get StatisticsSvc" << endreq;
00035     return sc;
00036   }
00037   return sc;
00038 }
00039 
00040 StatusCode AdReconFigs::execute()
00041 {
00042   // Add the current event into histograms
00043   DayaBay::RecHeader* recHeader = 
00044     get<DayaBay::RecHeader>(m_reconLocation);
00045   if(!recHeader){
00046     error() << "Failed to get reconstructed header: " << m_reconLocation
00047             << endreq;
00048     return StatusCode::FAILURE;
00049   }
00050 
00051   const DayaBay::RecTrigger* recTrigger = &(recHeader->recTrigger());
00052   if(!recTrigger){
00053     error() << "Failed to get reconstructed trigger: " << m_reconLocation
00054             << endreq;
00055     return StatusCode::FAILURE;
00056   }
00057   
00058   if(!recTrigger->detector().isAD()){
00059     // Not an AD, continue
00060     return StatusCode::SUCCESS;
00061   }
00062 
00063   // Wow... Look at the trouble we have to go through to get the run number
00064   int runNumber = 0;
00065   {
00066     // Add the current event into histograms
00067     DayaBay::ReadoutHeader* readoutHeader = 
00068       get<DayaBay::ReadoutHeader>("/Event/Readout/ReadoutHeader");
00069     if(!readoutHeader){
00070       error() << "Failed to get readout header." << endreq;
00071       return StatusCode::FAILURE;
00072     }
00073     
00074     const DayaBay::DaqCrate* ro = readoutHeader->daqCrate();
00075     if(!ro){
00076       error() << "Failed to get readout from header" << endreq;
00077       return StatusCode::FAILURE;
00078     }
00079     
00080     runNumber = ro->runNumber();
00081   }
00082 
00083   if(!m_firstTriggerTime){
00084     m_firstTriggerTime = new TimeStamp(recTrigger->triggerTime());
00085   }
00086 
00087   // Find histograms
00088   const DayaBay::Detector& detector = recTrigger->detector();
00089   TH1F* adEnergy = dynamic_cast<TH1F*>(this->getOrMakeHist(runNumber,
00090                                                            detector,
00091                                                            ADENERGY));
00092   TH2F* adZVsRho = dynamic_cast<TH2F*>(this->getOrMakeHist(runNumber,
00093                                                            detector,
00094                                                            ADZVSRHO));
00095   TH2F* adYVsX = dynamic_cast<TH2F*>(this->getOrMakeHist(runNumber,
00096                                                          detector,
00097                                                          ADYVSX));
00098   TH2F* adZVsXcal = dynamic_cast<TH2F*>(this->getOrMakeHist(runNumber,
00099                                                             detector,
00100                                                             ADZVSXCAL));
00101 
00102   if(recTrigger->energyStatus()==ReconStatus::kGood){
00103     adEnergy->Fill(recTrigger->energy());
00104   }
00105   if(recTrigger->positionStatus()==ReconStatus::kGood){
00106     // FIXME: Use CLHEP units
00107     double x = recTrigger->position().x() / 1000.;
00108     double y = recTrigger->position().y() / 1000.;
00109     double z = recTrigger->position().z() / 1000.;
00110     double rhoSq = x*x+y*y;
00111     double xCal = x*TMath::Cos(DayaBay::Calibration::AD_AxisB_phi)
00112       + y*TMath::Sin(DayaBay::Calibration::AD_AxisB_phi);
00113     adZVsRho->Fill( rhoSq/4., z );
00114     adYVsX->Fill(x, y);
00115     adZVsXcal->Fill(xCal, z);
00116   }
00117 
00118   m_lastTriggerTime[recTrigger->detector()] = recTrigger->triggerTime();
00119 
00120   return StatusCode::SUCCESS;
00121 }
00122 
00123 StatusCode AdReconFigs::finalize()
00124 {
00125   // Handle Normalized histograms
00126   for(unsigned int normIdx=0; normIdx<m_normalize.size(); normIdx++){
00127     TH1* hist = m_normalize[normIdx];
00128     hist->Sumw2();
00129     if(hist->GetNormFactor()>0){
00130       double scale = 1./hist->GetNormFactor();
00131       hist->SetNormFactor(1);
00132       hist->Scale( scale );
00133     }
00134     hist->SetBit(TH1::kIsAverage);
00135   }
00136   m_normalize.clear();
00137   // Clean-up histogram shortcuts
00138   std::map<int,TH1**>::iterator histIter, histEnd = m_shortCuts.end();
00139   for(histIter = m_shortCuts.begin(); histIter!=histEnd; histIter++){
00140     delete [] (histIter->second);
00141     histIter->second = 0;
00142   }
00143   if( m_statsSvc ) m_statsSvc->release();
00144   if(m_firstTriggerTime){
00145     delete m_firstTriggerTime;
00146     m_firstTriggerTime = 0;
00147   }
00148   return StatusCode::SUCCESS;
00149 }
00150 
00152 
00153 TH1* AdReconFigs::getOrMakeHist(int run, const DayaBay::Detector& detector,
00154                                 int histogram)
00155 {
00156   int siteInt = 0;
00157   switch(detector.site()){
00158   case Site::kUnknown:
00159     siteInt = 0; break;
00160   case Site::kDayaBay:
00161     siteInt = 1; break;
00162   case Site::kLingAo:
00163     siteInt = 2; break;
00164   case Site::kFar:
00165     siteInt = 3; break;
00166   case Site::kSAB:
00167     siteInt = 4; break;
00168   default:
00169     error() << "Unknown site: " << detector.detName() << endreq;
00170     return 0;
00171   }
00172   int detectorInt = int(detector.detectorId());
00173   std::map<int,TH1**>::iterator histIter = m_shortCuts.find(run);
00174   TH1** histograms = 0;
00175   if( histIter == m_shortCuts.end() ){
00176     // Initialize histogram shortcuts
00177     histograms = new TH1*[MAXRECHISTS];
00178     for(int i=0; i<MAXRECHISTS; i++) histograms[i] = 0;
00179     m_shortCuts[run] = histograms;
00180   }else{
00181     // Found run
00182     histograms = histIter->second;
00183   }
00184   
00185   TH1* hist = 
00186     histograms[siteInt * NDETECTORS * NRECHISTOGRAMS
00187                + detectorInt * NRECHISTOGRAMS
00188                + histogram];
00189   if(!hist){
00190     // Make this histogram
00191     std::string histName;
00192     std::string histTitle;
00193     if(detector.detectorId()){
00194       // Make detector histogram
00195       std::string reconStyle = "Unknown"; 
00196       for(int strIdx=m_reconLocation.size()-1; strIdx>=0; strIdx--){
00197         if(m_reconLocation[strIdx]=='/'){
00198           reconStyle=std::string(m_reconLocation,strIdx+1);
00199           break;
00200         }
00201       }
00202       switch(histogram){
00203       case ADENERGY:
00204         // AD Energy 
00205         histName = std::string("adEnergy_") + reconStyle;
00206         histTitle = std::string("AD Energy (") + reconStyle
00207           + std::string(")");
00208         hist = new TH1F(histName.c_str(),histTitle.c_str(),
00209                         1100,-1,10);
00210         hist->GetXaxis()->SetTitle("Energy [MeV]");
00211         hist->GetYaxis()->SetTitle("Events / keV");
00212         break;
00213       case ADZVSRHO:
00214         // AD Z vs Rho 
00215         histName = std::string("adZVsRho_") + reconStyle;
00216         histTitle = std::string("AD Reconstructed Position (") + reconStyle
00217           + std::string(")");
00218         hist = new TH2F(histName.c_str(),histTitle.c_str(),
00219                         400,0,1.6,400,-2,2);
00220         hist->GetXaxis()->SetTitle("( #rho / 2 m )^{2}");
00221         hist->GetYaxis()->SetTitle("z [m]");
00222         break;
00223       case ADYVSX:
00224         // AD Y vs X 
00225         histName = std::string("adYVsX_") + reconStyle;
00226         histTitle = std::string("AD Reconstructed Position (") + reconStyle
00227           + std::string(")");
00228         hist = new TH2F(histName.c_str(),histTitle.c_str(),
00229                         400,-3,3,400,-3,3);
00230         hist->GetXaxis()->SetTitle("x [m]");
00231         hist->GetYaxis()->SetTitle("y [m]");
00232         break;
00233       case ADZVSXCAL:
00234         // AD Z vs X_calibration  
00235         histName = std::string("adZVsXcal_") + reconStyle;
00236         histTitle = std::string("AD Reconstructed Position (") + reconStyle
00237           + std::string(")");
00238         hist = new TH2F(histName.c_str(),histTitle.c_str(),
00239                         400,-3,3,400,-2,2);
00240         hist->GetXaxis()->SetTitle("x_{calib} [m]");
00241         hist->GetYaxis()->SetTitle("z [m]");
00242         break;
00243       default:
00244         error() << "Unknown Histogram: " << histogram << endreq;
00245         return 0;
00246       }
00247     }
00248     debug() << "Making histogram: " << histName << endreq;
00249     m_statsSvc->put( this->getPath(run, detector.detName().c_str(), 
00250                                    histName.c_str()), 
00251                      hist );
00252     histograms[siteInt * NDETECTORS * NRECHISTOGRAMS
00253                + detectorInt * NRECHISTOGRAMS
00254                + histogram] = hist;
00255   }
00256 
00257   return hist;
00258 }
00259 
00260 std::string AdReconFigs::getPath(int run, const char* detectorName,
00261                                  const char* histName)
00262 {
00263   // Construct histogram path in statistics service
00264   std::stringstream path;
00265   path << "/file1/diagnostics/run_" << std::setfill('0') << std::setw(7) 
00266        << run;
00267   if(detectorName){
00268     path << "/detector_" << detectorName;
00269   }
00270   path << "/" << histName;
00271   return path.str();
00272 }
00273 
00274 void AdReconFigs::extendRange(TH1* hist, int xBinsAdd, int yBinsAdd/*=0*/)
00275 {
00276   // Automatically extend the histogram range, and replace
00277   //  Currently works for TH1F and TH2F
00278   info() << "Extending histogram range: " << hist->GetName() << endreq;
00279   TH1* extension = 0;
00280   if(hist->IsA() == TH2F::Class()){
00281     extension = new TH2F("","",
00282                          xBinsAdd + hist->GetNbinsX(),
00283                          hist->GetXaxis()->GetXmin(),
00284                          hist->GetXaxis()->GetXmax() 
00285                          + xBinsAdd*hist->GetXaxis()->GetBinWidth(0),
00286                          yBinsAdd + hist->GetNbinsY(),
00287                          hist->GetYaxis()->GetXmin(),
00288                          hist->GetYaxis()->GetXmax() 
00289                          + yBinsAdd*hist->GetYaxis()->GetBinWidth(0));
00290   }else if(hist->IsA() == TH1F::Class()){
00291     extension = new TH1F("","",
00292                          xBinsAdd + hist->GetNbinsX(),
00293                          hist->GetXaxis()->GetXmin(),
00294                          hist->GetXaxis()->GetXmax() 
00295                          + xBinsAdd*hist->GetXaxis()->GetBinWidth(0));
00296   }else{
00297     error() << "Cannot extend range of type: " << hist->ClassName() << endreq;
00298     return;
00299   }
00300   TList list;
00301   list.Add(extension);
00302   hist->Merge(&list);
00303   delete extension;
00304   return;
00305 }
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:25:15 2011 for AdBasicFigs by doxygen 1.4.7