Seg fault when accessing std::vector elements in event loop

Hi everyone,

This is my first post so if I’m doing something wrong or breaking site convention please let me know. I am hoping someone can help me solve this error that I’m getting. I am using beginInputFile() to access variables and then I want to fill histograms in execute(). I get a segmentation violation when I try to run this code (for now not even filling histograms, just trying to print out the value of the vector). The code I am posting below is a very condensed version but I think I included everything relevant. The error occurs when I try to print out the value of the pt vector.

#include <AsgTools/MessageCheck.h>
#include <MyAnalysis/Project1.h>
#include <xAODEventInfo/EventInfo.h>
#include <xAODJet/JetContainer.h>
#include <TSystem.h>
#include <PATInterfaces/CorrectionCode.h> // to check the return correction code status of tools                                                  
#include <TApplication.h>
#include <vector>

#include <xAODCore/ShallowAuxContainer.h>
#include <xAODCore/ShallowCopy.h>
#include <xAODCore/AuxContainerBase.h>
#include <JetCalibTools/IJetCalibrationTool.h>

Project1 :: Project1 (const std::string& name,
                                  ISvcLocator *pSvcLocator)
  : EL::AnaAlgorithm (name, pSvcLocator)                                                                                      
{
}


StatusCode Project1 :: beginInputFile ()                                                                                           
{
TTree* fChain = wk()->tree();

fChain->SetBranchAddress("tvar_event_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_njets", &tvar_event_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_njets);
  fChain->SetBranchAddress("tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt", &tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt);

  return StatusCode::SUCCESS;
}

StatusCode Project1 :: initialize ()

{
 ANA_MSG_INFO ("in initialize");
 return StatusCode::SUCCESS;
}

StatusCode Project1 :: execute ()
{
  ANA_MSG_INFO ("in execute");

wk()->tree()->GetEntry (wk()->treeEntry());

ANA_MSG_INFO ("pt value: " << tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt->at(0)); //here I try to print out the first value of this vector

 return StatusCode::SUCCESS;
}

StatusCode Project1 :: finalize ()
{                                                                                                                             
  ANA_MSG_INFO ("in finalize");

  return StatusCode::SUCCESS;
}

I initialize the variables in my header file as:

Int_t           tvar_event_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_njets;
std::vector<float>   *tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt;

and the functions:

 virtual StatusCode beginInputFile () override;                                                                                           
  virtual StatusCode initialize () override;
  virtual StatusCode execute () override;
  virtual StatusCode finalize () override;

both in the public in my class in the header file.

The stack trace from the segmentation violation is:

#5 0x00007f68a5481a7d in Project1::execute (this=0xfe3c060) at /afs/cern.ch/user/b/blubis/ROOTAnalysisTutorial/source/MyAnalysis/Root/Project1.cxx:341
#6 0x00007f68a6d8e422 in EL::AnaAlgorithm::sysExecute() () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBase/21.2.48/InstallArea/x86_64-slc6-gcc62-opt/lib/libAnaAlgorithmLib.so
#7 0x00007f68a6dea19b in EL::AnaAlgorithmWrapper::execute() () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBase/21.2.48/InstallArea/x86_64-slc6-gcc62-opt/lib/libEventLoop.so
#8 0x00007f68a6e1308a in EL::Worker::algsExecute() () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBase/21.2.48/InstallArea/x86_64-slc6-gcc62-opt/lib/libEventLoop.so
#9 0x00007f68a6dfa7e2 in EL::DirectWorker::run() () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBase/21.2.48/InstallArea/x86_64-slc6-gcc62-opt/lib/libEventLoop.so
#10 0x00007f68a6df9f86 in EL::DirectDriver::doSubmit(EL::Job const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) const () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBase/21.2.48/InstallArea/x86_64-slc6-gcc62-opt/lib/libEventLoop.so
#11 0x00007f68a6dfcad4 in EL::Driver::submitOnly(EL::Job const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) const () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBase/21.2.48/InstallArea/x86_64-slc6-gcc62-opt/lib/libEventLoop.so
#12 0x00007f68a6dfd1b9 in EL::Driver::submit(EL::Job const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) const () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBase/21.2.48/InstallArea/x86_64-slc6-gcc62-opt/lib/libEventLoop.so
#13 0x00007f68bafd3551 in ?? ()
#14 0x0000000005abef90 in ?? ()
#15 0x0000000000000088 in ?? ()
#16 0x0000000005a617b0 in ?? ()
#17 0x0000000f00000001 in ?? ()
#18 0x00007f68bafd3110 in ?? ()
#19 0x0000000001e84830 in ?? ()
#20 0x0000000005a61828 in ?? ()
#21 0x00007f68bafd3110 in ?? ()
#22 0x0000000005b3aaf0 in ?? ()
#23 0x0000000000000015 in ?? ()
#24 0x0000000001990410 in ?? ()
#25 0x00007f68b5993675 in llvm::RuntimeDyldImpl::resolveExternalSymbols() () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBaseExternals/21.2.48/InstallArea/x86_64-slc6-gcc62-opt/lib/libCling.so

There are several things that are weirding me out about this, but the number one thing is that the code posted above worked for a hot second, once, then reverted right back to throwing up the same error (nothing changed, I swear). I’ve also got a segmentation violation, immediately ran the code again, got an error like

terminate called after throwing an instance of ‘std::out_of_range’
what(): vector::_M_range_check: __n (which is 2) >= this->size() (which is 0)

then ran again, and back to segmentation violation. So, from the time that it ran correctly and printed out the pt values, I’m pretty sure the vectors are storing the information, but now it’s like I can’t access the info and nothing is stored in the vector since according to the second error the size is 0. So obviously something is wrong with how I’m trying to access the data.

So bottom line, I’m very confused. Any help would be appreciated!

Thanks,
Billie


ROOT Version: 5.32/03
Platform: CentOS7
Compiler: gcc version 6.2.0


At least:

std::vector<float>   *tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt = 0;

Done, thanks! Still doesn’t quite fix the error that I’m having.

Try:

if (tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt && (tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt->size() > 0)) {
  ANA_MSG_INFO ("pt value: " << tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt->at(0)); //here I try to print out the first value of this vector
}

Thanks! Tried that.

I wrote

if (tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt && (tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt->size() > 0)) {
  ANA_MSG_INFO ("pt value: " << tvar_jet_AntiKt10OrigLCTopoTrimmedPtFrac5SmallR20_pt->at(0)); //here I try to print out the first value of this vector
}

else
{
ANA_MSG_INFO ("not printing");
}

The code compiled and ran and I get an output of “pt value: ###”, where the number was like 5.6 something, but it was the same number over and over again for every event which shouldn’t be happening. Then, didn’t change anything in the code (didn’t even go in to look at it), ran again, and segmentation violation again. Opened the cxx file, looked at it, changed absolutely nothing, recompiled, and now it’s running and printing out “not printing”. So looks like it met the condition to print out the pt, then couldn’t even run, then didn’t meet the condition, all without me changing anything in the code.

And then, just for fun, tried running it again -> segmentation violation again. Recompiled, ran again -> segmentation violation. Open cxx file, scroll through, save without changing anything, recompile, run -> Outputs (“not printing”), so it’s running but not meeting the condition of the if statement. Run again directly after this without recompiling -> Outputs (“not printing”) again. Run again directly after this WITH recompiling -> Outputs (“not printing”) again.

When it was outputting “not printing”, for the first two times I let it run all the way through the files, then the last time I ctrl+C interrupted it during. Then immediately tried running again -> segmentation violation.

Check the structure of your tree (branches) using something like: wk()->tree()>Print();

If you don’t know how to deal with your tree, see how various flavours of automatically generated “analysis skeletons” deal with it.

Also, the backtrace shows Project1.cxx:341. What’s happening at that line? (You might need to look at the current backtrace to get the current line number, if you changed that file since.)

Hi Billie,

it’s much more useful to discuss these ATLAS-specific matters here:
https://groups.cern.ch/group/hn-atlas-PATHelp/Lists/Archive/100.aspx
(just send an email to the email address you’ll see at the bottom of the left panel)

Having said that, I think you are confusing the way ATLAS analyzes its data. They analyze either xAODs or ntuples. You obviously took the framework for the xAOD analysis but you actually use it to do the analysis in the ntuple way. See this tutorial


Basically, we don’t need these

TTree* fChain = wk()->tree();

to do the analysis, and we definitely don’t need the

wk()->tree()->GetEntry (wk()->treeEntry());

All this is already done for you behind the scenes.

Finally, the 21.2.48 AnalysisBase release is 8 months old, you are 30 releases behind. Can you try the most recent one, the 21.2.78? See
https://twiki.cern.ch/twiki/bin/viewauth/AtlasProtected/AnalysisBaseReleaseNotes21_2
for the full list.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.