Working in C++,making field of objects and sending them to other functions

Hi everybody,I’m currently working in root,more precisely coding in C++.However I encountered a problem not long ago with grouping objects and trying to send them to another function…Getting a crash,so here is my code(there is also a file full of declarations,but I think this is all I need for explanation):

int main( int argc, char *argv[] )
{
Analyzer *Data = new Analyzer(0,“Data”);
Analyzer *ggTo4e = new Analyzer(0,“ggTo4e”);
Analyzer *ggTo2mu2tau = new Analyzer(0,“ggTo2mu2tau”);
Analyzer *VBFH125 = new Analyzer(0,“VBFH125”);
Analyzer *ZZTo4l = new Analyzer(0,“ZZTo4l”);
Analyzer *ggH125 = new Analyzer(0,“ggH125”);
Analyzer *WminusH125 = new Analyzer(0,“WminusH125”);
Analyzer *ggTo2e2mu = new Analyzer(0,“ggTo2e2mu”);
Analyzer *ggTo4mu = new Analyzer(0,“ggTo4mu”);
Analyzer *WplusH125 = new Analyzer(0,“WplusH125”);
Analyzer *ggTo2e2tau = new Analyzer(0,“ggTo2e2tau”);
Analyzer *ggTo4tau = new Analyzer(0,“ggTo4tau”);
Analyzer *ttH125 = new Analyzer(0,“ttH125”);
Analyzer *ZZH125 = new Analyzer(0,“ZZH125”);

Analysis->InitializeHistos();
Analysis->Loop();
Analysis->DrawHistos();
}

So I created 14 objects,now how do I send them to InitializeHistos(),Loop() and DrawHistos() function?
Thank u in advance

Hi,

can you declare your objects in a header like

Analyzer *Data;
Analyzer *ggTo4e;
...

, keeping only the following in the main():

Data = new Analyzer(0, "Data");
ggT04e = new Analyzer(0, "ggto4e");
...

?

Sure,what is my next step to sending those objects to functions then?

You don’t need to send the objects to your functions, they are already allocated in the heap. For example, if you have

TH1F* data;

in a corresponding header, the following will work as expected:

#include "your_header.h"
int FillHisto()
{
        data->Fill(5, 1.);
        data->Fill(45, 3.);
        return 0;
}

int main(int argc, char** argv)
{
        TApplication app("test",&argc,argv);
        TCanvas *c = new TCanvas("c","Something",200,10,600,400);
        data = new TH1F("datahisto", "Data;X;Y", 50, 0., 50.);
        FillHisto();
        data->Draw();
        c->Modified(); c->Update();
        Int_t a; std::cin>>a; //prevents the window to close immediately
        return 0;
}

I would avoid declaring things at global scope. It leads to headaches later when trying to expand the functionality of your code. I’d suggest something like the following:

int main( int argc, char *argv[] )
{
   std::map< std::string, Analyzer* > analyzers;
   analyzers.at("data") = new Analyzer(0,"Data");
   analyzers.at("ggTo4e") = new Analyzer(0,"ggTo4e");
   analyzers.at("ggTo2mu2tau") = new Analyzer(0,"ggTo2mu2tau");
   ...

   Analysis->InitializeHistos(analyzers);
   Analysis->Loop(analyzers);
   Analysis->DrawHistos(analyzers);
}
1 Like

Thank you on your help.
I think it might work ,however gotta fix few errors first.
At first it said program doesn’t recognize Analysis,so I changed it to Analyzer,as I should,but I get those
errors now:
///////////////////////////////////////////////////
run.cpp:39: error: expected unqualified-id before ‘->’ token
run.cpp:40: error: expected unqualified-id before ‘->’ token
run.cpp:41: error: expected unqualified-id before ‘->’ token

Those are the lines it is mentioning:

Analyzer->InitializeHistos(analyzers);
Analyzer->Loop(analyzers);
Analyzer->DrawHistos(analyzers);
////////////////////////////////////////////////////
Any ideas what is causing this error?

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