/******************************************* * Root 6 macro: tbcCheckChannels.C * * Plot corrected times channel-by-channel * * M. Staric, September 2016 * *******************************************/ // constants const double syncTimeBase = 47.163878; // [ns] const int canvasW = 1000; // canvas width const int canvasH = 800; // canvas height TCanvas* c1 = 0; // ntuple variables int scrod = 0; int channel = 0; int window = 0; int pixel = 0; float tdc = 0; float tdc0 = 0; float adc = 0; float width = 0; float integral = 0; int flag = 0; // function prototypes double corTime(double tdc, int window, TH1F* tcor); std::string toString(int n); bool wait(); bool tbcTcorScrod(int scrodID, TTree* tree, std::string dir); // -- code --------------------------------------------------------- /** * Check time base corrections on individual channels (by ploting corrected times) * @param fileName root file with ntuple * @param scrodID if nonzero start with this scrod ID * @param dir path to time base correction root files (output of tbcFit.C) */ void tbcCheckChannels(std::string fileName, int scrodID = 0, std::string dir = "tbc") { gROOT->CloseFiles(); gROOT->Reset(); TFile* f = TFile::Open(fileName.c_str()); if(!f) return; TTree* tree = (TTree*) f->Get("tree"); if(!tree) return; tree->SetBranchAddress("scrod", &scrod); tree->SetBranchAddress("channel", &channel); tree->SetBranchAddress("window", &window); tree->SetBranchAddress("pixel", &pixel); tree->SetBranchAddress("tdc", &tdc); tree->SetBranchAddress("tdc0", &tdc0); tree->SetBranchAddress("adc", &adc); tree->SetBranchAddress("width", &width); tree->SetBranchAddress("integral", &integral); tree->SetBranchAddress("flag", &flag); c1 = new TCanvas("c1", "", canvasW, canvasH); c1->Divide(4, 3); c1->Show(); int nbins = 128; TH1F* Hscrod = new TH1F("Hscrod", "scrod ID's", nbins, 0, nbins); int nev = (int)tree->GetEntries(); for(int iev = 0; iev < nev; iev++) { tree->GetEntry(iev); if(flag == 2102) Hscrod->Fill(scrod); } for(int i = 0; i < nbins; i++) { if(Hscrod->GetBinContent(i+1) > 0 and (i == scrodID or scrodID == 0)) { if(!tbcTcorScrod(i, tree, dir)) return; } } } bool tbcTcorScrod(int scrodID, TTree* tree, std::string dir = "tbc") { std::vector corrections(128, 0); int nn = 0; std::string fName = dir + "/tbcScrod" + toString(scrodID) + ".root"; TFile* fcor = TFile::Open(fName.c_str()); if(!fcor) { cout<< "Time base corrections for scrod " << scrodID << "not found" << endl; return true; } for(int k = 0; k < 16; k++) { int chan = k * 8 + 7; std::string hname = "tcor_ch" + toString(chan); TH1F* h = (TH1F*) fcor->Get(hname.c_str()); if(!h) continue; for(int i = 0; i < 8; i++) corrections[chan - i] = h; nn++; } if(nn == 0) { cout << "No correctios available for scrod "<< scrodID << endl; return true; } TH1F* success = (TH1F*) fcor->Get("success"); std::vector Hlaser; for(int chan = 0; chan < 128; chan++) { std::string name = "chan" + toString(chan); std::string title = "channel " + toString(chan%8); auto* h = new TH2F(name.c_str(), title.c_str(), 256, 0, 256, 200, -5, 5); h->GetXaxis()->SetTitle("sample number"); h->GetYaxis()->SetTitle("time [ns]"); Hlaser.push_back(h); } std::vector HcalU; for(int asic = 0; asic < 16; asic++) { std::string name = "calU" + toString(asic); std::string title = "cal pulse (uncorrected)"; auto* h = new TH2F(name.c_str(), title.c_str(), 256, 0, 256, 200, 15, 30); h->GetXaxis()->SetTitle("sample number"); h->GetYaxis()->SetTitle("time [ns]"); HcalU.push_back(h); } std::vector HcalC; for(int asic = 0; asic < 16; asic++) { std::string name = "calC" + toString(asic); std::string title = "cal pulse (corrected)"; auto* h = new TH2F(name.c_str(), title.c_str(), 256, 0, 256, 200, 15, 30); h->GetXaxis()->SetTitle("sample number"); h->GetYaxis()->SetTitle("time [ns]"); HcalC.push_back(h); } std::vector HlasU; for(int asic = 0; asic < 16; asic++) { std::string name = "lasU" + toString(asic); std::string title = "laser (uncorrected)"; auto* h = new TH2F(name.c_str(), title.c_str(), 256, 0, 256, 200, -5, 5); h->GetXaxis()->SetTitle("sample number"); h->GetYaxis()->SetTitle("time [ns]"); HlasU.push_back(h); } std::vector HlasC; for(int asic = 0; asic < 16; asic++) { std::string name = "lasC" + toString(asic); std::string title = "laser (corrected)"; auto* h = new TH2F(name.c_str(), title.c_str(), 256, 0, 256, 200, -5, 5); h->GetXaxis()->SetTitle("sample number"); h->GetYaxis()->SetTitle("time [ns]"); HlasC.push_back(h); } int nev = (int)tree->GetEntries(); for(int iev = 0; iev < nev; iev++) { tree->GetEntry(iev); if(scrod != scrodID) continue; int sample = (int(tdc)+window*64)%256; // int sample = (int(tdc0)+window*64)%256; auto* tcor = corrections[channel]; double time = corTime(tdc, window, tcor) - corTime(tdc0, window, tcor); double t = (tdc - tdc0) * syncTimeBase / 128; int asic = channel / 8; if(flag == 2000) { time += 100; t += 100; if(tcor) { Hlaser[channel]->Fill(sample, time); HlasC[asic]->Fill(sample, time); } HlasU[asic]->Fill(sample, t); } else if(flag == 2102) { if(tcor) HcalC[asic]->Fill(sample, time); HcalU[asic]->Fill(sample, t); } } // Option_t* option = ""; Option_t* option = "colz"; int k = 0; for(int asic = 0; asic < 16; asic++) { k++; c1->cd(k); HcalU[asic]->Draw(option); k++; c1->cd(k); HcalC[asic]->Draw(option); k++; c1->cd(k); HlasU[asic]->Draw(option); k++; c1->cd(k); HlasC[asic]->Draw(option); for(int i = 0; i < 8; i++) { k++; c1->cd(k); int chan = asic * 8 + i; Hlaser[chan]->Draw(option); } int given = -1; if(success && corrections[asic*8+7]) given = success->GetBinContent(asic * 8 + 8); std::string title = "SCROD " + toString(scrodID) + ", ASIC " + toString(asic); switch (given) { case 0: title = title + " (" + corrections[asic*8+7]->GetTitle() + ")"; break; case 1: break; default: title = title + " (no corrections available)"; } c1->SetTitle(title.c_str()); c1->Update(); if(!wait()) return false; k = 0; } return true; } double corTime(double tdc, int window, TH1F* tcor) { if(!tcor) return 0; int sampleNum = int(tdc); double frac = tdc - sampleNum; sampleNum += window * 64; // counted from window 0 int n = sampleNum / 256; int k = sampleNum % 256; double period = tcor->GetBinContent(257); // = 2 * syncTimeBase double time = n * period + tcor->GetBinContent(k+1); // from sample 0 of window 0 time += (tcor->GetBinContent(k+2) - tcor->GetBinContent(k+1)) * frac; // add fraction return time; } string toString(int n) { stringstream ss; ss << n; string str; ss >> str; return str; } bool wait() { std::cout << "Type to continue or Q to quit "; char qq[10]; std::cin.getline(qq, 10); if (strcmp(qq, "q") == 0 || strcmp(qq, ".q") == 0 || strcmp(qq, "Q") == 0) return false; return true; }