Problem getting mean and sigma of the TH2F object created by calling tntuple->Draw()

hi, guys
I used a tntuple object to store data of charges (X,Y,Z,Q) and drawn a 2-D distribution of X vs Y.
but when I tried to get the Mean and the StdDev of the distribution, I got all “0” values and I dont know why.
my code is as follows:

    gROOT->cd();
    TNtuple* npc=new TNtuple("n","n","x:y:z:charge");
    npc->SetMarkerStyle(20);
    npc->SetMarkerSize(0.1);
    unsigned long pc_total_charge_count=0;
    for(int i=0;i<10;i++)//PropagatedCharge_tree->GetEntries();++i)
    {
        PropagatedCharge_tree->GetEntry(i);
        float x,y,z;                      //local position
        unsigned long q;                  //charge
        unsigned long pc_event_charge_count=0;
        for(auto& propagated_charge:propagated_charges)
        {
                x=propagated_charge->getLocalPosition().X();
                y=propagated_charge->getLocalPosition().Y();
                z=propagated_charge->getLocalPosition().Z();
                q=propagated_charge->getCharge();
                npc->Fill(x*1000.0,y*1000.0,z*1000.0,q);//convert to um
                pc_total_charge_count+=q;
                pc_event_charge_count+=q;
        }
        cpc_2d->cd();
        npc->Draw("y:x");
        TH2F* htemp=(TH2F*)gPad->GetPrimitive("htemp");
        htemp->GetXaxis()->SetLimits(-1000,1000);
        std::cout<<"sigma x:"<<htemp->GetStdDev(1)<<" sigma y:"<<htemp->GetStdDev(2)<<"mean :"<<htemp->GetMean(1)<<htemp->GetMean(2)<<htemp->GetMean(3)<<std::endl;
    }

Can someone help me with this? Thanks~
data_gene_simple_1_1s.root (56.9 KB) readpc2d.C (4.4 KB)

Please provide the minimal code (and data) to be able to understand what the problem is…

I just uploaded my code and data. Thanks for reminding.
:grinning:

OK, I can’t use your code, since I don’t have allpix-squared, but this simple test properly generate a 2D plot…

C:\Users\bellenot\rootdev\forum\thuwang-xz18>root -l
root [0] auto f = TFile::Open("data_gene_simple_1_1s.root", "READ");
Warning in <TClass::Init>: no dictionary for class allpix::SensorCharge is available
Warning in <TClass::Init>: no dictionary for class allpix::Object is available
Warning in <TClass::Init>: no dictionary for class allpix::PropagatedCharge is available
root [1] TTree *t = 0;
root [2] f->GetObject("PropagatedCharge", t);
root [3] t->Draw("detector1.local_position_.fCoordinates.fY:detector1.local_position_.fCoordinates.fX", "", "COLZ");
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [4]

You could also try to use ACLiC (.x readpc2d.C++) and debug your code to see where it fails…

Cheers, Bertrand.