예제 #1
0
파일: sivia.cpp 프로젝트: qbrefort/sonar
Sivia::Sivia(repere& R, struct sivia_struct *par) : R(R) {

    par->area = 0;

    // Create the function we want to apply SIVIA on.
    Variable x,y;
    double ei = par->ei;
    double xb=par->xb1,yb=par->yb1;
    Interval xbi=Interval(par->xb1-ei,par->xb1+ei),ybi=Interval(par->yb1-ei,par->yb1+ei);

    double arc = par->sonar_arc;

    double r = pow(par->sonar_radius,2);
    double th1 = par->th[0];
    double th2=th1+arc;
    double th21= par->th[1];
    double th22=th21 + arc;
    double th31= par->th[2];
    double th32=th31 + arc;
    double e=1;
    double epsilon = par->epsilon;

    double xin,yin;

    // First SONAR
    Function f(x,y,sqr(x-xbi)+sqr(y-ybi));

    NumConstraint c1(x,y,f(x,y)<=r+e);
    NumConstraint c2(x,y,f(x,y)>=e);
    NumConstraint c3(x,y,f(x,y)>r+e);
    NumConstraint c4(x,y,f(x,y)<e);


    double sign1,sign2;
    if(cos(th1)>0) sign1=1;
    else sign1=-1;
    if(cos(th2)<0) sign2=1;
    else sign2=-1;


    NumConstraint cth11(x,y,sign1*(y-ybi-((sin(th1))/(cos(th1)))*(x-xbi))<0);
    NumConstraint cth12(x,y,sign1*(y-ybi-((sin(th1))/(cos(th1)))*(x-xbi))>0);
    NumConstraint cth21(x,y,sign2*(y-ybi-((sin(th2))/(cos(th2)))*(x-xbi))<0);
    NumConstraint cth22(x,y,sign2*(y-ybi-((sin(th2))/(cos(th2)))*(x-xbi))>0);


//     Create contractors with respect to each
//     of the previous constraints.
    CtcFwdBwd out1(c1);
    CtcFwdBwd out2(c2);
    CtcFwdBwd in1(c3);
    CtcFwdBwd in2(c4);


    CtcFwdBwd outth1(cth12);
    CtcFwdBwd inth1(cth11);
    CtcFwdBwd inth2(cth21);
    CtcFwdBwd outth2(cth22);

//    CtcIn inside(f,Interval(-1,1));
//    CtcNotIn outside(f,Interval(-1,1));
    // Create a contractor that removes all the points
    // that do not satisfy either f(x,y)<=2 or f(x,y)>=0.
    // These points are "outside" of the solution set.
    CtcCompo outside1(out1,out2,outth1,outth2);

    // Create a contractor that removes all the points
    // that do not satisfy both f(x,y)>2 or f(x,y)<0.
    // These points are "inside" the solution set.
    CtcUnion inside11(in1,in2,inth1);
    CtcUnion inside1(inside11,inth2);


    // Second SONAR
    double xb2=par->xb2,yb2=par->yb2;
    Interval xb2i=Interval(par->xb2-ei,par->xb2+ei),yb2i=Interval(par->yb2-ei,par->yb2+ei);

    Function f2(x,y,sqr(x-xb2i)+sqr(y-yb2i));
    NumConstraint c21(x,y,f2(x,y)<=r+e);
    NumConstraint c22(x,y,f2(x,y)>=e);
    NumConstraint c23(x,y,f2(x,y)>r+e);
    NumConstraint c24(x,y,f2(x,y)<e);


    double sign21,sign22;
    if(cos(th21)>0) sign21=-1;
    else sign21=1;
    if(cos(th22)<0) sign22=1;
    else sign22=-1;


    NumConstraint cth211(x,y,sign21*(y-yb2i-((sin(th21))/(cos(th21)))*(x-xb2i))<0);
    NumConstraint cth212(x,y,sign21*(y-yb2i-((sin(th21))/(cos(th21)))*(x-xb2i))>0);
    NumConstraint cth221(x,y,sign22*(y-yb2i-((sin(th22))/(cos(th22)))*(x-xb2i))<0);
    NumConstraint cth222(x,y,sign22*(y-yb2i-((sin(th22))/(cos(th22)))*(x-xb2i))>0);

//     Create contractors with respect to each
//     of the previous constraints.
    CtcFwdBwd out21(c21);
    CtcFwdBwd out22(c22);
    CtcFwdBwd in21(c23);
    CtcFwdBwd in22(c24);


    CtcFwdBwd outth21(cth211);
    CtcFwdBwd inth21(cth212);
    CtcFwdBwd inth22(cth221);
    CtcFwdBwd outth22(cth222);

//    CtcIn inside(f,Interval(-1,1));
//    CtcNotIn outside(f,Interval(-1,1));
    // Create a contractor that removes all the points
    // that do not satisfy either f(x,y)<=2 or f(x,y)>=0.
    // These points are "outside" of the solution set.
    CtcCompo outside2(out21,out22,outth21,outth22);

    // Create a contractor that removes all the points
    // that do not satisfy both f(x,y)>2 or f(x,y)<0.
    // These points are "inside" the solution set.
    CtcUnion inside21(in21,in22,inth21);
    CtcUnion inside2(inside21,inth22);



    //Third SONAR

    double xb3=par->xb3,yb3=par->yb3;
    Interval xb3i=Interval(par->xb3-ei,par->xb3+ei),yb3i=Interval(par->yb3-ei,par->yb3+ei);

    Function f3(x,y,sqr(x-xb3i)+sqr(y-yb3i));
    NumConstraint c31(x,y,f3(x,y)<=r+e);
    NumConstraint c32(x,y,f3(x,y)>=e);
    NumConstraint c33(x,y,f3(x,y)>r+e);
    NumConstraint c34(x,y,f3(x,y)<e);


    double sign31,sign32;
    if(cos(th31)>0) sign31=-1;
    else sign31=1;
    if(cos(th32)<0) sign32=1;
    else sign32=-1;


    NumConstraint cth311(x,y,sign31*(y-yb3i-((sin(th31))/(cos(th31)))*(x-xb3i))<0);
    NumConstraint cth312(x,y,sign31*(y-yb3i-((sin(th31))/(cos(th31)))*(x-xb3i))>0);
    NumConstraint cth321(x,y,sign32*(y-yb3i-((sin(th32))/(cos(th32)))*(x-xb3i))<0);
    NumConstraint cth322(x,y,sign32*(y-yb3i-((sin(th32))/(cos(th32)))*(x-xb3i))>0);

//     Create contractors with respect to each
//     of the previous constraints.
    CtcFwdBwd out31(c31);
    CtcFwdBwd out32(c32);
    CtcFwdBwd in31(c33);
    CtcFwdBwd in32(c34);


    CtcFwdBwd outth31(cth311);
    CtcFwdBwd inth31(cth312);
    CtcFwdBwd inth32(cth321);
    CtcFwdBwd outth32(cth322);

//    CtcIn inside(f,Interval(-1,1));
//    CtcNotIn outside(f,Interval(-1,1));
    // Create a contractor that removes all the points
    // that do not satisfy either f(x,y)<=2 or f(x,y)>=0.
    // These points are "outside" of the solution set.
    CtcCompo outside3(out31,out32,outth31,outth32);

    // Create a contractor that removes all the points
    // that do not satisfy both f(x,y)>2 or f(x,y)<0.
    // These points are "inside" the solution set.
    CtcUnion inside31(in31,in32,inth31);
    CtcUnion inside3(inside31,inth32);

    //CtcQInter inter(inside,1);

    //Artifact MODELISATION

    double xa = par->xa;
    double ya = par->ya;

    double ra = par->ra;

    Function f_a(x,y,sqr(x-xa)+sqr(y-ya));

    NumConstraint ca1(x,y,f_a(x,y)<=sqr(ra));
    NumConstraint ca2(x,y,f_a(x,y)>=sqr(ra)-par->thick);
    NumConstraint ca3(x,y,f_a(x,y)>sqr(ra));
    NumConstraint ca4(x,y,f_a(x,y)<sqr(ra)-par->thick);

    CtcFwdBwd aout1(ca1);
    CtcFwdBwd aout2(ca2);
    CtcFwdBwd ain1(ca3);
    CtcFwdBwd ain2(ca4);

    CtcUnion ain(ain1,ain2);
    CtcCompo aout(aout1,aout2);


    //Robot MODELISATION

    double xr = par->xr; //robot position x
    double yr = par->yr; //robot position y

    double wr = par->wr; //robot width
    double lr = par->lr; //robot length
    double ep = par->thick;

    xr = par->xr - wr/2;
    NumConstraint inrx1(x,y,x>xr+ep);
    NumConstraint outrx1(x,y,x<xr+ep);
    NumConstraint inrx2(x,y,x<xr-ep);
    NumConstraint outrx2(x,y,x>xr-ep);
    NumConstraint inry1(x,y,y<yr-lr/2);
    NumConstraint outry1(x,y,y>yr-lr/2);
    NumConstraint inry2(x,y,y>yr+lr/2);
    NumConstraint outry2(x,y,y<yr+lr/2);

    CtcFwdBwd incrx1(inrx1);
    CtcFwdBwd incrx2(inrx2);
    CtcFwdBwd incry1(inry1);
    CtcFwdBwd incry2(inry2);

    CtcFwdBwd outcrx1(outrx1);
    CtcFwdBwd outcrx2(outrx2);
    CtcFwdBwd outcry1(outry1);
    CtcFwdBwd outcry2(outry2);

    CtcUnion inrtemp(incrx1,incrx2,incry1);
    CtcUnion inr1(inrtemp,incry2);
    CtcCompo outrtemp(outcrx1,outcrx2,outcry1);
    CtcCompo outr1(outrtemp,outcry2);

    //2nd rectangle
    xr = par->xr + wr/2;

    NumConstraint inrx21(x,y,x>xr+ep);
    NumConstraint outrx21(x,y,x<xr+ep);
    NumConstraint inrx22(x,y,x<xr-ep);
    NumConstraint outrx22(x,y,x>xr-ep);
    NumConstraint inry21(x,y,y<yr-lr/2);
    NumConstraint outry21(x,y,y>yr-lr/2);
    NumConstraint inry22(x,y,y>yr+lr/2);
    NumConstraint outry22(x,y,y<yr+lr/2);

    CtcFwdBwd incrx21(inrx21);
    CtcFwdBwd incrx22(inrx22);
    CtcFwdBwd incry21(inry21);
    CtcFwdBwd incry22(inry22);

    CtcFwdBwd outcrx21(outrx21);
    CtcFwdBwd outcrx22(outrx22);
    CtcFwdBwd outcry21(outry21);
    CtcFwdBwd outcry22(outry22);

    CtcUnion inrtemp2(incrx21,incrx22,incry21);
    CtcUnion inr2(inrtemp2,incry22);
    CtcCompo outrtemp2(outcrx21,outcrx22,outcry21);
    CtcCompo outr2(outrtemp2,outcry22);


    //3nd rectangle top rectangle
    yr=par->yr+par->lr/2;
    xr=par->xr;

    NumConstraint inrx31(x,y,x>xr+wr/2+ep);
    NumConstraint outrx31(x,y,x<xr+wr/2+ep);
    NumConstraint inrx32(x,y,x<xr-wr/2-ep);
    NumConstraint outrx32(x,y,x>xr-wr/2-ep);
    NumConstraint inry31(x,y,y<yr-ep);
    NumConstraint outry31(x,y,y>yr-ep);
    NumConstraint inry32(x,y,y>yr+ep);
    NumConstraint outry32(x,y,y<yr+ep);

    CtcFwdBwd incrx31(inrx31);
    CtcFwdBwd incrx32(inrx32);
    CtcFwdBwd incry31(inry31);
    CtcFwdBwd incry32(inry32);

    CtcFwdBwd outcrx31(outrx31);
    CtcFwdBwd outcrx32(outrx32);
    CtcFwdBwd outcry31(outry31);
    CtcFwdBwd outcry32(outry32);

    CtcUnion inrtemp3(incrx31,incrx32,incry31);
    CtcUnion inr3(inrtemp3,incry32);
    CtcCompo outrtemp3(outcrx31,outcrx32,outcry31);
    CtcCompo outr3(outrtemp3,outcry32);

    //4 rectangle bot

    yr=par->yr-par->lr/2;
    xr=par->xr;

    NumConstraint inrx41(x,y,x>xr+wr/2+ep);
    NumConstraint outrx41(x,y,x<xr+wr/2+ep);
    NumConstraint inrx42(x,y,x<xr-wr/2-ep);
    NumConstraint outrx42(x,y,x>xr-wr/2-ep);
    NumConstraint inry41(x,y,y<yr-ep);
    NumConstraint outry41(x,y,y>yr-ep);
    NumConstraint inry42(x,y,y>yr+ep);
    NumConstraint outry42(x,y,y<yr+ep);

    CtcFwdBwd incrx41(inrx41);
    CtcFwdBwd incrx42(inrx42);
    CtcFwdBwd incry41(inry41);
    CtcFwdBwd incry42(inry42);

    CtcFwdBwd outcrx41(outrx41);
    CtcFwdBwd outcrx42(outrx42);
    CtcFwdBwd outcry41(outry41);
    CtcFwdBwd outcry42(outry42);

    CtcUnion inrtemp4(incrx41,incrx42,incry41);
    CtcUnion inr4(inrtemp4,incry42);
    CtcCompo outrtemp4(outcrx41,outcrx42,outcry41);
    CtcCompo outr4(outrtemp4,outcry42);

    CtcCompo inrtp(inr1,inr2,inr3);
    CtcUnion outrtp(outr1,outr2,outr3);

    CtcCompo inr(inrtp,inr4);
    CtcUnion outr(outrtp,outr4);

    yr = par->yr;

    int maxq = 3; //nb of contractors
    int Qinter = 2;
    int ctcq = maxq - Qinter + 1; //nb for q-relaxed function of Ibex


    Array<Ctc> inside1r1(inside1,inr,ain);
    Array<Ctc> outside1r1(outside1,outr,aout);

    Array<Ctc> inside2r1(inside2,inr,ain);
    Array<Ctc> outside2r1(outside2,outr,aout);

    Array<Ctc> inside3r1(inside3,inr,ain);
    Array<Ctc> outside3r1(outside3,outr,aout);

    CtcQInter outside1r(outside1r1,Qinter);
    CtcQInter inside1r(inside1r1,ctcq);

    CtcQInter outside2r(outside2r1,Qinter);
    CtcQInter inside2r(inside2r1,ctcq);

    CtcQInter outside3r(outside3r1,Qinter);
    CtcQInter inside3r(inside3r1,ctcq);


    // Build the initial box.
    IntervalVector box(2);
    box[0]=Interval(-10,10);
    box[1]=Interval(-10,10);
    par->vin.clear();
    // Build the way boxes will be bisected.
    // "LargestFirst" means that the dimension bisected
    // is always the largest one.

    int nbox1=0;
    LargestFirst lf;
    IntervalVector viinside1(2);
    stack<IntervalVector> s;
    s.push(box);
    while (!s.empty()) {
        IntervalVector box=s.top();
        s.pop();
            contract_and_draw(inside1r,box,viinside1,1,par,nbox1,Qt::magenta,Qt::red);
            if (box.is_empty()) { continue; }

            contract_and_draw(outside1r,box,viinside1,0,par,nbox1,Qt::darkBlue,Qt::cyan);
            if (box.is_empty()) { continue; }

            if (box.max_diam()<epsilon) {
                R.DrawBox(box[0].lb(),box[0].ub(),box[1].lb(),box[1].ub(),QPen(Qt::yellow),QBrush(Qt::NoBrush));
            } else {
                pair<IntervalVector,IntervalVector> boxes=lf.bisect(box);
                s.push(boxes.first);
                s.push(boxes.second);
            }
    }

    if(par->isinside==1){
        robot_position_estimator(nbox1,par);
        par->isinside1=1;
        par->isinside=0;
        //cout<<"area1: "<<par->area<<endl;
    }

    IntervalVector box2(2);
    box2[0]=Interval(-10,10);
    box2[1]=Interval(-10,10);

    // Build the way boxes will be bisected.
    // "LargestFirst" means that the dimension bisected
    // is always the largest one.
    int nbox2=0;
    LargestFirst lf2;
    IntervalVector viinside2(2);
    stack<IntervalVector> s2;
    s2.push(box2);
    while (!s2.empty()) {
        IntervalVector box2=s2.top();
        s2.pop();
            contract_and_draw(inside2r,box2,viinside2,2,par,nbox2,Qt::magenta,Qt::red);
            if (box2.is_empty()) { continue; }

            contract_and_draw(outside2r,box2,viinside2,0,par,nbox2,Qt::darkBlue,Qt::cyan);
            if (box2.is_empty()) { continue; }

            if (box2.max_diam()<epsilon) {
                R.DrawBox(box2[0].lb(),box2[0].ub(),box2[1].lb(),box2[1].ub(),QPen(Qt::yellow),QBrush(Qt::NoBrush));
            } else {
                pair<IntervalVector,IntervalVector> boxes2=lf2.bisect(box2);
                s2.push(boxes2.first);
                s2.push(boxes2.second);
            }
    }
    if(par->isinside==1){
        robot_position_estimator(nbox2,par);
        par->isinside2=1;
        par->isinside=0;
        //cout<<"area2: "<<par->area<<endl;
    }
    IntervalVector box3(2);
    box3[0]=Interval(-10,10);
    box3[1]=Interval(-10,10);

    // Build the way boxes will be bisected.
    // "LargestFirst" means that the dimension bisected
    // is always the largest one.
    int nbox3=0;
    LargestFirst lf3;
    IntervalVector viinside3(2);
    stack<IntervalVector> s3;
    s3.push(box3);
    while (!s3.empty()) {
        IntervalVector box3=s3.top();
        s3.pop();
            contract_and_draw(inside3r,box3,viinside3,3,par,nbox3,Qt::magenta,Qt::red);
            if (box3.is_empty()) { continue; }

            contract_and_draw(outside3r,box3,viinside3,0,par,nbox3,Qt::darkBlue,Qt::cyan);
            if (box3.is_empty()) { continue; }

            if (box3.max_diam()<epsilon) {
                R.DrawBox(box3[0].lb(),box3[0].ub(),box3[1].lb(),box3[1].ub(),QPen(Qt::yellow),QBrush(Qt::NoBrush));
            } else {
                pair<IntervalVector,IntervalVector> boxes3=lf3.bisect(box3);
                s3.push(boxes3.first);
                s3.push(boxes3.second);
            }
    }
    if(par->isinside==1){
        robot_position_estimator(nbox3,par);
        par->isinside3=1;
        par->isinside=0;
        //cout<<"area3: "<<par->area<<endl;
    }
    par->state.clear();
    if (par->isinside1 ==1 || par->isinside2 ==1 || par->isinside3 ==1){
        double *aimth = new double[3];
        aimth[0] = get_angle(xb,yb,par->xin,par->yin)+M_PI ;
        aimth[1] = get_angle(xb2,yb2,par->xin,par->yin)+M_PI;
        aimth[2] = get_angle(xb3,yb3,par->xin,par->yin)+M_PI;

        R.DrawLine(xb,yb,xb+r*cos(aimth[0]),yb+r*sin(aimth[0]),QPen(Qt::red));
        R.DrawLine(xb2,yb2,xb2+r*cos(aimth[1]),yb2+r*sin(aimth[1]),QPen(Qt::red));
        R.DrawLine(xb3,yb3,xb3+r*cos(aimth[2]),yb3+r*sin(aimth[2]),QPen(Qt::red));

        par->state = std::string("found");
        double kp = par->kp;
        double u[3];

        for (int i=0;i<3;i++){
            u[i] =   -kp*atan(tan((par->th[i] - (aimth[i] - arc/2.0 ))/2));
            if(u[i]>par->sonar_speed) par->th[i] += par->sonar_speed;
            if(u[i]<-par->sonar_speed) par->th[i] += -par->sonar_speed;
            else par->th[i] += u[i];
        }
//        for (int i=0;i<3;i++){
//            u[i] =   atan(tan((par->th[i] - (aimth[i] - arc/2.0 ))/2));
//            par->th[i] -=u[i];
//        }
    }

    r = sqrt(r);
    //cout<<"th1"<<th1<<endl;
    R.DrawEllipse(xb,yb,par->ei,QPen(Qt::black),QBrush(Qt::NoBrush));
    R.DrawEllipse(xb2,yb2,par->ei,QPen(Qt::black),QBrush(Qt::NoBrush));
    R.DrawEllipse(xb3,yb3,par->ei,QPen(Qt::black),QBrush(Qt::NoBrush));

    R.DrawLine(xb,yb,xb+r*cos(th2),yb+r*sin(th2),QPen(Qt::green));
    R.DrawLine(xb2,yb2,xb2+r*cos(th22),yb2+r*sin(th22),QPen(Qt::green));
    R.DrawLine(xb3,yb3,xb3+r*cos(th32),yb3+r*sin(th32),QPen(Qt::green));

    R.DrawLine(xb,yb,xb+r*cos(th1),yb+r*sin(th1),QPen(Qt::green));
    R.DrawLine(xb2,yb2,xb2+r*cos(th21),yb2+r*sin(th21),QPen(Qt::green));
    R.DrawLine(xb3,yb3,xb3+r*cos(th31),yb3+r*sin(th31),QPen(Qt::green));

    R.DrawEllipse(par->xa,par->ya,par->ra,QPen(Qt::black),QBrush(Qt::NoBrush));

    R.DrawRobot(xr-wr/2,yr+lr/2,-3.14/2,wr,lr);
    R.Save("paving");

    par->vin.clear();
}
void MinBias1::Loop()
{
//   In a ROOT session, you can do:
//      Root > .L MinBias1.C
//      Root > MinBias1 t
//      Root > t.GetEntry(12); // Fill t data members with entry number 12
//      Root > t.Show();       // Show values of entry 12
//      Root > t.Show(16);     // Read and show values of entry 16
//      Root > t.Loop();       // Loop on all entries
//

//     This is the loop skeleton where:
//    jentry is the global entry number in the chain
//    ientry is the entry number in the current Tree
//  Note that the argument to GetEntry must be:
//    jentry for TChain::GetEntry
//    ientry for TTree::GetEntry and TBranch::GetEntry
//
//       To read only selected branches, Insert statements like:
// METHOD1:
//    fChain->SetBranchStatus("*",0);  // disable all branches
//    fChain->SetBranchStatus("branchname",1);  // activate branchname
// METHOD2: replace line
//    fChain->GetEntry(jentry);       //read all branches
//by  b_branchname->GetEntry(ientry); //read only this branch
   if (fChain == 0) return;

   Long64_t nentries = fChain->GetEntriesFast();

   Int_t nbytes = 0, nb = 0;
   
   FILE *Out1 = fopen("coef.txt", "w+");  
   FILE *Out2 = fopen("var.txt", "w+");

// distribution of <Energy deposition per readout> over 40000 events 
// without cut (all rechits) and with cut on rechit energy (0.5 GeV)
// for all readouts (phi, eta, depth) 1 (-ieta), 2 (+ieta)
     
   TH1F  *hCalo1[80][50][5][5];
   TH1F  *hCalo2[80][50][5][5];


// distribution of variances of energy deposition per 
// readout without cut (all rechits) and with cut on rechit energy (0.5 GeV)
// for all readouts (phi, eta, depth) 1 (-ieta), 2 (+ieta)
   
   TH1F  *hCalo1mom2[80][50][5][5];
   TH1F  *hCalo2mom2[80][50][5][5];
   
// 1D distribution of <energy deposition per readout>
// for single eta without cut (all rechits) and with cut on rechit energy (0.5 GeV)
// for all readouts (eta, depth) 1 (-ieta), 2 (+ieta)
   
   TH1F  *hCalo1eta[50][5][5];
   TH1F  *hCalo2eta[50][5][5];
    
// 1D distribution of variances of energy deposition per 
// readout for single eta without cut (all rechits) and with cut on rechit energy (0.5 GeV)
// for all readouts (eta, depth) 1 (-ieta), 2 (+ieta)
   
   TH1F  *hCalo1mom2eta[50][5][5];
   TH1F  *hCalo2mom2eta[50][5][5];

// 2D distribution of <energy deposition per readout>
// for single eta without cut (all rechits) and with cut on rechit energy (0.5 GeV)
// for all readouts (eta, depth) 1 (-ieta), 2 (+ieta)

   
   TH2F  *hCalo1etatwo[50][5][5];
   TH2F  *hCalo2etatwo[50][5][5];

// 2D distribution of variances of energy deposition per 
// readout for single eta without cut (all rechits) and with cut on rechit energy (0.5 GeV)
// for all readouts (eta, depth) 1 (-ieta), 2 (+ieta)
   
   TH2F  *hCalo1mom2etatwo[50][5][5];
   TH2F  *hCalo2mom2etatwo[50][5][5];
   
// 2D distribution of coefficients per 
// readout for single eta without cut (all rechits) and with cut on rechit energy (0.5 GeV)
// for all readouts (eta, depth) 1 (-ieta), 2 (+ieta)
   
   
   TH2F  *hCalo1etatwocoef[50][5][5];
   TH2F  *hCalo2etatwocoef[50][5][5];
   
   TH1F  *hCalo1etaonecoef[50][5][5];
   TH1F  *hCalo2etaonecoef[50][5][5];
   
   TH1F  *hCalo1etacoefdist[50][5][5]; 
   TH1F  *hCalo2etacoefdist[50][5][5];

   TH1F  *hCalo1etacoefdist_nonoise[50][5][5];
   TH1F  *hCalo2etacoefdist_nonoise[50][5][5];

   
   Float_t plmeang[80][80][5][5],plnoise[80][80][5][5],plerrg[80][80][5][5],plerr[80][5][5];
   Float_t plmeang_nn[80][80][5][5]; 
   Float_t plmean[80][5][5]; 
   Float_t plmean_nn[80][5][5];
   Float_t minmeang[80][80][5][5],minnoise[80][80][5][5],minerrg[80][80][5][5],minerr[80][5][5];
   Float_t minmeang_nn[80][80][5][5];
   Float_t minmean[80][5][5];
   Float_t minmean_nn[80][5][5];
   
   Int_t plneveta[80][5][5]; 
   Int_t plnevetaphi[80][80][5][5];
   Int_t minneveta[80][5][5]; 
   Int_t minnevetaphi[80][80][5][5];
   Int_t mysubdetpl0[80][5][5];
   Int_t mysubdetmin0[80][5][5];
   Int_t hb_excluded_min[80][80][5];
  
   cout<<" Read noise comment for neutrino samples/uncomment for data samples"<<endl;
      string line;
      
/*
   std::ifstream in20( "disp_11.txt" );
   while( std::getline( in20, line)){
   istringstream linestream(line);
   Float_t var;
   int subd,eta,phi,dep; 
   linestream>>subd>>eta>>phi>>dep>>var;
   if(phi == 1) cout<<subd<<" "<<var<<endl;
   if( eta > 0 )
   {
     plnoise[eta][phi][dep][subd] = var;
//     cout<<plnoise[eta][phi][dep]<<endl;
   } else
   {
     minnoise[abs(eta)][phi][dep][subd] = var;
//     cout<<minnoise[abs(eta)][phi][dep]<<endl;
   }
  }

*/ 
  for(Int_t ietak = 0; ietak < 80; ietak++ )
  {
   for(Int_t idep = 0; idep < 5; idep++ )
   {
    for(Int_t isub = 0; isub < 5; isub++ )
   {
    
    plneveta[ietak][idep][isub] = 0;
    minneveta[ietak][idep][isub] = 0;
    plmean[ietak][idep][isub] = 0;
    plmean_nn[ietak][idep][isub] = 0;
    minmean[ietak][idep][isub] = 0;
    minmean_nn[ietak][idep][isub] = 0;
    mysubdetmin0[ietak][idep][isub] = 0;
    mysubdetpl0[ietak][idep][isub] = 0;

    for(Int_t iphik = 0; iphik < 80; iphik++ )
    {
       plnevetaphi[ietak][iphik][idep][isub] = 0;
       minnevetaphi[ietak][iphik][idep][isub] = 0;
       plmeang[ietak][iphik][idep][isub] = 0;
       minmeang[ietak][iphik][idep][isub] = 0;
       plmeang_nn[ietak][iphik][idep][isub] = 0;
       minmeang_nn[ietak][iphik][idep][isub] = 0;
      if(isub==0)
      {
         hb_excluded_min[ietak][iphik][idep] = 0.;
      }
     }
    } 
   }
  }
  
   std::ifstream in21( "HB_exclusion.txt" );
   
   while( std::getline( in21, line)){
   istringstream linestream(line);
 
   int eta,phi,dep; 
   linestream>>dep>>eta>>phi;
    cout<<" Eta="<<eta<<endl;
   if( eta > 0 )
   {
     cout<<" eta > 0 "<<endl;
   } else
   {
     hb_excluded_min[abs(eta)][phi][dep] = 1;
   }
  }
  
  cout<< " Start to order histograms "<<endl;
  
//    
   char shCalo1[50];
   char shCalo2[50];
   
   char shCalo1mom2[50];
   char shCalo2mom2[50];
  
   char shCalo1eta[50];
   char shCalo2eta[50];

   
   char shCalo1mom2eta[50];
   char shCalo2mom2eta[50];
   
   char shCalo1etatwo[50];
   char shCalo2etatwo[50];

   char shCalo1mom2etatwo[50];
   char shCalo2mom2etatwo[50];   
   char shCalo1etatwocoef[50];
   char shCalo2etatwocoef[50];
   
   char shCalo1etaonecoef[50];
   char shCalo2etaonecoef[50];

   char shCalo1etacoefdist[50];
   char shCalo2etacoefdist[50];
   
   char shCalo1etacoefdist_nonoise[50];
   char shCalo2etacoefdist_nonoise[50];

   char sDet(2);
  
  
   int k;
   for(int i=1;i<73;i++){
    for(int j=1;j<43;j++){
     for(int l=1;l<5;l++){
       for(int m=1;m<5;m++){
        k = i*10000+j*100+l*10+m;
        sprintf(shCalo1,"hCalo1_%d",k);
        sprintf(shCalo2,"hCalo2_%d",k);
	
        sprintf(shCalo1mom2,"hCalo1mom2_%d",k);
        sprintf(shCalo2mom2,"hCalo2mom2_%d",k);

    if( j < 30 )
    {
// first order moment	
    hCalo1[i][j][l][m] = new TH1F(shCalo1, "hCalo1", 320, -0.1, 0.1); 
    hCalo2[i][j][l][m] = new TH1F(shCalo2, "hCalo2", 320, -0.1, 0.1);

// second order moment    
    hCalo1mom2[i][j][l][m] = new TH1F(shCalo1mom2, "hCalo1mom2", 320, 0., 0.5); 
    hCalo2mom2[i][j][l][m] = new TH1F(shCalo2mom2, "hCalo2mom2", 320, 0., 0.5); 
    
    }
      else
      {

// HF
// first order moment
//   cout<<" "<<i<<" "<<j<<" "<<k<<endl;

   if(j < 38)
   {	
    hCalo1[i][j][l][m] = new TH1F(shCalo1, "hCalo1", 320, -0.1, 0.1); 
    hCalo2[i][j][l][m] = new TH1F(shCalo2, "hCalo2", 320, -0.1, 0.1);
//
// second order moment    
    hCalo1mom2[i][j][l][m] = new TH1F(shCalo1mom2, "hCalo1mom2", 320, 0., 10.); 
    hCalo2mom2[i][j][l][m] = new TH1F(shCalo2mom2, "hCalo2mom2", 320, 0., 10.); 
   }
     else
     {
    hCalo1[i][j][l][m] = new TH1F(shCalo1,"hCalo1" , 320, -0.1, 0.1); 
    hCalo2[i][j][l][m] = new TH1F(shCalo2, "hCalo2", 320, -0.1, 0.1);

// second order moment    
    hCalo1mom2[i][j][l][m] = new TH1F(shCalo1mom2, "hCalo1mom2", 320, 0., 120.); 
    hCalo2mom2[i][j][l][m] = new TH1F(shCalo2mom2, "hCalo2mom2", 320, 0., 120.); 
       
     }  
    } // HE/HF boundary
      } // m
     } // l
    } // j
   } // i
 
   cout<<" First histos "<<endl;
   
     for(int j=1;j<43;j++)
     {
       for(int l=1;l<5;l++)
       {
         for(int m=1;m<5;m++)
         {
       
         Int_t jj = 100*j+10*l+m;
	 
        sprintf(shCalo1eta,"hCalo1eta_%d",jj);
        sprintf(shCalo2eta,"hCalo2eta_%d",jj);

	
        sprintf(shCalo1mom2eta,"hCalo1mom2eta_%d",jj);
        sprintf(shCalo2mom2eta,"hCalo2mom2eta_%d",jj);
 	
	sprintf(shCalo1etatwo,"hCalo1etatwo_%d",jj);
	sprintf(shCalo2etatwo,"hCalo2etatwo_%d",jj);
	
	sprintf(shCalo1mom2etatwo, "hCalo1mom2etatwo_%d",jj);
	sprintf(shCalo2mom2etatwo, "hCalo2mom2etatwo_%d",jj);
	
	sprintf(shCalo1etatwocoef,"hCalo1etatwocoef_%d",jj);
	sprintf(shCalo2etatwocoef,"hCalo2etatwocoef_%d",jj);
	sprintf(shCalo1etaonecoef,"hCalo1etaonecoef_%d",jj);
	sprintf(shCalo2etaonecoef,"hCalo2etaonecoef_%d",jj);
	
	sprintf(shCalo1etacoefdist,"hCalo1etacoefdist_%d",jj);
	sprintf(shCalo2etacoefdist,"hCalo2etacoefdist_%d",jj);
        sprintf(shCalo1etacoefdist_nonoise,"hCalo1etacoefdist_nn_%d",jj);
        sprintf(shCalo2etacoefdist_nonoise,"hCalo2etacoefdist_nn_%d",jj);
	
        hCalo1etatwocoef[j][l][m] = new TH2F(shCalo1etatwocoef, "hCalo1etatwocoef", 72, 0.5, 72.5, 320, 0., 2.); 
        hCalo2etatwocoef[j][l][m] = new TH2F(shCalo2etatwocoef, "hCalo2etatwocoef", 72, 0.5, 72.5, 320, 0., 2.);
        hCalo1etaonecoef[j][l][m] = new TH1F(shCalo1etaonecoef, "hCalo1etaonecoef", 72, 0.5, 72.5); 
        hCalo2etaonecoef[j][l][m] = new TH1F(shCalo2etaonecoef, "hCalo2etaonecoef", 72, 0.5, 72.5);
        hCalo1etacoefdist[j][l][m] = new TH1F(shCalo1etacoefdist, "hCalo1etacoefdist", 100, 0., 2.); 
        hCalo2etacoefdist[j][l][m] = new TH1F(shCalo2etacoefdist, "hCalo2etacoefdist", 100, 0., 2.);
        hCalo1etacoefdist_nonoise[j][l][m] = new TH1F(shCalo1etacoefdist_nonoise, "hCalo1etacoefdist_nonoise", 100, 0., 2.);
        hCalo2etacoefdist_nonoise[j][l][m] = new TH1F(shCalo2etacoefdist_nonoise, "hCalo2etacoefdist_nonoise", 100, 0., 2.);

       if( j < 30 )
       {	
	
        hCalo1eta[j][l][m] = new TH1F(shCalo1eta, "hCalo1eta", 320, -0.1, 0.1); 
        hCalo2eta[j][l][m] = new TH1F(shCalo2eta,"hCalo2eta" , 320, -0.1, 0.1);
	
        hCalo1mom2eta[j][l][m] = new TH1F(shCalo1mom2eta,"hCalo1mom2eta" , 320, 0., 0.2); 
        hCalo2mom2eta[j][l][m] = new TH1F(shCalo2mom2eta,"hCalo2mom2eta" , 320, 0., 0.2);        
        hCalo1etatwo[j][l][m] = new TH2F(shCalo1etatwo, "hCalo1etatwo", 72, 0.5, 72.5, 320, -1.5, 1.5); 
        hCalo2etatwo[j][l][m] = new TH2F(shCalo2etatwo, "hCalo2etatwo", 72, 0.5, 72.5, 320, -1.5, 1.5);
        if (j < 24)
        {
        hCalo1mom2etatwo[j][l][m] = new TH2F(shCalo1mom2etatwo, "hCalo1mom2etatwo", 72, 0.5, 72.5, 320, 0., 0.2); 
        hCalo2mom2etatwo[j][l][m] = new TH2F(shCalo2mom2etatwo, "hCalo2mom2etatwo", 72, 0.5, 72.5, 320, 0., 0.2);
	}
          else
          {
        hCalo1mom2etatwo[j][l][m] = new TH2F(shCalo1mom2etatwo, "hCalo1mom2etatwo", 72, 0.5, 72.5, 320, 0., 1.5);
        hCalo2mom2etatwo[j][l][m] = new TH2F(shCalo2mom2etatwo, "hCalo2mom2etatwo", 72, 0.5, 72.5, 320, 0., 1.5);
          }
       }
         else
	 {
	 if( j < 38 )
	 {
        hCalo1eta[j][l][m] = new TH1F(shCalo1eta, "hCalo1eta", 320, -0.1, 0.1); 
        hCalo2eta[j][l][m] = new TH1F(shCalo2eta,"hCalo2eta" , 320, -0.1, 0.1);
	
        hCalo1etatwo[j][l][m] = new TH2F(shCalo1etatwo, "hCalo1etatwo", 72, 0.5, 72.5, 320, -1.5, 1.5); 
        hCalo2etatwo[j][l][m] = new TH2F(shCalo2etatwo, "hCalo2etatwo", 72, 0.5, 72.5, 320, -1.5, 1.5);
	
        hCalo1mom2eta[j][l][m] = new TH1F(shCalo1mom2eta,"hCalo1mom2eta" , 320, 0., 0.2); 
        hCalo2mom2eta[j][l][m] = new TH1F(shCalo2mom2eta,"hCalo2mom2eta" , 320, 0., 0.2);        
	
        hCalo1mom2etatwo[j][l][m] = new TH2F(shCalo1mom2etatwo, "hCalo1mom2etatwo", 72, 0.5, 72.5, 320, 0., 10.); 
        hCalo2mom2etatwo[j][l][m] = new TH2F(shCalo2mom2etatwo, "hCalo2mom2etatwo", 72, 0.5, 72.5, 320, 0., 10.);
	 
	 }
	   else
	   {
	   
        hCalo1eta[j][l][m] = new TH1F(shCalo1eta, "hCalo1eta", 320, -0.1, 0.1); 
        hCalo2eta[j][l][m] = new TH1F(shCalo2eta,"hCalo2eta" , 320, -0.1, 0.1);
	
        hCalo1mom2eta[j][l][m] = new TH1F(shCalo1mom2eta,"hCalo1mom2eta" , 320, 0., 0.2); 
        hCalo2mom2eta[j][l][m] = new TH1F(shCalo2mom2eta,"hCalo2mom2eta" , 320, 0., 0.2);        
	
        hCalo1etatwo[j][l][m] = new TH2F(shCalo1etatwo, "hCalo1etatwo", 72, 0.5, 72.5, 320, -1.5, 1.5); 
        hCalo2etatwo[j][l][m] = new TH2F(shCalo2etatwo, "hCalo2etatwo", 72, 0.5, 72.5, 320, -1.5, 1.5);
	
        hCalo1mom2etatwo[j][l][m] = new TH2F(shCalo1mom2etatwo, "hCalo1mom2etatwo", 72, 0.5, 72.5, 320, 5., 20.); 
        hCalo2mom2etatwo[j][l][m] = new TH2F(shCalo2mom2etatwo, "hCalo2mom2etatwo", 72, 0.5, 72.5, 320, 5., 20.);
	
	   }
	 }
	 }// m
       } // l	 
     } // j == 50  

   cout<<" Second histos "<<endl;
   
   for (Long64_t jentry=0; jentry<nentries;jentry++) {
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);   nbytes += nb;
      // if (Cut(ientry) < 0) continue;
      
      if(mom0 == 0) continue;
      
       Float_t mean = mom1/mom0;
       Float_t disp = mom2/mom0 - (mom1/mom0)*(mom1/mom0);
       
       if(ieta<0) {

        hCalo1[iphi][abs(ieta)][depth][mysubd]->Fill(mom1/mom0);
        hCalo1eta[abs(ieta)][depth][mysubd]->Fill(mom1/mom0);
	hCalo1mom2[iphi][abs(ieta)][depth][mysubd]->Fill(disp);
        hCalo1mom2eta[abs(ieta)][depth][mysubd]->Fill(disp);
        hCalo1etatwo[abs(ieta)][depth][mysubd]->Fill((float)iphi, mom1/mom0);
        hCalo1mom2etatwo[abs(ieta)][depth][mysubd]->Fill((float)iphi, disp);

// Calculation of mean values ===============================
         mysubdetmin0[abs(ieta)][depth][mysubd] = mysubd;
	 minmeang[abs(ieta)][iphi][depth][mysubd]=minmeang[abs(ieta)][iphi][depth][mysubd] + (float)mean;
	 
	 if(mysubd == 1){
 	   if(hb_excluded_min[abs(ieta)][iphi][depth]>0) minmean[abs(ieta)][depth][mysubd]=minmean[abs(ieta)][depth][mysubd] + (float)mean;
	 } else {
	    minmean[abs(ieta)][depth][mysubd]=minmean[abs(ieta)][depth][mysubd] + (float)mean;
	 }
	 
         minneveta[abs(ieta)][depth][mysubd]++;
	 minnevetaphi[abs(ieta)][iphi][depth][mysubd]++;
	 
         minerrg[abs(ieta)][iphi][depth][mysubd] = minerrg[abs(ieta)][iphi][depth][mysubd] + mean*mean;
         minerr[abs(ieta)][depth][mysubd] = minerr[abs(ieta)][depth][mysubd] + mean*mean;
	 
// ==========================================================
	
      }	
      if(ieta>=0) {

         hCalo2[iphi][ieta][depth][mysubd]->Fill(mom1/mom0);
         hCalo2eta[abs(ieta)][depth][mysubd]->Fill(mom1/mom0);
	 hCalo2mom2[iphi][ieta][depth][mysubd]->Fill(disp);
         hCalo2mom2eta[abs(ieta)][depth][mysubd]->Fill(disp);
	 hCalo2etatwo[abs(ieta)][depth][mysubd]->Fill((float)iphi, mom1/mom0);
	 hCalo2mom2etatwo[abs(ieta)][depth][mysubd]->Fill((float)iphi, disp);

// Calculation of dispersion ===============================
          mysubdetpl0[abs(ieta)][depth][mysubd] = mysubd;
	  plmeang[ieta][iphi][depth][mysubd]=plmeang[ieta][iphi][depth][mysubd] + (float)mean;
	  plmean[ieta][depth][mysubd]=plmean[ieta][depth][mysubd] + (float)mean;
	  
	  plneveta[abs(ieta)][depth][mysubd]++;
	  plnevetaphi[abs(ieta)][iphi][depth][mysubd]++;
	  
	  plerrg[abs(ieta)][iphi][depth][mysubd] = plerrg[abs(ieta)][iphi][depth][mysubd] + mean*mean;
	  plerr[abs(ieta)][depth][mysubd] = plerr[abs(ieta)][depth][mysubd] + mean*mean;
	  
// ==========================================================

	 
      }
     
            
   } // jentry

   cout<<" Finish cycle "<<endl;


  Double_t perr,perrg;
  Double_t plmean_mean; 
  Double_t plmeang_mean; 
  Double_t minmean_mean; 
  Double_t minmeang_mean; 
  
  
  for (int idep = 1; idep <5; idep++ )
  {
  for(int ietak = 1; ietak != 42; ietak++ )
  {
  for(int isub = 1; isub < 5; isub++ )
  {
    
    if( plneveta[ietak][idep][isub] <= 0 ) continue;
    
    plmean_mean = plmean[ietak][idep][isub]/plneveta[ietak][idep][isub];
    perr = plerr[ietak][idep][isub]/plneveta[ietak][idep][isub] - plmean_mean*plmean_mean;
    
    
    for(Int_t iphik = 1; iphik != 73; iphik++ )
    {

          if(plnevetaphi[ietak][iphik][idep][isub] == 0) {
            Float_t ss = 1.;
	    fprintf(Out2,"%d %d %d %d %.8f\n",mysubdetpl0[ietak][idep][isub],ietak,iphik,idep,ss);
            fprintf(Out1,"%d %d %d %d %.5f %.5f\n",isub,idep, ietak,iphik,ss,ss); 
            continue;
           }
	
	   
       plmeang_mean = plmeang[ietak][iphik][idep][isub]/plnevetaphi[ietak][iphik][idep][isub];
       Float_t newdist = plmeang_mean/plmean_mean;
       hCalo2etacoefdist_nonoise[ietak][idep][isub]->Fill(newdist);
           
// Mean value and dispersion

       Double_t plmeang_mean_corr = plmean_mean/plmeang_mean;
       
       perrg = plerrg[ietak][iphik][idep][isub]/plnevetaphi[ietak][iphik][idep][isub] - plmeang_mean*plmeang_mean;
      
       Double_t yy = sqrt(plmean_mean*perrg/(4.*plmeang_mean*plmeang_mean*plmeang_mean));	
	 
       fprintf(Out2,"%d %d %d %d %.8f\n",mysubdetpl0[ietak][idep][isub],ietak,iphik,idep,plmeang_mean);
	
       Float_t zz = (Float_t)yy;  		       
//         if( isub == 1 ) fprintf(Out1,"%s %d %d %d %.5f %.5f\n","HB",ietak,iphik,idep,plmeang_mean_corr,zz);
//         if( isub == 2 ) fprintf(Out1,"%s %d %d %d %.5f %.5f\n","HE",ietak,iphik,idep,plmeang_mean_corr,zz);
//         if( isub == 3 ) fprintf(Out1,"%s %d %d %d %.5f %.5f\n","HO",ietak,iphik,idep,plmeang_mean_corr,zz);
//         if( isub == 4 ) fprintf(Out1,"%s %d %d %d %.5f %.5f\n","HF",ietak,iphik,idep,plmeang_mean_corr,zz);

       fprintf(Out1,"%d %d %d %d %.5f %.5f\n",isub,idep, ietak,iphik,plmeang_mean_corr,zz); 

         hCalo2etatwocoef[ietak][idep][isub]->Fill((float)iphik,plmeang_mean_corr);
         hCalo2etaonecoef[ietak][idep][isub]->Fill((float)iphik,plmeang_mean_corr);
	 hCalo2etacoefdist[ietak][idep][isub]->Fill(plmeang_mean_corr);
    }
  }  
  }
  }
   
  for (int idep = 1; idep <5; idep++ )
  {
  for(int ietak = 1; ietak != 42; ietak++ )
  {
  for(int isub = 1; isub < 5; isub++ )
  {
   
   int iieta = -1*ietak;
    
    if( minneveta[ietak][idep][isub] <= 0 ) continue;
    
    minmean_mean = minmean[ietak][idep][isub]/minneveta[ietak][idep][isub];
    
    perr = minerr[ietak][idep][isub]/minneveta[ietak][idep][isub] - minmean_mean*minmean_mean;
    
    if( idep == 1 && ietak == 1) cout<<" My "<< ietak<<" "<<idep<<" "<<minneveta[ietak][idep][isub]<<" "<<minerr[ietak][idep][isub]<<" "<<minmean_mean<<" "<<perr<<endl;
    
    for(Int_t iphik = 1; iphik != 73; iphik++ )
    {
    
          if(minnevetaphi[ietak][iphik][idep][isub] == 0) {
            Float_t ss = 1.;
            fprintf(Out1,"%d %d %d %d %.5f %.5f\n",isub,idep,iieta,iphik,ss,ss); 
            fprintf(Out2,"%d %d %d %d %.8f\n",mysubdetmin0[ietak][idep][isub],iieta,iphik,idep,ss);
            continue;
           }
	
	   
       minmeang_mean = minmeang[ietak][iphik][idep][isub]/minnevetaphi[ietak][iphik][idep][isub];
       Float_t newdist = minmeang_mean/minmean_mean;
       hCalo1etacoefdist_nonoise[ietak][idep][isub]->Fill(newdist);
           
// Mean value and dispersion

       Double_t minmeang_mean_corr = minmean_mean/minmeang_mean;
       
       perrg = minerrg[ietak][iphik][idep][isub]/minnevetaphi[ietak][iphik][idep][isub] - minmeang_mean*minmeang_mean;
      
       Double_t yy = sqrt(minmean_mean*perrg/(4.*minmeang_mean*minmeang_mean*minmeang_mean));	
			
	 

       fprintf(Out2,"%d %d %d %d %.8f\n",mysubdetmin0[ietak][idep][isub],iieta,iphik,idep,minmeang_mean);

	Float_t zz = (Float_t)yy;  
		       
       fprintf(Out1,"%d %d %d %d %.5f %.5f\n",isub,idep,iieta,iphik,minmeang_mean_corr,zz); 

         hCalo1etatwocoef[ietak][idep][isub]->Fill((float)iphik,minmeang_mean_corr);
         hCalo1etaonecoef[ietak][idep][isub]->Fill((float)iphik,minmeang_mean_corr);
	 hCalo1etacoefdist[ietak][idep][isub]->Fill(minmeang_mean_corr);
    }
  }  
  }
  }
 
  fclose(Out1);
  fclose(Out2);
  
     TFile efile("mom_initial_12mln.root","recreate");
  
     for(int j=1;j<43;j++)
     {
          for(int k=1;k<5;k++)
     {
          for(int m=1;m<5;m++)
     {

// Mean values     
        if(fabs(hCalo1eta[j][k][m]->GetEntries())>0.1) hCalo1eta[j][k][m]->Write();
	if(fabs(hCalo2eta[j][k][m]->GetEntries())>0.1) hCalo2eta[j][k][m]->Write();
		
	if(fabs(hCalo1etatwo[j][k][m]->GetEntries())>0.1) hCalo1etatwo[j][k][m]->Write();
	if(fabs(hCalo2etatwo[j][k][m]->GetEntries())>0.1) hCalo2etatwo[j][k][m]->Write();
	if(fabs(hCalo1mom2etatwo[j][k][m]->GetEntries())>0.1) hCalo1mom2etatwo[j][k][m]->Write();
        if(fabs(hCalo2mom2etatwo[j][k][m]->GetEntries())>0.1) hCalo2mom2etatwo[j][k][m]->Write();

// Variance
        if(fabs(hCalo1mom2eta[j][k][m]->GetEntries())>0.1) hCalo1mom2eta[j][k][m]->Write();
	if(fabs(hCalo2mom2eta[j][k][m]->GetEntries())>0.1) hCalo2mom2eta[j][k][m]->Write();

// Coefficients	
	if(fabs(hCalo1etatwocoef[j][k][m]->GetEntries())>0.1) hCalo1etatwocoef[j][k][m]->Write();
	if(fabs(hCalo2etatwocoef[j][k][m]->GetEntries())>0.1) hCalo2etatwocoef[j][k][m]->Write();
	if(fabs(hCalo1etaonecoef[j][k][m]->GetEntries())>0.1) hCalo1etaonecoef[j][k][m]->Write();
	if(fabs(hCalo2etaonecoef[j][k][m]->GetEntries())>0.1) hCalo2etaonecoef[j][k][m]->Write();
	if(fabs(hCalo1etacoefdist[j][k][m]->GetEntries())>0.1) hCalo1etacoefdist[j][k][m]->Write();
	if(fabs(hCalo2etacoefdist[j][k][m]->GetEntries())>0.1) hCalo2etacoefdist[j][k][m]->Write();
        if(fabs(hCalo1etacoefdist_nonoise[j][k][m]->GetEntries())>0.1) hCalo1etacoefdist_nonoise[j][k][m]->Write();
        if(fabs(hCalo2etacoefdist_nonoise[j][k][m]->GetEntries())>0.1) hCalo2etacoefdist_nonoise[j][k][m]->Write();
	
        for(int i=1;i<72;i++)
	{
	   if(fabs(hCalo1[i][j][k][m]->GetEntries())>0.1) hCalo1[i][j][k][m]->Write();
	   if(fabs(hCalo2[i][j][k][m]->GetEntries())>0.1) hCalo2[i][j][k][m]->Write();
	   if(fabs(hCalo1mom2[i][j][k][m]->GetEntries())>0.1) hCalo1mom2[i][j][k][m]->Write();
	   if(fabs(hCalo2mom2[i][j][k][m]->GetEntries())>0.1)  hCalo2mom2[i][j][k][m]->Write();
	   
	}
     }
     }
     }  
       
}