cpp code problem in the ma5 expert mode

Asked by Shyuan Guo

Hi, Ma5 team:

I am a newbie in the expert mode. Here's the problem i've encountered:
In my collider simulation, a new particle decays into 2leptons plus 2 jets, since the lepton could be originated from other particles thus i want to reconstruct the invariant mass in the expert mode. The input file is the lhco type file, and my strategy is that firstly find out the two leptons which stand nextest to each other and then the jets located next to these two leptons, finally reconstruct mass of these four particles. I've wrote my cpp code and it could pass the compiling, but when run the madanalysisjob the program crashed and give the following information:
**********************************************************************************************
        => file size: 1.14 Mo
        => sample format: LHCO file produced by an unknown generator (cross section assumed in pb).
        => progress: [> ]
 *** Break *** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0 0x00007fb46230fa0c in __libc_waitpid (pid=2956, stat_loc=stat_loc
entry=0x7ffcbc02d240, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:31
#1 0x00007fb462295232 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2 0x00007fb463339243 in TUnixSystem::StackTrace() () from /home/physics/root/lib/libCore.so
#3 0x00007fb46333af4c in TUnixSystem::DispatchSignals(ESignals) () from /home/physics/root/lib/libCore.so
#4 <signal handler called>
#5 0x000000000040d6c4 in MA5::sst::Execute(MA5::SampleFormat&, MA5::EventFormat const&) ()
#6 0x0000000000407838 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
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 0x000000000040d6c4 in MA5::sst::Execute(MA5::SampleFormat&, MA5::EventFormat const&) ()
#6 0x0000000000407838 in main ()
===========================================================
**************************************************************************************************

Here's my cpp code:
***********************************************************************************************
#include "SampleAnalyzer/User/Analyzer/sst.h"
using namespace MA5;
using namespace std;

// -----------------------------------------------------------------------------
// Initialize
// function called one time at the beginning of the analysis
// -----------------------------------------------------------------------------
bool sst::Initialize(const MA5::Configuration& cfg, const std::map<std::string,std::string>& parameters)
{
  // Initializing PhysicsService for MC
  PHYSICS->mcConfig().Reset();

 // Initializing PhysicsService for RECO
  PHYSICS->recConfig().Reset();
 PHYSICS->recConfig().UseDeltaRIsolation(0.5);

 // Initializing the histogram
 myHisto = plots_.Add_Histo("mhprec",50,0.0,700.0);

  cout << "END Initialization" << endl;

  return true;
}

// -----------------------------------------------------------------------------
// Execute
// function called each time one event is read
// -----------------------------------------------------------------------------
bool sst::Execute(SampleFormat& sample, const EventFormat& event)
{

 // Normalization of the histogram: L = 100 fb-1
 double nrm = 0.0001198 * 100000. / 10000.;
 std::vector<const RecLeptonFormat*> Leptons;
 std::vector<const RecJetFormat*> Jats;
 std::vector<const RecLeptonFormat*> MyLeptons;
 std::vector<const RecJetFormat*> MyJets;
 const RecJetFormat* clstJats1;
 const RecJetFormat* clstJats2;

  if (event.rec()!=0)
  {
 // Collect the electrons into Leptons
    for (unsigned int i=0;i<event.rec()->electrons().size();i++)
    {
      const RecLeptonFormat *elec = &(event.rec()->electrons()[i]);
      if (elec->pt()>10)
   Leptons.push_back(elec);
    }

    // Collect the muons into Leptons
    for (unsigned int i=0;i<event.rec()->muons().size();i++)
    {
      const RecLeptonFormat *mu = &(event.rec()->muons()[i]);
      if (mu->pt()>10)
   Leptons.push_back(mu);
    }
  // Collect the jets into Jats
    for (unsigned int i=0;i<event.rec()->jets().size();i++)
    {
      const RecJetFormat *jts = &(event.rec()->jets()[i]);
      if (jts->pt()>20)
   Jats.push_back(jts);
    }

  // Requiring the same-sign trilepton and four jets
  if (Leptons.size()==3)
  if (Leptons[0]->charge()*Leptons[1]->charge()>0 && Leptons[0]->charge()*Leptons[2]->charge()>0)
    if (Jats.size()==4)

    // Requiring two of the three leptons located in a relatively narrow cone, and find out the two jets next to them

  if (Leptons[0]->dr(Leptons[1])<Leptons[0]->dr(Leptons[2]))
   {if (Leptons[0]->dr(Leptons[1])<Leptons[1]->dr(Leptons[2]))
   {MyLeptons.push_back(Leptons[0]);
   MyLeptons.push_back(Leptons[1]);}
   else
   {
   MyLeptons.push_back(Leptons[1]);
   MyLeptons.push_back(Leptons[2]);
   }
   }
  else if (Leptons[0]->dr(Leptons[2])<Leptons[1]->dr(Leptons[2]))
   {MyLeptons.push_back(Leptons[0]);
   MyLeptons.push_back(Leptons[2]);}
   else
   {
   MyLeptons.push_back(Leptons[1]);
   MyLeptons.push_back(Leptons[2]);
   }
  clstJats1 = Jats[0];
  clstJats2 = Jats[0];
  for (unsigned int i=0;i<Jats.size();i++)
   {if (MyLeptons[0]->dr(Jats[i])<MyLeptons[0]->dr(clstJats1))
    clstJats1 = Jats[i];
    if (MyLeptons[1]->dr(Jats[i])<MyLeptons[1]->dr(clstJats2))
    clstJats2 = Jats[i];
   }
  MyJets.push_back(clstJats1);
  MyJets.push_back(clstJats2);

    // Next we should plot the invariant mass of the charged Higgs through MyLeptons and MyJets
  {TLorentzVector hp = MyLeptons[0]->momentum() + MyLeptons[1]->momentum() + MyJets[0]->momentum() + MyJets[1]->momentum();
 myHisto->IncrementNEvents(nrm);
 {myHisto->Fill(hp.M(),nrm);}}
  }
  return true;
}

// -----------------------------------------------------------------------------
// Finalize
// function called one time at the end of the analysis
// -----------------------------------------------------------------------------
void sst::Finalize(const SampleFormat& summary, const std::vector<SampleFormat>& files)
{
  // Saving histogram
  *out().GetStream() << "<Selection>\n";

  plots_.Write_TextFormat(out());

  // Finalizing cuts and histos
  plots_.Finalize();
}
**********************************************************************************************

Anything wrong with this code?
Thanks!

Question information

Language:
English Edit question
Status:
Solved
For:
MadAnalysis 5 Edit question
Assignee:
No assignee Edit question
Solved by:
Benjamin Fuks
Solved:
Last query:
Last reply:
Revision history for this message
Best Benjamin Fuks (fuks) said :
#1

Hi Shyuan,

It seems that you ar etrying to access some elements of a vector that are not existing. Some of your methods indeed refer to the first/second element of the jet/lepton collection without double checking whether the element exists.

Regards,

Benjamin

On 12 Nov 2016, at 03:52 , Shyuan Guo <email address hidden> wrote:

> New question #404007 on MadAnalysis 5:
> https://answers.launchpad.net/madanalysis5/+question/404007
>
> Hi, Ma5 team:
>
> I am a newbie in the expert mode. Here's the problem i've encountered:
> In my collider simulation, a new particle decays into 2leptons plus 2 jets, since the lepton could be originated from other particles thus i want to reconstruct the invariant mass in the expert mode. The input file is the lhco type file, and my strategy is that firstly find out the two leptons which stand nextest to each other and then the jets located next to these two leptons, finally reconstruct mass of these four particles. I've wrote my cpp code and it could pass the compiling, but when run the madanalysisjob the program crashed and give the following information:
> **********************************************************************************************
> => file size: 1.14 Mo
> => sample format: LHCO file produced by an unknown generator (cross section assumed in pb).
> => progress: [> ]
> *** Break *** segmentation violation
>
>
>
> ===========================================================
> There was a crash.
> This is the entire stack trace of all threads:
> ===========================================================
> #0 0x00007fb46230fa0c in __libc_waitpid (pid=2956, stat_loc=stat_loc
> entry=0x7ffcbc02d240, options=options
> entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:31
> #1 0x00007fb462295232 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
> #2 0x00007fb463339243 in TUnixSystem::StackTrace() () from /home/physics/root/lib/libCore.so
> #3 0x00007fb46333af4c in TUnixSystem::DispatchSignals(ESignals) () from /home/physics/root/lib/libCore.so
> #4 <signal handler called>
> #5 0x000000000040d6c4 in MA5::sst::Execute(MA5::SampleFormat&, MA5::EventFormat const&) ()
> #6 0x0000000000407838 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
> 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 0x000000000040d6c4 in MA5::sst::Execute(MA5::SampleFormat&, MA5::EventFormat const&) ()
> #6 0x0000000000407838 in main ()
> ===========================================================
> **************************************************************************************************
>
>
> Here's my cpp code:
> ***********************************************************************************************
> #include "SampleAnalyzer/User/Analyzer/sst.h"
> using namespace MA5;
> using namespace std;
>
> // -----------------------------------------------------------------------------
> // Initialize
> // function called one time at the beginning of the analysis
> // -----------------------------------------------------------------------------
> bool sst::Initialize(const MA5::Configuration& cfg, const std::map<std::string,std::string>& parameters)
> {
> // Initializing PhysicsService for MC
> PHYSICS->mcConfig().Reset();
>
> // Initializing PhysicsService for RECO
> PHYSICS->recConfig().Reset();
> PHYSICS->recConfig().UseDeltaRIsolation(0.5);
>
> // Initializing the histogram
> myHisto = plots_.Add_Histo("mhprec",50,0.0,700.0);
>
> cout << "END Initialization" << endl;
>
> return true;
> }
>
>
> // -----------------------------------------------------------------------------
> // Execute
> // function called each time one event is read
> // -----------------------------------------------------------------------------
> bool sst::Execute(SampleFormat& sample, const EventFormat& event)
> {
>
> // Normalization of the histogram: L = 100 fb-1
> double nrm = 0.0001198 * 100000. / 10000.;
> std::vector<const RecLeptonFormat*> Leptons;
> std::vector<const RecJetFormat*> Jats;
> std::vector<const RecLeptonFormat*> MyLeptons;
> std::vector<const RecJetFormat*> MyJets;
> const RecJetFormat* clstJats1;
> const RecJetFormat* clstJats2;
>
>
> if (event.rec()!=0)
> {
> // Collect the electrons into Leptons
> for (unsigned int i=0;i<event.rec()->electrons().size();i++)
> {
> const RecLeptonFormat *elec = &(event.rec()->electrons()[i]);
> if (elec->pt()>10)
> Leptons.push_back(elec);
> }
>
> // Collect the muons into Leptons
> for (unsigned int i=0;i<event.rec()->muons().size();i++)
> {
> const RecLeptonFormat *mu = &(event.rec()->muons()[i]);
> if (mu->pt()>10)
> Leptons.push_back(mu);
> }
> // Collect the jets into Jats
> for (unsigned int i=0;i<event.rec()->jets().size();i++)
> {
> const RecJetFormat *jts = &(event.rec()->jets()[i]);
> if (jts->pt()>20)
> Jats.push_back(jts);
> }
>
> // Requiring the same-sign trilepton and four jets
> if (Leptons.size()==3)
> if (Leptons[0]->charge()*Leptons[1]->charge()>0 && Leptons[0]->charge()*Leptons[2]->charge()>0)
> if (Jats.size()==4)
>
> // Requiring two of the three leptons located in a relatively narrow cone, and find out the two jets next to them
>
> if (Leptons[0]->dr(Leptons[1])<Leptons[0]->dr(Leptons[2]))
> {if (Leptons[0]->dr(Leptons[1])<Leptons[1]->dr(Leptons[2]))
> {MyLeptons.push_back(Leptons[0]);
> MyLeptons.push_back(Leptons[1]);}
> else
> {
> MyLeptons.push_back(Leptons[1]);
> MyLeptons.push_back(Leptons[2]);
> }
> }
> else if (Leptons[0]->dr(Leptons[2])<Leptons[1]->dr(Leptons[2]))
> {MyLeptons.push_back(Leptons[0]);
> MyLeptons.push_back(Leptons[2]);}
> else
> {
> MyLeptons.push_back(Leptons[1]);
> MyLeptons.push_back(Leptons[2]);
> }
> clstJats1 = Jats[0];
> clstJats2 = Jats[0];
> for (unsigned int i=0;i<Jats.size();i++)
> {if (MyLeptons[0]->dr(Jats[i])<MyLeptons[0]->dr(clstJats1))
> clstJats1 = Jats[i];
> if (MyLeptons[1]->dr(Jats[i])<MyLeptons[1]->dr(clstJats2))
> clstJats2 = Jats[i];
> }
> MyJets.push_back(clstJats1);
> MyJets.push_back(clstJats2);
>
> // Next we should plot the invariant mass of the charged Higgs through MyLeptons and MyJets
> {TLorentzVector hp = MyLeptons[0]->momentum() + MyLeptons[1]->momentum() + MyJets[0]->momentum() + MyJets[1]->momentum();
> myHisto->IncrementNEvents(nrm);
> {myHisto->Fill(hp.M(),nrm);}}
> }
> return true;
> }
>
> // -----------------------------------------------------------------------------
> // Finalize
> // function called one time at the end of the analysis
> // -----------------------------------------------------------------------------
> void sst::Finalize(const SampleFormat& summary, const std::vector<SampleFormat>& files)
> {
> // Saving histogram
> *out().GetStream() << "<Selection>\n";
>
> plots_.Write_TextFormat(out());
>
> // Finalizing cuts and histos
> plots_.Finalize();
> }
> **********************************************************************************************
>
> Anything wrong with this code?
> Thanks!
>
>
>
>
>
> --
> You received this question notification because you are an answer
> contact for MadAnalysis 5.

Revision history for this message
Shyuan Guo (shyuanguo) said :
#2

Hi Benjamin,

Thank you for your reply, and you're right, i've missed some curly brackets after the if condition.
Now that problem have been solved, and i've get the right histo SAF file, i wonder if the ma5 could translate the histo SAF file into a histogram automatically, did i miss some order in the cpp code?
thanks a lot!

Cheers,

Shyuan

Revision history for this message
Benjamin Fuks (fuks) said :
#3

Hi Shyuan,

Unfortunately, this is on our to-do list but with a small priority due to manpower issue.

Cheers,

Benjamin

On 14 Nov 2016, at 15:58 , Shyuan Guo <email address hidden> wrote:

> Question #404007 on MadAnalysis 5 changed:
> https://answers.launchpad.net/madanalysis5/+question/404007
>
> Status: Answered => Open
>
> Shyuan Guo is still having a problem:
> Hi Benjamin,
>
> Thank you for your reply, and you're right, i've missed some curly brackets after the if condition.
> Now that problem have been solved, and i've get the right histo SAF file, i wonder if the ma5 could translate the histo SAF file into a histogram automatically, did i miss some order in the cpp code?
> thanks a lot!
>
> Cheers,
>
> Shyuan
>
> --
> You received this question notification because you are an answer
> contact for MadAnalysis 5.

Revision history for this message
Shyuan Guo (shyuanguo) said :
#4

OK, thank a lot!

Revision history for this message
Shyuan Guo (shyuanguo) said :
#5

Thanks Benjamin Fuks, that solved my question.