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