SctDaqRootFile.cpp

00001 #include "SctDaqRootFile.h"
00002 #include "Exceptions.h"
00003 #include "Sct/SctParameters.h"
00004 #include "Sct/AbcdScans.h"
00005 
00006 #include <boost/scoped_ptr.hpp>
00007 
00008 #include <TFile.h>
00009 #include <TH1.h>
00010 #include <TH2.h>
00011 #include <TObjString.h>
00012 
00013 #include <sstream>
00014 
00015 using namespace std;
00016 using namespace boost;
00017 using namespace Sct;
00018 
00019 namespace SctTest {
00020 
00021 SctDaqRootFile::SctDaqRootFile(string name) : file(name.c_str()){
00022     if (!file.IsOpen()) throw NoDataFileException("No datafile: " + name, __FILE__, __LINE__);
00023 }
00024 
00025 int SctDaqRootFile::getCycleNum(string serialNum) {
00026     for (int i=1; i<=6; ++i) {
00027         ostringstream oss;
00028         oss << "ModuleName;" << i;
00029         scoped_ptr<TObjString> s ((TObjString*)file.Get(oss.str().c_str()));
00030         if (serialNum == s->String().Data()) return i;
00031     }
00032     throw NoDataFileException("Module " + serialNum + " not found in file", __FILE__, __LINE__);
00033 }
00034 
00035 auto_ptr<TH1F> SctDaqRootFile::getHistData(int cycleNum, int id) {
00036     ostringstream oss;
00037     oss << "h_scan" << id << ";" << cycleNum;
00038     auto_ptr<TH1F> hist ((TH1F*)file.Get(oss.str().c_str()));
00039     if (!hist.get()) throw DataNotValidException("Couldn't get histogram: " + oss.str(), __FILE__, __LINE__);
00040     return hist;
00041 }
00042 
00043 auto_ptr<TH1F> SctDaqRootFile::getTriggerData() {
00044     auto_ptr<TH1F> hist ((TH1F*)file.Get("h_scan_tsent"));
00045     if (!hist.get()) {
00046         //cout << "File contents:" << endl;
00047         //f.ls();
00048         ostringstream oss;
00049         oss << "Couldn't get histogram: h_scan_tsent in file: " << file.GetName();
00050         throw DataNotValidException(oss.str(), __FILE__, __LINE__);
00051     }
00052     return hist;
00053 }
00054 
00055 void SctDaqRootFile::dealWithOccupancy(TH1F& hist0Data, TH1F& hist1Data, TH1F& triggerData, unsigned short scanVar, bool forceConstTrigger) {
00056     bool bOccupancy = false;
00057     if (hist0Data.GetMaximum() <= 1)
00058         bOccupancy = true;
00059 
00060     //cout << "Occupancy hist? " << bOccupancy << endl;
00061 
00062     //Factor of 4 is for consistency with other scans
00063     for (int i=1; i<=triggerData.GetNbinsX(); ++i) {
00064         if (!bOccupancy && scanVar != ST_TOKEN)
00065             triggerData.SetBinContent(i, triggerData.GetBinContent(i)/4);
00066     }
00067 
00068     for (int i=0; i<hist0Data.GetNbinsX(); ++i) {
00069         for (int j=0; j<hist0Data.GetNbinsY(); ++j) {
00070             if (bOccupancy) {
00071         if (forceConstTrigger) {
00072             //Force triggers to all be the same....
00073             hist0Data.SetBinContent(i, j+1, triggerData.GetBinContent(1)*hist0Data.GetBinContent(i,j+1));
00074             hist1Data.SetBinContent(i, j+1, triggerData.GetBinContent(1)*hist1Data.GetBinContent(i,j+1));
00075         } else {
00076             hist0Data.SetBinContent(i, j+1, triggerData.GetBinContent(j+1)*hist0Data.GetBinContent(i,j+1));
00077             hist1Data.SetBinContent(i, j+1, triggerData.GetBinContent(j+1)*hist1Data.GetBinContent(i,j+1));
00078         }
00079             }
00080         }
00081     }
00082 }
00083 
00084 char* SctDaqRootFile::fillData16(const TH1F& hist0Data, const TH1F& hist1Data) {
00085     int nBins = hist0Data.GetNbinsY();
00086     unsigned short* data = new unsigned short[2*1024*nBins];
00087 
00088     for (int i=0; i<2048; ++i) {
00089         for (int j=0; j<nBins; ++j) {
00090             if (i<nChannelLink)
00091                 data[j*2048+i] = (unsigned short)hist0Data.GetBinContent(i, j+1);
00092             else if (i>=1024 && i<1024+nChannelLink)
00093                 data[j*2048+i] = (unsigned short)hist1Data.GetBinContent(i-1024, j+1);
00094             else
00095                 data[j*2048+i] = (unsigned short)0;
00096         }
00097     }
00098 
00099     return reinterpret_cast<char*>(data);
00100 }
00101 char* SctDaqRootFile::fillData32(const TH1F& hist0Data, const TH1F& hist1Data) {
00102     int nBins = hist0Data.GetNbinsY();
00103     unsigned int* data = new unsigned int[2*1024*nBins];
00104 
00105     for (int i=0; i<2048; ++i) {
00106         for (int j=0; j<nBins; ++j) {
00107             if (i<nChannelLink) {
00108                 data[j*2048+i] = (unsigned int)hist0Data.GetBinContent(i, j+1);
00109             } else if (i>=1024 && i<1024+nChannelLink) {
00110                 data[j*2048+i] = (unsigned int)hist1Data.GetBinContent(i-1024, j+1);
00111             } else {
00112                 data[j*2048+i] = (unsigned int)0;
00113             }
00114         }
00115     }
00116 
00117     return reinterpret_cast<char*>(data);
00118 }
00119 
00120 auto_ptr<TH2D> SctDaqRootFile::fillData(const TH1F& histData) {
00121     auto_ptr<TH2D> link (new TH2D("link", "Link data", histData.GetNbinsX(), 0, 100, histData.GetNbinsY(), 0, 100));
00122 
00123     for (int i=0; i<histData.GetNbinsX(); ++i) {
00124         for (int j=0; j<histData.GetNbinsY(); ++j) {
00125             link->SetBinContent(i+1, j+1, histData.GetBinContent(i,j+1));
00126         }
00127     }
00128     return link;
00129 }
00130 
00131 }

Generated on Mon Feb 6 14:01:29 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6