# \*\*\* Break \*\*\* segmentation violation while running macro

**URL:** <https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541>\
**Category:** Newbie\
**Created:** [August 29, 2021, 1:14pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541 "2021-08-29T13:14:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aya\_Beshr](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aya_beshr/32/27192_2.png) [@Aya\_Beshr](https://root-forum.cern.ch/u/Aya_Beshr)\
**Post date:** [August 29, 2021, 1:14pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/1 "2021-08-29T13:14:16Z")

</div>

Hi,

I am trying to run a macro test.C at first it compiled and run without any issues. When I add some piece of code from lines 616-750 and lines 798-809, it crashes with segmentation violation. I commented the lines above so returning back to original well-running code, but I get the same error don’t know why?  
I uploaded the macro test.C, the header test.h and log file with error message.

I run macro as:

```auto
gSystem->Load("libDelphes");
.L test.C
test m;
m.Loop();

```

More info:  
macro is created using MakeClass. It run over Drell-Yan sample generated locally by Madgraph5 and simulated with Delphes.

* * *

_ROOT Version:_ root-6.18.04  
_Platform:_ Ubuntu18.04  
_Delphes version:_ Delphes-3.4.2  
_Madgraph version:_ MG5\_aMC\_2.7.3

* * *

Thanks for help in advance,  
Aya

[test.C](https://root-forum.cern.ch/uploads/short-url/mVnnt7br1m2XVbhA7Z9Mu2DVXx3.C) (26.9 KB)  
[test.h](https://root-forum.cern.ch/uploads/short-url/hby042Jx9LGMEMuoVcX61ePnxwn.h) (69.8 KB)  
[log.txt](https://root-forum.cern.ch/uploads/short-url/PnBuOKB8WQGxkERuaXs9p3zpcq.txt) (43.8 KB)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 30, 2021, 8:15am UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/2 "2021-08-30T08:15:48Z")

</div>

Hi @Aya_Beshr ,  
I can’t tell what the cause of the crash is by reading the code (it has to do with creation of TFiles and/or writing objects to those TFiles).

Can you reduce the amount of code in the reproducer and/or make it so we can run it ourselves, so we can debug it (e.g. by providing mock input files)?

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Aya\_Beshr](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aya_beshr/32/27192_2.png) [@Aya\_Beshr](https://root-forum.cern.ch/u/Aya_Beshr)\
**Post date:** [August 30, 2021, 2:22pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/3 "2021-08-30T14:22:10Z")

</div>

Hi @eguiraud

I have reduced the code and also produced a very small DrellYan sample with only 10 events (for usage as input for testing). I have uploaded both.

[DemoAnalyzer.C](https://root-forum.cern.ch/uploads/short-url/iUbWAhQN41yqbqX2kEbgQYv8rxm.C) (5.2 KB)  
[DrellYan.root](https://root-forum.cern.ch/uploads/short-url/bITru3wN1bTTEpVzuTxGoXFn15b.root) (705.3 KB)

Thanks,  
Aya

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 30, 2021, 2:59pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/4 "2021-08-30T14:59:14Z")

</div>

Thanks @Aya_Beshr ,  
can you please upload DemoAnalyzer.h too?

---

<div class="post-metadata">

**Author:** ![Aya\_Beshr](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aya_beshr/32/27192_2.png) [@Aya\_Beshr](https://root-forum.cern.ch/u/Aya_Beshr)\
**Post date:** [August 30, 2021, 3:07pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/5 "2021-08-30T15:07:57Z")

</div>

Yah sure!  
[DemoAnalyzer.h](https://root-forum.cern.ch/uploads/short-url/kKAA5H6FDJuDF3bXILWOpKn3lkx.h) (69.9 KB)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 30, 2021, 3:10pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/6 "2021-08-30T15:10:27Z")

</div>

Ah I also need Delphes to run this…uhm it will take a while 😅

---

<div class="post-metadata">

**Author:** ![Aya\_Beshr](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aya_beshr/32/27192_2.png) [@Aya\_Beshr](https://root-forum.cern.ch/u/Aya_Beshr)\
**Post date:** [August 30, 2021, 3:17pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/7 "2021-08-30T15:17:32Z")

</div>

Sorry for that, you might not need to.

Actually, I have just run the original code on 10 events only. I found that there is a warning giving at the beginning:

```auto
Warning in <TFile::Init>: file /home/aya/Desktop/Pheno_Work/analysis/Results/output_demo.root probably not closed, cannot read free segments
Warning in <TFile::Init>: file /home/aya/Desktop/Pheno_Work/analysis/Results/output_demo.root has no keys

```

There is problem with closing the file, it may be the cause for segmentation breaking.

I defined it as:

```auto
TFile *op_file = new TFile("/home/aya/Desktop/Pheno_Work/analysis/Results/output_demo.root", "UPDATE");
op_file->Write();
op_file->Close();

```

Could you kindly clarify what is the key for a file? coz the problem is solved and code ran without issues when I replace `"UPDATE"` with `"RECREATE"`

Thanks,  
Aya

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 30, 2021, 3:21pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/8 "2021-08-30T15:21:37Z")

</div>

A key in a TFile is simply the “name of something in the file”.  
Most likely the error message means that you are re-opening a file without having closed it first.

I compiled delphes but still can’t run the code because of:

```auto
      TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("/media/aya/LinuxSpace/MyWork_Final_Samples/DY_BG_14TeV_SMfull_GEN/DY_BG_14TeV_SMfull_pythia8_CMSPhaseII-0PU_GEN-SIM.root");
      if (!f || !f->IsOpen()) {
         f = new TFile("/media/aya/LinuxSpace/MyWork_Final_Samples/DY_BG_14TeV_SMfull_GEN/DY_BG_14TeV_SMfull_pythia8_CMSPhaseII-0PU_GEN-SIM.root");

```

(I don’t have access to that file)

---

<div class="post-metadata">

**Author:** ![Aya\_Beshr](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aya_beshr/32/27192_2.png) [@Aya\_Beshr](https://root-forum.cern.ch/u/Aya_Beshr)\
**Post date:** [August 30, 2021, 3:25pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/9 "2021-08-30T15:25:45Z")

</div>

This file is on my local laptop, so you can kindly download the root file I just uploaded instead and define a path for it something like,

```auto
TFile *input_file = new TFile("/path/to/DrellYan.root", "READ");

```

or with any other way that you kindly prefer.

---

<div class="post-metadata">

**Author:** ![Aya\_Beshr](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aya_beshr/32/27192_2.png) [@Aya\_Beshr](https://root-forum.cern.ch/u/Aya_Beshr)\
**Post date:** [August 30, 2021, 3:27pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/10 "2021-08-30T15:27:52Z")

</div>

Sorry, I have changed the permission access for files, here it is  
[DrellYan.root](https://root-forum.cern.ch/uploads/short-url/bITru3wN1bTTEpVzuTxGoXFn15b.root) (705.3 KB)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 31, 2021, 8:25am UTC](https://root-forum.cern.ch/t/break-segmentation-violation-while-running-macro/46541/11 "2021-08-31T08:25:13Z")

</div>

Hi,  
after changing those filenames in `DemoAnalyzer.h` and `DemoAnalyzer.C` to opening the file `DrellYan.root` that you shared, things look fine on my side (i.e. I can’t reproduce the crash). This is what I run:

```auto
gSystem->Load("libDelphes")
.L DemoAnalyzer.C
DemoAnalyzer m
m.Loop()

```

and this is what I get:

```auto
root [1] DemoAnalyzer m
Warning in <TStreamerInfo::BuildCheck>:
   The StreamerInfo of class GenParticle read from file DrellYan.root
   has the same version (=2) as the active class but a different checksum.
   You should update the version to ClassDef(GenParticle,3).
   Do not try to write objects with the current class definition,
   the files will not be readable.

Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 2 of class 'GenParticle' is missing from
the on-file layout version 2:
   float CtgTheta; //
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 2 of class 'GenParticle' is missing from
the on-file layout version 2:
   float D0; //
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 2 of class 'GenParticle' is missing from
the on-file layout version 2:
   float DZ; //
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 2 of class 'GenParticle' is missing from
the on-file layout version 2:
   float T; //number
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 2 of class 'GenParticle' is missing from
the on-file layout version 2:
   float X; //number
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 2 of class 'GenParticle' is missing from
the on-file layout version 2:
   float Y; //number
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 2 of class 'GenParticle' is missing from
the on-file layout version 2:
   float Z; //number
(DemoAnalyzer &) @0x7fc284bd1000
root [2] m.Loop()
******START EVENT LOOP!****** , Event nb = 0
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 1
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 2
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 3
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 4
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 5
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 6
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 7
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 8
start loop overall loose muons
end loop overall loose muons
******START EVENT LOOP!****** , Event nb = 9
start loop overall loose muons
end loop overall loose muons
Loop Ends!
Writing Histograms!
Writing Histograms ends!
write output file ends!
ROOT file: 0x5602df70a1e0has been created sucessfully!

```

The warnings in TStreamerInfo could be problematic if you do I/O of `GenParticle`, but they might also be due to me using a ROOT version that’s not compatible with the latest Delphes version, I don’t know.

If you are still experiencing a crash, or if there are still warnings or errors that you want to clarify, please share all code and files that are needed to reproduce them.

Cheers,  
Enrico
