Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

PedStudy.cxx

Go to the documentation of this file.
00001 #include "PedStudy.h"
00002 
00003 #include <BeamDataUtil/BDDevices.h>
00004 #include <BeamDataUtil/BDSwicPeds.h>
00005 #include <HistMan/HistMan.h>
00006 #include <RawData/RawBeamMonBlock.h>
00007 #include <RawData/RawBeamMonHeaderBlock.h>
00008 #include <RawData/RawBeamSwicData.h>
00009 #include <RawData/RawDataBlock.h>
00010 #include <RawData/RawRecord.h>
00011 
00012 #include <TH2I.h>
00013 #include <TSystem.h>
00014 #include <TFile.h>
00015 #include <TTree.h>
00016 
00017 #include <MessageService/MsgService.h>
00018 CVSID("$Id: PedStudy.cxx,v 1.3 2005/05/12 17:52:36 bv Exp $");
00019 
00020 #include <list>
00021 #include <sstream>
00022 #include <fstream>
00023 using namespace std;
00024 
00025 PedStudy::PedStudy(const char* directories[])
00026 {
00027     cerr << "PedStudy\n";
00028 
00029     this->GetFiles(directories);
00030     this->Book();
00031     this->Fill();
00032 }
00033 
00034 void PedStudy::GetFiles(const char* directories[])
00035 {
00036     for (int ind=0; directories[ind]; ++ind) {
00037         void* dir = gSystem->OpenDirectory(directories[ind]);
00038         if (!dir) {
00039             MSG("BD",Msg::kWarning)
00040                 << "Skipping empty directory: " << directories[ind] << endl;
00041             continue;
00042         }
00043 
00044         const char* cptr=0;
00045         while ( (cptr = gSystem->GetDirEntry(dir)) ) {
00046             string file = cptr;
00047             if (string::npos == file.find(".mbeam.root")) continue;
00048             fFileMap[file] = directories[ind];
00049         }
00050     }
00051 }
00052 
00053 void PedStudy::Book()
00054 {
00055     int nfiles = fFileMap.size();
00056     HistMan hm("pedstudy");
00057     vector<string> swics = BDDevices::SwicDevices();
00058     for (size_t ind=0; ind<swics.size(); ++ind) {
00059         fHist[swics[ind]].all =
00060             hm.Book<TH2I>(swics[ind].substr(2).c_str(),
00061                           swics[ind].c_str(),
00062                           nfiles,0,nfiles,
00063                           96,0,96,"all");
00064     }
00065 }
00066 
00067 void PedStudy::Fill()
00068 {
00069     map<string,string>::iterator it, done = fFileMap.end();
00070     list<string> files;
00071     for (it=fFileMap.begin(); it != done; ++it) files.push_back(it->first);
00072     files.sort();
00073     list<string>::iterator lit, ldone = files.end();
00074 
00075     int count = 0;
00076     for (lit = files.begin(); lit != ldone; ++lit) {
00077         string file = *lit;
00078         string dir = fFileMap[file];
00079 
00080         MSG("BD",Msg::kInfo) << dir << "/" << file << endl;
00081         this->OneFile(count,file,dir);
00082         ++count;
00083     }
00084 }
00085 
00086 void PedStudy::OneFile(int count, string filename, string dirname)
00087 {
00088     HistMan hm("pedstudy");
00089 
00090     HistMap::iterator it, done=fHist.end();
00091     for (it=fHist.begin(); it != done; ++it) {
00092         string name = it->first.substr(2);
00093         string hmdir = name + "/" + filename;
00094         TProfile* hist = hm.Book<TProfile>(name.c_str(),hmdir.c_str(),
00095                                            96,0,96,hmdir.c_str());
00096         fHist[it->first].perfile.push_back(hist);
00097     }
00098 
00099     string rootfile = dirname + "/" + filename;
00100     TFile file(rootfile.c_str(),"READ");
00101     TTree* tree = (TTree*)(file.Get("BeamMon"));
00102     RawRecord* record = 0;
00103     for ( Int_t ient = 0; ient < tree -> GetEntries(); ient++ ) {
00104         tree -> SetBranchAddress("RawRecord",&record);
00105         tree->GetEntry(ient);
00106         TIter itr = record->GetRawBlockIter();
00107 
00108         const RawBeamMonBlock* rbmb = 0;;
00109         const RawBeamMonHeaderBlock* rbmhb = 0;
00110 
00111         // check all blocks
00112         RawDataBlock* rdb = 0;
00113         while ((rdb = dynamic_cast<RawDataBlock*>(itr()))) {
00114             if (rdb->InheritsFrom("RawBeamMonBlock"))
00115                 rbmb = dynamic_cast<const RawBeamMonBlock*>(rdb);
00116             if (rdb->InheritsFrom("RawBeamMonHeaderBlock"))
00117                 rbmhb = dynamic_cast<const RawBeamMonHeaderBlock*>(rdb);
00118         }
00119 
00120         if (!(rbmb && rbmhb)) continue;
00121 
00122         if (rbmb->TclkTriggerEvent() != 0xa9) continue;
00123         if (! BDSwicPeds::IsPedSpill(*rbmb)) continue;
00124 
00125         this->OneRecord(count,*rbmb);
00126     }
00127     delete record; record = 0;
00128 }
00129 
00130 void PedStudy::OneRecord(int count, const RawBeamMonBlock& block)
00131 {
00132     map<string,Hists>::iterator it, done = fHist.end();
00133     RawBeamSwicData swic;
00134     for (it = fHist.begin(); it != done; ++it) {
00135         const RawBeamData* rbd = block[it->first];
00136         if (!rbd) continue;
00137         swic.SetData(*rbd);
00138 
00139         vector<int> wire;
00140         swic.UnscaledWireData(wire);
00141         for (size_t ind=0; ind<wire.size(); ++ind) {
00142             it->second.all->Fill(count,ind,wire[ind]);
00143             it->second.perfile.back()->Fill(ind,wire[ind]);
00144 
00145         }
00146     }
00147 }
00148 
00149 
00150 
00151 void PedStudy::WriteOut(const char* filename)
00152 {
00153     HistMan hm("");
00154     hm.WriteOut(filename);
00155 }
00156 
00157 PedViewer* PedStudy::View()
00158 {
00159     return new PedViewer(*this);
00160 }
00161 
00162 
00163 #include <TCanvas.h>
00164 
00165 PedViewer::PedViewer(PedStudy& pedstudy)
00166     : ps(pedstudy)
00167 {
00168     colz = new TCanvas("colz","",600,500);
00169     oned = new TCanvas("oned","",600,500);
00170     colz->AddExec("updater",Form("pedviewer_updater((void*)%d)",this));
00171     oned->AddExec("clicker",Form("pedviewer_clicker((void*)%d)",this));
00172     cur = ps.fHist.begin();
00173 
00174     this->Update();
00175         
00176 }
00177 
00178 void PedViewer::Next()
00179 {
00180     PedStudy::HistMap::iterator next = cur;
00181     ++next;
00182     if (next == ps.fHist.end()) {
00183         cerr << "At end, looping to begin\n";
00184         cur = ps.fHist.begin();
00185     }
00186     else cur = next;
00187     this->Update();
00188 }
00189 void PedViewer::Prev()
00190 {
00191     if (cur == ps.fHist.begin()) {
00192         cerr << "At begining, looping to end\n";
00193         cur = ps.fHist.end();
00194     }
00195     --cur;
00196     this->Update();
00197 }
00198 
00199 void PedViewer::Update()
00200 {
00201     PedStudy::Hists& h = cur->second;
00202     if (!h.all) {
00203         cerr << "No histogram to update!\n";
00204         return;
00205     }
00206 
00207     colz->cd();
00208     h.all->Draw("COLZ");
00209 }
00210 
00211 
00212 
00213 void pedviewer_updater(void* ptr)
00214 {
00215     int event = gPad->GetEvent();
00216 
00217     if (event != 51) return;    // movement
00218 
00219     // from root/tutorials/DynamicSlice
00220     TObject *select = gPad->GetSelected();
00221     if(!select) return;
00222     if (!select->InheritsFrom("TH2")) {gPad->SetUniqueID(0); return;}
00223     gPad->GetCanvas()->FeedbackMode(kTRUE);
00224 
00225     // Get hist X coords
00226     int px = gPad->GetEventX();
00227     Float_t upx = gPad->AbsPixeltoX(px);
00228     Float_t x = gPad->PadtoX(upx);
00229 
00230     PedViewer* viewer = (PedViewer*)ptr;
00231     PedStudy::Hists& hists = viewer->cur->second;
00232 
00233     static int lastbinx = 0;
00234     int binx = hists.all->GetXaxis()->FindBin(x);
00235     if (!binx) return;
00236     if (binx>(int)hists.perfile.size()) return;
00237     if (binx == lastbinx) return;
00238     lastbinx = binx;
00239 
00240     viewer->cur_file_num = binx-1;
00241     TProfile* h1 = hists.perfile[binx-1];
00242     viewer->oned->cd();
00243     cerr << binx << ": " << h1->GetName() << " " << h1->GetTitle() << endl;
00244     h1->Draw();
00245     viewer->oned->Update();
00246     //viewer->colz->cd();
00247 }
00248 
00249 void pedviewer_clicker(void* ptr)
00250 {
00251     int event = gPad->GetEvent();
00252 
00253     if (event != 1) return;     // click
00254 
00255     // from root/tutorials/DynamicSlice
00256     TObject *select = gPad->GetSelected();
00257     if(!select) return;
00258     if (!select->InheritsFrom("TProfile")) {gPad->SetUniqueID(0); return;}
00259     gPad->GetCanvas()->FeedbackMode(kTRUE);
00260 
00261     // Get hist X coords
00262     int px = gPad->GetEventX();
00263     Float_t upx = gPad->AbsPixeltoX(px);
00264     Float_t x = gPad->PadtoX(upx);
00265 
00266     PedViewer* viewer = (PedViewer*)ptr;
00267     PedStudy::Hists& hists = viewer->cur->second;
00268 
00269     TProfile* h1 = hists.perfile[viewer->cur_file_num];
00270     int binx = h1->GetXaxis()->FindBin(x);
00271     if (!binx) return;
00272     if (binx>96) return;
00273 
00274     int channel = binx - 1;
00275 
00276     stringstream ss;
00277 
00278     ss << "E:" <<  h1->GetName()
00279        << " E:" << h1->GetTitle()
00280        << " " << channel
00281        << " " << h1->GetBinContent(binx)
00282        << ends;
00283 
00284     ofstream o("pedstudy.log",ios_base::app);
00285     o << ss.str() << endl;
00286     cerr << ss.str() << endl;
00287 }

Generated on Mon Nov 23 05:27:57 2009 for loon by  doxygen 1.3.9.1