std::pair<TH3F*,TH3F*> getEffHists(const char *file, const char *dirC, const char *histN, const char *histD){ TFile *efile = new TFile(file,"read"); TDirectoryFile *efileDIR = (TDirectoryFile*) efile->GetDirectory(dirC); TIter next(efileDIR->GetListOfKeys()); TKey *key; Char_t name[100]; TH3F *hpneff3d=0; TH3F *hpdeff3d=0; while ((key=(TKey*)next())) { sprintf(name,"%s",key->GetName()); if(strcmp((key->GetName()),(histN))==0){ //cout<<"[getEffHists] Your numerator for Eff "<<name<<endl; hpneff3d = (TH3F*) efileDIR->Get(name); } if(strcmp((key->GetName()),(histD))==0){ //cout<<"[getEffHists] Your denominator for Eff "<<name<<endl; hpdeff3d = (TH3F*) efileDIR->Get(name); } } //efileDIR->Close(); //efile->Close(); return std::pair<TH3F*,TH3F*>(hpneff3d,hpdeff3d); }
int main (int argc, char *argv[]) { map<string, string> opt; vector<string> argVector; parseOptions (argc, argv, opt, argVector); if (argVector.size () != 1 || opt.count ("help")) { printHelp (argv[0]); return 0; } TFile *fin; if (!(fin = TFile::Open (argVector.at (0).c_str (), "update"))) { cout << "Failed to open " << argVector.at (0) << "!" << endl; return 0; } TIter next0 (fin->GetListOfKeys ()); TObject *obj0; TH1D *cutFlow = 0; TDirectoryFile *dir = 0; while ((obj0 = next0 ())) { string obj0Class = ((TKey *) obj0)->GetClassName (), obj0Name = obj0->GetName (); if (obj0Class == "TDirectoryFile") { dir = (TDirectoryFile *) fin->Get (obj0Name.c_str ()); TIter next1 (dir->GetListOfKeys ()); TObject *obj1; while ((obj1 = next1 ())) { string obj1Class = ((TKey *) obj1)->GetClassName (), obj1Name = obj1->GetName (); if (obj1Class == "TH1D" && obj1Name.length () >= 7 && ((obj1Name.substr (obj1Name.length () - 7, 7) == "CutFlow" || obj1Name.substr (obj1Name.length () - 7, 7) == "cutFlow") || (obj1Name.length () >= 9 && (obj1Name.substr (obj1Name.length () - 9, 9) == "Selection" || obj1Name.substr (obj1Name.length () - 9, 9) == "selection")) || (obj1Name.length () >= 8 && (obj1Name.substr (obj1Name.length () - 8, 8) == "MinusOne" || obj1Name.substr (obj1Name.length () - 8, 8) == "minusOne"))) && !dir->Get ((obj1Name + "LowerLimit").c_str ()) && !dir->Get ((obj1Name + "UpperLimit").c_str ())) { cutFlow = (TH1D *) dir->Get (obj1Name.c_str ()); cutFlow->SetDirectory (0); getLimits (cutFlow, dir); } } } } fin->Close (); return 0; }
// Noise section Stat_t AnalysisClass::doHisto(Char_t * Variable, Char_t *SubDetName, Char_t *Layer,Char_t *label){ TH1 *hHisto=0; if (debug) cout << d1->GetTitle() << " " << Variable << " " << SubDetName << endl; pPar[0]=0; pPar[1]=0; TIter it(d1->GetListOfKeys()); TObject * o; while ( (o = it())) { TObject * d = d1->Get(o->GetName()); if(d->IsA()->InheritsFrom("TDirectory") && strstr(d->GetName(),SubDetName)){ if (debug) cout << "Found SubDet " << SubDetName << endl; TIter it2(((TDirectoryFile * )d)->GetListOfKeys()); TObject *o2; while( ( o2 = it2()) ){ TObject *d2 = ((TDirectoryFile * )d)->Get(o2->GetName()); if(d2->IsA()->InheritsFrom("TDirectory") && strstr(d2->GetName(),Layer) ){ if (debug) cout << "Found Layer" << Layer << endl; TIter it3(((TDirectoryFile * )d2)->GetListOfKeys()); TObject *o3; while( ( o3 = it3()) ){ TObject *d3 = ((TDirectoryFile * )d2)->Get(o3->GetName()); if(strstr(d3->GetName(),Variable) && strstr(d3->GetName(),label)){ hHisto = (TH1*) d3; if(hHisto->GetEntries()!=0) { pPar[0]=hHisto->GetMean(); pPar[1]=hHisto->GetRMS(); if (debug) cout << "Histo Title " << hHisto->GetTitle() << " mean: " << hHisto->GetMean() << " rms: " << hHisto->GetRMS() << " " << hHisto->GetEntries() << endl; } else{ cout<<"Empty Histogram "<< hHisto->GetTitle() << endl; pPar[0]=-10; pPar[1]=-10; } } } } } } } return hHisto->GetEntries(); }
// This will generate the following PDFs: // -- hDQ_muScTDiff_[DetName]_[BankName].png void GenerateTDiffPlots(std::string filename) { std::cout << "Generating pulse shape plots..." << std::endl; gROOT->Reset(); gROOT->SetStyle("Plain"); gStyle->SetCanvasBorderMode(0); // turn off canvas borders TFile* file = new TFile(filename.c_str(), "READ"); // The histograms TH1F *hDQ_muScTDiff; // Loop through the histograms in the file and get the island histograms (bank and channel names may differ between runs) TDirectoryFile* dir = (TDirectoryFile*) file->Get("DataQuality_LowLevel"); TIter nextDirKey(dir->GetListOfKeys()); // get the list of keys in the directory (all histograms should be in this folder) TKey *dirKey; while ( (dirKey = (TKey*)nextDirKey()) ) { // Get the names of all the histograms and count them up if (strcmp(dirKey->ReadObj()->ClassName(), "TH1F") == 0) { TCanvas *c1 = new TCanvas(); std::string histogram_name = dirKey->ReadObj()->GetName(); std::string histogram_location = "DataQuality_LowLevel/" + histogram_name; std::string pngname = "data_quality_figs/" + histogram_name + ".png"; if (histogram_name.find("muScTDiff") != std::string::npos) { file->GetObject(histogram_location.c_str(),hDQ_muScTDiff); hDQ_muScTDiff->GetYaxis()->SetTitleOffset(1.3); hDQ_muScTDiff->Draw(); c1->Print(pngname.c_str()); } } } }
void advancedNoiseAnalysis( unsigned int runNumber, unsigned int loop = 1) { string inputFileName = "./histo/run00" + toString( runNumber ) + "-ped-histo.root"; string outputFileName = "./histo/run00" + toString( runNumber ) + "-adv-noise.root"; // before opening the input and the output files, try to see if they // are not opened yet and in case close them before continue TList * listOfOpenedFile = (TList*) gROOT->GetListOfFiles(); for ( int i = 0; i < listOfOpenedFile->GetSize() ; ++i ) { TFile * file = (TFile*) listOfOpenedFile->At( i ) ; TString fileName(file->GetName()); TString inputFileName1( inputFileName.c_str() ); TString outputFileName1( outputFileName.c_str() ); if ( ( fileName.Contains( inputFileName1 ) ) || ( inputFileName1.Contains( fileName ) ) || ( fileName.Contains( outputFileName1 ) ) || ( outputFileName1.Contains( fileName ) ) ) { cout << "Closing " << fileName << " before reopen " << endl; file->Close(); } } // close also all the previously opened canvas TList * listOfOpenedCanvas = (TList*) gROOT->GetListOfCanvases(); for ( int i = 0 ; i < listOfOpenedCanvas->GetSize() ; ++i ) { TCanvas * canvas = (TCanvas*) listOfOpenedCanvas->At( i ); TString canvasName2 = canvas->GetName(); if ( canvasName2.Contains( "det" ) ) { canvas->Close(); } } // now safely open the file TFile * inputFile = TFile::Open( inputFileName.c_str() ) ; TFile * outputFile = TFile::Open( outputFileName.c_str(), "RECREATE") ; TList * outputHistoList = new TList; // look into the inputFile for a folder named string pedeProcessorFolderName = "PedestalAndNoiseCalculator"; TDirectoryFile * pedeProcessorFolder = (TDirectoryFile*) inputFile->Get( pedeProcessorFolderName.c_str() ); if ( pedeProcessorFolder == 0 ) { cerr << "No pedestal processor folder found in file " << inputFileName << endl; return ; } // this folder should contain one folder for each loop. string loopFolderName = "loop-" + toString( loop ); TDirectoryFile * loopFolder = (TDirectoryFile *) pedeProcessorFolder->Get( loopFolderName.c_str() ); if ( loopFolder == 0 ) { cerr << "No " << loopFolderName << " found in file " << inputFileName << endl; return ; } // guess the number of sensors from the number of subfolder in the loopfolder size_t nDetector = loopFolder->GetListOfKeys()->GetSize(); cout << "This file contains " << nDetector << " detectors" << endl; // prepare arrays to store the mean and the rms of the noise distribution if ( noiseMean == NULL ) { delete [] noiseMean; noiseMean = NULL; } if ( noiseRMS == NULL ) { delete [] noiseRMS; noiseRMS = NULL; } if ( channel == NULL ) { delete [] channel; channel = NULL; } noiseMean = new double[ nDetector * kNChan ]; noiseRMS = new double[ nDetector * kNChan ]; channel = new double[ kNChan ]; string canvasName = "comparison"; string canvasTitle = "Noise comparison"; TCanvas * comparisonCanvas = new TCanvas( canvasName.c_str(), canvasTitle.c_str(), 1000, 500 ); comparisonCanvas->Divide(1,2); TPad * topPad = (TPad*) comparisonCanvas->cd(1); topPad->Divide( nDetector ); TPad * middlePad = (TPad *) comparisonCanvas->cd(2); middlePad->Divide( kNChan ); // for each detector we have to get the noise map and to prepare 4 // separe histos and maps for ( unsigned int iDetector = 0; iDetector < nDetector; iDetector++ ) { // get the noise map. string noiseMapName = "detector-" + toString( iDetector ) ; noiseMapName += "/NoiseMap-d" + toString( iDetector ) ; noiseMapName += "-l" + toString( loop ) ; TH2D * noiseMap = ( TH2D* ) loopFolder->Get( noiseMapName.c_str() ); // create a folder in the output file TDirectory * subfolder = outputFile->mkdir( string( "detector_" + toString( iDetector ) ).c_str(), string( "detector_" + toString( iDetector ) ).c_str() ); subfolder->cd(); string canvasName = "det" + toString( iDetector ); string canvasTitle = "Detector " + toString( iDetector ); TCanvas * canvas = new TCanvas( canvasName.c_str(), canvasTitle.c_str(), 1000, 500 ); canvas->Divide( kNChan, 2 ); // ok now start the loop on channels for ( size_t iChan = 0 ; iChan < kNChan ; ++iChan ) { if ( iDetector == 0 ) channel[iChan] = iChan - 0.5; string tempName = "NoiseMap_d" + toString( iDetector ) + "_l" + toString( loop ) + "_ch" + toString( iChan ) ; string tempTitle = "NoiseMap Det. " + toString( iDetector ) + " - Ch. " + toString( iChan ) ; TH2D * noiseMapCh = new TH2D ( tempName.c_str() , tempTitle.c_str(), kXPixel / kNChan , -0.5 + xLimit[ iChan ] , -0.5 + xLimit[ iChan + 1 ], kYPixel, -0.5, -0.5 + kYPixel ); noiseMapCh->SetXTitle("X [pixel]"); noiseMapCh->SetYTitle("Y [pixel]"); noiseMapCh->SetZTitle("Noise [ADC]"); noiseMapCh->SetStats( false ); outputHistoList->Add( noiseMapCh ) ; tempName = "NoiseDist_d" + toString( iDetector ) + "_l" + toString( loop ) + "_ch" + toString( iChan ) ; tempTitle = "NoiseDist Det. " + toString( iDetector ) + " - Ch. " + toString( iChan ) ; TH1D * noiseDistCh = new TH1D( tempName.c_str(), tempTitle.c_str(), 50, 0., 10. ); noiseDistCh->SetXTitle("Noise [ADC]"); noiseDistCh->SetLineColor( kColor[iDetector] ); noiseDistCh->SetLineStyle( iChan + 2 ); noiseDistCh->SetLineWidth( 2 ); outputHistoList->Add( noiseDistCh ); // let's start looping on pixels now for ( size_t yPixel = 1 ; yPixel <= kYPixel ; ++yPixel ) { for ( size_t xPixel = xLimit[ iChan ] + 1; xPixel <= xLimit[ iChan +1 ] ; ++xPixel ) { double noise = noiseMap->GetBinContent( xPixel , yPixel ); noiseMapCh->Fill( xPixel - 1 , yPixel - 1, noise ); noiseDistCh->Fill( noise ); } } canvas->cd( iChan + 1 ) ; noiseMapCh->Draw("colz"); canvas->cd( iChan + kNChan + 1 ); noiseDistCh->Draw(); topPad->cd( iDetector + 1 ); if ( iChan == 0 ) { noiseDistCh->Draw(); } else { noiseDistCh->Draw("same"); } middlePad->cd( iChan + 1 ); if ( iDetector == 0 ) { noiseDistCh->Draw(); } else { noiseDistCh->Draw("same"); } noiseMean[ kNChan * iDetector + iChan ] = noiseDistCh->GetMean(); noiseRMS[ kNChan * iDetector + iChan ] = noiseDistCh->GetRMS(); } canvas->Write(); } canvasName = "summary"; canvasTitle = "Noise summary"; TCanvas * summaryCanvas = new TCanvas( canvasName.c_str(), canvasTitle.c_str(), 1000, 500 ); summaryCanvas->SetGridx(1); TLegend * legend = new TLegend(0.5, 4.8, 1.5, 4.3,"","br");; for ( size_t iDetector = 0 ; iDetector < nDetector ; ++iDetector ) { TGraphErrors * gr = new TGraphErrors( kNChan, channel, &noiseMean[ iDetector * kNChan ], NULL, &noiseRMS[ iDetector * kNChan ] ); gr->SetName( string( "NoisePerChannel_d" + toString( iDetector )).c_str()); gr->SetTitle(string("Detector " + toString( iDetector )).c_str()); gr->GetXaxis()->SetTitle("Channel #"); gr->GetYaxis()->SetTitle("Noise [ADC]"); gr->GetXaxis()->SetNdivisions( 5 ); gr->GetXaxis()->SetLabelSize( 0 ); gr->SetMarkerStyle( iDetector + 1 ); gr->SetMarkerColor( kColor[iDetector] ); gr->SetLineColor( kColor[iDetector] ); gr->SetLineWidth( 2 ); legend->AddEntry( gr, string("Detector " + toString( iDetector )).c_str(), "LP"); if ( iDetector == 0 ) { gr->Draw("ALP"); } else { gr->Draw("LP"); } } legend->Draw(); for ( size_t iChan = 0 ; iChan < kNChan ; ++iChan ) { TPaveLabel * label = new TPaveLabel( iChan - 0.75 , 3.2 , iChan -0.25 , 3, string("Ch " + toString( iChan ) ).c_str()); label->Draw(); } summaryCanvas->Write(); comparisonCanvas->Write(); outputHistoList->Write(); }
void CreatePictureBooks(const char* data_dir, int first_run, const int n_runs) { gROOT->Reset(); gROOT->SetStyle("Plain"); gStyle->SetCanvasBorderMode(0); // turn off canvas borders // Create the canvases TCanvas* trend_canvas = new TCanvas("trend_canvas", "trend_canvas"); TFile* files[n_runs]; TCanvas* individual_canvases[n_runs]; // Names for opening, closing and saving to PDFs std::stringstream individual_basepdfname[n_runs]; std::string individual_openpdfname[n_runs]; std::string individual_closepdfname[n_runs]; for (int iRun = 0; iRun < n_runs; ++iRun) { int run_number = first_run + iRun; // Open up the file for this run std::stringstream filename; filename << data_dir << "/hist/hist0" << run_number << ".root"; files[iRun] = new TFile(filename.str().c_str(), "READ"); // Create an individual canvas for this run std::stringstream canvasname; canvasname << "individual_canvas_run0" << run_number; individual_canvases[iRun] = new TCanvas(canvasname.str().c_str(),canvasname.str().c_str()); // Open a PDF for each run individual_basepdfname[iRun].str(""); individual_basepdfname[iRun] << "DQ_LowLevel_RunPlots_Run0" << run_number << ".pdf"; individual_openpdfname[iRun] = individual_basepdfname[iRun].str() + "["; individual_closepdfname[iRun] = individual_basepdfname[iRun].str() + "]"; individual_canvases[iRun]->Print(individual_openpdfname[iRun].c_str()); } // Get the names of all the histograms std::vector<std::string> histogram_names; TIter nextFileKey(files[0]->GetListOfKeys()); // get the list of keys for the top level in the first file (all files should be the same) TKey *fileKey; while (fileKey = (TKey*)nextFileKey()) { // Get the directory for low-level data quality plots first if (strcmp(fileKey->ReadObj()->ClassName(), "TDirectoryFile") == 0) { // Check that the directory has the correct name if (strcmp(fileKey->ReadObj()->GetName(), "DataQuality_LowLevel") == 0) { TDirectoryFile* dir = (TDirectoryFile*) fileKey->ReadObj(); TIter nextDirKey(dir->GetListOfKeys()); // get the list of keys in the directory (all histograms should be in this folder) TKey *dirKey; while (dirKey = (TKey*)nextDirKey()) { // Get the names of all the histograms and count them up if (strcmp(dirKey->ReadObj()->ClassName(), "TH1F") == 0 || strcmp(dirKey->ReadObj()->ClassName(), "TH2F") == 0) { histogram_names.push_back(dirKey->ReadObj()->GetName()); } // end if (class is TH1F) } // end while loop (through keys in the directory) } // end if (directory is DataQuality_LowLevel) } // end if (class is TDirectoryFile) } // end while loop (through keys in file) // Reorder the histogram names so that channels from the same board are next to each other (might make it easier to print 4 plots on the same page) std::vector<std::string> ordered_histogram_names = OrderHistogramNames(histogram_names); std::stringstream trend_basepdfname; trend_basepdfname << "DQ_LowLevel_TrendPlots_Runs0" << first_run << "-" << first_run + n_runs << ".pdf"; std::string trend_openpdfname = trend_basepdfname.str() + "["; std::string trend_closepdfname = trend_basepdfname.str() + "]"; trend_canvas->Print(trend_openpdfname.c_str()); // Now we have the histogram names, so loop though them for (int iHist = 0; iHist < ordered_histogram_names.size(); ++iHist) { std::string histname = ordered_histogram_names.at(iHist); std::string fullhistname = "DataQuality_LowLevel/" + histname; // Check to see if we actually want this plot if ( !WantPlot(histname)) { continue; } // Create the trend plot std::string trendplotname = histname + "_TrendPlot"; std::string trendplottitle = "Trend plot of " + histname; // Get some useful information from the histogram in the first run file (for the y-axis range of the trend plot) TH1F* hist = (TH1F*) files[0]->Get(fullhistname.c_str()); TH2F* trend_plot = new TH2F(trendplotname.c_str(), trendplottitle.c_str(), n_runs, first_run, first_run+n_runs, hist->GetNbinsX(), hist->GetXaxis()->GetXmin(), hist->GetXaxis()->GetXmax()); trend_plot->GetYaxis()->SetTitle(hist->GetXaxis()->GetTitle()); trend_plot->GetYaxis()->SetLabelSize(0.03); trend_plot->GetXaxis()->SetTitle("Run Number"); trend_plot->GetXaxis()->SetLabelSize(0.03); trend_plot->GetZaxis()->SetTitle(hist->GetYaxis()->GetTitle()); trend_plot->GetZaxis()->SetLabelSize(0.03); trend_plot->GetZaxis()->SetTitleOffset(0.85); trend_plot->GetZaxis()->SetTitleSize(0.03); trend_plot->SetStats(false); // Set the fraction trend plots to have a maximum of 1 so that red = bad if (histname.find("Fraction") != std::string::npos) { trend_plot->GetZaxis()->SetRangeUser(0,1); } // Copy the axis labels so that they show the detector and channel names else if (histname.find("hDQ_IslandRate") != std::string::npos) { for (int jBin = 1; jBin < hist->GetNbinsX(); ++jBin) { trend_plot->GetYaxis()->SetBinLabel(jBin, hist->GetXaxis()->GetBinLabel(jBin)); } trend_plot->GetYaxis()->SetTitle(false); trend_plot->GetYaxis()->SetLabelSize(0.02); trend_plot->GetZaxis()->SetLabelSize(0.02); } // Check to see if we want this histogram as an individual or a trend plot bool want_trend_plot = WantAsTrendPlot(histname); // Loop through the runs for (int iRun = 0; iRun < n_runs; ++iRun) { // Make sure this file exists (there may be some missing in our golden run ranges) if (files[iRun]->IsZombie()) continue; // Get the histogram TH1* hist = (TH1*) files[iRun]->Get(fullhistname.c_str()); if (want_trend_plot == false) { // Print the histogram out directly to the individual picture book individual_canvases[iRun]->cd(); ZoomIndividualPlot(hist); hist->Draw("COLZ"); individual_canvases[iRun]->SetLogz(WantLogZ(histname)); // change to a log-z scale if we want to individual_canvases[iRun]->Update(); individual_canvases[iRun]->Print(individual_basepdfname[iRun].str().c_str()); } else { // Fill this histogram into the trend plot for (int iBin = 1; iBin <= hist->GetNbinsX(); ++iBin) { trend_plot->Fill(first_run + iRun, hist->GetBinCenter(iBin), hist->GetBinContent(iBin)); // (x = run #, y = time stamp, z = N_TPI) } // end for loop (filling trend plot) } // end if (trend plot) } // end for loop (through runs) // Now export the trend plot to PDF (if applicable) if (want_trend_plot) { trend_canvas->cd(); ZoomTrendPlot(trend_plot); trend_plot->Draw("COLZ"); trend_canvas->Print(trend_basepdfname.str().c_str()); } } // Now go through and clean up for (int iRun = 0; iRun < n_runs; ++iRun) { individual_canvases[iRun]->Print(individual_closepdfname[iRun].c_str()); } trend_canvas->Print(trend_closepdfname.c_str()); }
void makeMuonTimingPlot (std::string fname) { TFile file(fname.c_str()); TDirectoryFile *dir = (TDirectoryFile*)file.Get("Muons"); TList *hlist = dir->GetListOfKeys(); TH1F *h1; bool foundHist = false; for (auto hist : *hlist) { TString name = hist->GetName(); if (!name.Contains("hMuTimeP")) continue; TH1F *h0 = (TH1F*)dir->Get(name.Data()); if (!foundHist) { h1 = (TH1F*)h0->Clone(); h1->Sumw2(); foundHist = true; } else { h1->Add(h0); } } TH1F *h2 = new TH1F("h2", "h2", 200, -100, 100); h2->GetXaxis()->SetTitle("muon time (ns)"); h2->GetYaxis()->SetTitle("Fraction of Muons/ns"); h2->GetYaxis()->SetTitleOffset(1.1); h2->GetXaxis()->SetTitleOffset(0.8); h2->SetTitle("CSC Muon Time"); h2->SetTitleFont(42); h2->SetTitleSize(0.052); h2->Sumw2(); h2->SetLineColor(kCyan+3); h2->SetFillColor(kCyan+3); for (int ibin = 1; ibin <= 200; ibin++) { h2->SetBinContent(ibin, h1->GetBinContent(ibin)); h2->SetBinError(ibin, h1->GetBinError(ibin)); if (ibin == 200) h2->SetEntries(h1->GetEntries()); } TCanvas c1("c1", "c1", 600, 400); gStyle->SetOptStat(""); double rms = h2->GetRMS(); double avg = h2->GetMean(); TLatex cms(0.17, 0.83, "CMS"); cms.SetNDC(); cms.SetTextFont(61); cms.SetTextSize(0.06); TLatex prelim(0.17, 0.81, "Preliminary"); prelim.SetNDC(); prelim.SetTextAlign(13); prelim.SetTextFont(52); prelim.SetTextSize(0.0456); TLatex data(0.17, 0.76, "Data 2016"); data.SetNDC(); data.SetTextAlign(13); data.SetTextFont(52); data.SetTextSize(0.0456); TLatex lumi(0.9, 0.93, "4.0 fb^{-1} (13 TeV)"); lumi.SetNDC(); lumi.SetTextAlign(31); lumi.SetTextFont(42); lumi.SetTextSize(0.052); TLatex mean(0.7, 0.81, Form("Mean %2.1f", avg)); mean.SetNDC(); mean.SetTextAlign(11); mean.SetTextFont(61); mean.SetTextSize(0.06); TLatex stdev(0.7, 0.76, Form("RMS %2.1f", rms)); stdev.SetNDC(); stdev.SetTextAlign(11); stdev.SetTextFont(61); stdev.SetTextSize(0.06); h2->GetXaxis()->SetRangeUser(-6*rms,6*rms); TH1F* h2norm = (TH1F*)h2->DrawNormalized("hist"); gPad->Update(); cms.Draw(); prelim.Draw(); data.Draw(); lumi.Draw(); mean.Draw(); stdev.Draw(); TPaveText *title = (TPaveText*)gPad->GetPrimitive("title"); title->SetBorderSize(0); title->SetFillColor(0); title->SetFillStyle(0); title->SetX1NDC(0.13); title->SetY1NDC(0.88); title->SetX2NDC(0.9); title->SetY2NDC(0.98); title->SetTextFont(42); title->SetTextSize(0.052); title->SetTextAlign(11); c1.Print("plots/muon_time_all.pdf"); c1.Print("plots/muon_time_all.png"); c1.Print("plots/muon_time_all.root"); }
//Noise section Stat_t AnalysisClass::doNoiseFit(Int_t RunNumber, Char_t *Variable, Char_t *SubDetName, Char_t *Layer, Char_t *label){ TH1 *hNtoFit=0; if (debug) cout << d1->GetTitle() << " " << Variable << " " << SubDetName << endl; pPar[0]=0; pPar[1]=0; TIter it(d1->GetListOfKeys()); TObject * o; while ( (o = it())) { TObject * d = d1->Get(o->GetName()); if(d->IsA()->InheritsFrom("TDirectory") && strstr(d->GetName(),SubDetName)){ if (debug) cout << "Found " << SubDetName << endl; TIter it2(((TDirectoryFile * )d)->GetListOfKeys()); TObject *o2; while( ( o2 = it2()) ){ TObject *d2 = ((TDirectoryFile * )d)->Get(o2->GetName()); if(d2->IsA()->InheritsFrom("TDirectory") && strstr(d2->GetName(),Layer) ){ if (debug) cout << "Found Layer" << Layer << endl; TIter it3(((TDirectoryFile * )d2)->GetListOfKeys()); TObject *o3; while( ( o3 = it3()) ){ TObject *d3 = ((TDirectoryFile * )d2)->Get(o3->GetName()); if(strstr(d3->GetName(),Variable) && strstr(d3->GetName(),label)){ hNtoFit = (TH1*) d3; if (debug) cout << "Found " << Variable << endl; if (hNtoFit->GetEntries()!=0) { cout<<"Fitting "<< hNtoFit->GetTitle() <<endl; // Setting fit range and start values Double_t fr[2]; Double_t sv[3], pllo[3], plhi[3]; fr[0]=hNtoFit->GetMean()-5*hNtoFit->GetRMS(); fr[1]=hNtoFit->GetMean()+5*hNtoFit->GetRMS(); Int_t imax=hNtoFit->GetMaximumBin(); Double_t xmax=hNtoFit->GetBinCenter(imax); Double_t ymax=hNtoFit->GetBinContent(imax); Int_t i[2]; Int_t iArea[2]; i[0]=hNtoFit->GetXaxis()->FindBin(fr[0]); i[1]=hNtoFit->GetXaxis()->FindBin(fr[1]); iArea[0]=hNtoFit->GetXaxis()->FindBin(fr[0]); iArea[1]=hNtoFit->GetXaxis()->FindBin(fr[1]); Double_t AreaFWHM=hNtoFit->Integral(iArea[0],iArea[1],"width"); sv[2]=AreaFWHM/(4*ymax); sv[1]=xmax; sv[0]=hNtoFit->Integral(i[0],i[1],"width"); plhi[0]=1000000.0; plhi[1]=10.0; plhi[2]=10.; pllo[0]=1.5 ; pllo[1]=0.1; pllo[2]=0.3; Char_t FunName[100]; sprintf(FunName,"FitfcnLG_%s%d",hNtoFit->GetName(),fRun); TF1 *ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName); if (ffitold) delete ffitold; gausFit = new TF1(FunName,Gauss,fr[0],fr[1],3); gausFit->SetParameters(sv); gausFit->SetParNames("Constant","GaussPeak","Sigma"); for (Int_t i=0; i<3; i++) { gausFit->SetParLimits(i,pllo[i],plhi[i]); } hNtoFit->Fit(gausFit,"R0"); gausFit->SetRange(fr[0],fr[1]); pGausS=gausFit->GetParameters(); epGausS=gausFit->GetParErrors(); chi2GausS =langausFit->GetChisquare(); // obtain chi^2 nDofGausS = langausFit->GetNDF();// obtain ndf TCanvas *cAllN = new TCanvas("NoiseFit",hNtoFit->GetTitle(),1); Char_t fitFileName[60]; sprintf(fitFileName,"Fits/Run_%d/%s/Fit_%s.png",RunNumber,SubDetName,hNtoFit->GetTitle()); hNtoFit->Draw("pe"); gStyle->SetOptFit(1111111); gausFit->Draw("lsame"); cAllN->Print(fitFileName,"png"); }else { pGausS[0]=-10; pGausS[1]=-10; pGausS[2]=-10; epGausS[0]=-10; epGausS[1]=-10; epGausS[2]=-10; } } } } } } } return hNtoFit->GetEntries(); }
Stat_t AnalysisClass::doFit(Int_t RunNumber,Char_t *Variable, Char_t *SubDetName, Char_t *Layer,Char_t *label){ TH1 *htoFit=0; pLanGausS[0]=0; pLanGausS[1]=0; pLanGausS[2]=0; pLanGausS[3]=0; epLanGausS[0]=0; epLanGausS[1]=0; epLanGausS[2]=0; epLanGausS[3]=0; if (debug) cout << d1->GetTitle() << " " << Variable << " " << SubDetName << endl; pPar[0]=0; pPar[1]=0; TIter it(d1->GetListOfKeys()); TObject * o; while ( (o = it())) { TObject * d = d1->Get(o->GetName()); if(d->IsA()->InheritsFrom("TDirectory") && strstr(d->GetName(),SubDetName)){ if (debug) cout << "Found " << SubDetName << endl; TIter it2(((TDirectoryFile * )d)->GetListOfKeys()); TObject *o2; while( ( o2 = it2()) ){ TObject *d2 = ((TDirectoryFile * )d)->Get(o2->GetName()); if(d2->IsA()->InheritsFrom("TDirectory") && strstr(d2->GetName(),Layer) ){ if (debug) cout << "Found Layer" << Layer << endl; TIter it3(((TDirectoryFile * )d2)->GetListOfKeys()); TObject *o3; while( ( o3 = it3()) ){ TObject *d3 = ((TDirectoryFile * )d2)->Get(o3->GetName()); if(strstr(d3->GetName(),Variable) && strstr(d3->GetName(),label)){ htoFit = (TH1*) d3; if (debug) cout << "Found " << Variable << endl; if (htoFit->GetEntries()!=0) { cout<<"Fitting "<< htoFit->GetTitle() <<endl; // Setting fit range and start values Double_t fr[2]; Double_t sv[4], pllo[4], plhi[4]; fr[0]=0.5*htoFit->GetMean(); fr[1]=3.0*htoFit->GetMean(); // (EM) parameters setting good for signal only Int_t imax=htoFit->GetMaximumBin(); Double_t xmax=htoFit->GetBinCenter(imax); Double_t ymax=htoFit->GetBinContent(imax); Int_t i[2]; Int_t iArea[2]; i[0]=htoFit->GetXaxis()->FindBin(fr[0]); i[1]=htoFit->GetXaxis()->FindBin(fr[1]); iArea[0]=htoFit->GetXaxis()->FindBin(fr[0]); iArea[1]=htoFit->GetXaxis()->FindBin(fr[1]); Double_t AreaFWHM=htoFit->Integral(iArea[0],iArea[1],"width"); sv[1]=xmax; sv[2]=htoFit->Integral(i[0],i[1],"width"); sv[3]=AreaFWHM/(4*ymax); sv[0]=sv[3]; plhi[0]=25.0; plhi[1]=200.0; plhi[2]=1000000.0; plhi[3]=50.0; pllo[0]=1.5 ; pllo[1]=10.0 ; pllo[2]=1.0 ; pllo[3]= 1.0; // create different landau+gaussians for different runs Char_t FunName[100]; sprintf(FunName,"FitfcnLG_%s%d",htoFit->GetName(),fRun); TF1 *ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName); if (ffitold) delete ffitold; langausFit = new TF1(FunName,langaufun,fr[0],fr[1],4); langausFit->SetParameters(sv); langausFit->SetParNames("Width","MP","Area","GSigma"); for (Int_t i=0; i<4; i++) { langausFit->SetParLimits(i,pllo[i],plhi[i]); } htoFit->Fit(langausFit,"R0"); // "R" fit in a range,"0" quiet fit langausFit->SetRange(fr[0],fr[1]); pLanGausS=langausFit->GetParameters(); epLanGausS=langausFit->GetParErrors(); chi2GausS =langausFit->GetChisquare(); // obtain chi^2 nDofGausS = langausFit->GetNDF(); // obtain ndf Double_t sPeak, sFWHM; langaupro(pLanGausS,sPeak,sFWHM); pLanConv[0]=sPeak; pLanConv[1]=sFWHM; cout << "langaupro: max " << sPeak << endl; cout << "langaupro: FWHM " << sFWHM << endl; TCanvas *cAll = new TCanvas("Fit",htoFit->GetTitle(),1); Char_t fitFileName[60]; sprintf(fitFileName,"Fits/Run_%d/%s/Fit_%s.png",RunNumber,SubDetName,htoFit->GetTitle()); htoFit->Draw("pe"); htoFit->SetStats(100); langausFit->Draw("lsame"); gStyle->SetOptFit(1111111); cAll->Print(fitFileName,"png"); } else { pLanGausS[0]=-10; pLanGausS[1]=-10; pLanGausS[2]=-10; pLanGausS[3]=-10; epLanGausS[0]=-10; epLanGausS[1]=-10; epLanGausS[2]=-10; epLanGausS[3]=-10; pLanConv[0]=-10; pLanConv[1]=-10; chi2GausS=-10; nDofGausS=-10; } } } } } } } return htoFit->GetEntries(); }
void mergeForest(TString fname = "/data/jisun/temp/*.root", TString outfile="/data/jisun/pp_2015_HeavyFlavor_AOD_tkpt1_D0pt1_eta2p5_D3d1_Prob0p05_1202.root", bool failOnError = true) { // First, find on of the files within 'fname' and use it to make a // list of trees. Unfortunately we have to know in advance at least // one of the tree names. hiEvtAnalyzer/HiTree is a safe choice for // HiForests. We also assume that every TTree is inside a // TDirectoryFile which is in the top level of the root file. TChain *dummyChain = new TChain("hltanalysis/HltTree"); dummyChain->Add(fname); TFile *testFile = dummyChain->GetFile(); TList *topKeyList = testFile->GetListOfKeys(); std::vector<TString> trees; std::vector<TString> dir; for(int i = 0; i < topKeyList->GetEntries(); ++i) { TDirectoryFile *dFile = (TDirectoryFile*)testFile->Get(topKeyList->At(i)->GetName()); if(strcmp(dFile->ClassName(), "TDirectoryFile") != 0) continue; TList *bottomKeyList = dFile->GetListOfKeys(); for(int j = 0; j < bottomKeyList->GetEntries(); ++j) { TString treeName = dFile->GetName(); treeName += "/"; treeName += bottomKeyList->At(j)->GetName(); TTree* tree = (TTree*)testFile->Get(treeName); if(strcmp(tree->ClassName(), "TTree") != 0 && strcmp(tree->ClassName(), "TNtuple") != 0) continue; trees.push_back(treeName); dir.push_back(dFile->GetName()); } } testFile->Close(); delete dummyChain; // Now use the list of tree names to make a new root file, filling // it with the trees from 'fname'. const int Ntrees = trees.size(); TChain* ch[Ntrees]; Long64_t nentries = 0; for(int i = 0; i < Ntrees; ++i){ ch[i] = new TChain(trees[i]); ch[i]->Add(fname); std::cout << "Tree loaded : " << trees[i] << std::endl; std::cout << "Entries : " << ch[i]->GetEntries() << std::endl; // If the number of entries in this tree is different from other // trees there is a problem. Quit and inform the user without // producing output. if(failOnError) { if(strcmp(trees[i],"HiForest/HiForestVersion") == 0) continue; if(i == 0) nentries = ch[i]->GetEntries(); else if (nentries != ch[i]->GetEntries()) { std::cout << "ERROR: number of entries in this tree does not match." << std::endl; std::cout << "First inconsistent file: " <<ch[i]->GetFile()->GetName()<<std::endl; std::cout << "Exiting. Please check input." << std::endl; return; } } else { std::cout << "WARN: error checking disabled" << std::endl; } } TFile* file = new TFile(outfile, "RECREATE"); for(int i = 0; i < Ntrees; ++i) { file->cd(); std::cout << trees[i] << std::endl; if (i==0) { file->mkdir(dir[i])->cd(); } else { if ( dir[i] != dir[i-1] ) file->mkdir(dir[i])->cd(); else file->cd(dir[i]); } ch[i]->Merge(file,0,"keep"); delete ch[i]; } //file->Write(); file->Close(); std::cout << "Done. Output: " << outfile << std::endl; }
void RunPlots(const char* data_dir, int first_run, const int n_runs) { gROOT->Reset(); gROOT->SetStyle("Plain"); gStyle->SetCanvasBorderMode(0); // turn off canvas borders TCanvas* c1 = new TCanvas("c1", "c1"); // Go through the runs and create a PDF for each one for (int iRun = 0; iRun < n_runs; ++iRun) { // Open up the file for this run std::stringstream filename; filename << data_dir << "/hist/hist0" << first_run + iRun << ".root"; TFile* file = new TFile(filename.str().c_str(), "READ"); // Create the output PDF file to save all the plots to std::stringstream basepdfname; basepdfname << "DQ_LowLevel_RunPlots_Run0" << first_run + iRun << ".pdf"; std::string openpdfname = basepdfname.str() + "["; std::string closepdfname = basepdfname.str() + "]"; c1->Print(openpdfname.c_str()); TIter nextFileKey(file->GetListOfKeys()); // get the list of keys in this file TKey *fileKey; while (fileKey = (TKey*)nextFileKey()) { // Get the directory for low-level data quality plots if (strcmp(fileKey->ReadObj()->ClassName(), "TDirectoryFile") == 0) { // Check that the directory has the correct name if (strcmp(fileKey->ReadObj()->GetName(), "DataQuality_LowLevel") == 0) { TDirectoryFile* dir = (TDirectoryFile*) fileKey->ReadObj(); TIter nextDirKey(dir->GetListOfKeys()); // get the list of keys in the directory (all histograms should be in this folder) TKey *dirKey; while (dirKey = (TKey*)nextDirKey()) { if (strcmp(dirKey->ReadObj()->ClassName(), "TH1F") == 0) { // Get histogram and print TH1F* hist = (TH1F*) dirKey->ReadObj(); hist->Draw(); c1->Update(); c1->Print(basepdfname.str().c_str()); } else if (strcmp(dirKey->ReadObj()->ClassName(), "TH2F") == 0) { TH2F* hist = (TH2F*) dirKey->ReadObj(); // Search through the histogram name if (strstr(hist->GetName(), "PulseShapes") != NULL) { c1->SetLogz(); } hist->Draw("COLZ"); c1->Update(); c1->Print(basepdfname.str().c_str()); } // end if statements // Undo any c1 log scales c1->SetLogz(0); } // end while loop through histograms in directory } // end if correctly name directory } // end if the key is a directory } // end while loop through keys in file // Close the PDF file c1->Print(closepdfname.c_str()); } // end loop through each run file }