Vector<double> vs double array[]

Hello rooters :laughing:
I have a question!!
I want to Dump an AOD file or an ASCI file, whatever!!
if I use vector method to dump the variables it will produce root files much bigger than using the method of double array[];:shock:
for example
I produced a root file with size 12.2MB by using the first method,
and 8.6 MB by using the second method.
is there a reason for that?
is true that arrays occupy less space than STL vectors?
what method do you advice me to use?
thanks for your cooperations

Hi,

The 2 techniques should be occupy rough the same amount of space. One possible cause for the larger file size vector is that the len/size of the individual vector is not setup correctly (I.e. if you provide the code you use to fill the vector, we may be able to spot the issue).

Cheers,
Philippe.

Hello
Here you you can find the 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(ptcos(phi));
(Photon_Py).push_back(ptsin(phi));
(Photon_Pz).push_back(ptsinh(eta));
(Photon_E).push_back(ptcosh(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]
conv.C (8.14 KB)

See root.cern.ch/phpBB2/viewtopic.php?t=6709 for more information