00001 #include "Displayer.h"
00002 #include "SctData/DefectList.h"
00003 #include "DisplayManager.h"
00004 #include <cmath>
00005 #include <TCanvas.h>
00006 #include <TH1.h>
00007 #include <TROOT.h>
00008 #include <TStyle.h>
00009 #include <iostream>
00010 #include <iomanip>
00011 #include <boost/lexical_cast.hpp>
00012
00013 using namespace std;
00014 using namespace boost;
00015 using namespace SctData;
00016
00017 namespace SctDataDisplay {
00018
00019 struct null_deleter
00020 {
00021 void operator()(void const *) const
00022 {
00023 }
00024 };
00025
00026 shared_ptr<TCanvas> Displayer::createCanvas(string name, string title) {
00027 unsigned int i=0;
00028 string rname;
00029 TCanvas* old = 0;
00030 do {
00031 ++i;
00032 rname = name + "_" + lexical_cast<string>(i);
00033 old = dynamic_cast<TCanvas*>(gROOT->GetListOfCanvases()->FindObject(rname.c_str()));
00034 } while(old != 0);
00035 int offset=gROOT->GetListOfCanvases()->GetSize()*20%200;
00036 gROOT->SetStyle("Plain");
00037 gStyle->SetOptStat(0);
00038 gStyle->SetTitleX(0.5);
00039 gStyle->SetTitleY(0.99);
00040 gStyle->SetTitleFontSize(0.07);
00041
00042 shared_ptr<TCanvas> canvas = shared_ptr<TCanvas>(new TCanvas(rname.c_str(), title.c_str(),
00043 offset, offset, 900, 650));
00044 if ( DisplayManager::batchMode() ) canvas->SetBatch();
00045 return canvas;
00046 }
00047
00048 void Displayer::divideCanvas(unsigned int n, TCanvas& canvas) {
00049 unsigned int x = (unsigned int)(ceil(sqrt((double)n)));
00050 unsigned int y = (unsigned int)(ceil((double)n/x));
00051 canvas.Divide(x, y, 0.0001, 0.0001);
00052 }
00053
00054 void Displayer::displayHistogram(shared_ptr<TH1> hist, std::string option){
00055 if (DisplayManager::rootMode()){
00056 hist.reset(hist.get(),null_deleter());
00057 }
00058 displayHistogram(*hist, option);
00059 }
00060
00061 void Displayer::displayHistogram(TH1& hist, std::string option){
00062 if (DisplayManager::rootMode()){
00063 shared_ptr<TDirectory> file=DisplayManager::getTDirectory();
00064 if (file) {
00065 hist.SetDirectory(file.get());
00066 file->cd();
00067
00068 std::string name ="h_";
00069 name += hist.GetName();
00070 hist.Write(name.c_str());
00071 }
00072 }else{
00073 int nlevels=100;
00074 double levels[nlevels];
00075 double h_max = hist.GetMaximum();
00076 if (h_max>0.5) h_max=1.0;
00077 for (unsigned i=0; i<nlevels; ++i){
00078 levels[i]=h_max*(double)i/(double)nlevels;
00079 }
00080 hist.SetContour(nlevels, levels);
00081 hist.Draw(option.c_str());
00082 }
00083 }
00084
00085 void Displayer::printDefectList(const DefectList& list, std::ostream& os) {
00086 const DefectList::DefectCollection& defects = list.getAllDefects();
00087
00088 for (DefectList::DefectCollection::const_iterator i=defects.begin(); i!=defects.end(); ++i) {
00089 os << setfill(' ') << setw(20) << i->getPrototype().getName()
00090 << "\tStart channel: " << i->getModuleElement().getFirst()
00091 << "\t# of channels: " << i->getModuleElement().getNChannels()
00092 << "\t" << i->getPrototype().getDescription() << std::endl;
00093 }
00094 os << "[ total number of defects = " << defects.size() << " ]" << std::endl;
00095 os << std::endl;
00096 }
00097
00098 }