Difficulty with classes and pointers

I want to make an histogram of a particle that isn't defined in the classes files.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  
#include "SampleAnalyzer/User/Analyzer/user_1.h"
#include <iostream>

using namespace MA5;
using namespace std;
  
// -----------------------------------------------------------------------------
// Initialize
// function called one time at the beginning of the analysis
// -----------------------------------------------------------------------------

bool user_1::Initialize(const MA5::Configuration& cfg, const std::map<std::string,std::string>& parameters)
{
  cout << "BEGIN Initialization" << endl;
  // initialize variables, histos

  // Initializing PhysicsService for MC
  PHYSICS->mcConfig().Reset();

  // definition of the multiparticle "hadronic"
  PHYSICS->mcConfig().AddHadronicId(-5);
  PHYSICS->mcConfig().AddHadronicId(-4);
  PHYSICS->mcConfig().AddHadronicId(-3);
  PHYSICS->mcConfig().AddHadronicId(-2);
  PHYSICS->mcConfig().AddHadronicId(-1);
  PHYSICS->mcConfig().AddHadronicId(1);
  PHYSICS->mcConfig().AddHadronicId(2);
  PHYSICS->mcConfig().AddHadronicId(3);
  PHYSICS->mcConfig().AddHadronicId(4);
  PHYSICS->mcConfig().AddHadronicId(5);
  PHYSICS->mcConfig().AddHadronicId(21);

  // definition of the multiparticle "invisible"
  PHYSICS->mcConfig().AddInvisibleId(-16);
  PHYSICS->mcConfig().AddInvisibleId(-14);
  PHYSICS->mcConfig().AddInvisibleId(-12);
  PHYSICS->mcConfig().AddInvisibleId(12);
  PHYSICS->mcConfig().AddInvisibleId(14);
  PHYSICS->mcConfig().AddInvisibleId(16);
  PHYSICS->mcConfig().AddInvisibleId(1000022);

  // Initializing each selection item
  myHisto = new TH1F("mTTbar", "mTTbar", 100, 0.0, 1000.0);

  cout << "END   Initialization" << endl;
  return true;
}
void user_1::Finalize(const SampleFormat& summary, const std::vector<SampleFormat>& files)
{
  cout << "BEGIN Finalization" << endl;

 // Saving histogram

  TFile *myOutput = new TFile ("output.root","RECREATE");

  myOutput->cd();

  myHisto->Write();

  cout << "END   Finalization" << endl;
}

// -----------------------------------------------------------------------------
// Execute
// function called each time one event is read
// -----------------------------------------------------------------------------
bool user_1::Execute(SampleFormat& sample, const EventFormat& event)
{
  // ***************************************************************************
  // Example of analysis with generated particles
  // Concerned samples : LHE/STDHEP/HEPMC
  // ***************************************************************************
 
    cout << "---------------NEW EVENT-------------------" << endl;
      
      unsigned int n = event.mc()->particles().size();

      for (unsigned int i=0;i<n;i++)
      {
      const MCParticleFormat* prt = &event.mc()->particles()[i]; //prt é um objecto da classe MCParticleFormat
      
       if (const MCParticleFormat.pdgid() = 6000006) //corre se o pdgid for 6000006 
       {
       myHisto->Fill(prt->m());
       }
      }
     
return true
}

When I try "make" in Build directory it gives the following error:
1
2
3
4
5
6
7
8
9
SampleAnalyzer/User/Analyzer/user_1.cpp: In member function ‘virtual bool MA5::user_1::Execute(MA5::SampleFormat&, const MA5::EventFormat&)’:
SampleAnalyzer/User/Analyzer/user_1.cpp:108:12: error: expected primary-expression before ‘constif (const MCParticleFormat.pdgid() = 6000006) //corre se o pdgid for 6000006 
            ^
SampleAnalyzer/User/Analyzer/user_1.cpp:108:12: error: expected ‘)’ before ‘const’
SampleAnalyzer/User/Analyzer/user_1.cpp:115:1: error: expected ‘;’ before ‘}’ token
 }
 ^
make: *** [SampleAnalyzer/User/Analyzer/user_1.o] Error 1


The classes I am using are in the following url:
http://madanalysis.irmp.ucl.ac.be/doc/v1_0_0/annotated.html

I am trying to do an histogram of a particle with pdgid = 6000006, but I don't know what I should do.

Can anyone help?
Last edited on
¿why did you write const there?
= is assignment, == is comparison.

if (MCParticleFormat.pdgid() == 6000006)
Edit: just notice that `MCParticleFormat' is the name of a class
however `pdgid()' is a message that you would send to an object (¿what object do you want to operate on?)


> TFile *myOutput = new TFile ("output.root","RECREATE");
http://www.cplusplus.com/forum/general/138037/
TFile myOutput("output.root","RECREATE");
Last edited on
Thank you for the reply. I edited my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool user_1::Execute(SampleFormat& sample, const EventFormat& event) 
{
      unsigned int n = event.mc()->particles().size();
      
      for (unsigned int i=0;i<n;i++)
      {
      const MCParticleFormat* prt = &event.mc()->particles()[i];

      if ( prt==0 ) return false;
      if ( (prt->pdgid()!=6000006) ) return false; 

      if ( (prt->pdgid()==6000006) ) { myHisto->Fill(prt->m()) };
      }
     
return true
}


But now it gives this error:

1
2
3
4
5
6
7
8
SampleAnalyzer/User/Analyzer/user_1.cpp: In member function ‘virtual bool MA5::user_1::Execute(MA5::SampleFormat&, const MA5::EventFormat&)’:
SampleAnalyzer/User/Analyzer/user_1.cpp:111:64: error: expected ‘;’ before ‘}’ token
       if ( (prt->pdgid()==6000006) ) { myHisto->Fill(prt->m()) };
                                                                ^
SampleAnalyzer/User/Analyzer/user_1.cpp:115:1: error: expected ‘;’ before ‘}’ token
 }
 ^
make: *** [SampleAnalyzer/User/Analyzer/user_1.o] Error 1


I think this is related to the scope, but I don't know what is wrong.

Btw, thank you for the tip about the code for the myoutput file.



statements end with a semicolon
if ( (prt->pdgid()==6000006) ) { myHisto->Fill(prt->m()); /*<- the semicolon*/ } /*here is no needed*/

Also, that test is unnecessary, you already checked that in line 10.

However
if ( (prt->pdgid()!=6000006) ) return false;
would break the loop, greater values of `i' would not be tested, ¿is that what you want?
Only noticed now that those "return false" statements would break the loop, and I want to run all the values.

I also had no ; after return true, but now my code had no errors. Thank you!

Already tested, and run without any poblems :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool user_1::Execute(SampleFormat& sample, const EventFormat& event) 
{
	unsigned int n = event.mc()->particles().size();

	for (unsigned int i=0;i<n;i++)
	{
		const MCParticleFormat* prt = &event.mc()->particles()[i];

		if ( prt==0 or prt->pdgid() not_eq 6000006)
			continue; //would stop this iteration and continue with the next one

		myHisto->Fill(prt->m());
	}

	return true;
}
Last edited on
Topic archived. No new replies allowed.