Can not get an object from a Key

Hi,
I am trying to loop over all histograms that I created to scale them, but I receive a segmentation fault! I would be so glad if you can help me to resove this problem since I do not want to scale them one-by-one!

this is the code I use

 auto dir = gDirectory;
    TList *lis = dir->GetList();
    TIter next(lis);
    TKey *key;
    while ((key = (TKey*) next())) {
        if (key->InheritsFrom("TH1")) {
            auto h = (TH1D*) key->ReadObj();
            cout << h->GetName() << endl;
        }
    }

and here is the error.

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x0000003732eac65e in waitpid () from /lib64/libc.so.6
#1  0x0000003732e3e609 in do_system () from /lib64/libc.so.6
#2  0x00007fdbda147f5a in TUnixSystem::StackTrace() () from /afs/cern.ch/sw/lcg/app/releases/ROOT/6.05.02/x86_64-slc6-gcc49-opt/root/lib/libCore.so
#3  0x00007fdbda149f2c in TUnixSystem::DispatchSignals(ESignals) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/6.05.02/x86_64-slc6-gcc49-opt/root/lib/libCore.so
#4  <signal handler called>
#5  0x0000003732f336bf in __strlen_sse42 () from /lib64/libc.so.6
#6  0x00007fdbda04ad45 in TString::TString(char const*) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/6.05.02/x86_64-slc6-gcc49-opt/root/lib/libCore.so
#7  0x00007fdbd9335342 in TH1::Chi2Test(TH1 const*, char const*, double*) const () from /afs/cern.ch/sw/lcg/app/releases/ROOT/6.05.02/x86_64-slc6-gcc49-opt/root/lib/libHist.so
#8  0x0000000000458ad3 in StudyOneLeptonChannel::finalize() ()
#9  0x0000000000439236 in NtupleAnalysis::Loop(long long, char const*, char const*) ()
#10 0x000000000042b01f in Driver(char const*, long long, char const*) ()
#11 0x000000000042c94a in main ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
[root.cern.ch/bugs](http://root.cern.ch/bugs). Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x0000003732f336bf in __strlen_sse42 () from /lib64/libc.so.6
#6  0x00007fdbda04ad45 in TString::TString(char const*) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/6.05.02/x86_64-slc6-gcc49-opt/root/lib/libCore.so
#7  0x00007fdbd9335342 in TH1::Chi2Test(TH1 const*, char const*, double*) const () from /afs/cern.ch/sw/lcg/app/releases/ROOT/6.05.02/x86_64-slc6-gcc49-opt/root/lib/libHist.so
#8  0x0000000000458ad3 in StudyOneLeptonChannel::finalize() ()
#9  0x0000000000439236 in NtupleAnalysis::Loop(long long, char const*, char const*) ()
#10 0x000000000042b01f in Driver(char const*, long long, char const*) ()
#11 0x000000000042c94a in main ()
===========================================================

Hi,

you should leave keys aside since you are not working on a file:

   auto dir = gDirectory;
   auto lis = dir->GetList();
   for (auto obj : *lis) {
      if (obj->InheritsFrom("TH1")) {
         auto h = (TH1 *) obj;
         cout << h->GetName() << endl;
      }
   }

Also be careful with the cast to TH1D, unless you know that all histograms are TH1Ds.

Cheers,
D

thanks a lot, it works fine