Vector<double> vs double array[] "Code"

[code]#include<stdio.h>
#include<iostream.h>
#include<TFile.h>
#include<TTree.h>
#include<math.h>
#include
#include
#include
#include
using namespace std;
int W()
{

std::ifstream in;
in.open(“data.dat”);
TFile *f = new TFile(“MAAB.root”,“recreate”);
TTree *t = new TTree (“signal”,“data from ascii file”);
// some declarations
Double_t eta,phi,m,bt,em,d1,d2,nt,pt;
Int_t idx,ty,Trigger;

Int_t Ele_N=0,Jet_N=0,Muon_N=0,Tau_N=0,Photon_N=0;

Double_t Miss_Phi,Miss_Px,Miss_Py,Miss_E;
//you can change the range of the following arrays

vector *Photon_Eta=0,*Photon_Phi=0,*Photon_Px=0,*Photon_Py=0,*Photon_Pz=0,*Photon_E=0,*Photon_Pt=0,*Photon_isem=0;

vector *Ele_Eta=0,*Ele_Phi=0,*Ele_Px=0,*Ele_Py=0,*Ele_Pz=0,*Ele_E=0,*Ele_Pt=0,*Ele_isem=0;
vector *Ele_charge=0;
vector *Muon_Eta=0,*Muon_Phi=0,*Muon_Px=0,*Muon_Py=0,*Muon_Pz=0,*Muon_E=0,*Muon_Pt=0,*Muon_isem=0,*Muon_siso=0;
vector *Muon_charge=0;
vector *Tau_Eta=0,*Tau_Phi=0,*Tau_Px=0,*Tau_Py=0,*Tau_Pz=0,*Tau_E=0,*Tau_Pt=0,*Tau_isem=0;
vector *Tau_traN=0;
vector *Jet_Eta=0,*Jet_Phi=0,*Jet_Px=0,*Jet_Py=0,*Jet_Pz=0,*Jet_E=0,*Jet_Pt=0,*Jet_isem=0,*Jet_m=0;
vector *Jet_b=0,*Jet_traN=0;
//here I creat the leave

t->Branch(“Jet_N”,&Jet_N,“Jet_N/I”); // N jets
t->Branch(“Ele_N”,&Ele_N,“Ele_N/I”); // N elctrons
t->Branch(“Muon_N”,&Muon_N,“Muon_N/I”); // N muons
t->Branch(“Tau_N”,&Tau_N,“Tau_N/I”); // N taus
t->Branch(“Photon_N”,&Photon_N,“Photon_N/I”); // N photons
t->Branch(“Trigger”,&Trigger,“Trigger/I”); //Trigger information

//Photons
t->Branch(“Photon_Eta”,&Photon_Eta);
t->Branch(“Photon_Phi”,&Photon_Phi);
t->Branch(“Photon_Px”,&Photon_Px);
t->Branch(“Photon_Py”,&Photon_Py);
t->Branch(“Photon_Pz”,&Photon_Pz);
t->Branch(“Photon_E”,&Photon_E);
t->Branch(“Photon_Pt”,&Photon_Pt);
t->Branch(“Photon_isem”,&Photon_isem);

//jets
t->Branch(“Jet_Eta”,&Jet_Eta);
t->Branch(“Jet_Phi”,&Jet_Phi);
t->Branch(“Jet_Px”,&Jet_Px);
t->Branch(“Jet_Py”,&Jet_Py);
t->Branch(“Jet_Pz”,&Jet_Pz);
t->Branch(“Jet_E”,&Jet_E);
t->Branch(“Jet_Pt”,&Jet_Pt);
t->Branch(“Jet_isem”,&Jet_isem);
t->Branch(“Jet_traN”,&Jet_traN);
t->Branch(“Jet_m”,&Jet_m);
t->Branch(“Jet_b”,&Jet_b);//Jet tagging

//electrons
t->Branch(“Ele_Eta”,&Ele_Eta);
t->Branch(“Ele_Phi”,&Ele_Phi);
t->Branch(“Ele_Px”,&Ele_Px);
t->Branch(“Ele_Py”,&Ele_Py);
t->Branch(“Ele_Pz”,&Ele_Pz);
t->Branch(“Ele_E”,&Ele_E);
t->Branch(“Ele_Pt”,&Ele_Pt);
t->Branch(“Ele_isem”,&Ele_isem);
t->Branch(“Ele_charge”,&Ele_charge);

//muons
t->Branch(“Muon_Eta”,&Muon_Eta);
t->Branch(“Muon_Phi”,&Muon_Phi);
t->Branch(“Muon_Px”,&Muon_Px);
t->Branch(“Muon_Py”,&Muon_Py);
t->Branch(“Muon_Pz”,&Muon_Pz);
t->Branch(“Muon_E”,&Muon_E);
t->Branch(“Muon_Pt”,&Muon_Pt);
t->Branch(“Muon_isem”,&Muon_isem);
t->Branch(“Muon_charge”,&Muon_charge);
t->Branch(“Muon_siso”,&Muon_siso);
//taus
t->Branch(“Tau_Eta”,&Tau_Eta);
t->Branch(“Tau_Phi”,&Tau_Phi);
t->Branch(“Tau_Px”,&Tau_Px);
t->Branch(“Tau_Py”,&Tau_Py);
t->Branch(“Tau_Pz”,&Tau_Pz);
t->Branch(“Tau_E”,&Tau_E);
t->Branch(“Tau_Pt”,&Tau_Pt);
t->Branch(“Tau_isem”,&Tau_isem);
t->Branch(“Tau_traN”,&Tau_traN);

//Missing energy
t->Branch(“Miss_Phi”,&Miss_Phi,“Miss_Phi/D”);
t->Branch(“Miss_Px”,&Miss_Px,“Miss_Px/D”);
t->Branch(“Miss_Py”,&Miss_Py,“Miss_Py/D”);
t->Branch(“Miss_E”,&Miss_E,“Miss_E/D”);

Int_t Event_N=0,k=0;
while (1) {
if (k==1 && Event_N>=2 ||!in.good()) {
t->Fill();
Jet_N=Ele_N=Muon_N=Tau_N=Photon_N=0;
(*Photon_Eta).clear();(*Photon_Phi).clear();(*Photon_Px).clear();(*Photon_Py).clear();(*Photon_Pz).clear();(*Photon_E).clear();(*Photon_Pt).clear();(*Photon_isem).clear();

(*Ele_Eta).clear();(*Ele_Phi).clear();(*Ele_Px).clear();(*Ele_Py).clear();(*Ele_Pz).clear();(*Ele_E).clear();(*Ele_Pt).clear();(*Ele_isem).clear();
(*Ele_charge).clear();
(*Muon_Eta).clear();(*Muon_Phi).clear();(*Muon_Px).clear();(*Muon_Py).clear();(*Muon_Pz).clear();(*Muon_E).clear();(*Muon_Pt).clear();(*Muon_isem).clear();
(*Muon_siso).clear();
(*Muon_charge).clear();
(*Tau_Eta).clear();(*Tau_Phi).clear();(*Tau_Px).clear();(*Tau_Py).clear();(*Tau_Pz).clear();(*Tau_E).clear();(*Tau_Pt).clear();(*Tau_isem).clear();
(*Tau_traN).clear();
(*Jet_Eta).clear();(*Jet_Phi).clear();(*Jet_Px).clear();(*Jet_Py).clear();(*Jet_Pz).clear();(*Jet_E).clear();(*Jet_Pt).clear();(*Jet_isem).clear();(*Jet_m).clear();
(*Jet_b).clear();(*Jet_traN).clear();
}

if (!in.good()) break;

std::string line;
 
getline (in, line);

if (line.find("#") != line.npos||line.find_first_of ("0123456789") ==

line.npos)continue;

//if (line.find_first_of ("0123456789") == line.npos) {
//  continue;
// }

std::istringstream input_line;
input_line.str (line);
input_line >> idx;
if (idx == 0) {
    input_line >>Event_N;
    input_line >>Trigger;
    k=1;
    continue;
}

// reseting the k value
k=0;

input_line >> ty;// particle type
input_line >> eta;//psedurapidity
input_line >> phi;//azmothalangle
input_line >> pt;//Trnsverse momentum
input_line >> m; // invariant mass for jets
input_line >> nt; // if jet, or tau is # tracks, otherwise charge for leptons
input_line >> bt; // 0.0, 1.0 or 2.0 btag, or special muon  isolation info // 
input_line >> em;//some isolation parameters
input_line >> d1; //Now it is dumy
input_line >> d2; //Now it is dumy

switch(ty){
        case 0:  //photons
        // Photon_Eta[Photon_N]=eta;
        (*Photon_Eta).push_back(eta);
        (*Photon_Phi).push_back(phi);
        (*Photon_Px).push_back(pt*cos(phi));
        (*Photon_Py).push_back(pt*sin(phi));
        (*Photon_Pz).push_back(pt*sinh(eta));
        (*Photon_E).push_back(pt*cosh(eta));
        (*Photon_Pt).push_back(pt);
        (*Photon_isem).push_back(em);
         Photon_N++;
         break;

        case 1:  // electrons
        (*Ele_Eta).push_back(eta);
        (*Ele_Phi).push_back(phi);
        (*Ele_Px).push_back(pt*cos(phi));
        (*Ele_Py).push_back(pt*sin(phi));
        (*Ele_Pz).push_back(pt*sinh(eta));
        (*Ele_E).push_back(pt*cosh(eta));
        (*Ele_Pt).push_back(pt);
        (*Ele_isem).push_back(em);
        (*Ele_charge).push_back((int)nt);
         Ele_N++;
        break;

       case 2: // muons
        (*Muon_Eta).push_back(eta);
        (*Muon_Phi).push_back(phi);
        (*Muon_Px).push_back(pt*cos(phi));
        (*Muon_Py).push_back(pt*sin(phi));
        (*Muon_Pz).push_back(pt*sinh(eta));
        (*Muon_E).push_back(pt*cosh(eta));
        (*Muon_Pt).push_back(pt);
        (*Muon_isem).push_back(em);
        (*Muon_charge).push_back((int)nt);
        (*Muon_siso).push_back((int)bt);
         Muon_N++;
         break;

       case 3:  //taus
        (*Tau_Eta).push_back(eta);
        (*Tau_Phi).push_back(phi);
        (*Tau_Px).push_back(pt*cos(phi));
        (*Tau_Py).push_back(pt*sin(phi));
        (*Tau_Pz).push_back(pt*sinh(eta));
        (*Tau_E).push_back(pt*cosh(eta));
        (*Tau_Pt).push_back(pt);
        (*Tau_isem).push_back(em);
        (*Tau_traN).push_back((int)nt);
        Tau_N++;

         break;

       case 4:   //jets
        (*Jet_Eta).push_back(eta);
        (*Jet_Phi).push_back(phi);
        (*Jet_Px).push_back(pt*cos(phi));
        (*Jet_Py).push_back(pt*sin(phi));
        (*Jet_Pz).push_back(pt*sinh(eta));
        (*Jet_E).push_back(sqrt(pt*cosh(eta)*pt*cosh(eta)+m*m));
        (*Jet_Pt).push_back(pt);
        (*Jet_m).push_back(m);
        (*Jet_isem).push_back(em);
        (*Jet_traN).push_back((int)nt);
        (*Jet_b).push_back((int)bt);             
         Jet_N++;
         break;
       
       case 6:    //missing energy
         Miss_Phi=phi;
         Miss_Px=pt*cos(phi);
         Miss_Py=pt*sin(phi);
         Miss_E=pt;
         break;
       default:
           cout<< " no particle number found"<< ty<<endl;
                 break;
   }

}

cout <<"Total number of Event "<<Event_N<<endl;
in.close();
f->Write();
f->Close();
return 0;
}[/code]

wow… very long code on my monitor it’s long about 1 meter :slight_smile:

Which is the main point?

Hello
I sent you a question before yesterday (See root.cern.ch/phpBB2/viewtopic.php?t=6707)
it is under the title vector vs double array
then you asked me to to send you the code
please check the previous messege

Oh yes, (have two not connected posts si not good)

I’m not sure that this is the only reason but for the size issue i see two explanation:

  1. the STL container has a field containing the number of objects, adding a branch to store this number you are duplicating this information

  2. if you have many different stl contaners with the same lenght you also dupicating the information on the length, this has a technical name but I don’t know that :slight_smile:.

So, in my opinion, it’s reasonable that when you use the STL containers the file is larger than the array version. You can have a more compact representation building a class for each particle type collecting all the information, then store these objects in a branch for each collection, an abstract example:

class Photon {
  double en;
...
}
...

vector<Photon*> photon_vec;
photon_vec.push_bash(new Photon(...));

Hi,

Volpig is correct. In my previous answer I assume that the number of elements in the container was large compare to the number of containers in which case the extra indices being written would be a small number.

Anyway, I agree with Volpig, that if you are willing to use an stl container you ought to push it further and also use object (however you should consider using a vector of object instead of a vector of pointers)

Also, note that in ROOT 5.19/04 you can store the stl containers directly. i.e. you can replace:

vector<Photon> *photon_vec_ptr = new vector<Photon>; ... tree->Branch("photon",&photon_vec_ptr);by

vector<Photon*> photon_vec; ... tree->Branch("photon",&photon_vec);
Hence simplifying your code (no need of (*photon_vec_ptr).clear(), just photo_vec.clear());

Cheers,
Philippe.