Dear All,
I’m trying to access a histogram via a TCanvas that is saved in a root file with the GetPrimitive() function. It keep returning a null-pointer and I have no clue why. When I open the file with a TBrowser I can see that the histogram is a TH2F with the name “BPix_BpO_SEC1_LYR3_LDR1F_MOD1_ROC0” but when I try to get that TH2F with that name it returns nothing. Even more strange, when I “ListPrimitives()” I get back that there is a TH1 on the Canvas, but when I try to get that TH1 and Draw it it is again a null pointer. Maybe someone can help?
So here is my code, the problem appears in the canvasHisto function:
#include <iostream>
#include "TStyle.h"
#include "TFile.h"
#include "TCanvas.h"
#include "TH2.h"
#include "TObject.h"
#include "TString.h"
// This is for analyzing PixelAlive runs taken by the telescope
TString fileName ( int runNo = 100026, bool pixel = false, bool caldel = false) {
if ( pixel ) return Form ( "PixelAlive_Fed_1_Run_%d.root", runNo ) ;
else if ( caldel ) return Form ( "CalDel_1_Run_%d.root", runNo ) ;
//else if ( caldel ) return Form ( "CalDel_1.root" ) ;
}//end TString fileName
TString rocName ( TString arm = "BpO", int sectionMod = 1, int ladder = 1, int roc = 0 ) {
return "BPix_" + arm + Form ( "_SEC%d", sectionMod ) + "_LYR3" + Form ( "_LDR%dF", ladder ) + Form ( "_MOD%d", sectionMod ) + Form ( "_ROC%d", roc ) ;
}//end TString rocName
TString moduleName ( TString arm = "BpO", int sectionMod = 1, int ladder = 1 ) {
return "BPix_" + arm + Form ( "_SEC%d", sectionMod ) + "_LYR3" + Form ( "_LDR%dF", ladder ) + Form ( "_MOD%d", sectionMod ) ;
}//end TString moduleName
TString dirName ( int rocNo = 0, TString arm = "BpO", int sectionMod = 1, int ladder = 1, bool pixel = false, bool caldel = false ) {
TString dir0 = "BPix" ;
TString dir1 = dir0 + "_" + arm ;
TString dir2 = dir1 + Form ( "_SEC%d", sectionMod );
TString dir3 = dir2 + "_LYR3" ;
TString dir4 = dir3 + Form ( "_LDR%dF", ladder ) ;
TString dir5 = dir4 + Form ( "_MOD%d", sectionMod ) ;
TString dir6 = dir5 + Form ( "_ROC%d", rocNo ) ;
if ( pixel ) return dir0 + "/" + dir1 + "/" + dir2 + "/" + dir3 + "/" + dir4 + "/" + dir5 + "/" + dir6 ;
else if ( caldel ) return dir0 + "/" + dir1 + "/" + dir2 + "/" + dir3 + "/" + dir4 + "/" + dir5 + "/" + dir6 + "_c" ;
}//end TString dirName
TH2F *moduleHisto ( TString arm, int sectionMod = 1, int ladder = 1, bool pixel = false, bool caldel = false ) {
TString module = moduleName( arm, sectionMod, ladder ) ;
TH2F *moduleHist = new TH2F ( module, module, 8*52, 0-0.5, 8*52-0.5, 2*80, 0-0.5, 2*80-0.5 ) ;
for ( int iRoc = 0; iRoc < 4; iRoc++ ) {
TString directory = dirName ( iRoc, arm, sectionMod, ladder, pixel, caldel ) ;
TH2 *h = (TH2*)gDirectory -> Get ( directory ) ;
if ( h==NULL ) {
std::cout << "Can not access histogram! " << directory.Data() << std::endl ;
return NULL ;
}
int rocX = h -> GetNbinsX ( ) ;
int rocY = h -> GetNbinsY ( ) ;
int rocNx = iRoc * rocX, rocNy = 0 ;
if ( iRoc >= 8 ) {
rocNy = 81 ;
rocNx = 8 * rocX - ((iRoc-7) * rocX) + 1; // To fix the roc positions
}// end if iRoc
for ( int iX = rocNx; iX < rocX + rocNx; iX++ ) {
for ( int iY = rocNy; iY < rocY + rocNy; iY++ ) {
moduleHist -> SetBinContent ( iX, iY, h->GetBinContent( iX - rocNx, iY- rocNy ) ) ;
}// end for iX
} // end for iY
}// end for iRoc
gStyle -> SetPalette ( 51 ) ;
moduleHist -> SetStats ( false ) ;
return moduleHist ;
}//end TH2F moduleHisto
TH2F *canvasHisto ( TString arm, int sectionMod = 1, int ladder = 1, bool pixel = false, bool caldel = false ) {
TString module = moduleName( arm, sectionMod, ladder ) ;
TH2F *moduleHist = new TH2F ( module, module, 8*52, 0-0.5, 8*52-0.5, 2*80, 0-0.5, 2*80-0.5 ) ;
for ( int iRoc = 0; iRoc < 16; iRoc++ ) {
TString directory = dirName ( iRoc, arm, sectionMod, ladder, pixel, caldel ) ;
TCanvas *moduleCanvas = new TCanvas ( ) ;
TCanvas *c = (TCanvas*)gDirectory -> Get ( directory ) ;
if ( c==NULL ) {
std::cout << "Can not access canvas! " << directory.Data() << std::endl ;
return NULL ;
}
c->GetListOfPrimitives()->Print();
TString roc = rocName ( arm, sectionMod, ladder ) ;
//TH2 *h = (TH2*)c -> GetPrimitive ( roc ) ;
//TH2F *h = (TH2F*)c -> GetPrimitive ( "BPix_BpO_SEC1_LYR3_LDR1F_MOD1_ROC0" ) ;
//TH1 *h = (TH1*)c -> GetPrimitive ( "BPix_BpO_SEC1_LYR3_LDR1F_MOD1_ROC0" ) ;
TH1 *h = (TH1*)c -> GetPrimitive ( roc ) ;
//h->Draw();
if ( h==NULL ) {
std::cout << "Can not access histogram! " << roc.Data() << std::endl ;
return NULL ;
}
int rocX = h -> GetNbinsX ( ) ;
int rocY = h -> GetNbinsY ( ) ;
int rocNx = iRoc * rocX, rocNy = 0 ;
if ( iRoc >= 8 ) {
rocNy = 80 ;
rocNx = 8 * rocX - ((iRoc-7) * rocX) ; // To fix the roc positions
}// end if iRoc
for ( int iX = 0 + rocNx; iX < rocX + rocNx ; iX++ ) {
for ( int iY = 0 + rocNy; iY < rocY + rocNy ; iY++ ) {
//moduleHist -> SetBinContent ( iX, iY, h->GetBinContent( iX - rocNx, iY- rocNy ) ) ;
}// end for iX
} // end for iY
}// end for iRoc
//gStyle -> SetPalette ( 51 ) ;
moduleHist -> SetStats ( false ) ;
return moduleHist ;
}//end TCanvas moduleCanvas ( )
void easyAnalysis ( int runNumber = 100026 ) {
bool pixelAlive = false ;
bool calDelCalibration = true ;
TCanvas *canvasUpstream = new TCanvas();
canvasUpstream -> Divide ( 4, 2 ) ;
TFile *file = new TFile ( fileName( runNumber, pixelAlive, calDelCalibration ) ) ;
if ( !file -> IsOpen ( ) ) {
std::cout << "Input file can't be opened! " << fileName ( runNumber, pixelAlive, calDelCalibration ) << std::endl ;
return ;
}
for ( int iLadder = 1; iLadder < 5; iLadder++ ) {
if ( pixelAlive ) {
TH2F *hist1 = moduleHisto ( "BpO", 1, iLadder, pixelAlive, calDelCalibration ) ;
if ( hist1==NULL ) continue ;
canvasUpstream -> cd ( iLadder ) ;
hist1 -> Draw ( "COLZ" ) ;
TH2F *hist4 = moduleHisto ( "BpO", 4, iLadder, pixelAlive, calDelCalibration ) ;
if ( hist4==NULL ) continue ;
canvasUpstream -> cd ( iLadder + 4 ) ;
hist4 -> Draw ( "COLZ" ) ;
} else if ( calDelCalibration ) {
TH2F *hist1 = canvasHisto ( "BpO", 1, iLadder, pixelAlive, calDelCalibration ) ;
if ( hist1==NULL ) return ;
canvasUpstream -> cd ( iLadder ) ;
hist1 -> Draw ( "COLZ" ) ;
TH2F *hist4 = moduleHisto ( "BpO", 4, iLadder, pixelAlive, calDelCalibration ) ;
if ( hist4==NULL ) return ;
canvasUpstream -> cd ( iLadder + 4 ) ;
hist4 -> Draw ( "COLZ" ) ;
}//end if pixelAlive/calDelCalibration
}//end for iLadder
/* TCanvas *canvasDownstream = new TCanvas();
canvasDownstream -> Divide ( 4, 2 ) ;
for ( int iLadder = 1; iLadder < 5; iLadder++ ) {
if ( pixelAlive ) {
TH2F *hist1 = moduleHisto ( "BmO", 1, iLadder, pixelAlive, calDelCalibration ) ;
if ( hist1==NULL ) continue ;
canvasDownstream -> cd ( iLadder ) ;
hist1 -> Draw ( "COLZ" ) ;
TH2F *hist4 = moduleHisto ( "BmO", 4, iLadder, pixelAlive, calDelCalibration ) ;
if ( hist4==NULL ) continue ;
canvasDownstream -> cd ( iLadder + 4 ) ;
hist4 -> Draw ( "COLZ" ) ;
} else if ( calDelCalibration ) {
TH2F *hist1 = canvasHisto ( "BmO", 1, iLadder, pixelAlive, calDelCalibration ) ;
if ( hist1==NULL ) return ;
canvasUpstream -> cd ( iLadder ) ;
hist1 -> Draw ( "COLZ" ) ;
TH2F *hist4 = moduleHisto ( "BmO", 4, iLadder, pixelAlive, calDelCalibration ) ;
if ( hist4==NULL ) return ;
canvasUpstream -> cd ( iLadder + 4 ) ;
hist4 -> Draw ( "COLZ" ) ;
}//end if pixelAlive/calDelCalibration
}//end for iLadder
*/
}//end void accessHistos ( )
_ROOT Version: 6.12/06
_Platform: CentOS7
Compiler: Not Provided